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")
111 fn rustfmt_usage_text() {
112 let args = ["--help"];
113 let (stdout, _) = rustfmt(&args);
114 assert!(stdout.contains("Format Rust code\n\nusage: rustfmt [options] <file>..."));
118 fn mod_resolution_error_multiple_candidate_files() {
119 // See also https://github.com/rust-lang/rustfmt/issues/5167
120 let default_path = Path::new("tests/mod-resolver/issue-5167/src/a.rs");
121 let secondary_path = Path::new("tests/mod-resolver/issue-5167/src/a/mod.rs");
122 let error_message = format!(
123 "file for module found at both {:?} and {:?}",
124 default_path.canonicalize().unwrap(),
125 secondary_path.canonicalize().unwrap(),
128 let args = ["tests/mod-resolver/issue-5167/src/lib.rs"];
129 let (_stdout, stderr) = rustfmt(&args);
130 assert!(stderr.contains(&error_message))
134 fn mod_resolution_error_sibling_module_not_found() {
135 let args = ["tests/mod-resolver/module-not-found/sibling_module/lib.rs"];
136 let (_stdout, stderr) = rustfmt(&args);
137 // Module resolution fails because we're unable to find `a.rs` in the same directory as lib.rs
138 assert!(stderr.contains("a.rs does not exist"))
142 fn mod_resolution_error_relative_module_not_found() {
143 let args = ["tests/mod-resolver/module-not-found/relative_module/lib.rs"];
144 let (_stdout, stderr) = rustfmt(&args);
145 // The file `./a.rs` and directory `./a` both exist.
146 // Module resolution fails becuase we're unable to find `./a/b.rs`
148 assert!(stderr.contains("a/b.rs does not exist"));
150 assert!(stderr.contains("a\\b.rs does not exist"));
154 fn mod_resolution_error_path_attribute_does_not_exist() {
155 let args = ["tests/mod-resolver/module-not-found/bad_path_attribute/lib.rs"];
156 let (_stdout, stderr) = rustfmt(&args);
157 // The path attribute points to a file that does not exist
158 assert!(stderr.contains("does_not_exist.rs does not exist"));