|
| 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::io::Write as _; |
| 16 | +use std::process::Command; |
| 17 | +use std::process::Output; |
| 18 | +use std::process::Stdio; |
| 19 | + |
| 20 | +use testutils::ensure_running_outside_ci; |
| 21 | +use testutils::is_external_tool_installed; |
| 22 | + |
| 23 | +use crate::common::default_toml_from_schema; |
| 24 | + |
| 25 | +#[test] |
| 26 | +fn test_config_schema_default_values_are_consistent_with_schema() { |
| 27 | + if !is_external_tool_installed("taplo") { |
| 28 | + ensure_running_outside_ci("`taplo` must be in the PATH"); |
| 29 | + eprintln!("Skipping test because taplo is not installed on the system"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + let Some(schema_defaults) = default_toml_from_schema() else { |
| 34 | + ensure_running_outside_ci("`jq` must be in the PATH"); |
| 35 | + eprintln!("Skipping test because jq is not installed on the system"); |
| 36 | + return; |
| 37 | + }; |
| 38 | + |
| 39 | + // Taplo requires an absolute URL to the schema :/ |
| 40 | + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 41 | + let mut taplo_child = Command::new("taplo") |
| 42 | + .args([ |
| 43 | + "check", |
| 44 | + "--schema", |
| 45 | + &format!("file://{}/src/config-schema.json", root.display()), |
| 46 | + "-", // read from stdin |
| 47 | + ]) |
| 48 | + .stdin(Stdio::piped()) |
| 49 | + .stdout(Stdio::piped()) |
| 50 | + .stderr(Stdio::piped()) |
| 51 | + .spawn() |
| 52 | + .unwrap(); |
| 53 | + |
| 54 | + { |
| 55 | + let mut stdin = taplo_child.stdin.take().unwrap(); |
| 56 | + write!(stdin, "{schema_defaults}").unwrap(); |
| 57 | + // pipe is closed here by dropping it |
| 58 | + } |
| 59 | + |
| 60 | + let Output { status, stderr, .. } = taplo_child.wait_with_output().unwrap(); |
| 61 | + if !status.success() { |
| 62 | + eprintln!( |
| 63 | + "taplo exited with status {status}:\n{}", |
| 64 | + String::from_utf8_lossy(&stderr) |
| 65 | + ); |
| 66 | + eprintln!("while validating synthetic defaults TOML:\n{schema_defaults}"); |
| 67 | + panic!("Schema defaults are not valid according to schema"); |
| 68 | + } |
| 69 | +} |
0 commit comments