From e362297e9fb2be50fde04a457d0804a1412b3d87 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sun, 28 Jul 2024 16:03:46 -0500 Subject: [PATCH] tests: add new load_config tests --- src/bin/main.rs | 137 ++++++++++++++++++ .../just-style-edition/rustfmt.toml | 1 + .../style-edition-and-edition/rustfmt.toml | 2 + .../version-edition/rustfmt.toml | 2 + .../rustfmt.toml | 3 + .../version-style-edition/rustfmt.toml | 2 + 6 files changed, 147 insertions(+) create mode 100644 tests/config/style-edition/just-style-edition/rustfmt.toml create mode 100644 tests/config/style-edition/style-edition-and-edition/rustfmt.toml create mode 100644 tests/config/style-edition/version-edition/rustfmt.toml create mode 100644 tests/config/style-edition/version-style-edition-and-edition/rustfmt.toml create mode 100644 tests/config/style-edition/version-style-edition/rustfmt.toml diff --git a/src/bin/main.rs b/src/bin/main.rs index 0876c515dde..0c200efd334 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -765,3 +765,140 @@ fn emit_mode_from_emit_str(emit_str: &str) -> Result { _ => Err(format_err!("Invalid value for `--emit`")), } } + +#[cfg(test)] +mod test { + use super::*; + use rustfmt_config_proc_macro::nightly_only_test; + + fn get_config(path: Option<&Path>, options: Option) -> 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"