Skip to content

Commit 9375b5a

Browse files
tests: add new load_config tests
1 parent 1f8c5eb commit 9375b5a

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

src/bin/main.rs

+124
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,127 @@ 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+
773+
fn get_config<O: CliOptions>(path: Option<&Path>, options: Option<O>) -> Config {
774+
load_config(path, options).unwrap().0
775+
}
776+
777+
#[test]
778+
fn flag_sets_style_edition_override_correctly() {
779+
let mut options = GetOptsOptions::default();
780+
options.style_edition = Some(StyleEdition::Edition2024);
781+
let config = get_config(None, Some(options));
782+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
783+
}
784+
785+
#[test]
786+
fn edition_sets_style_edition_override_correctly() {
787+
let mut options = GetOptsOptions::default();
788+
options.edition = Some(Edition::Edition2024);
789+
let config = get_config(None, Some(options));
790+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
791+
}
792+
793+
#[test]
794+
fn version_sets_style_edition_override_correctly() {
795+
let mut options = GetOptsOptions::default();
796+
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
797+
let config = get_config(None, Some(options));
798+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
799+
}
800+
801+
#[test]
802+
fn style_edition_flag_has_correct_precedence_over_edition() {
803+
let mut options = GetOptsOptions::default();
804+
options.style_edition = Some(StyleEdition::Edition2021);
805+
options.edition = Some(Edition::Edition2024);
806+
let config = get_config(None, Some(options));
807+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
808+
}
809+
810+
#[test]
811+
fn style_edition_flag_has_correct_precedence_over_version() {
812+
let mut options = GetOptsOptions::default();
813+
options.style_edition = Some(StyleEdition::Edition2018);
814+
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
815+
let config = get_config(None, Some(options));
816+
assert_eq!(config.style_edition(), StyleEdition::Edition2018);
817+
}
818+
819+
#[test]
820+
fn style_edition_flag_has_correct_precedence_over_edition_version() {
821+
let mut options = GetOptsOptions::default();
822+
options.style_edition = Some(StyleEdition::Edition2021);
823+
options.edition = Some(Edition::Edition2018);
824+
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
825+
let config = get_config(None, Some(options));
826+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
827+
}
828+
829+
#[test]
830+
fn style_edition_inline_has_correct_precedence_over_edition_version() {
831+
let mut options = GetOptsOptions::default();
832+
options.edition = Some(Edition::Edition2018);
833+
options.inline_config = HashMap::from([
834+
("version".to_owned(), "One".to_owned()),
835+
("style_edition".to_owned(), "2024".to_owned()),
836+
]);
837+
let config = get_config(None, Some(options));
838+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
839+
}
840+
841+
#[test]
842+
fn style_edition_config_file_trumps_edition_flag_version_inline() {
843+
let mut options = GetOptsOptions::default();
844+
let config_file = Some(Path::new("tests/config/style-edition/just-style-edition"));
845+
options.edition = Some(Edition::Edition2018);
846+
options.inline_config = HashMap::from([("version".to_owned(), "One".to_owned())]);
847+
let config = get_config(config_file, Some(options));
848+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
849+
}
850+
851+
#[test]
852+
fn style_edition_config_file_trumps_edition_config_and_version_inline() {
853+
let mut options = GetOptsOptions::default();
854+
let config_file = Some(Path::new(
855+
"tests/config/style-edition/style-edition-and-edition",
856+
));
857+
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
858+
let config = get_config(config_file, Some(options));
859+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
860+
assert_eq!(config.edition(), Edition::Edition2024);
861+
}
862+
863+
#[test]
864+
fn version_config_trumps_edition_config_and_flag() {
865+
let mut options = GetOptsOptions::default();
866+
let config_file = Some(Path::new("tests/config/style-edition/version-edition"));
867+
options.edition = Some(Edition::Edition2018);
868+
let config = get_config(config_file, Some(options));
869+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
870+
}
871+
872+
#[test]
873+
fn style_edition_config_file_trumps_version_config() {
874+
let options = GetOptsOptions::default();
875+
let config_file = Some(Path::new(
876+
"tests/config/style-edition/version-style-edition",
877+
));
878+
let config = get_config(config_file, Some(options));
879+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
880+
}
881+
882+
#[test]
883+
fn style_edition_config_file_trumps_edition_version_config() {
884+
let options = GetOptsOptions::default();
885+
let config_file = Some(Path::new(
886+
"tests/config/style-edition/version-style-edition-and-edition",
887+
));
888+
let config = get_config(config_file, Some(options));
889+
assert_eq!(config.style_edition(), StyleEdition::Edition2021);
890+
}
891+
}
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)