Skip to content

Commit b3516b1

Browse files
committed
Add a empty line to the crontab when it's not present
1 parent 930b038 commit b3516b1

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
# v1.1.1
4+
Fixes:
5+
- Crontabs must end with an empty line [PR #2](https://github.com/mintware-de/native-cron/pull/2)
6+
37
# v1.1.0
48
Features:
59
- Added a DateTimeDefinition which represents the date / time part of cronjob lines. [PR #1](https://github.com/mintware-de/native-cron/pull/1)

src/Content/Crontab.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public function parse(string $content): void
113113

114114
public function build(): string
115115
{
116-
return implode("\n", array_map(fn (CrontabLineInterface $l) => $l->build(), $this->lines));
116+
$content = implode("\n", array_map(fn (CrontabLineInterface $l) => $l->build(), $this->lines));
117+
if (!str_ends_with($content, "\n")) {
118+
$content .= "\n";
119+
}
120+
121+
return $content;
117122
}
118123
}

tests/Content/CrontabTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ public function testParseAndBuild(): void
4545
# Edit this file to introduce tasks to be run by cron.
4646
4747
*/2 * * * * test argument
48+
4849
TEXT;
4950

5051
$crontab = new Crontab(false);
5152
$crontab->parse($content);
5253
$lines = $crontab->getLines();
53-
self::assertCount(3, $lines);
54+
self::assertCount(4, $lines);
5455
self::assertInstanceOf(CommentLine::class, $lines[0]);
5556
self::assertInstanceOf(BlankLine::class, $lines[1]);
5657
self::assertInstanceOf(CronJobLine::class, $lines[2]);
@@ -78,6 +79,7 @@ public function testParseAndBuildWithLeading(): void
7879
# Edit this file to introduce tasks to be run by cron.
7980
8081
*/2 * * * * test argument
82+
8183
TEXT;
8284
self::assertEquals($expected, $crontab->build());
8385
}
@@ -88,12 +90,13 @@ public function testParseAndBuildSystemCrontab(): void
8890
# Edit this file to introduce tasks to be run by cron.
8991
9092
*/2 * * * * root test argument
93+
9194
TEXT;
9295

9396
$crontab = new Crontab();
9497
$crontab->parse($content);
9598
$lines = $crontab->getLines();
96-
self::assertCount(3, $lines);
99+
self::assertCount(4, $lines);
97100
self::assertInstanceOf(CommentLine::class, $lines[0]);
98101
self::assertInstanceOf(BlankLine::class, $lines[1]);
99102
self::assertInstanceOf(CronJobLine::class, $lines[2]);
@@ -124,6 +127,7 @@ public function testParseAndBuildRealExample(): void
124127
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
125128
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
126129
#
130+
127131
TEXT;
128132

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

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

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

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

189196
self::assertEquals($content, $crontab->build());
190197
}
198+
199+
public function testBuildShouldAppendABlankLine(): void
200+
{
201+
$crontab = new Crontab();
202+
self::assertEquals("\n", $crontab->build());
203+
}
191204
}

0 commit comments

Comments
 (0)