]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/tests/rustfmt/main.rs
Rollup merge of #97166 - nnethercote:move-conditions-out, r=estebank
[rust.git] / src / tools / rustfmt / tests / rustfmt / main.rs
1 //! Integration tests for rustfmt.
2
3 use std::env;
4 use std::fs::remove_file;
5 use std::path::Path;
6 use std::process::Command;
7
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") {
13         bin_dir.pop();
14     }
15     let cmd = bin_dir.join(format!("rustfmt{}", env::consts::EXE_SUFFIX));
16
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();
22
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     }
30 }
31
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)||* {
36             panic!(
37                 "Output not expected for rustfmt {:?}\n\
38                  expected: {}\n\
39                  actual stdout:\n{}\n\
40                  actual stderr:\n{}",
41                 $args,
42                 stringify!($( $check$check_args )&&*),
43                 stdout,
44                 stderr
45             );
46         }
47     };
48 }
49
50 #[ignore]
51 #[test]
52 fn print_config() {
53     assert_that!(
54         &["--print-config", "unknown"],
55         starts_with("Unknown print-config option")
56     );
57     assert_that!(&["--print-config", "default"], contains("max_width = 100"));
58     assert_that!(&["--print-config", "minimal"], contains("PATH required"));
59     assert_that!(
60         &["--print-config", "minimal", "minimal-config"],
61         contains("doesn't work with standard input.")
62     );
63
64     let (stdout, stderr) = rustfmt(&[
65         "--print-config",
66         "minimal",
67         "minimal-config",
68         "src/shape.rs",
69     ]);
70     assert!(
71         Path::new("minimal-config").exists(),
72         "stdout:\n{}\nstderr:\n{}",
73         stdout,
74         stderr
75     );
76     remove_file("minimal-config").unwrap();
77 }
78
79 #[ignore]
80 #[test]
81 fn inline_config() {
82     // single invocation
83     assert_that!(
84         &[
85             "--print-config",
86             "current",
87             ".",
88             "--config=color=Never,edition=2018"
89         ],
90         contains("color = \"Never\"") && contains("edition = \"2018\"")
91     );
92
93     // multiple overriding invocations
94     assert_that!(
95         &[
96             "--print-config",
97             "current",
98             ".",
99             "--config",
100             "color=never,edition=2018",
101             "--config",
102             "color=always,format_strings=true"
103         ],
104         contains("color = \"Always\"")
105             && contains("edition = \"2018\"")
106             && contains("format_strings = true")
107     );
108 }
109
110 #[test]
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>..."));
115 }
116
117 #[test]
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(),
126     );
127
128     let args = ["tests/mod-resolver/issue-5167/src/lib.rs"];
129     let (_stdout, stderr) = rustfmt(&args);
130     assert!(stderr.contains(&error_message))
131 }
132
133 #[test]
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"))
139 }
140
141 #[test]
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`
147     #[cfg(not(windows))]
148     assert!(stderr.contains("a/b.rs does not exist"));
149     #[cfg(windows)]
150     assert!(stderr.contains("a\\b.rs does not exist"));
151 }
152
153 #[test]
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"));
159 }