|
1 | | -#[cfg(test)] |
2 | 1 | use crate::config::ConfigToml; |
3 | 2 | use crate::config::types::RawMcpServerConfig; |
4 | 3 | use crate::features::FEATURES; |
5 | 4 | use schemars::r#gen::SchemaGenerator; |
6 | | -#[cfg(test)] |
7 | 5 | use schemars::r#gen::SchemaSettings; |
8 | 6 | use schemars::schema::InstanceType; |
9 | 7 | use schemars::schema::ObjectValidation; |
10 | | -#[cfg(test)] |
11 | 8 | use schemars::schema::RootSchema; |
12 | 9 | use schemars::schema::Schema; |
13 | 10 | use schemars::schema::SchemaObject; |
14 | | -#[cfg(test)] |
15 | 11 | use std::path::Path; |
16 | 12 |
|
17 | | -/// Build the config schema used by the fixture test. |
18 | | -#[cfg(test)] |
19 | | -pub(crate) fn config_schema() -> RootSchema { |
20 | | - SchemaSettings::draft07() |
21 | | - .with(|settings| { |
22 | | - settings.option_add_null_type = false; |
23 | | - }) |
24 | | - .into_generator() |
25 | | - .into_root_schema_for::<ConfigToml>() |
26 | | -} |
27 | | - |
28 | | -/// Write the config schema fixture to disk. |
29 | | -#[cfg(test)] |
30 | | -pub(crate) fn write_config_schema(out_path: &Path) -> anyhow::Result<()> { |
31 | | - let schema = config_schema(); |
32 | | - let json = serde_json::to_vec_pretty(&schema)?; |
33 | | - std::fs::write(out_path, json)?; |
34 | | - Ok(()) |
35 | | -} |
36 | | - |
37 | 13 | /// Schema for the `[features]` map with known + legacy keys only. |
38 | 14 | pub(crate) fn features_schema(schema_gen: &mut SchemaGenerator) -> Schema { |
39 | 15 | let mut object = SchemaObject { |
@@ -74,32 +50,50 @@ pub(crate) fn mcp_servers_schema(schema_gen: &mut SchemaGenerator) -> Schema { |
74 | 50 | Schema::Object(object) |
75 | 51 | } |
76 | 52 |
|
| 53 | +/// Build the config schema for `config.toml`. |
| 54 | +pub fn config_schema() -> RootSchema { |
| 55 | + SchemaSettings::draft07() |
| 56 | + .with(|settings| { |
| 57 | + settings.option_add_null_type = false; |
| 58 | + }) |
| 59 | + .into_generator() |
| 60 | + .into_root_schema_for::<ConfigToml>() |
| 61 | +} |
| 62 | + |
| 63 | +/// Render the config schema as pretty-printed JSON. |
| 64 | +pub fn config_schema_json() -> anyhow::Result<Vec<u8>> { |
| 65 | + let schema = config_schema(); |
| 66 | + let json = serde_json::to_vec_pretty(&schema)?; |
| 67 | + Ok(json) |
| 68 | +} |
| 69 | + |
| 70 | +/// Write the config schema fixture to disk. |
| 71 | +pub fn write_config_schema(out_path: &Path) -> anyhow::Result<()> { |
| 72 | + let json = config_schema_json()?; |
| 73 | + std::fs::write(out_path, json)?; |
| 74 | + Ok(()) |
| 75 | +} |
| 76 | + |
77 | 77 | #[cfg(test)] |
78 | 78 | mod tests { |
79 | | - use super::*; |
80 | | - use pretty_assertions::assert_eq; |
| 79 | + use super::config_schema_json; |
| 80 | + use similar::TextDiff; |
81 | 81 |
|
82 | 82 | #[test] |
83 | 83 | fn config_schema_matches_fixture() { |
84 | | - let schema = config_schema(); |
85 | | - let schema_value = serde_json::to_value(schema).expect("serialize config schema"); |
86 | | - let fixture_path = codex_utils_cargo_bin::find_resource!("../../docs/config.schema.json") |
| 84 | + let fixture_path = codex_utils_cargo_bin::find_resource!("config.schema.json") |
87 | 85 | .expect("resolve config schema fixture path"); |
88 | 86 | let fixture = std::fs::read_to_string(fixture_path).expect("read config schema fixture"); |
89 | | - let fixture_value: serde_json::Value = |
90 | | - serde_json::from_str(&fixture).expect("parse config schema fixture"); |
91 | | - assert_eq!( |
92 | | - fixture_value, schema_value, |
93 | | - "Current schema for `config.toml` doesn't match the fixture. Run `just write-config-schema` to overwrite with your changes." |
94 | | - ); |
95 | | - } |
96 | | - |
97 | | - /// Overwrite the config schema fixture with the current schema. |
98 | | - #[test] |
99 | | - #[ignore] |
100 | | - fn write_config_schema_fixture() { |
101 | | - let fixture_path = codex_utils_cargo_bin::find_resource!("../../docs/config.schema.json") |
102 | | - .expect("resolve config schema fixture path"); |
103 | | - write_config_schema(&fixture_path).expect("write config schema fixture"); |
| 87 | + let schema_json = config_schema_json().expect("serialize config schema"); |
| 88 | + let schema_str = String::from_utf8(schema_json).expect("decode schema json"); |
| 89 | + if fixture != schema_str { |
| 90 | + let diff = TextDiff::from_lines(&fixture, &schema_str) |
| 91 | + .unified_diff() |
| 92 | + .to_string(); |
| 93 | + let short = diff.lines().take(50).collect::<Vec<_>>().join("\n"); |
| 94 | + panic!( |
| 95 | + "Current schema for `config.toml` doesn't match the fixture. Run `just write-config-schema` to overwrite with your changes.\n\n{short}" |
| 96 | + ); |
| 97 | + } |
104 | 98 | } |
105 | 99 | } |
0 commit comments