]> git.proxmox.com Git - rustc.git/blame - src/tools/rustfmt/tests/cargo-fmt/main.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / src / tools / rustfmt / tests / cargo-fmt / main.rs
CommitLineData
f20569fa
XL
1// Integration tests for cargo-fmt.
2
3use std::env;
5e7ed085 4use std::path::Path;
f20569fa
XL
5use std::process::Command;
6
7/// Run the cargo-fmt executable and return its output.
8fn 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
31macro_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]
52fn 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]
61fn print_config() {
62 assert_that!(
63 &["--", "--print-config", "current", "."],
64 contains("max_width = ")
65 );
66}
67
68#[ignore]
69#[test]
70fn 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}
5e7ed085
FG
75
76#[ignore]
77#[test]
78fn 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}