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