From ad212c77bfceb86f0199970ff534662b3b4ba9a3 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 14 May 2024 16:30:19 -0700 Subject: [PATCH] Fix output `\r\n` stripping (#2035) Test if output ends with `\r\n` before `\n`, since the test for `\n` will succeed if output is terminated with `\r\n`, so `\r\n` is properly stripped from output text. --- src/output.rs | 12 ++++++------ tests/backticks.rs | 17 +++++++++++++++++ tests/lib.rs | 1 + 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 tests/backticks.rs diff --git a/src/output.rs b/src/output.rs index af8ec6a..f9f4ab0 100644 --- a/src/output.rs +++ b/src/output.rs @@ -17,13 +17,13 @@ pub(crate) fn output(mut command: Command) -> Result { } match str::from_utf8(&output.stdout) { Err(error) => Err(OutputError::Utf8(error)), - Ok(utf8) => Ok( - if utf8.ends_with('\n') { - &utf8[0..utf8.len() - 1] - } else if utf8.ends_with("\r\n") { - &utf8[0..utf8.len() - 2] + Ok(output) => Ok( + if output.ends_with("\r\n") { + &output[0..output.len() - 2] + } else if output.ends_with('\n') { + &output[0..output.len() - 1] } else { - utf8 + output } .to_owned(), ), diff --git a/tests/backticks.rs b/tests/backticks.rs new file mode 100644 index 0000000..026cfbf --- /dev/null +++ b/tests/backticks.rs @@ -0,0 +1,17 @@ +use super::*; + +#[test] +fn trailing_newlines_are_stripped() { + Test::new() + .shell(false) + .args(["--evaluate", "foos"]) + .justfile( + " +set shell := ['python3', '-c'] + +foos := `print('foo' * 4)` + ", + ) + .stdout("foofoofoofoo") + .run(); +} diff --git a/tests/lib.rs b/tests/lib.rs index b501b7e..bcd1b1a 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -36,6 +36,7 @@ mod allow_duplicate_recipes; mod assert_stdout; mod assert_success; mod attributes; +mod backticks; mod byte_order_mark; mod changelog; mod choose;