From ab4eadec32aa132a244822f91df95ff2296daa31 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sat, 7 Feb 2026 08:10:00 -0500 Subject: [PATCH] Fix Exporting an egg in yaml format (#2172) --- .../Eggs/Sharing/EggExporterService.php | 2 +- .../Services/Eggs/EggExporterServiceTest.php | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Services/Eggs/EggExporterServiceTest.php diff --git a/app/Services/Eggs/Sharing/EggExporterService.php b/app/Services/Eggs/Sharing/EggExporterService.php index bdaa3e217..94fbcda3f 100644 --- a/app/Services/Eggs/Sharing/EggExporterService.php +++ b/app/Services/Eggs/Sharing/EggExporterService.php @@ -92,7 +92,7 @@ class EggExporterService return $this->yamlExport($decoded); } - return str_replace(["\r\n", '\\r\\n', '\\n'], "\n", $data); + return str_replace("\r\n", "\n", $data); } if (is_array($data)) { diff --git a/tests/Unit/Services/Eggs/EggExporterServiceTest.php b/tests/Unit/Services/Eggs/EggExporterServiceTest.php new file mode 100644 index 000000000..70c372e5a --- /dev/null +++ b/tests/Unit/Services/Eggs/EggExporterServiceTest.php @@ -0,0 +1,66 @@ +service = new EggExporterService(); + } + + public function test_yaml_export_preserves_literal_backslash_n_in_scripts(): void + { + $script = <<<'BASH' +if [[ "${STEAM_USER}" == "" ]] || [[ "${STEAM_PASS}" == "" ]]; then + echo -e "steam user is not set.\n" + echo -e "Using anonymous user.\n" + STEAM_USER=anonymous + STEAM_PASS="" + STEAM_AUTH="" +else + echo -e "user set to ${STEAM_USER}" +fi +BASH; + + $result = $this->callYamlExport($script); + + $this->assertStringContainsString('echo -e "steam user is not set.\n"', $result); + $this->assertStringContainsString('echo -e "Using anonymous user.\n"', $result); + } + + public function test_yaml_export_preserves_literal_backslash_r_backslash_n(): void + { + $script = 'echo -e "line ending\\r\\n"'; + + $result = $this->callYamlExport($script); + + $this->assertSame($script, $result); + } + + public function test_yaml_export_normalizes_real_crlf_to_lf(): void + { + $script = "line one\r\nline two\r\nline three"; + + $result = $this->callYamlExport($script); + + $this->assertSame("line one\nline two\nline three", $result); + } + + /** + * Call the protected yamlExport method via reflection. + */ + private function callYamlExport(mixed $data): mixed + { + $reflection = new \ReflectionMethod($this->service, 'yamlExport'); + + return $reflection->invoke($this->service, $data); + } +}