|
| 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 | +#[test] |
| 50 | +fn version() { |
| 51 | + assert_that!(&["--version"], starts_with("rustfmt ")); |
| 52 | + assert_that!(&["--version"], starts_with("rustfmt ")); |
| 53 | + assert_that!(&["--", "-V"], starts_with("rustfmt ")); |
| 54 | + assert_that!(&["--", "--version"], starts_with("rustfmt ")); |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn print_config() { |
| 59 | + assert_that!( |
| 60 | + &["--", "--print-config", "current", "."], |
| 61 | + contains("max_width = ") |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn rustfmt_help() { |
| 67 | + assert_that!(&["--", "--help"], contains("Format Rust code")); |
| 68 | + assert_that!(&["--", "-h"], contains("Format Rust code")); |
| 69 | + assert_that!(&["--", "--help=config"], contains("Configuration Options:")); |
| 70 | +} |
0 commit comments