Skip to content
This repository was archived by the owner on Oct 9, 2026. It is now read-only.

Commit 457cc98

Browse files
committed
Test pruning on every setting and on generated files
Every setting the schema declares is dropped at its default and kept otherwise. 400 seeded files of random settings, comments, headers and old default prompts must keep their meaning, prune idempotently, keep every comment, leave nothing removable and not depend on key order. This found that instances resolved differently when set to its default, so resolution now fills it in like enabled.
1 parent 0293b15 commit 457cc98

3 files changed

Lines changed: 361 additions & 0 deletions

File tree

‎src/config/prune.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,6 @@ fn remove(table: &mut dyn TableLike, path: &[&str]) {
146146
[] => {}
147147
}
148148
}
149+
150+
#[cfg(test)]
151+
mod tests;

‎src/config/prune/tests.rs‎

Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
//! Pruning is a function of config text, so it is checked on every setting
2+
//! the schema declares and on many generated files, against the properties
3+
//! it promises rather than examples.
4+
use super::*;
5+
use crate::config::{store, Config};
6+
use serde_json::Value;
7+
use std::collections::BTreeSet;
8+
use std::path::Path;
9+
10+
/// Every setting the schema declares, with its schema node.
11+
fn settings() -> Vec<(String, Value)> {
12+
let root = Config::schema();
13+
let mut out = Vec::new();
14+
fn walk(node: &Value, root: &Value, key: &str, out: &mut Vec<(String, Value)>) {
15+
let node = match node.get("$ref").and_then(Value::as_str) {
16+
Some(reference) => &root["$defs"][reference.rsplit('/').next().unwrap()],
17+
None => node,
18+
};
19+
match node.get("properties").and_then(Value::as_object) {
20+
Some(properties) => {
21+
for (name, child) in properties {
22+
let key = match key {
23+
"" => name.clone(),
24+
_ => format!("{key}.{name}"),
25+
};
26+
walk(child, root, &key, out);
27+
}
28+
}
29+
None => out.push((key.to_owned(), node.clone())),
30+
}
31+
}
32+
walk(&root, &root, "", &mut out);
33+
out.retain(|(key, node)| {
34+
key != "version"
35+
&& !key.starts_with("plugins.external")
36+
&& node.get("type").and_then(Value::as_str) != Some("object")
37+
});
38+
out
39+
}
40+
41+
/// A valid value other than the default, when the schema allows one.
42+
fn other(node: &Value) -> Option<Value> {
43+
let default = node.get("default");
44+
if let Some(choices) = node.get("enum").and_then(Value::as_array) {
45+
return choices
46+
.iter()
47+
.find(|choice| Some(*choice) != default)
48+
.cloned();
49+
}
50+
let kind = match node.get("type") {
51+
Some(Value::Array(kinds)) => kinds.iter().find(|kind| *kind != "null")?.as_str()?,
52+
Some(kind) => kind.as_str()?,
53+
None => return None,
54+
};
55+
Some(match kind {
56+
"boolean" => Value::Bool(!default.and_then(Value::as_bool).unwrap_or(false)),
57+
"integer" => {
58+
let minimum = node.get("minimum").and_then(Value::as_i64).unwrap_or(0);
59+
let maximum = node
60+
.get("maximum")
61+
.and_then(Value::as_i64)
62+
.unwrap_or(i64::MAX);
63+
let base = default.and_then(Value::as_i64).unwrap_or(minimum);
64+
Value::from(if base < maximum { base + 1 } else { base - 1 })
65+
}
66+
"number" => Value::from(default.and_then(Value::as_f64).unwrap_or(0.0) + 0.5),
67+
"string" => Value::from("custom-value"),
68+
"array" => match default.and_then(Value::as_array) {
69+
Some(items) if items.len() > 1 => Value::Array(items.iter().rev().cloned().collect()),
70+
Some(items) if !items.is_empty() => Value::Array(Vec::new()),
71+
_ => return None,
72+
},
73+
_ => return None,
74+
})
75+
}
76+
77+
/// The text `config set` reads for `value`.
78+
fn text(value: &Value) -> String {
79+
match value {
80+
Value::String(text) => text.clone(),
81+
Value::Array(items) => {
82+
let items: Vec<String> = items.iter().map(Value::to_string).collect();
83+
format!("[{}]", items.join(", "))
84+
}
85+
other => other.to_string(),
86+
}
87+
}
88+
89+
fn leaves(value: &toml::Value, prefix: &str, out: &mut BTreeSet<String>) {
90+
match value.as_table() {
91+
Some(table) => {
92+
for (key, child) in table {
93+
let key = match prefix {
94+
"" => key.clone(),
95+
_ => format!("{prefix}.{key}"),
96+
};
97+
leaves(child, &key, out);
98+
}
99+
}
100+
None => {
101+
out.insert(prefix.to_owned());
102+
}
103+
}
104+
}
105+
106+
fn file_keys(path: &Path) -> BTreeSet<String> {
107+
let value: toml::Value = toml::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
108+
let mut keys = BTreeSet::new();
109+
leaves(&value, "", &mut keys);
110+
keys
111+
}
112+
113+
#[test]
114+
fn every_setting_is_dropped_at_its_default_and_kept_otherwise() {
115+
let settings = settings();
116+
assert!(
117+
settings.len() > 25,
118+
"{:?}",
119+
settings.iter().map(|(key, _)| key).collect::<Vec<_>>()
120+
);
121+
let mut checked = 0;
122+
for (key, node) in &settings {
123+
let dir = tempfile::tempdir().unwrap();
124+
let path = dir.path().join("config.toml");
125+
if let Some(default) = node.get("default").filter(|default| !default.is_null()) {
126+
if store::set(&path, key, &text(default)).is_ok() {
127+
assert_eq!(
128+
file_keys(&path),
129+
BTreeSet::from(["version".to_owned()]),
130+
"{key} = default"
131+
);
132+
checked += 1;
133+
}
134+
}
135+
if let Some(value) = other(node) {
136+
let path = dir.path().join("other.toml");
137+
match store::set(&path, key, &text(&value)) {
138+
Ok(()) => {
139+
assert_eq!(
140+
file_keys(&path),
141+
BTreeSet::from(["version".to_owned(), key.clone()]),
142+
"{key} = {value}"
143+
);
144+
checked += 1;
145+
}
146+
// A value the configuration rejects is never written.
147+
Err(_) => assert!(!path.exists(), "{key}"),
148+
}
149+
}
150+
}
151+
assert!(checked > settings.len(), "{checked} of {}", settings.len());
152+
}
153+
154+
/// A small seeded generator, so every case is reproducible from its seed.
155+
struct Rng(u64);
156+
157+
impl Rng {
158+
fn next(&mut self) -> u64 {
159+
self.0 ^= self.0 << 13;
160+
self.0 ^= self.0 >> 7;
161+
self.0 ^= self.0 << 17;
162+
self.0
163+
}
164+
165+
fn chance(&mut self, percent: u64) -> bool {
166+
self.next() % 100 < percent
167+
}
168+
169+
fn shuffle<T>(&mut self, items: &mut [T]) {
170+
for index in (1..items.len()).rev() {
171+
items.swap(index, (self.next() % (index as u64 + 1)) as usize);
172+
}
173+
}
174+
}
175+
176+
fn edit_value(value: &Value) -> toml_edit::Value {
177+
match value {
178+
Value::Bool(flag) => (*flag).into(),
179+
Value::Number(number) => match number.as_i64() {
180+
Some(integer) => integer.into(),
181+
None => number.as_f64().unwrap().into(),
182+
},
183+
Value::String(text) => text.as_str().into(),
184+
Value::Array(items) => toml_edit::Value::Array(items.iter().map(edit_value).collect()),
185+
_ => unreachable!("settings are scalars or arrays"),
186+
}
187+
}
188+
189+
/// A config file of random settings at their default or another value, in
190+
/// random order, with random comments, explicit headers and an old default
191+
/// prompt.
192+
fn generate(rng: &mut Rng, settings: &[(String, Value)]) -> (DocumentMut, Vec<String>) {
193+
let mut document = DocumentMut::new();
194+
document.insert("version", toml_edit::value(1));
195+
let mut chosen: Vec<(String, Value)> = Vec::new();
196+
for (key, node) in settings {
197+
if !rng.chance(40) {
198+
continue;
199+
}
200+
let default = node
201+
.get("default")
202+
.filter(|default| !default.is_null())
203+
.cloned();
204+
let value = match rng.chance(50) {
205+
true => default.or_else(|| other(node)),
206+
false => other(node).or(default),
207+
};
208+
if let Some(value) = value {
209+
chosen.push((key.clone(), value));
210+
}
211+
}
212+
if rng.chance(25) {
213+
let legacy = LEGACY_DEFAULTS[0].1[(rng.next() % 2) as usize];
214+
chosen.retain(|(key, _)| key != LEGACY_DEFAULTS[0].0);
215+
chosen.push((LEGACY_DEFAULTS[0].0.to_owned(), Value::from(legacy)));
216+
}
217+
rng.shuffle(&mut chosen);
218+
let mut comments = Vec::new();
219+
for (key, value) in chosen {
220+
let path: Vec<&str> = key.split('.').collect();
221+
let mut table = document.as_table_mut();
222+
for segment in &path[..path.len() - 1] {
223+
if !table.contains_key(segment) {
224+
let mut child = toml_edit::Table::new();
225+
child.set_implicit(rng.chance(50));
226+
if rng.chance(10) {
227+
let comment = format!("# about {segment} {}\n", comments.len());
228+
child.set_implicit(false);
229+
child.decor_mut().set_prefix(comment.clone());
230+
comments.push(comment.trim().to_owned());
231+
}
232+
table.insert(segment, Item::Table(child));
233+
}
234+
table = table[segment].as_table_mut().unwrap();
235+
}
236+
let leaf = path[path.len() - 1];
237+
table.insert(leaf, toml_edit::value(edit_value(&value)));
238+
if rng.chance(10) {
239+
let comment = format!("# note {}", comments.len());
240+
table
241+
.key_mut(leaf)
242+
.unwrap()
243+
.leaf_decor_mut()
244+
.set_prefix(format!("{comment}\n"));
245+
comments.push(comment);
246+
}
247+
}
248+
(document, comments)
249+
}
250+
251+
fn resolve(text: &str) -> Option<toml::Value> {
252+
toml::Value::try_from(Config::from_toml_in(text, Path::new("")).ok()?).ok()
253+
}
254+
255+
fn pruned(document: &DocumentMut) -> DocumentMut {
256+
let mut document = document.clone();
257+
prune(&mut document, resolve);
258+
document
259+
}
260+
261+
fn document_leaves(document: &DocumentMut) -> BTreeSet<String> {
262+
removable(document.as_table(), false)
263+
.into_iter()
264+
.map(|path| path.join("."))
265+
.collect()
266+
}
267+
268+
#[test]
269+
fn pruning_generated_files_keeps_their_meaning_and_nothing_else() {
270+
let settings = settings();
271+
let mut cases = 0;
272+
for seed in 1..=400u64 {
273+
let mut rng = Rng(seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1);
274+
let (document, comments) = generate(&mut rng, &settings);
275+
let input = document.to_string();
276+
let Some(meaning) = resolve(&input) else {
277+
continue;
278+
};
279+
cases += 1;
280+
let output = pruned(&document);
281+
let text = output.to_string();
282+
assert_eq!(
283+
resolve(&text).as_ref(),
284+
Some(&meaning),
285+
"seed {seed}: meaning\n{input}\n---\n{text}"
286+
);
287+
assert_eq!(
288+
pruned(&output).to_string(),
289+
text,
290+
"seed {seed}: idempotent\n{text}"
291+
);
292+
for path in removable(output.as_table(), false)
293+
.into_iter()
294+
.chain(removable(output.as_table(), true))
295+
{
296+
if path == ["version"] {
297+
continue;
298+
}
299+
let path: Vec<&str> = path.iter().map(String::as_str).collect();
300+
let mut candidate = output.clone();
301+
remove(candidate.as_table_mut(), &path);
302+
assert_ne!(
303+
resolve(&candidate.to_string()).as_ref(),
304+
Some(&meaning),
305+
"seed {seed}: {} could still go\n{text}",
306+
path.join(".")
307+
);
308+
}
309+
for comment in &comments {
310+
assert!(
311+
text.contains(comment.as_str()),
312+
"seed {seed}: lost {comment}\n{input}\n---\n{text}"
313+
);
314+
}
315+
let mut shuffled = DocumentMut::new();
316+
let mut rng = Rng(seed | 1);
317+
let mut lines: Vec<(String, toml_edit::Value)> = Vec::new();
318+
fn flatten(table: &dyn TableLike, prefix: &str, out: &mut Vec<(String, toml_edit::Value)>) {
319+
for (key, item) in table.iter() {
320+
let key = match prefix {
321+
"" => key.to_owned(),
322+
_ => format!("{prefix}.{key}"),
323+
};
324+
match item.as_table_like() {
325+
Some(child) => flatten(child, &key, out),
326+
None => out.push((key, item.as_value().unwrap().clone())),
327+
}
328+
}
329+
}
330+
flatten(document.as_table(), "", &mut lines);
331+
rng.shuffle(&mut lines);
332+
for (key, value) in lines {
333+
let path: Vec<&str> = key.split('.').collect();
334+
let mut table = shuffled.as_table_mut();
335+
for segment in &path[..path.len() - 1] {
336+
if !table.contains_key(segment) {
337+
let mut child = toml_edit::Table::new();
338+
child.set_implicit(true);
339+
table.insert(segment, Item::Table(child));
340+
}
341+
table = table[segment].as_table_mut().unwrap();
342+
}
343+
let mut value = value;
344+
value.decor_mut().clear();
345+
table.insert(path[path.len() - 1], toml_edit::value(value));
346+
}
347+
let uncommented = |document: &DocumentMut| document_leaves(&pruned(document));
348+
if comments.is_empty() {
349+
assert_eq!(
350+
uncommented(&shuffled),
351+
document_leaves(&output),
352+
"seed {seed}: order\n{input}"
353+
);
354+
}
355+
}
356+
assert!(cases > 300, "{cases} generated files resolved");
357+
}

‎src/plugin/config.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ impl PluginsConfig {
442442
if !(1..=64).contains(&instances) || (!manifest.parallel && instances != 1) {
443443
return Err(ConfigError(format!("plugins.{reference}.instances: expected 1..=64 for a parallel plugin, or 1 for a serial plugin")));
444444
}
445+
entry.instances = Some(instances);
445446
manifest
446447
.validate(&entry.options)
447448
.map_err(|error| ConfigError(format!("plugins.{reference}: {error}")))?;

0 commit comments

Comments
 (0)