From fe7efc6283a6908dd3068720c002f52acf662d5d Mon Sep 17 00:00:00 2001 From: Jedrzej Kosinski Date: Mon, 4 May 2026 19:40:05 -0700 Subject: [PATCH] deploy_environment: add CRLF / lone-CR newline tests Per @guill review on PR #13425: add tests covering Windows-style line endings, since editors often save files with CRLF and we want to confirm the CR never leaks into the returned value. - `_write_env_file` now opens with `newline=""` so the on-disk bytes match the literal string passed in (deterministic across host OSes, no `\n` -> `\r\n` translation on Windows). New tests: - `test_crlf_line_ending`: `"...\r\n"` -> value (no trailing CR). - `test_crlf_multiline_only_first_line_used`: `readline(128)` stops at the translated newline boundary for CRLF lines. - `test_crlf_with_surrounding_whitespace`: leading/trailing spaces + CRLF still yield the bare value. - `test_lone_cr_line_ending`: classic-Mac / legacy editor `"...\r"` is also handled by universal-newlines decoding. 15/15 unit tests pass. Co-authored-by: Amp Amp-Thread-ID: https://ampcode.com/threads/T-019df5a8-36be-7107-a4af-c7e4f51687df --- tests-unit/deploy_environment_test.py | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests-unit/deploy_environment_test.py b/tests-unit/deploy_environment_test.py index 24359c5d3..c3497fbb0 100644 --- a/tests-unit/deploy_environment_test.py +++ b/tests-unit/deploy_environment_test.py @@ -18,8 +18,14 @@ def _reset_cache_and_install_dir(tmp_path, monkeypatch): def _write_env_file(tmp_path, content: str) -> str: + """Write the env file with exact content (no newline translation). + + `newline=""` disables Python's text-mode newline translation so the bytes + on disk match the literal string passed in, regardless of host OS. + Newline-style tests (CRLF, lone CR) rely on this. + """ path = os.path.join(str(tmp_path), ".comfy_environment") - with open(path, "w", encoding="utf-8") as f: + with open(path, "w", encoding="utf-8", newline="") as f: f.write(content) return path @@ -40,6 +46,26 @@ class TestGetDeployEnvironment: _write_env_file(tmp_path, "first-line\nsecond-line\n") assert get_deploy_environment() == "first-line" + def test_crlf_line_ending(self, tmp_path): + # Windows editors often save text files with CRLF line endings. + # The CR must not end up in the returned value. + _write_env_file(tmp_path, "local-desktop2-standalone\r\n") + assert get_deploy_environment() == "local-desktop2-standalone" + + def test_crlf_multiline_only_first_line_used(self, tmp_path): + _write_env_file(tmp_path, "first-line\r\nsecond-line\r\n") + assert get_deploy_environment() == "first-line" + + def test_crlf_with_surrounding_whitespace(self, tmp_path): + _write_env_file(tmp_path, " local-desktop2-standalone \r\n") + assert get_deploy_environment() == "local-desktop2-standalone" + + def test_lone_cr_line_ending(self, tmp_path): + # Classic-Mac / some legacy editors use a bare CR. + # Universal-newlines decoding treats it as a line terminator too. + _write_env_file(tmp_path, "local-desktop2-standalone\r") + assert get_deploy_environment() == "local-desktop2-standalone" + def test_empty_file_falls_back_to_default(self, tmp_path): _write_env_file(tmp_path, "") assert get_deploy_environment() == "local-git"