about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCaleb Cartwright <caleb.cartwright@outlook.com>2024-07-28 16:03:46 -0500
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2024-07-28 19:37:36 -0500
commitd66ab31b535a293d9d47bbbaeb6e8733bf0375db (patch)
tree6c0a5786b685f02c41a3bb64f3e0dec2f88989e1
parentd65daa9c4fb9be42a21a26a91451606a87f80702 (diff)
downloadrust-d66ab31b535a293d9d47bbbaeb6e8733bf0375db.tar.gz
rust-d66ab31b535a293d9d47bbbaeb6e8733bf0375db.zip
tests: add new load_config tests
-rw-r--r--src/bin/main.rs138
-rw-r--r--tests/config/style-edition/just-style-edition/rustfmt.toml1
-rw-r--r--tests/config/style-edition/style-edition-and-edition/rustfmt.toml2
-rw-r--r--tests/config/style-edition/version-edition/rustfmt.toml2
-rw-r--r--tests/config/style-edition/version-style-edition-and-edition/rustfmt.toml3
-rw-r--r--tests/config/style-edition/version-style-edition/rustfmt.toml2
6 files changed, 148 insertions, 0 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs
index 0876c515dde..6959f56ccc7 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -765,3 +765,141 @@ fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode> {
         _ => Err(format_err!("Invalid value for `--emit`")),
     }
 }
+
+#[cfg(test)]
+#[allow(dead_code)]
+mod test {
+    use super::*;
+    use rustfmt_config_proc_macro::nightly_only_test;
+
+    fn get_config<O: CliOptions>(path: Option<&Path>, options: Option<O>) -> Config {
+        load_config(path, options).unwrap().0
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn flag_sets_style_edition_override_correctly() {
+        let mut options = GetOptsOptions::default();
+        options.style_edition = Some(StyleEdition::Edition2024);
+        let config = get_config(None, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2024);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn edition_sets_style_edition_override_correctly() {
+        let mut options = GetOptsOptions::default();
+        options.edition = Some(Edition::Edition2024);
+        let config = get_config(None, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2024);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn version_sets_style_edition_override_correctly() {
+        let mut options = GetOptsOptions::default();
+        options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
+        let config = get_config(None, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2024);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn style_edition_flag_has_correct_precedence_over_edition() {
+        let mut options = GetOptsOptions::default();
+        options.style_edition = Some(StyleEdition::Edition2021);
+        options.edition = Some(Edition::Edition2024);
+        let config = get_config(None, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2021);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn style_edition_flag_has_correct_precedence_over_version() {
+        let mut options = GetOptsOptions::default();
+        options.style_edition = Some(StyleEdition::Edition2018);
+        options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
+        let config = get_config(None, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2018);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn style_edition_flag_has_correct_precedence_over_edition_version() {
+        let mut options = GetOptsOptions::default();
+        options.style_edition = Some(StyleEdition::Edition2021);
+        options.edition = Some(Edition::Edition2018);
+        options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
+        let config = get_config(None, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2021);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn style_edition_inline_has_correct_precedence_over_edition_version() {
+        let mut options = GetOptsOptions::default();
+        options.edition = Some(Edition::Edition2018);
+        options.inline_config = HashMap::from([
+            ("version".to_owned(), "One".to_owned()),
+            ("style_edition".to_owned(), "2024".to_owned()),
+        ]);
+        let config = get_config(None, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2024);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn style_edition_config_file_trumps_edition_flag_version_inline() {
+        let mut options = GetOptsOptions::default();
+        let config_file = Some(Path::new("tests/config/style-edition/just-style-edition"));
+        options.edition = Some(Edition::Edition2018);
+        options.inline_config = HashMap::from([("version".to_owned(), "One".to_owned())]);
+        let config = get_config(config_file, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2024);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn style_edition_config_file_trumps_edition_config_and_version_inline() {
+        let mut options = GetOptsOptions::default();
+        let config_file = Some(Path::new(
+            "tests/config/style-edition/style-edition-and-edition",
+        ));
+        options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
+        let config = get_config(config_file, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2021);
+        assert_eq!(config.edition(), Edition::Edition2024);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn version_config_trumps_edition_config_and_flag() {
+        let mut options = GetOptsOptions::default();
+        let config_file = Some(Path::new("tests/config/style-edition/version-edition"));
+        options.edition = Some(Edition::Edition2018);
+        let config = get_config(config_file, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2024);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn style_edition_config_file_trumps_version_config() {
+        let options = GetOptsOptions::default();
+        let config_file = Some(Path::new(
+            "tests/config/style-edition/version-style-edition",
+        ));
+        let config = get_config(config_file, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2021);
+    }
+
+    #[nightly_only_test]
+    #[test]
+    fn style_edition_config_file_trumps_edition_version_config() {
+        let options = GetOptsOptions::default();
+        let config_file = Some(Path::new(
+            "tests/config/style-edition/version-style-edition-and-edition",
+        ));
+        let config = get_config(config_file, Some(options));
+        assert_eq!(config.style_edition(), StyleEdition::Edition2021);
+    }
+}
diff --git a/tests/config/style-edition/just-style-edition/rustfmt.toml b/tests/config/style-edition/just-style-edition/rustfmt.toml
new file mode 100644
index 00000000000..3501136812c
--- /dev/null
+++ b/tests/config/style-edition/just-style-edition/rustfmt.toml
@@ -0,0 +1 @@
+style_edition = "2024"
diff --git a/tests/config/style-edition/style-edition-and-edition/rustfmt.toml b/tests/config/style-edition/style-edition-and-edition/rustfmt.toml
new file mode 100644
index 00000000000..92844e03ab6
--- /dev/null
+++ b/tests/config/style-edition/style-edition-and-edition/rustfmt.toml
@@ -0,0 +1,2 @@
+style_edition = "2021"
+edition = "2024"
diff --git a/tests/config/style-edition/version-edition/rustfmt.toml b/tests/config/style-edition/version-edition/rustfmt.toml
new file mode 100644
index 00000000000..16ea9a13f36
--- /dev/null
+++ b/tests/config/style-edition/version-edition/rustfmt.toml
@@ -0,0 +1,2 @@
+version = "Two"
+edition = "2018"
diff --git a/tests/config/style-edition/version-style-edition-and-edition/rustfmt.toml b/tests/config/style-edition/version-style-edition-and-edition/rustfmt.toml
new file mode 100644
index 00000000000..187ba13cfb6
--- /dev/null
+++ b/tests/config/style-edition/version-style-edition-and-edition/rustfmt.toml
@@ -0,0 +1,3 @@
+version = "Two"
+edition = "2018"
+style_edition = "2021"
diff --git a/tests/config/style-edition/version-style-edition/rustfmt.toml b/tests/config/style-edition/version-style-edition/rustfmt.toml
new file mode 100644
index 00000000000..c894bd981bc
--- /dev/null
+++ b/tests/config/style-edition/version-style-edition/rustfmt.toml
@@ -0,0 +1,2 @@
+version = "Two"
+style_edition = "2021"