Skip to content

Commit 7f6229b

Browse files
tests: restructure and extend cargo-fmt tests
1 parent 74df7b3 commit 7f6229b

File tree

23 files changed

+443
-0
lines changed

23 files changed

+443
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Generated by Cargo
66
# will have compiled files and executables
77
/target
8+
tests/cargo-fmt/**/target
89

910
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
1011
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock

Diff for: src/cargo-fmt/test/message_format.rs

+80
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+
}

Diff for: src/cargo-fmt/test/mod.rs

+137
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+
}

Diff for: src/cargo-fmt/test/targets.rs

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
use super::*;
2+
3+
struct ExpTarget {
4+
path: &'static str,
5+
edition: &'static str,
6+
kind: &'static str,
7+
}
8+
9+
mod all_targets {
10+
use super::*;
11+
12+
fn assert_correct_targets_loaded(
13+
manifest_suffix: &str,
14+
source_root: &str,
15+
exp_targets: &[ExpTarget],
16+
exp_num_targets: usize,
17+
) {
18+
let root_path = Path::new("tests/cargo-fmt/source").join(source_root);
19+
let get_path = |exp: &str| PathBuf::from(&root_path).join(exp).canonicalize().unwrap();
20+
let manifest_path = Path::new(&root_path).join(manifest_suffix);
21+
let targets = get_targets(&CargoFmtStrategy::All, Some(manifest_path.as_path()))
22+
.expect("Targets should have been loaded");
23+
24+
assert_eq!(targets.len(), exp_num_targets);
25+
26+
for target in exp_targets {
27+
assert!(targets.contains(&Target {
28+
path: get_path(target.path),
29+
edition: target.edition.to_owned(),
30+
kind: target.kind.to_owned(),
31+
}));
32+
}
33+
}
34+
35+
mod different_crate_and_dir_names {
36+
use super::*;
37+
38+
fn assert_correct_targets_loaded(manifest_suffix: &str) {
39+
let exp_targets = vec![
40+
ExpTarget {
41+
path: "dependency-dir-name/subdep-dir-name/src/lib.rs",
42+
edition: "2018",
43+
kind: "lib",
44+
},
45+
ExpTarget {
46+
path: "dependency-dir-name/src/lib.rs",
47+
edition: "2018",
48+
kind: "lib",
49+
},
50+
ExpTarget {
51+
path: "src/main.rs",
52+
edition: "2018",
53+
kind: "main",
54+
},
55+
];
56+
super::assert_correct_targets_loaded(
57+
manifest_suffix,
58+
"divergent-crate-dir-names",
59+
&exp_targets,
60+
3,
61+
);
62+
}
63+
64+
#[test]
65+
fn correct_targets_from_root() {
66+
assert_correct_targets_loaded("Cargo.toml");
67+
}
68+
69+
#[test]
70+
fn correct_targets_from_sub_local_dep() {
71+
assert_correct_targets_loaded("dependency-dir-name/Cargo.toml");
72+
}
73+
}
74+
75+
mod workspaces {
76+
use super::*;
77+
78+
fn assert_correct_targets_loaded(manifest_suffix: &str) {
79+
let exp_targets = vec![
80+
ExpTarget {
81+
path: "ws/a/src/main.rs",
82+
edition: "2018",
83+
kind: "bin",
84+
},
85+
ExpTarget {
86+
path: "ws/b/src/main.rs",
87+
edition: "2018",
88+
kind: "bin",
89+
},
90+
ExpTarget {
91+
path: "ws/c/src/lib.rs",
92+
edition: "2018",
93+
kind: "lib",
94+
},
95+
ExpTarget {
96+
path: "ws/a/d/src/lib.rs",
97+
edition: "2018",
98+
kind: "lib",
99+
},
100+
ExpTarget {
101+
path: "e/src/main.rs",
102+
edition: "2018",
103+
kind: "main",
104+
},
105+
ExpTarget {
106+
path: "ws/a/d/f/src/lib.rs",
107+
edition: "2018",
108+
kind: "lib",
109+
},
110+
];
111+
super::assert_correct_targets_loaded(
112+
manifest_suffix,
113+
"workspaces/path-dep-above",
114+
&exp_targets,
115+
6,
116+
);
117+
}
118+
119+
#[test]
120+
fn includes_outside_workspace_deps() {
121+
assert_correct_targets_loaded("ws/Cargo.toml");
122+
}
123+
124+
#[test]
125+
fn includes_workspace_from_dep_above() {
126+
assert_correct_targets_loaded("e/Cargo.toml");
127+
}
128+
129+
#[test]
130+
fn includes_all_packages_from_workspace_subdir() {
131+
assert_correct_targets_loaded("ws/a/d/f/Cargo.toml");
132+
}
133+
}
134+
}
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+
]
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"
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+
}

0 commit comments

Comments
 (0)