| 1 |
// Copyright 2025 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::path::Path; |
| 16 |
|
| 17 |
use itertools::Itertools as _; |
| 18 |
|
| 19 |
fn validate_config_toml(config_toml: String) -> Result<(), String> { |
| 20 |
let config = toml_edit::de::from_str(&config_toml).unwrap(); |
| 21 |
|
| 22 |
// TODO: Fix unfortunate duplication with `test_config_schema.rs`. |
| 23 |
const SCHEMA_SRC: &str = include_str!("../src/config-schema.json"); |
| 24 |
let schema = serde_json::from_str(SCHEMA_SRC).expect("`config-schema.json` to be valid JSON"); |
| 25 |
let validator = |
| 26 |
jsonschema::validator_for(&schema).expect("`config-schema.json` to be a valid schema"); |
| 27 |
let evaluation = validator.evaluate(&config); |
| 28 |
if evaluation.flag().valid { |
| 29 |
Ok(()) |
| 30 |
} else { |
| 31 |
Err(evaluation |
| 32 |
.iter_errors() |
| 33 |
.map(|err| format!("* {}: {}", err.instance_location, err.error)) |
| 34 |
.join("\n")) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
pub(crate) fn check_config_file_valid( |
| 39 |
path: &Path, |
| 40 |
config_toml: String, |
| 41 |
) -> datatest_stable::Result<()> { |
| 42 |
if let Err(err) = validate_config_toml(config_toml) { |
| 43 |
panic!("Failed to validate `{}`:\n{err}", path.display()); |
| 44 |
} |
| 45 |
Ok(()) |
| 46 |
} |
| 47 |
|
| 48 |
pub(crate) fn check_config_file_invalid( |
| 49 |
path: &Path, |
| 50 |
config_toml: String, |
| 51 |
) -> datatest_stable::Result<()> { |
| 52 |
if let Ok(()) = validate_config_toml(config_toml) { |
| 53 |
panic!("Validation for `{}` unexpectedly passed", path.display()); |
| 54 |
} |
| 55 |
Ok(()) |
| 56 |
} |