1 //! Integration tests for rustfmt.
4 use std::fs::remove_file;
6 use std::process::Command;
8 /// Run the rustfmt executable and return its output.
9 fn rustfmt(args: &[&str]) -> (String, String) {
10 let mut bin_dir = env::current_exe().unwrap();
11 bin_dir.pop(); // chop off test exe name
12 if bin_dir.ends_with("deps") {
15 let cmd = bin_dir.join(format!("rustfmt{}", env::consts::EXE_SUFFIX));
17 // Ensure the rustfmt binary runs from the local target dir.
18 let path = env::var_os("PATH").unwrap_or_default();
19 let mut paths = env::split_paths(&path).collect::<Vec<_>>();
20 paths.insert(0, bin_dir);
21 let new_path = env::join_paths(paths).unwrap();
23 match Command::new(&cmd).args(args).env("PATH", new_path).output() {
25 String::from_utf8(output.stdout).expect("utf-8"),
26 String::from_utf8(output.stderr).expect("utf-8"),
28 Err(e) => panic!("failed to run `{:?} {:?}`: {}", cmd, args, e),
32 macro_rules! assert_that {
33 ($args:expr, $($check:ident $check_args:tt)&&+) => {
34 let (stdout, stderr) = rustfmt($args);
35 if $(!stdout.$check$check_args && !stderr.$check$check_args)||* {
37 "Output not expected for rustfmt {:?}\n\
42 stringify!($( $check$check_args )&&*),
54 &["--print-config", "unknown"],
55 starts_with("Unknown print-config option")
57 assert_that!(&["--print-config", "default"], contains("max_width = 100"));
58 assert_that!(&["--print-config", "minimal"], contains("PATH required"));
60 &["--print-config", "minimal", "minimal-config"],
61 contains("doesn't work with standard input.")
64 let (stdout, stderr) = rustfmt(&[
71 Path::new("minimal-config").exists(),
72 "stdout:\n{}\nstderr:\n{}",
76 remove_file("minimal-config").unwrap();
88 "--config=color=Never,edition=2018"
90 contains("color = \"Never\"") && contains("edition = \"2018\"")
93 // multiple overriding invocations
100 "color=never,edition=2018",
102 "color=always,format_strings=true"
104 contains("color = \"Always\"")
105 && contains("edition = \"2018\"")
106 && contains("format_strings = true")