1 //! Integration tests for rustfmt.
4 use std::fs::remove_file;
6 use std::process::Command;
8 use rustfmt_config_proc_macro::rustfmt_only_ci_test;
10 /// Run the rustfmt executable and return its output.
11 fn rustfmt(args: &[&str]) -> (String, String) {
12 let mut bin_dir = env::current_exe().unwrap();
13 bin_dir.pop(); // chop off test exe name
14 if bin_dir.ends_with("deps") {
17 let cmd = bin_dir.join(format!("rustfmt{}", env::consts::EXE_SUFFIX));
19 // Ensure the rustfmt binary runs from the local target dir.
20 let path = env::var_os("PATH").unwrap_or_default();
21 let mut paths = env::split_paths(&path).collect::<Vec<_>>();
22 paths.insert(0, bin_dir);
23 let new_path = env::join_paths(paths).unwrap();
25 match Command::new(&cmd).args(args).env("PATH", new_path).output() {
27 String::from_utf8(output.stdout).expect("utf-8"),
28 String::from_utf8(output.stderr).expect("utf-8"),
30 Err(e) => panic!("failed to run `{:?} {:?}`: {}", cmd, args, e),
34 macro_rules! assert_that {
35 ($args:expr, $($check:ident $check_args:tt)&&+) => {
36 let (stdout, stderr) = rustfmt($args);
37 if $(!stdout.$check$check_args && !stderr.$check$check_args)||* {
39 "Output not expected for rustfmt {:?}\n\
44 stringify!($( $check$check_args )&&*),
52 #[rustfmt_only_ci_test]
56 &["--print-config", "unknown"],
57 starts_with("Unknown print-config option")
59 assert_that!(&["--print-config", "default"], contains("max_width = 100"));
60 assert_that!(&["--print-config", "minimal"], contains("PATH required"));
62 &["--print-config", "minimal", "minimal-config"],
63 contains("doesn't work with standard input.")
66 let (stdout, stderr) = rustfmt(&[
73 Path::new("minimal-config").exists(),
74 "stdout:\n{}\nstderr:\n{}",
78 remove_file("minimal-config").unwrap();
81 #[rustfmt_only_ci_test]
90 "--config=color=Never,edition=2018"
92 contains("color = \"Never\"") && contains("edition = \"2018\"")
95 // multiple overriding invocations
102 "color=never,edition=2018",
104 "color=always,format_strings=true"
106 contains("color = \"Always\"")
107 && contains("edition = \"2018\"")
108 && contains("format_strings = true")
113 fn rustfmt_usage_text() {
114 let args = ["--help"];
115 let (stdout, _) = rustfmt(&args);
116 assert!(stdout.contains("Format Rust code\n\nusage: rustfmt [options] <file>..."));
120 fn mod_resolution_error_multiple_candidate_files() {
121 // See also https://github.com/rust-lang/rustfmt/issues/5167
122 let default_path = Path::new("tests/mod-resolver/issue-5167/src/a.rs");
123 let secondary_path = Path::new("tests/mod-resolver/issue-5167/src/a/mod.rs");
124 let error_message = format!(
125 "file for module found at both {:?} and {:?}",
126 default_path.canonicalize().unwrap(),
127 secondary_path.canonicalize().unwrap(),
130 let args = ["tests/mod-resolver/issue-5167/src/lib.rs"];
131 let (_stdout, stderr) = rustfmt(&args);
132 assert!(stderr.contains(&error_message))
136 fn mod_resolution_error_sibling_module_not_found() {
137 let args = ["tests/mod-resolver/module-not-found/sibling_module/lib.rs"];
138 let (_stdout, stderr) = rustfmt(&args);
139 // Module resolution fails because we're unable to find `a.rs` in the same directory as lib.rs
140 assert!(stderr.contains("a.rs does not exist"))
144 fn mod_resolution_error_relative_module_not_found() {
145 let args = ["tests/mod-resolver/module-not-found/relative_module/lib.rs"];
146 let (_stdout, stderr) = rustfmt(&args);
147 // The file `./a.rs` and directory `./a` both exist.
148 // Module resolution fails because we're unable to find `./a/b.rs`
150 assert!(stderr.contains("a/b.rs does not exist"));
152 assert!(stderr.contains("a\\b.rs does not exist"));
156 fn mod_resolution_error_path_attribute_does_not_exist() {
157 let args = ["tests/mod-resolver/module-not-found/bad_path_attribute/lib.rs"];
158 let (_stdout, stderr) = rustfmt(&args);
159 // The path attribute points to a file that does not exist
160 assert!(stderr.contains("does_not_exist.rs does not exist"));
164 fn rustfmt_emits_error_on_line_overflow_true() {
165 // See also https://github.com/rust-lang/rustfmt/issues/3164
168 "error_on_line_overflow=true",
169 "tests/cargo-fmt/source/issue_3164/src/main.rs",
172 let (_stdout, stderr) = rustfmt(&args);
173 assert!(stderr.contains(
174 "line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option)"