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.
This commit is contained in:
Casey Rodarmor 2024-05-14 16:30:19 -07:00 committed by GitHub
parent b7c284972d
commit ad212c77bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 6 deletions

View File

@ -17,13 +17,13 @@ pub(crate) fn output(mut command: Command) -> Result<String, OutputError> {
}
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(),
),

17
tests/backticks.rs Normal file
View File

@ -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();
}

View File

@ -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;