Skip to content

Commit 4b0a231

Browse files
tests: restructure and extend cargo-fmt tests
1 parent 1565af9 commit 4b0a231

File tree

9 files changed

+310
-0
lines changed

9 files changed

+310
-0
lines changed

src/cargo-fmt/test/message_format.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use super::*;
2+
3+
#[test]
4+
fn invalid_message_format() {
5+
assert_eq!(
6+
convert_message_format_to_rustfmt_args("awesome", &mut vec![]),
7+
Err(String::from(
8+
"invalid --message-format value: awesome. Allowed values are: short|json|human"
9+
)),
10+
);
11+
}
12+
13+
#[test]
14+
fn json_message_format_and_check_arg() {
15+
let mut args = vec![String::from("--check")];
16+
assert_eq!(
17+
convert_message_format_to_rustfmt_args("json", &mut args),
18+
Err(String::from(
19+
"cannot include --check arg when --message-format is set to json"
20+
)),
21+
);
22+
}
23+
24+
#[test]
25+
fn json_message_format_and_emit_arg() {
26+
let mut args = vec![String::from("--emit"), String::from("checkstyle")];
27+
assert_eq!(
28+
convert_message_format_to_rustfmt_args("json", &mut args),
29+
Err(String::from(
30+
"cannot include --emit arg when --message-format is set to json"
31+
)),
32+
);
33+
}
34+
35+
#[test]
36+
fn json_message_format() {
37+
let mut args = vec![String::from("--edition"), String::from("2018")];
38+
assert!(convert_message_format_to_rustfmt_args("json", &mut args).is_ok());
39+
assert_eq!(
40+
args,
41+
vec![
42+
String::from("--edition"),
43+
String::from("2018"),
44+
String::from("--emit"),
45+
String::from("json")
46+
]
47+
);
48+
}
49+
50+
#[test]
51+
fn human_message_format() {
52+
let exp_args = vec![String::from("--emit"), String::from("json")];
53+
let mut act_args = exp_args.clone();
54+
assert!(convert_message_format_to_rustfmt_args("human", &mut act_args).is_ok());
55+
assert_eq!(act_args, exp_args);
56+
}
57+
58+
#[test]
59+
fn short_message_format() {
60+
let mut args = vec![String::from("--check")];
61+
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
62+
assert_eq!(args, vec![String::from("--check"), String::from("-l")]);
63+
}
64+
65+
#[test]
66+
fn short_message_format_included_short_list_files_flag() {
67+
let mut args = vec![String::from("--check"), String::from("-l")];
68+
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
69+
assert_eq!(args, vec![String::from("--check"), String::from("-l")]);
70+
}
71+
72+
#[test]
73+
fn short_message_format_included_long_list_files_flag() {
74+
let mut args = vec![String::from("--check"), String::from("--files-with-diff")];
75+
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
76+
assert_eq!(
77+
args,
78+
vec![String::from("--check"), String::from("--files-with-diff")]
79+
);
80+
}

src/cargo-fmt/test/mod.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
use super::*;
2+
3+
mod message_format;
4+
mod targets;
5+
6+
#[test]
7+
fn default_options() {
8+
let empty: Vec<String> = vec![];
9+
let o = Opts::from_iter(&empty);
10+
assert_eq!(false, o.quiet);
11+
assert_eq!(false, o.verbose);
12+
assert_eq!(false, o.version);
13+
assert_eq!(false, o.check);
14+
assert_eq!(empty, o.packages);
15+
assert_eq!(empty, o.rustfmt_options);
16+
assert_eq!(false, o.format_all);
17+
assert_eq!(None, o.manifest_path);
18+
assert_eq!(None, o.message_format);
19+
}
20+
21+
#[test]
22+
fn good_options() {
23+
let o = Opts::from_iter(&[
24+
"test",
25+
"-q",
26+
"-p",
27+
"p1",
28+
"-p",
29+
"p2",
30+
"--message-format",
31+
"short",
32+
"--check",
33+
"--",
34+
"--edition",
35+
"2018",
36+
]);
37+
assert_eq!(true, o.quiet);
38+
assert_eq!(false, o.verbose);
39+
assert_eq!(false, o.version);
40+
assert_eq!(true, o.check);
41+
assert_eq!(vec!["p1", "p2"], o.packages);
42+
assert_eq!(vec!["--edition", "2018"], o.rustfmt_options);
43+
assert_eq!(false, o.format_all);
44+
assert_eq!(Some(String::from("short")), o.message_format);
45+
}
46+
47+
#[test]
48+
fn unexpected_option() {
49+
assert!(
50+
Opts::clap()
51+
.get_matches_from_safe(&["test", "unexpected"])
52+
.is_err()
53+
);
54+
}
55+
56+
#[test]
57+
fn unexpected_flag() {
58+
assert!(
59+
Opts::clap()
60+
.get_matches_from_safe(&["test", "--flag"])
61+
.is_err()
62+
);
63+
}
64+
65+
#[test]
66+
fn mandatory_separator() {
67+
assert!(
68+
Opts::clap()
69+
.get_matches_from_safe(&["test", "--emit"])
70+
.is_err()
71+
);
72+
assert!(
73+
!Opts::clap()
74+
.get_matches_from_safe(&["test", "--", "--emit"])
75+
.is_err()
76+
);
77+
}
78+
79+
#[test]
80+
fn multiple_packages_one_by_one() {
81+
let o = Opts::from_iter(&[
82+
"test",
83+
"-p",
84+
"package1",
85+
"--package",
86+
"package2",
87+
"-p",
88+
"package3",
89+
]);
90+
assert_eq!(3, o.packages.len());
91+
}
92+
93+
#[test]
94+
fn multiple_packages_grouped() {
95+
let o = Opts::from_iter(&[
96+
"test",
97+
"--package",
98+
"package1",
99+
"package2",
100+
"-p",
101+
"package3",
102+
"package4",
103+
]);
104+
assert_eq!(4, o.packages.len());
105+
}
106+
107+
#[test]
108+
fn empty_packages_1() {
109+
assert!(Opts::clap().get_matches_from_safe(&["test", "-p"]).is_err());
110+
}
111+
112+
#[test]
113+
fn empty_packages_2() {
114+
assert!(
115+
Opts::clap()
116+
.get_matches_from_safe(&["test", "-p", "--", "--check"])
117+
.is_err()
118+
);
119+
}
120+
121+
#[test]
122+
fn empty_packages_3() {
123+
assert!(
124+
Opts::clap()
125+
.get_matches_from_safe(&["test", "-p", "--verbose"])
126+
.is_err()
127+
);
128+
}
129+
130+
#[test]
131+
fn empty_packages_4() {
132+
assert!(
133+
Opts::clap()
134+
.get_matches_from_safe(&["test", "-p", "--check"])
135+
.is_err()
136+
);
137+
}

src/cargo-fmt/test/targets.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::*;
2+
3+
mod all_targets {
4+
use super::*;
5+
6+
const TESTS_ROOT: &'static str = "tests/cargo-fmt/source";
7+
8+
mod different_crate_and_dir_names {
9+
use super::*;
10+
11+
fn assert_correct_targets_loaded(manifest_suffix: &str) {
12+
let root_path = Path::new(TESTS_ROOT).join("divergent-crate-dir-names");
13+
let get_path = |exp: &str| PathBuf::from(&root_path).join(exp).canonicalize().unwrap();
14+
let manifest_path = Path::new(&root_path).join(manifest_suffix);
15+
let targets = get_targets(&CargoFmtStrategy::All, Some(manifest_path.as_path()))
16+
.expect("Targets should have been loaded");
17+
18+
assert_eq!(targets.len(), 3);
19+
assert!(targets.contains(&Target {
20+
path: get_path("dependency-dir-name/subdep-dir-name/src/lib.rs"),
21+
edition: "2018".to_owned(),
22+
kind: "lib".to_owned(),
23+
}));
24+
assert!(targets.contains(&Target {
25+
path: get_path("dependency-dir-name/src/lib.rs"),
26+
edition: "2018".to_owned(),
27+
kind: "lib".to_owned(),
28+
}));
29+
assert!(targets.contains(&Target {
30+
path: get_path("src/main.rs"),
31+
edition: "2018".to_owned(),
32+
kind: "bin".to_owned(),
33+
}));
34+
}
35+
36+
#[test]
37+
fn correct_targets_from_root() {
38+
assert_correct_targets_loaded("Cargo.toml");
39+
}
40+
41+
#[test]
42+
fn correct_targets_from_sub_local_dep() {
43+
assert_correct_targets_loaded("dependency-dir-name/Cargo.toml");
44+
}
45+
}
46+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "cargo-fmt-test"
3+
version = "0.1.0"
4+
authors = ["calebcartwright"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
indexmap = "1.0.2"
9+
10+
[workspace]
11+
members = [
12+
"dependency-dir-name",
13+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "dependency-crate-name"
3+
version = "0.1.0"
4+
authors = ["calebcartwright"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
subdep-crate-name = { path = "subdep-dir-name" }
9+
indexmap = "1.0.2"
10+
rusty-hook = "0.8.4"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[cfg(test)]
2+
mod tests {
3+
#[test]
4+
fn it_works() {
5+
assert_eq!(2 + 2, 4);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "subdep-crate-name"
3+
version = "0.1.0"
4+
authors = ["calebcartwright"]
5+
edition = "2018"
6+
7+
[dependencies]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[cfg(test)]
2+
mod tests {
3+
#[test]
4+
fn sub_test_that_works() {
5+
assert_eq!(3 + 3, 6);
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

0 commit comments

Comments
 (0)