Skip to content

Conversation

@artengin
Copy link
Contributor

@artengin artengin commented Sep 5, 2025

Normalize .env rendering with a single trailing newline

When using addEmptyLine() on an EnvFile where no new variables are added
but existing ones are modified, multiple empty lines could accumulate at the end.
This change ensures that .env files always end with exactly one newline,
keeping the file clean and consistent.

Summary by CodeRabbit

  • Bug Fixes

    • Rendered environment output is now trimmed and ends with a single newline, removing stray trailing whitespace and ensuring consistent EOF formatting.
    • Improves compatibility with tools that expect a newline at the end of files.
  • Tests

    • Added tests covering insertion of empty lines and complex environment layouts to ensure stable rendering.

@artengin
Copy link
Contributor Author

artengin commented Sep 5, 2025

Hi there!

I'm using your package and really love it—thank you for your work)
I ran into a small issue, so I made a pull request to address it.
I'd be grateful if you could take a look and consider merging it.

Thanks again!

@artengin
Copy link
Contributor Author

If there are any other changes needed, I’ll be happy to fix them right away. 🙂

@jaxwilko
Copy link
Member

@artengin sorry for the delay in response, your change LGTM.

Could you also add this as an additional test:

public function testEnvComplexWithEmptyLine()
{
    $env = new EnvFile('');

    $env->addEmptyLine();
    $env->addEmptyLine();

    $env->set('VAR_ONE', '1');

    $env->addEmptyLine();
    $env->addEmptyLine();

    $env->set('VAR_TWO', '2');

    $env->addEmptyLine();
    $env->addEmptyLine();

    $this->assertEquals("\n\nVAR_ONE=1\n\n\nVAR_TWO=2\n", $env->render());
}

Just to prove multiple new lines still work at the top / in the middle of the the file and are only stripped at the bottom :)

@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2025

Walkthrough

EnvPrinter's render now returns the trimmed output with a single trailing newline (rtrim(...).PHP_EOL). Two tests were added to exercise empty-line insertion and rendering normalization, including a case that preserves multiple empty lines around variables.

Changes

Cohort / File(s) Summary of changes
Rendering behavior update
src/Printer/EnvPrinter.php
Changed render to return rtrim($output) . PHP_EOL instead of returning the raw accumulated $output, ensuring no trailing whitespace and exactly one newline at EOF.
Empty-line handling tests
tests/EnvFileTest.php
Added testUpdateEnvWithEmptyLine and testEnvComplexWithEmptyLine to verify empty-line insertion and final rendering, including expected newline normalization and preservation of inserted blank lines around variables.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Test
  participant EnvFile
  participant EnvPrinter

  Test->>EnvFile: addEmptyLine() / set variables
  Test->>EnvPrinter: render(EnvFile)
  EnvPrinter->>EnvPrinter: accumulate output lines
  Note over EnvPrinter: rtrim(output) then append PHP_EOL
  EnvPrinter-->>Test: normalized_output_with_single_newline
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I hop through lines where blanks reside,
I nudge the ends to trim and guide.
Four quiet beats, then vars align,
One final newline — tidy, fine.
Thump-thump! The .env sleeps satisfied.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly describes the core change—ensuring a single trailing newline when rendering .env files—using clear and specific phrasing without unnecessary detail.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4ff9a7a and 2de5c5d.

📒 Files selected for processing (1)
  • tests/EnvFileTest.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/EnvFileTest.php

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jaxwilko jaxwilko merged commit 4e7d804 into wintercms:main Oct 15, 2025
14 checks passed
@artengin
Copy link
Contributor Author

Thanks so much! 😊

@artengin artengin deleted the render-single-trailing-newline-in-env branch November 11, 2025 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants