Skip to content

Commit d71f22d

Browse files
committed
Add tests that call rustfmt by passing input via stdin
1 parent b4de150 commit d71f22d

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

tests/rustfmt/main.rs

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,49 @@
22
33
use std::env;
44
use std::fs::remove_file;
5+
use std::io::{Result, Write};
56
use std::path::Path;
6-
use std::process::Command;
7+
use std::process::{Command, Output, Stdio};
78

89
/// Run the rustfmt executable and return its output.
910
fn rustfmt(args: &[&str]) -> (String, String) {
11+
match rustfm_builder(|rustfmt| rustfmt.args(args).output()) {
12+
Ok(output) => (
13+
String::from_utf8(output.stdout).expect("utf-8"),
14+
String::from_utf8(output.stderr).expect("utf-8"),
15+
),
16+
Err(e) => panic!("failed to run `rustfmt {:?}`: {}", args, e),
17+
}
18+
}
19+
20+
/// Run the rustfmt executable and take input from stdin
21+
fn rustfmt_std_input(args: &[&str], input: &str) -> (String, String) {
22+
let output = rustfm_builder(|cmd| {
23+
let mut rustfmt = cmd
24+
.args(args)
25+
.stdin(Stdio::piped())
26+
.stdout(Stdio::piped())
27+
.stderr(Stdio::piped())
28+
.spawn()
29+
.unwrap();
30+
31+
rustfmt
32+
.stdin
33+
.as_mut()
34+
.unwrap()
35+
.write_all(input.as_bytes())?;
36+
rustfmt.wait_with_output()
37+
});
38+
match output {
39+
Ok(output) => (
40+
String::from_utf8(output.stdout).expect("utf-8"),
41+
String::from_utf8(output.stderr).expect("utf-8"),
42+
),
43+
Err(e) => panic!("failed to run `rustfmt {:?}`: {}", args, e),
44+
}
45+
}
46+
47+
fn rustfm_builder<F: Fn(&mut Command) -> Result<Output>>(f: F) -> Result<Output> {
1048
let mut bin_dir = env::current_exe().unwrap();
1149
bin_dir.pop(); // chop off test exe name
1250
if bin_dir.ends_with("deps") {
@@ -20,13 +58,8 @@ fn rustfmt(args: &[&str]) -> (String, String) {
2058
paths.insert(0, bin_dir);
2159
let new_path = env::join_paths(paths).unwrap();
2260

23-
match Command::new(&cmd).args(args).env("PATH", new_path).output() {
24-
Ok(output) => (
25-
String::from_utf8(output.stdout).expect("utf-8"),
26-
String::from_utf8(output.stderr).expect("utf-8"),
27-
),
28-
Err(e) => panic!("failed to run `{:?} {:?}`: {}", cmd, args, e),
29-
}
61+
let mut rustfmt = Command::new(&cmd);
62+
f(rustfmt.env("PATH", new_path))
3063
}
3164

3265
macro_rules! assert_that {
@@ -157,3 +190,29 @@ fn mod_resolution_error_path_attribute_does_not_exist() {
157190
// The path attribute points to a file that does not exist
158191
assert!(stderr.contains("does_not_exist.rs does not exist"));
159192
}
193+
194+
mod rustfmt_stdin_formatting {
195+
use super::rustfmt_std_input;
196+
197+
#[rustfmt::skip]
198+
#[test]
199+
fn changes_are_output_to_stdout() {
200+
let args = [];
201+
let source = "fn main () { println!(\"hello world!\"); }";
202+
let (stdout, _stderr) = rustfmt_std_input(&args, source);
203+
let expected_output =
204+
r#"fn main() {
205+
println!("hello world!");
206+
}"#;
207+
assert!(stdout.contains(expected_output))
208+
}
209+
210+
#[test]
211+
fn properly_formatted_input_is_output_to_stdout_unchanged() {
212+
// NOTE: Technicallly a newline is added, but nothing meaningful is changed
213+
let args = [];
214+
let source = "fn main() {}";
215+
let (stdout, _stderr) = rustfmt_std_input(&args, source);
216+
assert!(stdout.trim_end() == source)
217+
}
218+
}

0 commit comments

Comments
 (0)