File: test_config_schema.rs

package info (click to toggle)
jujutsu 0.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,756 kB
  • sloc: sh: 283; makefile: 31
file content (69 lines) | stat: -rw-r--r-- 2,407 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2025 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Write as _;
use std::process::Command;
use std::process::Output;
use std::process::Stdio;

use testutils::ensure_running_outside_ci;
use testutils::is_external_tool_installed;

use crate::common::default_toml_from_schema;

#[test]
fn test_config_schema_default_values_are_consistent_with_schema() {
    if !is_external_tool_installed("taplo") {
        ensure_running_outside_ci("`taplo` must be in the PATH");
        eprintln!("Skipping test because taplo is not installed on the system");
        return;
    }

    let Some(schema_defaults) = default_toml_from_schema() else {
        ensure_running_outside_ci("`jq` must be in the PATH");
        eprintln!("Skipping test because jq is not installed on the system");
        return;
    };

    // Taplo requires an absolute URL to the schema :/
    let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let mut taplo_child = Command::new("taplo")
        .args([
            "check",
            "--schema",
            &format!("file://{}/src/config-schema.json", root.display()),
            "-", // read from stdin
        ])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();

    {
        let mut stdin = taplo_child.stdin.take().unwrap();
        write!(stdin, "{schema_defaults}").unwrap();
        // pipe is closed here by dropping it
    }

    let Output { status, stderr, .. } = taplo_child.wait_with_output().unwrap();
    if !status.success() {
        eprintln!(
            "taplo exited with status {status}:\n{}",
            String::from_utf8_lossy(&stderr)
        );
        eprintln!("while validating synthetic defaults TOML:\n{schema_defaults}");
        panic!("Schema defaults are not valid according to schema");
    }
}