use super::{CONFIG_FILE_NAME, LoadedConfig, load_repository_config};
use crate::config_schema::{
ForbiddenStringsOptions, MarkdownAutofixOptions, MarkdownRule, PolicySetting, RulesFileEntry,
};
use crate::policy_registry::{PolicyId, Severity};
use crate::test_support::{fixture, remove};
use std::path::PathBuf;
fn repository_root() -> PathBuf {
return PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../..");
}
fn load_shipped() -> LoadedConfig {
let root: PathBuf = repository_root();
return load_repository_config(root.as_path()).unwrap_or_else(|error| {
panic!(
"the repository's {CONFIG_FILE_NAME} under {} must load: {}",
root.display(),
error.message
)
});
}
#[test]
fn the_repository_ships_a_configuration_file() {
let loaded: LoadedConfig = load_shipped();
assert_eq!(
loaded.source,
Some(repository_root().join(CONFIG_FILE_NAME))
);
}
#[test]
fn the_repository_configuration_lists_all_four_optional_policies() {
let loaded: LoadedConfig = load_shipped();
for (id, severity) in [
(PolicyId::MarkdownAutofix, Severity::Warn),
(PolicyId::ForbiddenRootContext, Severity::Error),
(PolicyId::DependentVersionBump, Severity::Error),
(PolicyId::ForbiddenStrings, Severity::Error),
] {
assert_eq!(
loaded.config.policies.setting(id),
PolicySetting {
id,
severity,
explicit: true
},
"{id:?} must be listed in the repository configuration at the incumbent's severity"
);
}
}
#[test]
fn the_repository_configuration_sets_rules_file() {
let loaded: LoadedConfig = load_shipped();
assert_eq!(
loaded.config.policies.forbidden_strings,
ForbiddenStringsOptions {
builtin_rules: true,
rules_files: Some(vec![
RulesFileEntry {
path: String::from("forbidden-strings.append.txt"),
optional: false,
},
RulesFileEntry {
path: String::from("forbidden-strings.append.local.txt"),
optional: true,
},
]),
}
);
}
#[test]
fn the_repository_configuration_keeps_the_markdown_options() {
let loaded: LoadedConfig = load_shipped();
assert_eq!(
loaded.config.policies.markdown_autofix,
MarkdownAutofixOptions {
rules: vec![MarkdownRule::LfsImageUrl],
exclude: vec![String::from("package/ssg/")],
}
);
}
#[test]
fn the_repository_configuration_activates_the_catalog_policy_at_error() {
let loaded: LoadedConfig = load_shipped();
assert_eq!(
loaded.config.policies.setting(PolicyId::CatalogSpecifiers),
PolicySetting {
id: PolicyId::CatalogSpecifiers,
severity: Severity::Error,
explicit: true
},
"the catalog policy is listed at error; change it deliberately and update this control"
);
}
#[test]
fn a_configuration_that_does_not_name_the_catalog_policy_behaves_as_before() {
let root: PathBuf = fixture("config-before-catalog");
std::fs::write(
root.join(CONFIG_FILE_NAME),
concat!(
"{ \"policies\": { \"markdown/autofix\": [\"warn\", { \"rules\": [\"lfs-image-url\"], \"exclude\": [\"package/ssg/\"] }],\n",
" \"mono/dependent-version-bump\": \"error\", \"mono/forbidden-root-context\": \"error\",\n",
" \"security/forbidden-strings\": [\"error\", { \"builtinRules\": true }] } }\n"
),
)
.expect("older configuration");
let older: LoadedConfig = load_repository_config(root.as_path()).expect("the older file loads");
for (id, severity, explicit) in [
(PolicyId::MarkdownAutofix, Severity::Warn, true),
(PolicyId::ForbiddenRootContext, Severity::Error, true),
(PolicyId::DependentVersionBump, Severity::Error, true),
(PolicyId::ForbiddenStrings, Severity::Error, true),
(PolicyId::CatalogSpecifiers, Severity::Off, false),
] {
assert_eq!(older.config.policies.setting(id), PolicySetting { id, severity, explicit }, "{id:?}");
}
std::fs::write(root.join(CONFIG_FILE_NAME), "{}\n").expect("empty configuration");
let empty: LoadedConfig = load_repository_config(root.as_path()).expect("the empty file loads");
assert_eq!(empty.config.policies.setting(PolicyId::CatalogSpecifiers).severity, Severity::Off);
std::fs::remove_file(root.join(CONFIG_FILE_NAME)).expect("remove");
let absent: LoadedConfig = load_repository_config(root.as_path()).expect("no file loads");
assert_eq!(absent.config.policies.setting(PolicyId::CatalogSpecifiers).severity, Severity::Off);
remove(root.as_path());
}