tests: restructure and extend cargo-fmt tests

This commit is contained in:
Caleb Cartwright 2021-09-20 18:58:38 -05:00 committed by Caleb Cartwright
parent 74df7b3265
commit 7f6229b9aa
23 changed files with 443 additions and 0 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@
# Generated by Cargo
# will have compiled files and executables
/target
tests/cargo-fmt/**/target
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock

View File

@ -0,0 +1,80 @@
use super::*;
#[test]
fn invalid_message_format() {
assert_eq!(
convert_message_format_to_rustfmt_args("awesome", &mut vec![]),
Err(String::from(
"invalid --message-format value: awesome. Allowed values are: short|json|human"
)),
);
}
#[test]
fn json_message_format_and_check_arg() {
let mut args = vec![String::from("--check")];
assert_eq!(
convert_message_format_to_rustfmt_args("json", &mut args),
Err(String::from(
"cannot include --check arg when --message-format is set to json"
)),
);
}
#[test]
fn json_message_format_and_emit_arg() {
let mut args = vec![String::from("--emit"), String::from("checkstyle")];
assert_eq!(
convert_message_format_to_rustfmt_args("json", &mut args),
Err(String::from(
"cannot include --emit arg when --message-format is set to json"
)),
);
}
#[test]
fn json_message_format() {
let mut args = vec![String::from("--edition"), String::from("2018")];
assert!(convert_message_format_to_rustfmt_args("json", &mut args).is_ok());
assert_eq!(
args,
vec![
String::from("--edition"),
String::from("2018"),
String::from("--emit"),
String::from("json")
]
);
}
#[test]
fn human_message_format() {
let exp_args = vec![String::from("--emit"), String::from("json")];
let mut act_args = exp_args.clone();
assert!(convert_message_format_to_rustfmt_args("human", &mut act_args).is_ok());
assert_eq!(act_args, exp_args);
}
#[test]
fn short_message_format() {
let mut args = vec![String::from("--check")];
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
assert_eq!(args, vec![String::from("--check"), String::from("-l")]);
}
#[test]
fn short_message_format_included_short_list_files_flag() {
let mut args = vec![String::from("--check"), String::from("-l")];
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
assert_eq!(args, vec![String::from("--check"), String::from("-l")]);
}
#[test]
fn short_message_format_included_long_list_files_flag() {
let mut args = vec![String::from("--check"), String::from("--files-with-diff")];
assert!(convert_message_format_to_rustfmt_args("short", &mut args).is_ok());
assert_eq!(
args,
vec![String::from("--check"), String::from("--files-with-diff")]
);
}

137
src/cargo-fmt/test/mod.rs Normal file
View File

@ -0,0 +1,137 @@
use super::*;
mod message_format;
mod targets;
#[test]
fn default_options() {
let empty: Vec<String> = vec![];
let o = Opts::from_iter(&empty);
assert_eq!(false, o.quiet);
assert_eq!(false, o.verbose);
assert_eq!(false, o.version);
assert_eq!(false, o.check);
assert_eq!(empty, o.packages);
assert_eq!(empty, o.rustfmt_options);
assert_eq!(false, o.format_all);
assert_eq!(None, o.manifest_path);
assert_eq!(None, o.message_format);
}
#[test]
fn good_options() {
let o = Opts::from_iter(&[
"test",
"-q",
"-p",
"p1",
"-p",
"p2",
"--message-format",
"short",
"--check",
"--",
"--edition",
"2018",
]);
assert_eq!(true, o.quiet);
assert_eq!(false, o.verbose);
assert_eq!(false, o.version);
assert_eq!(true, o.check);
assert_eq!(vec!["p1", "p2"], o.packages);
assert_eq!(vec!["--edition", "2018"], o.rustfmt_options);
assert_eq!(false, o.format_all);
assert_eq!(Some(String::from("short")), o.message_format);
}
#[test]
fn unexpected_option() {
assert!(
Opts::clap()
.get_matches_from_safe(&["test", "unexpected"])
.is_err()
);
}
#[test]
fn unexpected_flag() {
assert!(
Opts::clap()
.get_matches_from_safe(&["test", "--flag"])
.is_err()
);
}
#[test]
fn mandatory_separator() {
assert!(
Opts::clap()
.get_matches_from_safe(&["test", "--emit"])
.is_err()
);
assert!(
!Opts::clap()
.get_matches_from_safe(&["test", "--", "--emit"])
.is_err()
);
}
#[test]
fn multiple_packages_one_by_one() {
let o = Opts::from_iter(&[
"test",
"-p",
"package1",
"--package",
"package2",
"-p",
"package3",
]);
assert_eq!(3, o.packages.len());
}
#[test]
fn multiple_packages_grouped() {
let o = Opts::from_iter(&[
"test",
"--package",
"package1",
"package2",
"-p",
"package3",
"package4",
]);
assert_eq!(4, o.packages.len());
}
#[test]
fn empty_packages_1() {
assert!(Opts::clap().get_matches_from_safe(&["test", "-p"]).is_err());
}
#[test]
fn empty_packages_2() {
assert!(
Opts::clap()
.get_matches_from_safe(&["test", "-p", "--", "--check"])
.is_err()
);
}
#[test]
fn empty_packages_3() {
assert!(
Opts::clap()
.get_matches_from_safe(&["test", "-p", "--verbose"])
.is_err()
);
}
#[test]
fn empty_packages_4() {
assert!(
Opts::clap()
.get_matches_from_safe(&["test", "-p", "--check"])
.is_err()
);
}

View File

@ -0,0 +1,134 @@
use super::*;
struct ExpTarget {
path: &'static str,
edition: &'static str,
kind: &'static str,
}
mod all_targets {
use super::*;
fn assert_correct_targets_loaded(
manifest_suffix: &str,
source_root: &str,
exp_targets: &[ExpTarget],
exp_num_targets: usize,
) {
let root_path = Path::new("tests/cargo-fmt/source").join(source_root);
let get_path = |exp: &str| PathBuf::from(&root_path).join(exp).canonicalize().unwrap();
let manifest_path = Path::new(&root_path).join(manifest_suffix);
let targets = get_targets(&CargoFmtStrategy::All, Some(manifest_path.as_path()))
.expect("Targets should have been loaded");
assert_eq!(targets.len(), exp_num_targets);
for target in exp_targets {
assert!(targets.contains(&Target {
path: get_path(target.path),
edition: target.edition.to_owned(),
kind: target.kind.to_owned(),
}));
}
}
mod different_crate_and_dir_names {
use super::*;
fn assert_correct_targets_loaded(manifest_suffix: &str) {
let exp_targets = vec![
ExpTarget {
path: "dependency-dir-name/subdep-dir-name/src/lib.rs",
edition: "2018",
kind: "lib",
},
ExpTarget {
path: "dependency-dir-name/src/lib.rs",
edition: "2018",
kind: "lib",
},
ExpTarget {
path: "src/main.rs",
edition: "2018",
kind: "main",
},
];
super::assert_correct_targets_loaded(
manifest_suffix,
"divergent-crate-dir-names",
&exp_targets,
3,
);
}
#[test]
fn correct_targets_from_root() {
assert_correct_targets_loaded("Cargo.toml");
}
#[test]
fn correct_targets_from_sub_local_dep() {
assert_correct_targets_loaded("dependency-dir-name/Cargo.toml");
}
}
mod workspaces {
use super::*;
fn assert_correct_targets_loaded(manifest_suffix: &str) {
let exp_targets = vec![
ExpTarget {
path: "ws/a/src/main.rs",
edition: "2018",
kind: "bin",
},
ExpTarget {
path: "ws/b/src/main.rs",
edition: "2018",
kind: "bin",
},
ExpTarget {
path: "ws/c/src/lib.rs",
edition: "2018",
kind: "lib",
},
ExpTarget {
path: "ws/a/d/src/lib.rs",
edition: "2018",
kind: "lib",
},
ExpTarget {
path: "e/src/main.rs",
edition: "2018",
kind: "main",
},
ExpTarget {
path: "ws/a/d/f/src/lib.rs",
edition: "2018",
kind: "lib",
},
];
super::assert_correct_targets_loaded(
manifest_suffix,
"workspaces/path-dep-above",
&exp_targets,
6,
);
}
#[test]
fn includes_outside_workspace_deps() {
assert_correct_targets_loaded("ws/Cargo.toml");
}
#[test]
fn includes_workspace_from_dep_above() {
assert_correct_targets_loaded("e/Cargo.toml");
}
#[test]
fn includes_all_packages_from_workspace_subdir() {
assert_correct_targets_loaded("ws/a/d/f/Cargo.toml");
}
}
}

View File

@ -0,0 +1,13 @@
[package]
name = "cargo-fmt-test"
version = "0.1.0"
authors = ["calebcartwright"]
edition = "2018"
[dependencies]
indexmap = "1.0.2"
[workspace]
members = [
"dependency-dir-name",
]

View File

@ -0,0 +1,10 @@
[package]
name = "dependency-crate-name"
version = "0.1.0"
authors = ["calebcartwright"]
edition = "2018"
[dependencies]
subdep-crate-name = { path = "subdep-dir-name" }
indexmap = "1.0.2"
rusty-hook = "0.8.4"

View File

@ -0,0 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

View File

@ -0,0 +1,7 @@
[package]
name = "subdep-crate-name"
version = "0.1.0"
authors = ["calebcartwright"]
edition = "2018"
[dependencies]

View File

@ -0,0 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn sub_test_that_works() {
assert_eq!(3 + 3, 6);
}
}

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -0,0 +1,6 @@
[package]
name = "e"
version = "0.1.0"
edition = "2018"
[dependencies]
c = { path = "../ws/c" }

View File

@ -0,0 +1 @@
struct E{ }

View File

@ -0,0 +1,5 @@
[workspace]
members = [
"a",
"b"
]

View File

@ -0,0 +1,6 @@
[package]
name = "a"
version = "0.1.0"
edition = "2018"
[dependencies]
d = { path = "./d" }

View File

@ -0,0 +1,7 @@
[package]
name = "d"
version = "0.1.0"
edition = "2018"
[dependencies]
e = { path = "../../../e" }
f = { path = "f" }

View File

@ -0,0 +1,4 @@
[package]
name = "f"
version = "0.1.0"
edition = "2018"

View File

@ -0,0 +1 @@
struct F{ }

View File

@ -0,0 +1 @@
struct D{ }

View File

@ -0,0 +1 @@
struct D{ }

View File

@ -0,0 +1,6 @@
[package]
name = "b"
version = "0.1.0"
edition = "2018"
[dependencies]
c = { path = "../c" }

View File

@ -0,0 +1 @@
struct B{ }

View File

@ -0,0 +1,4 @@
[package]
name = "c"
version = "0.1.0"
edition = "2018"

View File

@ -0,0 +1 @@
struct C{ }