1 // Integration tests for cargo-fmt.
5 use std::process::Command;
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") {
14 let cmd = bin_dir.join(format!("cargo-fmt{}", env::consts::EXE_SUFFIX));
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();
22 match Command::new(&cmd).args(args).env("PATH", new_path).output() {
24 String::from_utf8(output.stdout).expect("utf-8"),
25 String::from_utf8(output.stderr).expect("utf-8"),
27 Err(e) => panic!("failed to run `{:?} {:?}`: {}", cmd, args, e),
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 {
36 "Output not expected for cargo-fmt {:?}\n\
42 stringify!($check_args),
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 "));
63 &["--", "--print-config", "current", "."],
64 contains("max_width = ")
71 assert_that!(&["--", "--help"], contains("Format Rust code"));
72 assert_that!(&["--", "-h"], contains("Format Rust code"));
73 assert_that!(&["--", "--help=config"], contains("Configuration Options:"));
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",
91 "tests/mod-resolver/test-submodule-issue-5119/Cargo.toml",
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())))