]> git.proxmox.com Git - rustc.git/blob - src/tools/rustfmt/tests/cargo-fmt/main.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / src / tools / rustfmt / tests / cargo-fmt / main.rs
1 // Integration tests for cargo-fmt.
2
3 use std::env;
4 use std::process::Command;
5
6 /// Run the cargo-fmt executable and return its output.
7 fn cargo_fmt(args: &[&str]) -> (String, String) {
8 let mut bin_dir = env::current_exe().unwrap();
9 bin_dir.pop(); // chop off test exe name
10 if bin_dir.ends_with("deps") {
11 bin_dir.pop();
12 }
13 let cmd = bin_dir.join(format!("cargo-fmt{}", env::consts::EXE_SUFFIX));
14
15 // Ensure cargo-fmt runs the rustfmt binary from the local target dir.
16 let path = env::var_os("PATH").unwrap_or_default();
17 let mut paths = env::split_paths(&path).collect::<Vec<_>>();
18 paths.insert(0, bin_dir);
19 let new_path = env::join_paths(paths).unwrap();
20
21 match Command::new(&cmd).args(args).env("PATH", new_path).output() {
22 Ok(output) => (
23 String::from_utf8(output.stdout).expect("utf-8"),
24 String::from_utf8(output.stderr).expect("utf-8"),
25 ),
26 Err(e) => panic!("failed to run `{:?} {:?}`: {}", cmd, args, e),
27 }
28 }
29
30 macro_rules! assert_that {
31 ($args:expr, $check:ident $check_args:tt) => {
32 let (stdout, stderr) = cargo_fmt($args);
33 if !stdout.$check$check_args {
34 panic!(
35 "Output not expected for cargo-fmt {:?}\n\
36 expected: {}{}\n\
37 actual stdout:\n{}\n\
38 actual stderr:\n{}",
39 $args,
40 stringify!($check),
41 stringify!($check_args),
42 stdout,
43 stderr
44 );
45 }
46 };
47 }
48
49 #[ignore]
50 #[test]
51 fn version() {
52 assert_that!(&["--version"], starts_with("rustfmt "));
53 assert_that!(&["--version"], starts_with("rustfmt "));
54 assert_that!(&["--", "-V"], starts_with("rustfmt "));
55 assert_that!(&["--", "--version"], starts_with("rustfmt "));
56 }
57
58 #[ignore]
59 #[test]
60 fn print_config() {
61 assert_that!(
62 &["--", "--print-config", "current", "."],
63 contains("max_width = ")
64 );
65 }
66
67 #[ignore]
68 #[test]
69 fn rustfmt_help() {
70 assert_that!(&["--", "--help"], contains("Format Rust code"));
71 assert_that!(&["--", "-h"], contains("Format Rust code"));
72 assert_that!(&["--", "--help=config"], contains("Configuration Options:"));
73 }