]> git.lizzy.rs Git - rust.git/blob - tests/rustfmt/main.rs
feat: v2 support for nested tuples w/o spaces
[rust.git] / 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 }