tor-config: Tests for ignored config key handling

This commit is contained in:
Ian Jackson 2022-05-24 12:39:49 +01:00
parent 09f26f2d68
commit bb8e205b0d
2 changed files with 60 additions and 8 deletions

View File

@ -168,23 +168,19 @@ mod test {
.unwrap();
// This tests that the example settings do not *contradict* the defaults.
// But it does not prove that the example template file does not contain misspelled
// (and therefore ignored) items - which might even contradict the defaults if
// their spelling was changed.
//
// Really we should test that too, but that's dependent on a fix for
// https://gitlab.torproject.org/tpo/core/arti/-/issues/417
// which is blocked on serde-ignored not handling serde(flatten).
//
// Also we should ideally test that every setting from the config appears here in
// the file. Possibly that could be done with some kind of stunt Deserializer,
// but it's not trivial.
let parsed: ArtiCombinedConfig = tor_config::resolve(cfg).unwrap();
let (parsed, ignored): (ArtiCombinedConfig, _) =
tor_config::resolve_and_ignored(cfg).unwrap();
let default = (ArtiConfig::default(), TorClientConfig::default());
assert_eq!(&parsed, &default);
assert_eq!(&parsed, &empty_config);
assert_eq!(ignored, &[]);
let built_default = (
ArtiConfigBuilder::default().build().unwrap(),
TorClientConfigBuilder::default().build().unwrap(),

View File

@ -378,8 +378,13 @@ fn ok_unquoted(s: &str) -> bool {
}
#[cfg(test)]
#[allow(unreachable_pub)] // impl_standard_builder wants to make pub fns
#[allow(clippy::unwrap_used)] // OK in tests
mod test {
use super::*;
use crate::*;
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
fn parse_test_set(l: &[&str]) -> BTreeSet<IgnoredKey> {
l.iter()
@ -455,4 +460,55 @@ mod test {
chk(r#"foo[10]"#, &[me("foo"), AI(10)]);
chk(r#"[10].bar"#, &[AI(10), me("bar")]); // weird
}
#[derive(Debug, Clone, Builder, Eq, PartialEq)]
#[builder(build_fn(error = "ConfigBuildError"))]
#[builder(derive(Debug, Serialize, Deserialize))]
struct TestConfigA {
#[builder(default)]
a: String,
}
impl_standard_builder! { TestConfigA }
impl TopLevel for TestConfigA {
type Builder = TestConfigABuilder;
}
#[derive(Debug, Clone, Builder, Eq, PartialEq)]
#[builder(build_fn(error = "ConfigBuildError"))]
#[builder(derive(Debug, Serialize, Deserialize))]
struct TestConfigB {
#[builder(default)]
b: String,
}
impl_standard_builder! { TestConfigB }
impl TopLevel for TestConfigB {
type Builder = TestConfigBBuilder;
}
#[test]
fn test_resolve() {
let test_data = r#"
wombat = 42
a = "hi"
"#;
let source = config::File::from_str(test_data, config::FileFormat::Toml);
let cfg = config::Config::builder()
.add_source(source)
.build()
.unwrap();
let _: (TestConfigA, TestConfigB) = resolve_without_ignored(cfg.clone()).unwrap();
let ((a, b), ign): ((TestConfigA, TestConfigB), _) = resolve_and_ignored(cfg).unwrap();
let ign = ign.into_iter().map(|ik| ik.to_string()).collect_vec();
assert_eq! { &a, &TestConfigA { a: "hi".into() } };
assert_eq! { &b, &TestConfigB { b: "".into() } };
assert_eq! { ign, &["wombat"] };
let _ = TestConfigA::builder();
let _ = TestConfigB::builder();
}
}