Skip to content

Commit

Permalink
Add a empty line to the crontab when it's not present
Browse files Browse the repository at this point in the history
  • Loading branch information
devtronic committed Nov 3, 2022
1 parent 930b038 commit b3516b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# v1.1.1
Fixes:
- Crontabs must end with an empty line [PR #2](https://github.com/mintware-de/native-cron/pull/2)

# v1.1.0
Features:
- Added a DateTimeDefinition which represents the date / time part of cronjob lines. [PR #1](https://github.com/mintware-de/native-cron/pull/1)
Expand Down
7 changes: 6 additions & 1 deletion src/Content/Crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public function parse(string $content): void

public function build(): string
{
return implode("\n", array_map(fn (CrontabLineInterface $l) => $l->build(), $this->lines));
$content = implode("\n", array_map(fn (CrontabLineInterface $l) => $l->build(), $this->lines));
if (!str_ends_with($content, "\n")) {
$content .= "\n";
}

return $content;
}
}
17 changes: 15 additions & 2 deletions tests/Content/CrontabTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ public function testParseAndBuild(): void
# Edit this file to introduce tasks to be run by cron.
*/2 * * * * test argument
TEXT;

$crontab = new Crontab(false);
$crontab->parse($content);
$lines = $crontab->getLines();
self::assertCount(3, $lines);
self::assertCount(4, $lines);
self::assertInstanceOf(CommentLine::class, $lines[0]);
self::assertInstanceOf(BlankLine::class, $lines[1]);
self::assertInstanceOf(CronJobLine::class, $lines[2]);
Expand Down Expand Up @@ -78,6 +79,7 @@ public function testParseAndBuildWithLeading(): void
# Edit this file to introduce tasks to be run by cron.
*/2 * * * * test argument
TEXT;
self::assertEquals($expected, $crontab->build());
}
Expand All @@ -88,12 +90,13 @@ public function testParseAndBuildSystemCrontab(): void
# Edit this file to introduce tasks to be run by cron.
*/2 * * * * root test argument
TEXT;

$crontab = new Crontab();
$crontab->parse($content);
$lines = $crontab->getLines();
self::assertCount(3, $lines);
self::assertCount(4, $lines);
self::assertInstanceOf(CommentLine::class, $lines[0]);
self::assertInstanceOf(BlankLine::class, $lines[1]);
self::assertInstanceOf(CronJobLine::class, $lines[2]);
Expand Down Expand Up @@ -124,6 +127,7 @@ public function testParseAndBuildRealExample(): void
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
TEXT;

$crontab = new Crontab();
Expand All @@ -137,6 +141,7 @@ public function testAddLine(): void
# Edit this file to introduce tasks to be run by cron.
*/2 * * * * test argument
TEXT;

$crontab = new Crontab(false);
Expand All @@ -153,6 +158,7 @@ public function testRemove(): void
$content = <<<TEXT
# Edit this file to introduce tasks to be run by cron.
*/2 * * * * test argument
TEXT;

$crontab = new Crontab(false);
Expand All @@ -171,6 +177,7 @@ public function testRemoveWhere(): void
$content = <<<TEXT
# Edit this file to introduce tasks to be run by cron.
*/2 * * * * test argument
TEXT;

$crontab = new Crontab(false);
Expand All @@ -188,4 +195,10 @@ public function testRemoveWhere(): void

self::assertEquals($content, $crontab->build());
}

public function testBuildShouldAppendABlankLine(): void
{
$crontab = new Crontab();
self::assertEquals("\n", $crontab->build());
}
}

0 comments on commit b3516b1

Please sign in to comment.