Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid extraneous newline when printing to stdout #123

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 144 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ miette = { version = "7.2.0", default-features = false, features = ["derive", "f
rayon = "1.10.0"

[dev-dependencies]
assert_cmd = { version = "2.0.16", default-features = false }
pretty_assertions = "1.4.0"
tempfile = { version = "3.12.0", default-features = false }
walkdir = "2.5.0"

[build-dependencies]
Expand Down
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ mod test {
let file_name = format!(
"{}.expected",
path.file_name()
.expect(&format!(
.unwrap_or_else(|| panic!(
"cannot get filename component of {}",
path.display()
))
Expand All @@ -198,10 +198,9 @@ mod test {
o.join(file_name)
};

let formatted = format(&input, false, false).expect(&format!(
"cannot format source file {}",
t.path().to_string_lossy()
));
let formatted = format(&input, false, false).unwrap_or_else(|_| {
panic!("cannot format source file {}", t.path().to_string_lossy())
});

if !update_baseline {
let expected = std::fs::read_to_string(output).expect("cannot read baseline");
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ fn main() -> Result<()> {
.read_to_string(&mut buf)
.map_err(Error::Io)
.wrap_err("while reading input from stdin")?;
println!("{}", format(&buf, "<stdin>")?);

// When printing the result do not insert extra newlines.
let formatted = format(&buf, "<stdin>")?;
print!("{formatted}");
} else {
let failed = args
.input_files
Expand Down Expand Up @@ -95,7 +98,8 @@ fn main() -> Result<()> {
return Some(input_file);
}
} else {
println!("{formatted}");
// When printing the result do not insert extra newlines.
print!("{formatted}");
}

None
Expand Down
Loading
Loading