Skip to content

Commit 13dd199

Browse files
committed
canonicalize json by sorting, prettier json test diff
1 parent 77c0c41 commit 13dd199

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

codex-rs/core/src/config/schema.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,25 @@ pub fn write_config_schema(out_path: &Path) -> anyhow::Result<()> {
7777
#[cfg(test)]
7878
mod tests {
7979
use super::config_schema_json;
80-
use pretty_assertions::assert_eq;
80+
use serde_json::Map;
81+
use serde_json::Value;
82+
use similar::TextDiff;
83+
84+
fn canonicalize(value: &Value) -> Value {
85+
match value {
86+
Value::Array(items) => Value::Array(items.iter().map(canonicalize).collect()),
87+
Value::Object(map) => {
88+
let mut entries: Vec<_> = map.iter().collect();
89+
entries.sort_by(|(left, _), (right, _)| left.cmp(right));
90+
let mut sorted = Map::with_capacity(map.len());
91+
for (key, child) in entries {
92+
sorted.insert(key.clone(), canonicalize(child));
93+
}
94+
Value::Object(sorted)
95+
}
96+
_ => value.clone(),
97+
}
98+
}
8199

82100
#[test]
83101
fn config_schema_matches_fixture() {
@@ -89,9 +107,21 @@ mod tests {
89107
let schema_json = config_schema_json().expect("serialize config schema");
90108
let schema_value: serde_json::Value =
91109
serde_json::from_slice(&schema_json).expect("decode schema json");
92-
assert_eq!(
93-
fixture_value, schema_value,
94-
"Current schema for `config.toml` doesn't match the fixture. Run `just write-config-schema` to overwrite with your changes."
95-
);
110+
let fixture_value = canonicalize(&fixture_value);
111+
let schema_value = canonicalize(&schema_value);
112+
if fixture_value != schema_value {
113+
let expected =
114+
serde_json::to_string_pretty(&fixture_value).expect("serialize fixture json");
115+
let actual =
116+
serde_json::to_string_pretty(&schema_value).expect("serialize schema json");
117+
let diff = TextDiff::from_lines(&expected, &actual)
118+
.unified_diff()
119+
.header("fixture", "generated")
120+
.to_string();
121+
panic!(
122+
"Current schema for `config.toml` doesn't match the fixture. \
123+
Run `just write-config-schema` to overwrite with your changes.\n\n{diff}"
124+
);
125+
}
96126
}
97127
}

0 commit comments

Comments
 (0)