Skip to content

Commit e55fc6b

Browse files
ehusstopecongiro
authored andcommitted
Fix using --help, --verbose, etc. (rust-lang#3620)
1 parent 37695b3 commit e55fc6b

File tree

2 files changed

+81
-11
lines changed

2 files changed

+81
-11
lines changed

src/cargo-fmt/main.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ fn execute() -> i32 {
8585
};
8686

8787
if opts.version {
88-
return handle_command_status(get_version());
88+
return handle_command_status(get_rustfmt_info(&[String::from("--version")]));
89+
}
90+
if opts.rustfmt_options.iter().any(|s| {
91+
["--print-config", "-h", "--help", "-V", "--version"].contains(&s.as_str())
92+
|| s.starts_with("--help=")
93+
|| s.starts_with("--print-config=")
94+
}) {
95+
return handle_command_status(get_rustfmt_info(&opts.rustfmt_options));
8996
}
9097

9198
let strategy = CargoFmtStrategy::from_opts(&opts);
@@ -118,10 +125,10 @@ fn handle_command_status(status: Result<i32, io::Error>) -> i32 {
118125
}
119126
}
120127

121-
fn get_version() -> Result<i32, io::Error> {
128+
fn get_rustfmt_info(args: &[String]) -> Result<i32, io::Error> {
122129
let mut command = Command::new("rustfmt")
123130
.stdout(std::process::Stdio::inherit())
124-
.args(&[String::from("--version")])
131+
.args(args)
125132
.spawn()
126133
.map_err(|e| match e.kind() {
127134
io::ErrorKind::NotFound => io::Error::new(
@@ -143,14 +150,7 @@ fn format_crate(
143150
strategy: &CargoFmtStrategy,
144151
rustfmt_args: Vec<String>,
145152
) -> Result<i32, io::Error> {
146-
let targets = if rustfmt_args
147-
.iter()
148-
.any(|s| ["--print-config", "-h", "--help", "-V", "--version"].contains(&s.as_str()))
149-
{
150-
BTreeSet::new()
151-
} else {
152-
get_targets(strategy)?
153-
};
153+
let targets = get_targets(strategy)?;
154154

155155
// Currently only bin and lib files get formatted.
156156
run_rustfmt(&targets, &rustfmt_args, verbosity)

tests/cargo-fmt/main.rs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)