Skip to content

Commit e362297

Browse files
tests: add new load_config tests
1 parent 1f8c5eb commit e362297

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

src/bin/main.rs

+137
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,140 @@ fn emit_mode_from_emit_str(emit_str: &str) -> Result<EmitMode> {
765765
_ => Err(format_err!("Invalid value for `--emit`")),
766766
}
767767
}
768+
769+
#[cfg(test)]
770+
mod test {
771+
use super::*;
772+
use rustfmt_config_proc_macro::nightly_only_test;
773+
774+
fn get_config<O: CliOptions>(path: Option<&Path>, options: Option<O>) -> Config {
775+
load_config(path, options).unwrap().0
776+
}
777+
778+
#[nightly_only_test]
779+
#[test]
780+
fn flag_sets_style_edition_override_correctly() {
781+
let mut options = GetOptsOptions::default();
782+
options.style_edition = Some(StyleEdition::Edition2024);
783+
let config = get_config(None, Some(options));
784+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
785+
}
786+
787+
#[nightly_only_test]
788+
#[test]
789+
fn edition_sets_style_edition_override_correctly() {
790+
let mut options = GetOptsOptions::default();
791+
options.edition = Some(Edition::Edition2024);
792+
let config = get_config(None, Some(options));
793+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
794+
}
795+
796+
#[nightly_only_test]
797+
#[test]
798+
fn version_sets_style_edition_override_correctly() {
799+
let mut options = GetOptsOptions::default();
800+
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
801+
let config = get_config(None, Some(options));
802+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
803+
}
804+
805+
#[nightly_only_test]
806+
#[test]
807+
fn style_edition_flag_has_correct_precedence_over_edition() {
808+
let mut options = GetOptsOptions::default();
809+
options.style_edition = Some(StyleEdition::Edition2021);
810+
options.edition = Some(Edition::Edition2024);
811+
let config = get_config(None, Some(options));
812+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
813+
}
814+
815+
#[nightly_only_test]
816+
#[test]
817+
fn style_edition_flag_has_correct_precedence_over_version() {
818+
let mut options = GetOptsOptions::default();
819+
options.style_edition = Some(StyleEdition::Edition2018);
820+
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
821+
let config = get_config(None, Some(options));
822+
assert_eq!(config.style_edition(), StyleEdition::Edition2018);
823+
}
824+
825+
#[nightly_only_test]
826+
#[test]
827+
fn style_edition_flag_has_correct_precedence_over_edition_version() {
828+
let mut options = GetOptsOptions::default();
829+
options.style_edition = Some(StyleEdition::Edition2021);
830+
options.edition = Some(Edition::Edition2018);
831+
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
832+
let config = get_config(None, Some(options));
833+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
834+
}
835+
836+
#[nightly_only_test]
837+
#[test]
838+
fn style_edition_inline_has_correct_precedence_over_edition_version() {
839+
let mut options = GetOptsOptions::default();
840+
options.edition = Some(Edition::Edition2018);
841+
options.inline_config = HashMap::from([
842+
("version".to_owned(), "One".to_owned()),
843+
("style_edition".to_owned(), "2024".to_owned()),
844+
]);
845+
let config = get_config(None, Some(options));
846+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
847+
}
848+
849+
#[nightly_only_test]
850+
#[test]
851+
fn style_edition_config_file_trumps_edition_flag_version_inline() {
852+
let mut options = GetOptsOptions::default();
853+
let config_file = Some(Path::new("tests/config/style-edition/just-style-edition"));
854+
options.edition = Some(Edition::Edition2018);
855+
options.inline_config = HashMap::from([("version".to_owned(), "One".to_owned())]);
856+
let config = get_config(config_file, Some(options));
857+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
858+
}
859+
860+
#[nightly_only_test]
861+
#[test]
862+
fn style_edition_config_file_trumps_edition_config_and_version_inline() {
863+
let mut options = GetOptsOptions::default();
864+
let config_file = Some(Path::new(
865+
"tests/config/style-edition/style-edition-and-edition",
866+
));
867+
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
868+
let config = get_config(config_file, Some(options));
869+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
870+
assert_eq!(config.edition(), Edition::Edition2024);
871+
}
872+
873+
#[nightly_only_test]
874+
#[test]
875+
fn version_config_trumps_edition_config_and_flag() {
876+
let mut options = GetOptsOptions::default();
877+
let config_file = Some(Path::new("tests/config/style-edition/version-edition"));
878+
options.edition = Some(Edition::Edition2018);
879+
let config = get_config(config_file, Some(options));
880+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
881+
}
882+
883+
#[nightly_only_test]
884+
#[test]
885+
fn style_edition_config_file_trumps_version_config() {
886+
let options = GetOptsOptions::default();
887+
let config_file = Some(Path::new(
888+
"tests/config/style-edition/version-style-edition",
889+
));
890+
let config = get_config(config_file, Some(options));
891+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
892+
}
893+
894+
#[nightly_only_test]
895+
#[test]
896+
fn style_edition_config_file_trumps_edition_version_config() {
897+
let options = GetOptsOptions::default();
898+
let config_file = Some(Path::new(
899+
"tests/config/style-edition/version-style-edition-and-edition",
900+
));
901+
let config = get_config(config_file, Some(options));
902+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
903+
}
904+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style_edition = "2024"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
style_edition = "2021"
2+
edition = "2024"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = "Two"
2+
edition = "2018"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version = "Two"
2+
edition = "2018"
3+
style_edition = "2021"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = "Two"
2+
style_edition = "2021"

0 commit comments

Comments
 (0)