]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/tests/cargo-fmt/main.rs
:arrow_up: rust-analyzer
[rust.git] / src / tools / rustfmt / tests / cargo-fmt / main.rs
1 // Integration tests for cargo-fmt.
2
3 use std::env;
4 use std::path::Path;
5 use std::process::Command;
6
7 /// Run the cargo-fmt executable and return its output.
8 fn cargo_fmt(args: &[&str]) -> (String, String) {
9     let mut bin_dir = env::current_exe().unwrap();
10     bin_dir.pop(); // chop off test exe name
11     if bin_dir.ends_with("deps") {
12         bin_dir.pop();
13     }
14     let cmd = bin_dir.join(format!("cargo-fmt{}", env::consts::EXE_SUFFIX));
15
16     // Ensure cargo-fmt runs the rustfmt binary from the local target dir.
17     let path = env::var_os("PATH").unwrap_or_default();
18     let mut paths = env::split_paths(&path).collect::<Vec<_>>();
19     paths.insert(0, bin_dir);
20     let new_path = env::join_paths(paths).unwrap();
21
22     match Command::new(&cmd).args(args).env("PATH", new_path).output() {
23         Ok(output) => (
24             String::from_utf8(output.stdout).expect("utf-8"),
25             String::from_utf8(output.stderr).expect("utf-8"),
26         ),
27         Err(e) => panic!("failed to run `{:?} {:?}`: {}", cmd, args, e),
28     }
29 }
30
31 macro_rules! assert_that {
32     ($args:expr, $check:ident $check_args:tt) => {
33         let (stdout, stderr) = cargo_fmt($args);
34         if !stdout.$check$check_args {
35             panic!(
36                 "Output not expected for cargo-fmt {:?}\n\
37                  expected: {}{}\n\
38                  actual stdout:\n{}\n\
39                  actual stderr:\n{}",
40                 $args,
41                 stringify!($check),
42                 stringify!($check_args),
43                 stdout,
44                 stderr
45             );
46         }
47     };
48 }
49
50 #[ignore]
51 #[test]
52 fn version() {
53     assert_that!(&["--version"], starts_with("rustfmt "));
54     assert_that!(&["--version"], starts_with("rustfmt "));
55     assert_that!(&["--", "-V"], starts_with("rustfmt "));
56     assert_that!(&["--", "--version"], starts_with("rustfmt "));
57 }
58
59 #[ignore]
60 #[test]
61 fn print_config() {
62     assert_that!(
63         &["--", "--print-config", "current", "."],
64         contains("max_width = ")
65     );
66 }
67
68 #[ignore]
69 #[test]
70 fn rustfmt_help() {
71     assert_that!(&["--", "--help"], contains("Format Rust code"));
72     assert_that!(&["--", "-h"], contains("Format Rust code"));
73     assert_that!(&["--", "--help=config"], contains("Configuration Options:"));
74 }
75
76 #[ignore]
77 #[test]
78 fn cargo_fmt_out_of_line_test_modules() {
79     // See also https://github.com/rust-lang/rustfmt/issues/5119
80     let expected_modified_files = [
81         "tests/mod-resolver/test-submodule-issue-5119/src/lib.rs",
82         "tests/mod-resolver/test-submodule-issue-5119/tests/test1.rs",
83         "tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub1.rs",
84         "tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub2.rs",
85         "tests/mod-resolver/test-submodule-issue-5119/tests/test1/sub3/sub4.rs",
86     ];
87     let args = [
88         "-v",
89         "--check",
90         "--manifest-path",
91         "tests/mod-resolver/test-submodule-issue-5119/Cargo.toml",
92     ];
93     let (stdout, _) = cargo_fmt(&args);
94     for file in expected_modified_files {
95         let path = Path::new(file).canonicalize().unwrap();
96         assert!(stdout.contains(&format!("Diff in {}", path.display())))
97     }
98 }