1 // Integration tests for cargo-fmt.
5 use std::process::Command;
7 use rustfmt_config_proc_macro::rustfmt_only_ci_test;
9 /// Run the cargo-fmt executable and return its output.
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") {
16 let cmd = bin_dir.join(format!("cargo-fmt{}", env::consts::EXE_SUFFIX));
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();
24 match Command::new(&cmd).args(args).env("PATH", new_path).output() {
26 String::from_utf8(output.stdout).expect("utf-8"),
27 String::from_utf8(output.stderr).expect("utf-8"),
29 Err(e) => panic!("failed to run `{:?} {:?}`: {}", cmd, args, e),
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 {
38 "Output not expected for cargo-fmt {:?}\n\
44 stringify!($check_args),
52 #[rustfmt_only_ci_test]
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 "));
61 #[rustfmt_only_ci_test]
65 &["--", "--print-config", "current", "."],
66 contains("max_width = ")
70 #[rustfmt_only_ci_test]
73 assert_that!(&["--", "--help"], contains("Format Rust code"));
74 assert_that!(&["--", "-h"], contains("Format Rust code"));
75 assert_that!(&["--", "--help=config"], contains("Configuration Options:"));
78 #[rustfmt_only_ci_test]
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",
93 "tests/mod-resolver/test-submodule-issue-5119/Cargo.toml",
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())))
102 #[rustfmt_only_ci_test]
104 fn cargo_fmt_emits_error_on_line_overflow_true() {
105 // See also https://github.com/rust-lang/rustfmt/issues/3164
109 "tests/cargo-fmt/source/issue_3164/Cargo.toml",
112 "error_on_line_overflow=true",
115 let (_stdout, stderr) = cargo_fmt(&args);
116 assert!(stderr.contains(
117 "line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option)"