about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorCaleb Cartwright <caleb.cartwright@outlook.com>2021-09-20 18:58:38 -0500
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2021-09-23 21:12:57 -0500
commit7f6229b9aada69389be4ead0ecbec8aed5892a24 (patch)
treefa906cbf779bdc8626a67e2b9daacdae14868c74 /src
parent74df7b3265702949105161b36aee8b0975907210 (diff)
downloadrust-7f6229b9aada69389be4ead0ecbec8aed5892a24.tar.gz
rust-7f6229b9aada69389be4ead0ecbec8aed5892a24.zip
tests: restructure and extend cargo-fmt tests
Diffstat (limited to 'src')
-rw-r--r--src/cargo-fmt/test/message_format.rs80
-rw-r--r--src/cargo-fmt/test/mod.rs137
-rw-r--r--src/cargo-fmt/test/targets.rs134
3 files changed, 351 insertions, 0 deletions
diff --git a/src/cargo-fmt/test/message_format.rs b/src/cargo-fmt/test/message_format.rs
new file mode 100644
index 00000000000..bf44924f13c
--- /dev/null
+++ b/src/cargo-fmt/test/message_format.rs
@@ -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")]
+    );
+}
diff --git a/src/cargo-fmt/test/mod.rs b/src/cargo-fmt/test/mod.rs
new file mode 100644
index 00000000000..360503632c7
--- /dev/null
+++ b/src/cargo-fmt/test/mod.rs
@@ -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()
+    );
+}
diff --git a/src/cargo-fmt/test/targets.rs b/src/cargo-fmt/test/targets.rs
new file mode 100644
index 00000000000..b7e7fabdf71
--- /dev/null
+++ b/src/cargo-fmt/test/targets.rs
@@ -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");
+        }
+    }
+}