2019-07-15 06:58:54 -05:00
|
|
|
//! Integration tests for rustfmt.
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs::remove_file;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
/// Run the rustfmt executable and return its output.
|
|
|
|
fn rustfmt(args: &[&str]) -> (String, String) {
|
|
|
|
let mut bin_dir = env::current_exe().unwrap();
|
|
|
|
bin_dir.pop(); // chop off test exe name
|
|
|
|
if bin_dir.ends_with("deps") {
|
|
|
|
bin_dir.pop();
|
|
|
|
}
|
|
|
|
let cmd = bin_dir.join(format!("rustfmt{}", env::consts::EXE_SUFFIX));
|
|
|
|
|
|
|
|
// Ensure the rustfmt binary runs from the local target dir.
|
|
|
|
let path = env::var_os("PATH").unwrap_or_default();
|
|
|
|
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
|
|
|
|
paths.insert(0, bin_dir);
|
|
|
|
let new_path = env::join_paths(paths).unwrap();
|
|
|
|
|
|
|
|
match Command::new(&cmd).args(args).env("PATH", new_path).output() {
|
|
|
|
Ok(output) => (
|
|
|
|
String::from_utf8(output.stdout).expect("utf-8"),
|
|
|
|
String::from_utf8(output.stderr).expect("utf-8"),
|
|
|
|
),
|
|
|
|
Err(e) => panic!("failed to run `{:?} {:?}`: {}", cmd, args, e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! assert_that {
|
2019-09-04 21:15:19 -05:00
|
|
|
($args:expr, $($check:ident $check_args:tt)&&+) => {
|
2019-07-15 06:58:54 -05:00
|
|
|
let (stdout, stderr) = rustfmt($args);
|
2019-09-04 21:15:19 -05:00
|
|
|
if $(!stdout.$check$check_args && !stderr.$check$check_args)||* {
|
2019-07-15 06:58:54 -05:00
|
|
|
panic!(
|
|
|
|
"Output not expected for rustfmt {:?}\n\
|
2019-09-04 21:15:19 -05:00
|
|
|
expected: {}\n\
|
2019-07-15 06:58:54 -05:00
|
|
|
actual stdout:\n{}\n\
|
|
|
|
actual stderr:\n{}",
|
|
|
|
$args,
|
2019-09-04 21:15:19 -05:00
|
|
|
stringify!($( $check$check_args )&&*),
|
2019-07-15 06:58:54 -05:00
|
|
|
stdout,
|
|
|
|
stderr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:36:23 -05:00
|
|
|
#[ignore]
|
2019-07-15 06:58:54 -05:00
|
|
|
#[test]
|
|
|
|
fn print_config() {
|
|
|
|
assert_that!(
|
|
|
|
&["--print-config", "unknown"],
|
|
|
|
starts_with("Unknown print-config option")
|
|
|
|
);
|
|
|
|
assert_that!(&["--print-config", "default"], contains("max_width = 100"));
|
|
|
|
assert_that!(&["--print-config", "minimal"], contains("PATH required"));
|
|
|
|
assert_that!(
|
|
|
|
&["--print-config", "minimal", "minimal-config"],
|
|
|
|
contains("doesn't work with standard input.")
|
|
|
|
);
|
|
|
|
|
|
|
|
let (stdout, stderr) = rustfmt(&[
|
|
|
|
"--print-config",
|
|
|
|
"minimal",
|
|
|
|
"minimal-config",
|
|
|
|
"src/shape.rs",
|
|
|
|
]);
|
|
|
|
assert!(
|
|
|
|
Path::new("minimal-config").exists(),
|
|
|
|
"stdout:\n{}\nstderr:\n{}",
|
|
|
|
stdout,
|
|
|
|
stderr
|
|
|
|
);
|
|
|
|
remove_file("minimal-config").unwrap();
|
|
|
|
}
|
2019-09-04 21:15:19 -05:00
|
|
|
|
|
|
|
#[ignore]
|
|
|
|
#[test]
|
|
|
|
fn inline_config() {
|
|
|
|
// single invocation
|
|
|
|
assert_that!(
|
|
|
|
&[
|
|
|
|
"--print-config",
|
|
|
|
"current",
|
|
|
|
".",
|
|
|
|
"--config=color=Never,edition=2018"
|
|
|
|
],
|
|
|
|
contains("color = \"Never\"") && contains("edition = \"2018\"")
|
|
|
|
);
|
|
|
|
|
|
|
|
// multiple overriding invocations
|
|
|
|
assert_that!(
|
2019-09-23 19:26:50 -05:00
|
|
|
&[
|
|
|
|
"--print-config",
|
|
|
|
"current",
|
|
|
|
".",
|
|
|
|
"--config",
|
|
|
|
"color=never,edition=2018",
|
|
|
|
"--config",
|
|
|
|
"color=always,format_strings=true"
|
|
|
|
],
|
|
|
|
contains("color = \"Always\"")
|
|
|
|
&& contains("edition = \"2018\"")
|
|
|
|
&& contains("format_strings = true")
|
2019-09-04 21:15:19 -05:00
|
|
|
);
|
|
|
|
}
|