Skip to content

Commit 4e7d804

Browse files
artenginjaxwilko
andauthored
fix: render single trailing newline in .env (#9)
* fix: render single trailing newline in .env * refactor: update test and env fixtures for case with empty lines * refactor: update test for case with empty lines * Added extra test case --------- Co-authored-by: Jack Wilkinson <[email protected]>
1 parent 350b143 commit 4e7d804

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Printer/EnvPrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public function render(array $ast): string
2929
}
3030
}
3131

32-
return $output;
32+
return rtrim($output) . PHP_EOL;
3333
}
3434
}

tests/EnvFileTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,38 @@ public function testUpdateEnvWithTrailingComment()
223223
$result = $env->render();
224224
$this->assertStringContainsString('VAR_NO_VALUE=this is a test', $result);
225225
}
226+
227+
public function testUpdateEnvWithEmptyLine()
228+
{
229+
$filePath = __DIR__ . '/fixtures/env/test.env';
230+
231+
$env = EnvFile::open($filePath);
232+
233+
$env->addEmptyLine();
234+
$env->addEmptyLine();
235+
$env->addEmptyLine();
236+
$env->addEmptyLine();
237+
238+
$this->assertEquals(file_get_contents($filePath), $env->render());
239+
}
240+
241+
public function testEnvComplexWithEmptyLine()
242+
{
243+
$env = new EnvFile('');
244+
245+
$env->addEmptyLine();
246+
$env->addEmptyLine();
247+
248+
$env->set('VAR_ONE', '1');
249+
250+
$env->addEmptyLine();
251+
$env->addEmptyLine();
252+
253+
$env->set('VAR_TWO', '2');
254+
255+
$env->addEmptyLine();
256+
$env->addEmptyLine();
257+
258+
$this->assertEquals(sprintf('%1$s%1$sVAR_ONE=1%1$s%1$s%1$sVAR_TWO=2%1$s', PHP_EOL), $env->render());
259+
}
226260
}

0 commit comments

Comments
 (0)