#![allow(
clippy::expect_used,
reason = "a broken test setup should fail the test loudly"
)]
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicUsize, Ordering};
fn workdir(files: &[(&str, &str)]) -> PathBuf {
static NEXT: AtomicUsize = AtomicUsize::new(0);
let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!(
"cli-{}-{}",
std::process::id(),
NEXT.fetch_add(1, Ordering::Relaxed)
));
let _ = fs::remove_dir_all(&dir);
for (name, text) in files {
let path = dir.join(name);
fs::create_dir_all(path.parent().expect("a parent")).expect("create dirs");
fs::write(path, text).expect("write file");
}
fs::create_dir_all(dir.join(".git")).expect("mark the repository root");
dir
}
fn no_hang(start: std::time::Instant) {
assert!(start.elapsed().as_secs() < 10, "took {:?}", start.elapsed());
}
#[test]
fn formatting_refuses_changed_hidden_problems_without_writing() {
let text = "{\"a\":1,\n// jsonvet-ignore duplicate-key\n\"a\":2,\"b\":1,\"b\":2}\n";
let dir = workdir(&[("s.jsonc", text)]);
let (code, _, out) = jsonvet_stderr(&dir, &["fmt", "s.jsonc"]);
assert_eq!(code, 1, "{out}");
assert!(
out.contains("formatting would change which problems an inline suppression hides"),
"{out}"
);
assert_eq!(
fs::read_to_string(dir.join("s.jsonc")).expect("read input"),
text
);
}
#[test]
fn runtime_notes_follow_report_destination_and_format() {
let dir = workdir(&[]);
let input = "{\"$schema\":\"missing.json\"}\n";
for format in ["text", "json", "sarif"] {
for fix in [false, true] {
let mut args = vec!["--format", format, "-"];
if fix {
args.insert(0, "--fix");
}
let (code, out, err) = jsonvet_stdin(&dir, &args, input);
assert_eq!(code, 0, "{format} fix={fix}: {err}");
if fix {
assert_eq!(out, input);
}
if format == "text" || !fix {
assert!(err.contains("missing.json"), "{format} fix={fix}: {err:?}");
}
if format == "text" {
continue;
}
let report: serde_json::Value =
serde_json::from_str(if fix { &err } else { &out }).expect("one structured report");
let notes = if format == "json" {
&report["notes"]
} else {
&report["runs"][0]["invocations"][0]["toolExecutionNotifications"]
};
assert_eq!(
notes.as_array().map(Vec::len),
Some(1),
"{format} fix={fix}: {notes}"
);
let note = if format == "json" {
¬es[0]
} else {
¬es[0]["message"]["text"]
};
assert!(
note.as_str()
.expect("runtime note")
.contains("missing.json"),
"{report}"
);
}
}
}
fn command(bin: &str) -> Command {
let mut command = Command::new(bin);
for var in [
"NO_COLOR",
"FORCE_COLOR",
"GITHUB_ACTIONS",
"LC_ALL",
"LC_MESSAGES",
"LANG",
] {
command.env_remove(var);
}
command
}
fn jsonvet(dir: &Path, args: &[&str]) -> (i32, String) {
let (code, out, _) = jsonvet_stderr(dir, args);
(code, out)
}
#[test]
fn reports_and_exit_codes() {
let dir = workdir(&[
("ok.json", "{\"a\": 1, // c\n}"),
("dup.json", "{\n \"a\": 1,\n \"a\": 2\n}"),
("bom.json", "\u{FEFF}{}"),
]);
let run = |args: &[&str]| jsonvet(&dir, args);
assert_eq!(run(&["ok.json"]), (0, String::new()));
assert_eq!(jsonvet_stderr(&dir, &[]), (0, String::new(), String::new()));
assert_eq!(
run(&["ok.json", "dup.json", "gone.json"]),
(
1,
"dup.json:3:3: error[duplicate-key] duplicate key $.a (first at 2:3)\ngone.json: error: no such file\n"
.to_owned()
)
);
let (code, out) = run(&["--dialect=json:*.json", "bom.json"]);
assert_eq!(
(code, out.as_str()),
(
0,
"bom.json:1:1: warning[byte-order-mark] byte order mark at the start of the file\n"
)
);
assert_eq!(
run(&["--dialect=json:*.json", "--max-warnings", "0", "bom.json"]).0,
1
);
assert_eq!(
run(&["--dialect=json:*.json", "--max-warnings", "1", "bom.json"]).0,
0
);
assert_eq!(
run(&[
"--dialect=json:*.json",
"--ignore",
"byte-order-mark",
"bom.json"
]),
(0, String::new())
);
}
#[test]
fn a_point_problem_has_no_end_column_in_annotations_or_sarif() {
let dir = workdir(&[
("comma.json", "[1 2]"),
("wrap.json5", "{\"ab\":1,\"a\\\nb\":2}"),
]);
let (_, out, _) = run_jsonvet(&dir, &["comma.json"], true);
assert!(
out.contains("::error file=comma.json,line=1,col=3,title=jsonvet (syntax)"),
"{out}"
);
let (_, out, _) = run_jsonvet(&dir, &["--format", "sarif", "comma.json"], false);
let sarif: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let region = &sarif["runs"][0]["results"][0]["locations"][0]["physicalLocation"]["region"];
assert_eq!(
*region,
serde_json::json!({"startLine": 1, "startColumn": 3})
);
let (_, out, _) = run_jsonvet(&dir, &["--format", "sarif", "wrap.json5"], false);
let sarif: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let region = &sarif["runs"][0]["results"][0]["locations"][0]["physicalLocation"]["region"];
assert_eq!(region["startColumn"], 9, "{region}");
assert_eq!(region["endLine"], 2, "{region}");
}
#[test]
fn github_annotations_follow_each_problem() {
let dir = workdir(&[
("bom.json", "\u{FEFF}{}"),
("dup.json", "{\"a\":1,\"a\":2}"),
]);
let run = |actions: &str| {
let out = command(env!("CARGO_BIN_EXE_jsonvet"))
.args(["--dialect=json:*.json", "bom.json", "dup.json"])
.current_dir(&dir)
.env("GITHUB_ACTIONS", actions)
.output()
.expect("run jsonvet");
String::from_utf8(out.stdout).expect("UTF-8 output")
};
assert_eq!(
run("true"),
"bom.json:1:1: warning[byte-order-mark] byte order mark at the start of the file\n\
::warning file=bom.json,line=1,col=1,endColumn=1,title=jsonvet (byte-order-mark)::byte order mark at the start of the file\n\
dup.json:1:8: error[duplicate-key] duplicate key $.a (first at 1:2)\n\
::error file=dup.json,line=1,col=8,endColumn=10,title=jsonvet (duplicate-key)::duplicate key $.a (first at 1:2)\n\
::notice title=jsonvet::1 error and 1 warning in 2 of 2 files\n"
);
assert_eq!(
run("false"),
"bom.json:1:1: warning[byte-order-mark] byte order mark at the start of the file\n\
dup.json:1:8: error[duplicate-key] duplicate key $.a (first at 1:2)\n"
);
fs::write(dir.join("ok.json"), "{}").expect("write");
assert_eq!(
run_jsonvet(&dir, &["ok.json"], true),
(0, String::new(), String::new())
);
}
#[test]
fn each_output_path_escapes_names_and_messages() {
let key = "\u{202a}##[k:%,";
let doubled = format!("{{\"{key}\": 1, \"{key}\": 2}}");
let dir = workdir(&[
("##[\u{2069}a.json", "[1,"),
("m.json", &doubled),
#[cfg(unix)]
("::a\n\u{1b}[2J,.json", "[1,"),
]);
let end =
"error[syntax] unclosed `[`: expected a value, found the end of the file (opened at 1:1)";
let message = "duplicate key $[\"\\u202a##\\u005bk:%,\"] (first at 1:2)";
let text = format!("m.json:1:17: error[duplicate-key] {message}\n");
let cases = [
(
"##[\u{2069}a.json",
false,
format!("##\\u005b\\u2069a.json:1:4: {end}\n"),
),
("m.json", false, text.clone()),
#[cfg(unix)]
(
"::a\n\u{1b}[2J,.json",
true,
format!(
"./::a\\n\\u001b[2J,.json:1:4: {end}\n\
::error file=%3A%3Aa\\n\\u001b[2J%2C.json,line=1,col=4,title=jsonvet (syntax)::unclosed `[`: expected a value, found the end of the file (opened at 1:1)\n\
::notice title=jsonvet::1 error in 1 of 1 file\n"
),
),
(
"m.json",
true,
format!(
"{text}::error file=m.json,line=1,col=17,endColumn=26,title=jsonvet (duplicate-key)::{}\n\
::notice title=jsonvet::1 error in 1 of 1 file\n",
message.replace('%', "%25")
),
),
#[cfg(unix)]
(
"::gone\u{1b}[2J.json",
true,
"./::gone\\u001b[2J.json: error: no such file\n\
::error file=%3A%3Agone\\u001b[2J.json,title=jsonvet::no such file\n\
::notice title=jsonvet::1 error in 1 of 1 file\n"
.to_owned(),
),
];
for (name, annotate, expected) in cases {
let (_, out, _) = run_jsonvet(&dir, &[name], annotate);
assert_eq!(out, expected, "{name:?}");
}
let args = ["--format", "sarif", "##[\u{2069}a.json", "m.json"];
let (_, sarif, _) = run_jsonvet(&dir, &args, true);
assert!(!sarif.contains("##["), "{sarif}");
let sarif: serde_json::Value = serde_json::from_str(&sarif).expect("JSON output");
assert_eq!(
sarif["runs"][0]["results"][1]["message"]["text"],
"duplicate key $[\"\\u202a##[k:%,\"] (first at 1:2)"
);
}
#[test]
fn a_closed_pipe_keeps_the_verdict() {
let dir = workdir(&[("bad.json", "[1,")]);
let files = vec!["bad.json"; 3000];
let mut child = command(env!("CARGO_BIN_EXE_jsonvet"))
.args(&files)
.current_dir(&dir)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.expect("run jsonvet");
drop(child.stdout.take());
let out = child.wait_with_output().expect("wait for jsonvet");
assert_eq!(String::from_utf8_lossy(&out.stderr), "");
assert_eq!(out.status.code(), Some(1));
}
#[cfg(unix)]
#[test]
fn help_into_a_closed_pipe_succeeds() {
for flag in ["--help", "--version"] {
let (reader, writer) = std::io::pipe().expect("a pipe");
drop(reader);
let out = command(env!("CARGO_BIN_EXE_jsonvet"))
.arg(flag)
.stdout(writer)
.output()
.expect("run jsonvet");
assert_eq!(String::from_utf8_lossy(&out.stderr), "", "{flag}");
assert_eq!(out.status.code(), Some(0), "{flag}");
}
}
#[test]
fn help_version_and_the_check_json5_name() {
let dir = workdir(&[("dup.json", "{\"a\":1,\"a\":2}"), ("sub.json", "{}")]);
let run = |args: &[&str]| jsonvet(&dir, args);
let version = format!("jsonvet {}\n", env!("CARGO_PKG_VERSION"));
assert_eq!(run(&["-V"]), (0, version.clone()));
assert_eq!(run(&["--version"]), (0, version));
for flag in ["-h", "--help"] {
let (code, out) = run(&[flag]);
assert_eq!(code, 0);
assert!(out.starts_with("Check JSON, I-JSON,"), "{flag}");
assert!(
out.contains("schemas setting") && out.contains("local $schema"),
"{flag}"
);
}
let out = command(env!("CARGO_BIN_EXE_check-json5"))
.arg("dup.json")
.current_dir(&dir)
.output()
.expect("run check-json5");
assert_eq!(out.status.code(), Some(1));
assert_eq!(String::from_utf8_lossy(&out.stdout), run(&["dup.json"]).1);
fs::create_dir(dir.join("folder.json")).expect("create a folder");
assert_eq!(
run(&["folder.json"]),
(0, String::new()),
"a folder is walked, and this one is empty"
);
}
#[cfg(unix)]
#[test]
fn only_regular_files_are_read() {
use std::os::unix::fs::symlink;
use std::time::{Duration, Instant};
let dir = workdir(&[("real.json", "[1,")]);
symlink("/dev/zero", dir.join("zero.json")).expect("symlink");
symlink("real.json", dir.join("link.json")).expect("symlink");
let fifo = Command::new("mkfifo")
.arg(dir.join("fifo.json"))
.status()
.expect("run mkfifo");
assert!(fifo.success());
let mut child = command(env!("CARGO_BIN_EXE_jsonvet"))
.args(["zero.json", "fifo.json", "link.json"])
.current_dir(&dir)
.stdout(std::process::Stdio::piped())
.spawn()
.expect("run jsonvet");
let start = Instant::now();
while child.try_wait().expect("wait").is_none() {
if start.elapsed() > Duration::from_secs(10) {
let _ = child.kill();
panic!("jsonvet still running after 10 seconds");
}
std::thread::sleep(Duration::from_millis(20));
}
let out = child.wait_with_output().expect("output");
assert_eq!(out.status.code(), Some(1));
assert_eq!(
String::from_utf8(out.stdout).expect("UTF-8 output"),
"zero.json: error: not a regular file\n\
fifo.json: error: not a regular file\n\
link.json:1:4: error[syntax] unclosed `[`: expected a value, found the end of the file (opened at 1:1)\n"
);
}
#[test]
fn strict_globs_keep_their_1x_meaning() {
let dir = workdir(&[
("package.json", "{// c\n}"),
("a/package.json", "{// c\n}"),
("tsconfig.json", "{// c\n}"),
(".vscode/s.json", "{// c\n}"),
]);
let (code, out) = jsonvet(
&dir,
&[
"--strict",
"*.json",
"--strict",
"!tsconfig*.json",
"--strict",
"!.vscode/*",
"package.json",
"a/package.json",
"tsconfig.json",
".vscode/s.json",
],
);
assert_eq!(
(code, out.as_str()),
(
1,
"package.json:1:2: error[syntax] comments are not allowed in json; the jsonc dialect allows this\na/package.json:1:2: error[syntax] comments are not allowed in json; the jsonc dialect allows this\n"
)
);
}
#[test]
fn an_unknown_option_is_a_usage_error_unless_it_is_a_file() {
let dir = workdir(&[("bad.json", "[1,"), ("--x.json", "[1,")]);
let run = |args: &[&str]| jsonvet_stderr(&dir, args);
let (code, out, err) = run(&["--strcit", "*.json", "bad.json"]);
assert_eq!((code, out.as_str()), (2, ""));
assert!(
err.starts_with("jsonvet: unknown option `--strcit`\n"),
"{err}"
);
let found = "--x.json:1:4: error[syntax] unclosed `[`: expected a value, found the end of the file (opened at 1:1)\n";
assert_eq!(run(&["--x.json"]), (1, found.to_owned(), String::new()));
assert_eq!(
run(&["--", "--x.json"]),
(1, found.to_owned(), String::new())
);
let (code, out, err) = run(&["-q", "bad.json"]);
assert_eq!((code, out.as_str()), (2, ""));
assert!(err.starts_with("jsonvet: unknown option `-q`\n"), "{err}");
fs::write(dir.join("-q"), "[1,").expect("write file");
let found = "-q:1:4: error[syntax] unclosed `[`: expected a value, found the end of the file (opened at 1:1)\n";
assert_eq!(run(&["-q"]), (1, found.to_owned(), String::new()));
}
#[test]
fn a_file_named_like_an_option_with_a_value_is_a_file() {
let dir = workdir(&[
("a.json", "{\"a\": 1 // c\n}\n"),
("--dialect=json:*.json", "{}"),
]);
assert_eq!(
jsonvet(&dir, &["--dialect=json:*.json", "a.json"]),
(0, String::new())
);
fs::remove_file(dir.join("--dialect=json:*.json")).expect("remove file");
assert_eq!(jsonvet(&dir, &["--dialect=json:*.json", "a.json"]).0, 1);
}
#[test]
fn help_and_version_letters_in_a_file_name_are_a_file() {
let dir = workdir(&[("bad.json", "[1,"), ("-h.json", "{}"), ("-V.json", "{}")]);
let (code, out) = jsonvet(&dir, &["bad.json", "-h.json", "-V.json"]);
assert_eq!(
(code, out.as_str()),
(
1,
"bad.json:1:4: error[syntax] unclosed `[`: expected a value, found the end of the file (opened at 1:1)\n"
)
);
}
#[cfg(target_os = "linux")]
#[test]
fn a_file_name_that_is_not_utf8_is_opened_as_given() {
use std::os::unix::ffi::OsStrExt;
let dir = workdir(&[]);
let names = [b"caf\xe9.json".as_slice(), b"--\xff.json", b"-x\xff.json"].map(OsStr::from_bytes);
for name in names {
fs::write(dir.join(name), "[1,]").expect("write file");
}
let args = [[OsStr::new("--dialect=json:*")].as_slice(), &names].concat();
let (_, out, _) = run_jsonvet(&dir, &args, false);
let found = ".json:1:4: error[syntax] trailing comma before `]` is not allowed in json; the jwcc and json5 dialects allow this\n";
assert_eq!(
out,
format!("caf\u{FFFD}{found}--\u{FFFD}{found}-x\u{FFFD}{found}")
);
}
#[test]
fn config_file_and_its_search() {
let dir = workdir(&[
(
"jsonvet.toml",
"rules = { duplicate-key = \"warning\" }\ndialects = [{ files = \"*.data\", dialect = \"jsonl\" }]\n",
),
("sub/x.data", "{\"a\": 1, \"a\": 2}\n\n"),
]);
let (code, out) = jsonvet(&dir.join("sub"), &["x.data"]);
assert_eq!(code, 1);
assert_eq!(
out,
"x.data:1:10: warning[duplicate-key] duplicate key $.a (first at 1:2)\nx.data:2:1: error[syntax] blank line; JSON Lines needs a value on every line\n"
);
assert_eq!(
jsonvet(
&dir.join("sub"),
&["--error", "duplicate-key", "--dialect", "json5", "x.data"]
)
.1
.lines()
.next(),
Some("x.data:1:10: error[duplicate-key] duplicate key $.a (first at 1:2)")
);
assert_eq!(jsonvet(&dir.join("sub"), &["--no-config", "x.data"]).0, 1);
}
#[test]
fn config_severities_take_the_warn_and_off_aliases() {
let dir = workdir(&[
(
"jsonvet.toml",
"dialects = [{ files = \"*.json\", dialect = \"json\" }]\nrules = { duplicate-key = \"warn\", byte-order-mark = \"off\" }\n",
),
("x.json", "\u{FEFF}{\"a\": 1, \"a\": 2}"),
]);
assert_eq!(
jsonvet(&dir, &["x.json"]),
(
0,
"x.json:1:11: warning[duplicate-key] duplicate key $.a (first at 1:3)\n".to_owned()
)
);
}
#[test]
fn per_file_rules_set_severities_for_the_files_they_match() {
let dup = "{\"a\": 1, \"a\": 2}\n";
let dir = workdir(&[
(
"jsonvet.toml",
"per-file-rules = [\n { files = \"legacy/*.json\", duplicate-key = \"ignore\" },\n { files = \"legacy/b.json\", duplicate-key = \"warning\" },\n { files = \"*.jsonl\", duplicate-key = \"off\" },\n]\nrules = { duplicate-key = \"error\" }\n",
),
("legacy/a.json", dup),
("legacy/b.json", dup),
("new/c.json", dup),
("logs/d.jsonl", dup),
]);
let files = [
"legacy/a.json",
"legacy/b.json",
"new/c.json",
"logs/d.jsonl",
];
assert_eq!(
jsonvet(&dir, &files),
(
1,
"legacy/b.json:1:10: warning[duplicate-key] duplicate key $.a (first at 1:2)\nnew/c.json:1:10: error[duplicate-key] duplicate key $.a (first at 1:2)\n".to_owned()
)
);
assert_eq!(
jsonvet(&dir.join("legacy"), &["a.json"]),
(0, String::new())
);
let (code, out) = jsonvet(&dir, &["--error", "duplicate-key", "legacy/a.json"]);
assert_eq!(code, 1, "{out}");
}
#[test]
fn a_bad_per_file_rules_entry_is_a_config_error() {
let dir = workdir(&[
("per-file.toml", "per-file-rules = 1\n"),
(
"per-file-rule.toml",
"[[per-file-rules]]\nfiles = \"*.json\"\nduplicate-keys = \"ignore\"\n",
),
(
"per-file-severity.toml",
"per-file-rules = [{ files = \"*.json\", syntax = \"of\" }]\n",
),
(
"per-file-files.toml",
"per-file-rules = [{ syntax = \"off\" }]\n",
),
(
"per-file-file.toml",
"per-file-rules = [{ file = \"*.json\", syntax = \"off\" }]\n",
),
(
"per-file-level.toml",
"[[per-file-rules]]\nfiles = \"*.json\"\nduplicate-key = 1\n",
),
("per-file-entry.toml", "per-file-rules = [1]\n"),
("per-file-glob.toml", "per-file-rules = [{ files = 1 }]\n"),
("rule-level.toml", "rules = { syntax = 1 }\n"),
("d.json", "{}"),
]);
for (config, message) in [
(
"per-file.toml:1:18",
"`per-file-rules` must be an array of tables",
),
(
"per-file-rule.toml:3:1",
"unknown rule `duplicate-keys` (did you mean `duplicate-key`?); expected one of \
syntax, duplicate-key, invalid-number, invalid-code-point, byte-order-mark, \
missing-final-newline, mixed-line-endings, unused-suppression",
),
(
"per-file-severity.toml:1:48",
"unknown severity `of` (did you mean `off`?); expected error, warning or ignore",
),
(
"per-file-files.toml:1:19",
"each `per-file-rules` entry needs a `files` string",
),
(
"per-file-file.toml:1:21",
"unknown key `file` (did you mean `files`?) in a `per-file-rules` entry; expected \
files and rule ids",
),
(
"per-file-level.toml:3:17",
"`per-file-rules.duplicate-key` must be a string",
),
("rule-level.toml:1:20", "`rules.syntax` must be a string"),
(
"per-file-glob.toml:1:29",
"each `per-file-rules` entry needs a `files` string",
),
(
"per-file-entry.toml:1:19",
"each `per-file-rules` entry must be a table",
),
] {
let file = config.split(':').next().unwrap_or_default();
let (code, _, err) = jsonvet_stderr(&dir, &["--config", file, "d.json"]);
assert_eq!(code, 2);
assert_eq!(err, format!("jsonvet: {config}: {message}\n"));
}
}
#[test]
fn config_globs_are_relative_to_the_config_file() {
let toml = "dialects = [{ files = \"data/*.json\", dialect = \"json\" }]\n";
let pyproject = format!("[tool.jsonvet]\n{toml}");
let unquoted = "data/x.json:1:2: error[syntax] unquoted keys are not allowed in json; the json5 dialect allows this\n";
for config in [
("jsonvet.toml", toml),
("pyproject.toml", pyproject.as_str()),
] {
let dir = workdir(&[
config,
("data/x.json", "{a: 1}"),
("outside/x.json", "{a: 1}"),
(
"outside/c.toml",
"dialects = [{ files = \"*.json\", dialect = \"json\" }]\n",
),
]);
assert_eq!(jsonvet(&dir, &["data/x.json"]), (1, unquoted.to_owned()));
let absolute = dir.join("data/x.json");
let absolute = absolute.to_str().expect("a UTF-8 path");
assert_eq!(jsonvet(&dir, &[absolute]).0, 1, "{absolute}");
assert_eq!(jsonvet(&dir.join("data"), &["x.json"]).0, 1);
assert_eq!(jsonvet(&dir.join("outside"), &["../data/x.json"]).0, 1);
let cli = ["--no-config", "--dialect", "json:data/*.json", "x.json"];
assert_eq!(jsonvet(&dir.join("data"), &cli), (0, String::new()));
let other = ["--config", "outside/c.toml", "data/x.json"];
assert_eq!(jsonvet(&dir, &other), (0, String::new()));
}
}
#[test]
fn each_file_takes_the_nearest_config_file() {
let dup = "{\"a\": 1, \"a\": 2}\n";
let dir = workdir(&[
("jsonvet.toml", "rules = { duplicate-key = \"ignore\" }\n"),
("d.json", dup),
(
"packages/a/jsonvet.toml",
"dialects = [{ files = \"*.json\", dialect = \"json\" }]\n",
),
("packages/a/x.json", dup),
("packages/a/y.json", "{a: 1}\n"),
("packages/b/d.json", dup),
]);
let nested = "packages/a/x.json:1:10: error[duplicate-key] duplicate key $.a (first at 1:2)\npackages/a/y.json:1:2: error[syntax] unquoted keys are not allowed in json; the json5 dialect allows this\n";
let files = [
"d.json",
"packages/a/x.json",
"packages/a/y.json",
"packages/b/d.json",
];
assert_eq!(jsonvet(&dir, &files), (1, nested.to_owned()));
let up = ["d.json", "../a/x.json", "../a/y.json", "../b/../../d.json"];
assert_eq!(
jsonvet(&dir.join("packages/b"), &up),
(1, nested.replace("packages/a", "../a"))
);
assert_eq!(
jsonvet(&dir, &["packages/a/../b/d.json"]),
(0, String::new())
);
let (code, out, _) = jsonvet_stdin(&dir, &["--stdin-filename", "packages/a/x.json"], dup);
assert_eq!((code, out.lines().next()), (1, nested.lines().next()));
let all = [
"--config",
"jsonvet.toml",
"packages/a/x.json",
"packages/a/y.json",
];
assert_eq!(jsonvet(&dir, &all), (0, String::new()));
assert_eq!(jsonvet(&dir, &["--no-config", "d.json"]).0, 1);
}
#[test]
fn a_bad_nested_config_file_stops_the_run() {
let dir = workdir(&[
("ok.json", "{}"),
("sub/jsonvet.toml", "rules = { nope = \"error\" }\n"),
("sub/x.json", "{}"),
]);
assert_eq!(jsonvet(&dir, &["ok.json"]), (0, String::new()));
let (code, out, err) = jsonvet_stderr(&dir, &["ok.json", "sub/x.json"]);
assert_eq!((code, out.as_str()), (2, ""));
let config = Path::new("sub").join("jsonvet.toml");
let message = format!("{}:1:11: unknown rule `nope`", config.display());
assert!(err.contains(&message), "{err}");
assert_eq!(jsonvet_stderr(&dir, &["."]).0, 2);
}
#[test]
fn config_search_stops_at_the_repository_root() {
let outer = workdir(&[("jsonvet.toml", "rules = { duplicate-key = \"ignore\" }\n")]);
let repo = outer.join("repo");
fs::create_dir_all(repo.join(".git")).expect("inner repository");
fs::write(repo.join("d.json"), "{\"a\": 1, \"a\": 2}").expect("write");
assert_eq!(
jsonvet(&repo, &["d.json"]).0,
1,
"the outer jsonvet.toml must not apply"
);
}
#[test]
fn pyproject_tool_table_and_bad_configs() {
let dir = workdir(&[
(
"pyproject.toml",
"[project]\nname = \"x\"\n\n[tool.jsonvet]\nrules = { duplicate-key = \"ignore\" }\n",
),
("d.json", "{\"a\": 1, \"a\": 2}"),
("bad.toml", "rules = { nope = \"error\" }\n"),
(
"bad-entry.toml",
"dialects = [{ files = \"*\", dialect = \"json\", dialekt = \"json5\" }]\n",
),
]);
assert_eq!(jsonvet(&dir, &["d.json"]), (0, String::new()));
assert_eq!(jsonvet(&dir, &["--config", "bad.toml", "d.json"]).0, 2);
assert_eq!(
jsonvet(&dir, &["--config", "bad-entry.toml", "d.json"]).0,
2
);
let typo = workdir(&[
(
"pyproject.toml",
"[tool.jsonvet]\nrule = { duplicate-key = \"ignore\" }\n",
),
("d.json", "{}"),
]);
assert_eq!(jsonvet(&typo, &["d.json"]).0, 2);
let plain = workdir(&[
("pyproject.toml", "[project]\nname = \"x\"\n"),
("d.json", "{\"a\": 1, \"a\": 2}"),
]);
assert_eq!(jsonvet(&plain, &["d.json"]).0, 1);
let wrong_types = workdir(&[
("rules.toml", "rules = 1\n"),
("dialects.toml", "dialects = 1\n"),
("forged.toml", "\"\\n::error::x\" = \"error\"\n"),
("typo.toml", "[rules]\nduplicate-keys = \"error\"\n"),
("key.toml", "# x\n rule = {}\n"),
("severity.toml", "[rules]\nsyntax = \"eror\"\n"),
(
"dialect.toml",
"[[dialects]]\nfiles = \"*.json\"\ndialect = \"jsno\"\n",
),
("entry.toml", "[[dialects]]\nfile = \"*.json\"\n"),
("syntax.toml", "[rules\n"),
("d.json", "{}"),
]);
for (config, message) in [
("rules.toml:1:9", "`rules` must be a table"),
(
"dialects.toml:1:12",
"`dialects` must be an array of tables",
),
(
"forged.toml:1:1",
"unknown key `\\n::error::x`; expected rules, per-file-rules, per-file-format, dialects, exclude, schemas or format",
),
(
"typo.toml:2:1",
"unknown rule `duplicate-keys` (did you mean `duplicate-key`?); expected one of \
syntax, duplicate-key, invalid-number, invalid-code-point, byte-order-mark, \
missing-final-newline, mixed-line-endings, unused-suppression",
),
(
"key.toml:2:3",
"unknown key `rule` (did you mean `rules`?); expected rules, per-file-rules, per-file-format, dialects, exclude, schemas or format",
),
(
"severity.toml:2:10",
"unknown severity `eror` (did you mean `error`?); expected error, warning or ignore",
),
(
"dialect.toml:3:11",
"unknown dialect `jsno` (did you mean `json`?); expected one of json, i-json, \
jsonc, jwcc, json5, jsonl, ijson, hujson, ndjson, json-lines, or auto",
),
(
"entry.toml:2:1",
"unknown key `file` (did you mean `files`?) in a `dialects` entry; expected files \
and dialect",
),
("syntax.toml:1:7", "unclosed table, expected `]`"),
] {
let file = config.split(':').next().unwrap_or_default();
let (code, _, err) = jsonvet_stderr(&wrong_types, &["--config", file, "d.json"]);
assert_eq!(code, 2);
assert_eq!(err, format!("jsonvet: {config}: {message}\n"));
}
}
#[test]
fn sarif_holds_every_problem_and_keeps_the_exit_codes() {
let dir = workdir(&[
("dup.json", "{\n \"a\": 1,\n \"a\": 2\n}"),
("bom.json", "\u{FEFF}{}"),
("ok.json", "{}"),
("open.json", "[1, /* x"),
("unclosed.json", "{\"a\": [1"),
]);
let run = |args: &[&str]| {
let (code, out, err) = run_jsonvet(&dir, args, true);
assert_eq!(err, "", "{args:?}");
let sarif: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
(code, sarif)
};
let (code, mut sarif) = run(&["--format", "sarif", "dup.json", "gone.json", "ok.json"]);
assert_eq!(code, 1);
let version = &mut sarif["runs"][0]["tool"]["driver"]["version"];
assert_eq!(*version, env!("CARGO_PKG_VERSION"));
*version = "[version]".into();
insta::assert_snapshot!(serde_json::to_string_pretty(&sarif).expect("JSON"));
let (code, sarif) = run(&["--format=sarif", "--dialect=json:*.json", "bom.json"]);
assert_eq!(code, 0);
assert_eq!(sarif["runs"][0]["results"][0]["level"], "warning");
let args = [
"--format=sarif",
"--dialect=json:*.json",
"--max-warnings",
"0",
"bom.json",
];
assert_eq!(run(&args).0, 1);
let (code, sarif) = run(&["--format", "sarif", "ok.json"]);
assert_eq!(code, 0);
assert_eq!(sarif["runs"][0]["results"], serde_json::json!([]));
let (_, sarif) = run(&["--format", "sarif", "open.json"]);
let open = &sarif["runs"][0]["results"][0];
assert_eq!(
open["message"]["text"],
"unterminated block comment (started at 1:5)"
);
let start = &open["relatedLocations"][0];
assert_eq!(start["message"]["text"], "starts here");
assert_eq!(start["physicalLocation"]["region"]["startColumn"], 5);
let (_, sarif) = run(&["--format", "sarif", "unclosed.json"]);
let unclosed = &sarif["runs"][0]["results"][0];
assert_eq!(
unclosed["message"]["text"],
"unclosed `[`: expected `,` or `]`, found the end of the file (opened at 1:7)"
);
let opened = &unclosed["relatedLocations"][0];
assert_eq!(opened["message"]["text"], "opened here");
assert_eq!(opened["physicalLocation"]["region"]["startColumn"], 7);
}
#[test]
fn color_forced_off_a_terminal_keeps_one_line_per_problem() {
let dir = workdir(&[
("dup.json", "{\n \"a\": 1,\n \"a\": 2\n}\n"),
("open.json", "{\"a\": [1, True"),
("ok.json", "{}"),
]);
let run = |args: &[&str], vars: &[(&str, &str)]| {
let mut command = command(env!("CARGO_BIN_EXE_jsonvet"));
command
.args(args)
.args(["dup.json", "open.json", "ok.json", "gone.json"])
.current_dir(&dir)
.envs(vars.iter().copied());
let out = command.output().expect("run jsonvet");
assert_eq!(out.status.code(), Some(1), "{args:?} {vars:?}");
String::from_utf8(out.stdout).expect("UTF-8 output")
};
let plain = "dup.json:3:3: error[duplicate-key] duplicate key $.a (first at 2:3)\n\
open.json:1:11: error[syntax] expected a value, found `True`; did you mean `true`?\n\
gone.json: error: no such file\n";
let colored = "dup.json:3:3: \u{1b}[1m\u{1b}[91merror[duplicate-key]\u{1b}[0m duplicate key $.a (first at 2:3)\n\
open.json:1:11: \u{1b}[1m\u{1b}[91merror[syntax]\u{1b}[0m expected a value, found `True`; did you mean `true`?\n\
gone.json: \u{1b}[1m\u{1b}[91merror\u{1b}[0m: no such file\n";
assert_eq!(run(&["--color", "always"], &[]), colored);
assert_eq!(run(&[], &[]), plain);
assert_eq!(run(&["--color", "never"], &[]), plain);
assert_eq!(run(&["--color=auto"], &[("NO_COLOR", "1")]), plain);
assert_eq!(run(&[], &[("FORCE_COLOR", "1")]), colored);
assert_eq!(run(&[], &[("FORCE_COLOR", "")]), plain);
assert_eq!(run(&[], &[("FORCE_COLOR", "0")]), plain);
assert_eq!(run(&[], &[("FORCE_COLOR", "false")]), plain);
assert_eq!(run(&[], &[("FORCE_COLOR", "true")]), colored);
assert_eq!(
run(&["--color", "always"], &[("FORCE_COLOR", "0")]),
colored
);
assert_eq!(run(&[], &[("FORCE_COLOR", "1"), ("NO_COLOR", "1")]), plain);
assert_eq!(run(&[], &[("FORCE_COLOR", "1"), ("NO_COLOR", "")]), colored);
assert_eq!(run(&["--color", "never"], &[("FORCE_COLOR", "1")]), plain);
assert_eq!(run(&["--color", "always"], &[("NO_COLOR", "1")]), colored);
for format in ["sarif", "json"] {
let out = run(&["--color", "always", "--format", format], &[]);
assert!(!out.contains('\u{1b}'), "{out}");
assert!(
serde_json::from_str::<serde_json::Value>(&out).is_ok(),
"{out}"
);
}
let (code, _, err) = jsonvet_stderr(&dir, &["--color", "yes", "ok.json"]);
assert_eq!(code, 2);
assert!(
err.starts_with("jsonvet: --color takes auto, always or never, not `yes`\n"),
"{err}"
);
}
#[test]
fn a_utf16_or_utf32_file_names_its_encoding() {
let dir = workdir(&[]);
let files = [
("le16.json", &b"\xFF\xFE[\x00]\x00"[..], "UTF-16 (LE)"),
("be16.json", b"\xFE\xFF\x00[\x00]", "UTF-16 (BE)"),
("le32.json", b"\xFF\xFE\x00\x00[\x00\x00\x00", "UTF-32 (LE)"),
("be32.json", b"\x00\x00\xFE\xFF\x00\x00\x00[", "UTF-32 (BE)"),
];
for (name, bytes, encoding) in files {
fs::write(dir.join(name), bytes).expect("write file");
assert_eq!(
jsonvet(&dir, &[name]),
(
1,
format!("{name}: error: file is {encoding}; jsonvet reads UTF-8\n")
)
);
}
}
#[test]
fn a_utf16_or_utf32_file_without_a_mark_names_its_encoding() {
let dir = workdir(&[]);
let run = |args: &[&str]| jsonvet(&dir, args);
let files = [
("le16.json", &b"{\x00}\x00"[..], "UTF-16 (LE)"),
("be16.json", b"\x00[\x00]", "UTF-16 (BE)"),
("le32.json", b"[\x00\x00\x00]\x00\x00\x00", "UTF-32 (LE)"),
("be32.json", b"\x00\x00\x00[\x00\x00\x00]", "UTF-32 (BE)"),
];
for (name, bytes, encoding) in files {
fs::write(dir.join(name), bytes).expect("write file");
assert_eq!(
run(&[name]),
(
1,
format!("{name}: error: file is {encoding}; jsonvet reads UTF-8\n")
)
);
}
fs::write(dir.join("nul.json5"), b"'\x00a\x00'").expect("write file");
assert_eq!(run(&["nul.json5"]), (0, String::new()));
fs::write(dir.join("nul32.json5"), b"\"\x00\x00\x00\"").expect("write file");
assert_eq!(run(&["nul32.json5"]), (0, String::new()));
for (name, bytes) in [
("zeros.json", &b"\x00\x00\x00\x00"[..]),
("odd.json", b"\x00[\x00\x00"),
] {
fs::write(dir.join(name), bytes).expect("write file");
let (code, out) = run(&[name]);
assert_eq!(code, 1);
assert!(out.contains("error[syntax]"), "{name}: {out}");
}
fs::write(dir.join("plain.json"), b"[1]").expect("write file");
assert_eq!(run(&["plain.json"]), (0, String::new()));
}
#[test]
fn sarif_columns_count_code_points() {
let dir = workdir(&[("e.jsonc", "{\r\n\"\u{1F600}\": x}\r\n")]);
let (code, out) = jsonvet(&dir, &["--format", "sarif", "e.jsonc"]);
assert_eq!(code, 1);
let sarif: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let run = &sarif["runs"][0];
assert_eq!(run["columnKind"], "unicodeCodePoints");
let result = &run["results"][0];
assert_eq!(result["ruleId"], "syntax");
let region = &result["locations"][0]["physicalLocation"]["region"];
assert_eq!(
(®ion["startLine"], ®ion["startColumn"]),
(&2.into(), &6.into())
);
}
#[test]
fn json_holds_every_problem_and_keeps_the_exit_codes() {
let dir = workdir(&[
("schema.json", r#"{"type": "object"}"#),
("bad.json", "[1,"),
("dup.json", "{\n \"a\": 1,\n \"a\": 2\n}"),
("bom.json", "\u{FEFF}{}"),
("arr.json", "[]"),
]);
let args = [
"--format",
"json",
"--schema",
"schema.json",
"--dialect=json:*.json",
"bad.json",
"dup.json",
"bom.json",
"arr.json",
"gone.json",
];
let (code, out, err) = run_jsonvet(&dir, &args, true);
assert_eq!((code, err.as_str()), (1, ""));
let expected = r#"{"version":1,"diagnostics":[
{"file":"bad.json","rule":"syntax","url":"https://simonvanlierde.com/jsonvet/docs/usage.html#syntax","severity":"error","message":"unclosed `[`: expected a value, found the end of the file (opened at 1:1)","line":1,"column":4,"offset":3,"related":[{"message":"opened here","line":1,"column":1,"offset":0}]},
{"file":"dup.json","rule":"duplicate-key","url":"https://simonvanlierde.com/jsonvet/docs/usage.html#duplicate-key","severity":"error","message":"duplicate key $.a (first at 2:3)","line":3,"column":3,"offset":14,"related":[{"message":"first copy","line":2,"column":3,"offset":4}]},
{"file":"bom.json","rule":"byte-order-mark","url":"https://simonvanlierde.com/jsonvet/docs/usage.html#byte-order-mark","severity":"warning","message":"byte order mark at the start of the file","line":1,"column":1,"offset":0,"related":[]},
{"file":"arr.json","rule":"schema","url":"https://simonvanlierde.com/jsonvet/docs/usage.html#schema","severity":"error","message":"[] is not of type \"object\" (at $)","line":1,"column":1,"offset":0,"related":[]},
{"file":"gone.json","rule":"unreadable","url":"https://simonvanlierde.com/jsonvet/docs/usage.html#unreadable","severity":"error","message":"no such file","line":null,"column":null,"offset":null,"related":[]}
]}
"#;
assert_eq!(out, expected);
let json: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
assert_eq!(json["diagnostics"].as_array().map(Vec::len), Some(5));
let (code, out, _) = jsonvet_stderr(&dir, &["--format=json", "--dialect=json:*", "bom.json"]);
assert_eq!(code, 0);
assert!(out.contains(r#""severity":"warning""#), "{out}");
let args = [
"--format=json",
"--dialect=json:*",
"--max-warnings=0",
"bom.json",
];
assert_eq!(jsonvet_stderr(&dir, &args).0, 1);
for args in [&["--format", "json", "arr.json"][..], &["--format", "json"]] {
assert_eq!(
jsonvet_stderr(&dir, args),
(
0,
"{\"version\":1,\"diagnostics\":[\n]}\n".to_owned(),
String::new()
)
);
}
}
#[test]
fn json_strings_stay_valid_whatever_the_names_hold() {
let dir = workdir(&[]);
let name = "q\"b\\s\n::error::\u{1b}[2J\u{202e}##[x.json";
let key = "\u{7}\u{202a}##[k";
let args = ["--format", "json", "--stdin-filename", name];
let (code, out, _) = jsonvet_stdin(&dir, &args, &format!("{{\"{key}\":1,\"{key}\":2}}"));
assert_eq!(code, 1);
assert!(out.starts_with(
"{\"version\":1,\"diagnostics\":[\n{\"file\":\"q\\\"b\\\\s\\u000a::error::\\u001b[2J\\u202e##\\u005bx.json\""
), "{out}");
assert!(
out.lines()
.all(|line| !line.contains("##[") && !line.starts_with("::")),
"{out}"
);
let json: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let diagnostic = &json["diagnostics"][0];
assert_eq!(diagnostic["file"], name);
assert_eq!(
diagnostic["message"],
"duplicate key $[\"\\u0007\\u202a##[k\"] (first at 1:2)"
);
}
#[cfg(target_os = "linux")]
#[test]
fn json_shows_a_name_that_is_not_utf8_lossily() {
use std::os::unix::ffi::OsStrExt;
let dir = workdir(&[]);
let name = OsStr::from_bytes(b"\xed\xa0\x80\xff.json");
let args = [OsStr::new("--format"), OsStr::new("json"), name];
let (_, out, _) = run_jsonvet(&dir, &args, false);
let json: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
assert_eq!(
json["diagnostics"][0]["file"],
"\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}.json"
);
assert_eq!(json["diagnostics"][0]["rule"], "unreadable");
}
#[test]
fn the_last_matching_dialect_wins_and_auto_means_the_extension() {
let dir = workdir(&[
("x.cfg", "{'a': 1}"),
("y.cfg", "{'a': 1}"),
("c.jsonc", "{// c\n}"),
]);
let run = |args: &[&str]| jsonvet(&dir, args);
let jsonc = |name: &str| {
format!(
"{name}:1:2: error[syntax] single-quoted strings are not allowed in jsonc; the json5 dialect allows this\n"
)
};
let args = [
"--dialect",
"jsonc:*.cfg",
"--dialect",
"auto:x.cfg",
"x.cfg",
"y.cfg",
];
assert_eq!(run(&args), (1, jsonc("y.cfg")));
let args = [
"--dialect",
"auto:x.cfg",
"--dialect",
"jsonc:*.cfg",
"x.cfg",
"y.cfg",
];
assert_eq!(run(&args), (1, jsonc("x.cfg") + &jsonc("y.cfg")));
let args = ["--dialect", "json", "--dialect", "auto", "c.jsonc"];
assert_eq!(run(&args), (0, String::new()));
assert_eq!(run(&["--dialect", "json", "c.jsonc"]).0, 1);
}
#[cfg(windows)]
#[test]
fn a_backslash_path_matches_a_slash_pattern() {
let dir = workdir(&[("sub/a.json", "{a: 1}")]);
let (code, out) = jsonvet(&dir, &["--dialect", "json:sub/*.json", "sub\\a.json"]);
assert_eq!(
(code, out.as_str()),
(
1,
"sub\\a.json:1:2: error[syntax] unquoted keys are not allowed in json; the json5 dialect allows this\n"
)
);
}
#[test]
fn each_extension_has_its_dialect() {
let single = "{'a': 1}";
let dir = workdir(&[
("a.jsonc", single),
("a.hujson", single),
("a.json", single),
("a.cfg", single),
(".babelrc", single),
("a.jsonl", "{}\n{}\n"),
]);
for (name, dialect) in [("a.jsonc", "jsonc"), ("a.hujson", "jwcc")] {
assert_eq!(
jsonvet(&dir, &[name]),
(
1,
format!(
"{name}:1:2: error[syntax] single-quoted strings are not allowed in {dialect}; the json5 dialect allows this\n"
)
)
);
}
for name in ["a.json", "a.cfg", ".babelrc", "a.jsonl"] {
assert_eq!(jsonvet(&dir, &[name]), (0, String::new()), "{name}");
}
assert_eq!(jsonvet(&dir, &["--dialect", "json5", "a.jsonl"]).0, 1);
}
#[cfg(target_os = "linux")]
#[test]
fn a_failed_write_is_an_error() {
let dir = workdir(&[("bad.json", "[1,")]);
for args in [&["bad.json"][..], &["--version"]] {
let full = fs::File::create("/dev/full").expect("open /dev/full");
let out = command(env!("CARGO_BIN_EXE_jsonvet"))
.args(args)
.current_dir(&dir)
.stdout(full)
.output()
.expect("run jsonvet");
assert_eq!(out.status.code(), Some(2), "{args:?}");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.starts_with("jsonvet: cannot write output:"),
"{stderr}"
);
}
}
#[cfg(unix)]
#[test]
fn a_file_that_cannot_be_read_says_why() {
let dir = workdir(&[("a.json", "{}")]);
let (code, out) = jsonvet(&dir, &["a.json/b.json", "gone.json"]);
assert_eq!(code, 1);
let lines: Vec<&str> = out.lines().collect();
assert!(
lines[0].starts_with("a.json/b.json: error: cannot read: "),
"{out}"
);
assert_eq!(lines[1], "gone.json: error: no such file");
}
fn checked_once(args: &[&str], name: &str) {
let dir = workdir(&[
("a.json", "{\"a\":1,\"a\":2}\n"),
("sub/b.json", "{\"b\":1,\"b\":2}\n"),
]);
#[cfg(unix)]
std::os::unix::fs::symlink("a.json", dir.join("link.json")).expect("symlink");
let (code, out) = jsonvet(&dir, args);
assert_eq!(code, 1, "{out}");
assert_eq!(out.lines().filter(|l| l.contains(name)).count(), 1, "{out}");
}
#[test]
fn the_same_argument_twice_is_checked_once() {
checked_once(&["a.json", "a.json"], "a.json:");
}
#[test]
fn a_dot_slash_spelling_is_the_same_file() {
checked_once(&["a.json", "./a.json"], "a.json:");
}
#[test]
fn a_folder_and_a_file_in_it_are_checked_once() {
checked_once(&["sub", "sub/b.json"], "b.json:");
}
#[cfg(unix)]
#[test]
fn a_symlink_is_the_same_file() {
checked_once(&["a.json", "link.json"], "duplicate");
}
#[cfg(target_os = "linux")]
#[test]
fn a_file_too_big_for_memory_fails_alone() {
let dir = workdir(&[("ok.json", "{}")]);
fs::File::create(dir.join("huge.json"))
.and_then(|file| file.set_len(1 << 40))
.expect("create a sparse 1 TiB file");
let output = command("sh")
.args([
"-c",
"ulimit -v 67108864 2>/dev/null || [ \"$(ulimit -v)\" -le 67108864 ] || exit 2; exec \"$@\"",
"jsonvet-memory-test",
])
.arg(env!("CARGO_BIN_EXE_jsonvet"))
.args(["huge.json", "ok.json"])
.current_dir(&dir)
.output()
.expect("run with a 64 GiB address-space limit");
let out = String::from_utf8(output.stdout).expect("UTF-8 output");
assert_eq!(output.status.code(), Some(1), "{out}");
assert!(out.contains("huge.json: error: cannot read"), "{out}");
}
#[test]
fn a_missing_file_given_twice_is_reported_once() {
checked_once(&["a.json", "gone.json", "gone.json"], "gone.json:");
}
fn jsonvet_stdin(dir: &Path, args: &[&str], input: &str) -> (i32, String, String) {
use std::io::Write as _;
use std::process::Stdio;
let mut child = command(env!("CARGO_BIN_EXE_jsonvet"))
.args(args)
.current_dir(dir)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("run jsonvet");
let mut stdin = child.stdin.take().expect("a stdin pipe");
if let Err(e) = stdin.write_all(input.as_bytes()) {
assert_eq!(e.kind(), std::io::ErrorKind::BrokenPipe, "write stdin: {e}");
}
drop(stdin);
let out = child.wait_with_output().expect("wait for jsonvet");
(
out.status.code().expect("an exit code"),
String::from_utf8(out.stdout).expect("UTF-8 output"),
String::from_utf8(out.stderr).expect("UTF-8 errors"),
)
}
#[test]
fn a_dash_reads_standard_input() {
let dir = workdir(&[(
"jsonvet.toml",
"dialects = [{ dialect = \"json\", files = \"*.cfg\" }]\n",
)]);
let run = |args: &[&str], input: &str| jsonvet_stdin(&dir, args, input);
let dup = "{\"a\":1,\"a\":2}\n";
assert_eq!(
run(&["-"], dup),
(
1,
"-:1:8: error[duplicate-key] duplicate key $.a (first at 1:2)\n".to_owned(),
String::new()
)
);
let single = "{'a': 1}\n";
assert_eq!(run(&["-"], single).0, 0);
let (code, out, _) = run(&["--stdin-filename", "x.jsonc", "-"], single);
assert_eq!(code, 1);
assert!(out.starts_with("x.jsonc:1:2: error[syntax]"), "{out}");
let (code, out, _) = run(&["--stdin-filename=sub/a.cfg"], "{,}");
assert_eq!(code, 1);
assert!(out.starts_with("sub/a.cfg:1:2: error[syntax]"), "{out}");
assert_eq!(
run(&["-"], ""),
(
1,
"-:1:1: error[syntax] expected a value, found the end of the file\n".to_owned(),
String::new()
)
);
assert_eq!(run(&["-", "-"], "{}").0, 2);
}
#[test]
fn list_rules_and_explain() {
let dir = workdir(&[]);
let (code, out) = jsonvet(&dir, &["--list-rules"]);
assert_eq!(code, 0);
assert!(
out.starts_with("syntax (error)\n the first syntax error"),
"{out}"
);
let (code, out) = jsonvet(&dir, &["--explain", "duplicate-key"]);
assert_eq!(code, 0);
assert!(
out.contains("Bad: {\"port\": 80, \"port\": 8080}"),
"{out}"
);
let (code, out, err) = jsonvet_stderr(&dir, &["--explain=duplicat-key"]);
assert_eq!((code, out.as_str()), (2, ""));
assert!(
err.starts_with("jsonvet: unknown rule `duplicat-key` (did you mean `duplicate-key`?)"),
"{err}"
);
}
#[test]
fn piped_input_with_no_file_is_not_read() {
let dir = workdir(&[]);
assert_eq!(
jsonvet_stdin(&dir, &[], "{\"a\":1,\"a\":2}"),
(0, String::new(), String::new())
);
}
#[test]
fn sarif_keeps_other_brackets() {
let dir = workdir(&[("a.json", "[{\"a\": 1, \"a\": 2}]")]);
let (code, out) = jsonvet(&dir, &["--format", "sarif", "a.json"]);
assert_eq!(code, 1);
assert!(out.contains("duplicate key $[0].a"), "{out}");
}
#[test]
fn sarif_keeps_25000_results_and_counts_the_rest() {
let dir = workdir(&[("a.jsonl", &"{\"a\": 1, \"a\": 2}\n".repeat(25_002))]);
let (code, out, err) = jsonvet_stderr(&dir, &["--format", "sarif", "a.jsonl"]);
assert_eq!(code, 1);
let sarif: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let run = &sarif["runs"][0];
assert_eq!(run["results"].as_array().expect("results").len(), 25_000);
let text = "2 more results not listed: SARIF output keeps the first 25000";
let note = &run["invocations"][0]["toolExecutionNotifications"][0];
assert_eq!(note["message"]["text"], text);
assert_eq!(err, format!("jsonvet: {text}\n"));
}
fn assert_escaped(err: &str) {
assert!(err.starts_with("jsonvet: "), "{err}");
assert!(!err.contains("\n::") && !err.contains('\u{1b}'), "{err:?}");
}
#[test]
fn usage_errors_show_control_characters_escaped() {
let dir = workdir(&[("ok.json", "{}")]);
for arg in [
"--format=\n::error title=pwn::x.json",
"--dialect=\n::error::x",
"--dialect=\u{1b}[31m",
"--error=\n::error::x",
"--max-warnings=\u{1b}]0;x",
"--strcit\n::error::x",
"-q\n::error::x",
] {
let (code, out, err) = jsonvet_stderr(&dir, &[arg, "ok.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{arg:?}");
assert_escaped(&err);
}
}
fn jsonvet_stderr(dir: &Path, args: &[&str]) -> (i32, String, String) {
run_jsonvet(dir, args, false)
}
fn run_jsonvet(dir: &Path, args: &[impl AsRef<OsStr>], annotate: bool) -> (i32, String, String) {
let mut command = command(env!("CARGO_BIN_EXE_jsonvet"));
command.args(args).current_dir(dir);
if annotate {
command.env("GITHUB_ACTIONS", "true");
}
let out = command.output().expect("run jsonvet");
(
out.status.code().expect("an exit code"),
String::from_utf8(out.stdout).expect("UTF-8 output"),
String::from_utf8(out.stderr).expect("UTF-8 errors"),
)
}
mod schema {
use std::fmt::Write as _;
use super::*;
const SCHEMA: &str = r#"{
// Every file below breaks it the same three ways.
type: "object",
properties: {
name: { type: "string" },
port: { type: "integer" },
tags: { type: "array", items: { type: "string" } },
},
required: ["name"],
additionalProperties: false,
}
"#;
#[test]
fn schema_errors_point_at_the_value_in_every_dialect() {
let dir = workdir(&[
("schema.json", SCHEMA),
(
"strict.json",
"{\"name\": \"é\", \"port\": \"80\",\n \"tags\": [\"a\", 1], \"x\": 0}",
),
(
"i.json",
"{\"name\": \"é\", \"port\": \"80\",\n \"tags\": [\"a\", 1], \"x\": 0}",
),
(
"c.jsonc",
"{\"name\": /* é */ \"n\", \"port\": \"80\", // c\n \"tags\": [\"a\", 1], \"x\": 0}",
),
(
"w.hujson",
"{\"name\": \"n\", \"port\": \"80\",\n \"tags\": [\"a\", 1,], \"x\": 0,}",
),
(
"f.json5",
"{name: 'n', 'po\\u0072t': '80',\n tags: ['a', +0x1], x: 0,}",
),
(
"l.jsonl",
"{\"name\": \"n\", \"port\": 80}\n{\"port\": \"80\", \"tags\": [\"a\", 1], \"x\": 0}\n",
),
]);
let (code, out, _) = jsonvet_stderr(
&dir,
&[
"--schema",
"schema.json",
"--dialect",
"json:strict.json",
"--dialect",
"i-json:i.json",
"strict.json",
"i.json",
"c.jsonc",
"w.hujson",
"f.json5",
"l.jsonl",
],
);
assert_eq!(code, 1);
let expected = "\
strict.json:1:23: error[schema] \"80\" is not of type \"integer\" (at $.port)
strict.json:2:16: error[schema] 1 is not of type \"string\" (at $.tags[1])
strict.json:2:20: error[schema] Additional properties are not allowed ('x' was unexpected) (at $.x)
i.json:1:23: error[schema] \"80\" is not of type \"integer\" (at $.port)
i.json:2:16: error[schema] 1 is not of type \"string\" (at $.tags[1])
i.json:2:20: error[schema] Additional properties are not allowed ('x' was unexpected) (at $.x)
c.jsonc:1:31: error[schema] \"80\" is not of type \"integer\" (at $.port)
c.jsonc:2:16: error[schema] 1 is not of type \"string\" (at $.tags[1])
c.jsonc:2:20: error[schema] Additional properties are not allowed ('x' was unexpected) (at $.x)
w.hujson:1:23: error[schema] \"80\" is not of type \"integer\" (at $.port)
w.hujson:2:16: error[schema] 1 is not of type \"string\" (at $.tags[1])
w.hujson:2:21: error[schema] Additional properties are not allowed ('x' was unexpected) (at $.x)
f.json5:1:26: error[schema] \"80\" is not of type \"integer\" (at $.port)
f.json5:2:14: error[schema] 1 is not of type \"string\" (at $.tags[1])
f.json5:2:21: error[schema] Additional properties are not allowed ('x' was unexpected) (at $.x)
l.jsonl:2:1: error[schema] \"name\" is a required property (at $)
l.jsonl:2:10: error[schema] \"80\" is not of type \"integer\" (at $.port)
l.jsonl:2:30: error[schema] 1 is not of type \"string\" (at $.tags[1])
l.jsonl:2:34: error[schema] Additional properties are not allowed ('x' was unexpected) (at $.x)
";
assert_eq!(out, expected);
let (_, annotated, _) = run_jsonvet(&dir, &["--schema", "schema.json", "f.json5"], true);
assert!(annotated.contains(
"::error file=f.json5,line=1,col=26,title=jsonvet (schema)::\"80\" is not of type"
));
let (_, out, _) = jsonvet_stderr(
&dir,
&["--format", "sarif", "--schema", "schema.json", "f.json5"],
);
let sarif: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let result = &sarif["runs"][0]["results"][0];
assert_eq!(result["ruleId"], "schema");
let region = &result["locations"][0]["physicalLocation"]["region"];
assert_eq!(
(®ion["startLine"], ®ion["startColumn"]),
(&1.into(), &26.into())
);
}
#[test]
fn a_file_with_a_syntax_error_gets_no_schema_errors() {
let dir = workdir(&[
("schema.json", SCHEMA),
("bad.json", "{\"port\": \"80\", \"x\": [1,"),
("dup.json", "{\"name\": \"n\", \"name\": \"m\"}"),
]);
let (code, out, _) =
jsonvet_stderr(&dir, &["--schema", "schema.json", "bad.json", "dup.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
"bad.json:1:24: error[syntax] unclosed `[`: expected a value, found the end of the file (opened at 1:21)\n\
dup.json:1:15: error[duplicate-key] duplicate key $.name (first at 1:2)\n"
);
}
#[test]
fn a_config_schemas_entry_and_a_local_dollar_schema_check_like_the_schema_option() {
let dir = workdir(&[
("schema.json", SCHEMA),
(
"jsonvet.toml",
"schemas = [{ files = \"conf/*.json\", schema = \"schema.json\" }]\n",
),
("conf/a.json", "{\"port\": \"80\", \"x\": 0}"),
(
"own/b.json",
"{\"$schema\": \"../schema.json\", \"port\": \"80\"}",
),
("plain.json", "{\"port\": \"80\", \"x\": 0}"),
(
"remote.json",
"{\"$schema\": \"https://example.com/s.json\", \"port\": \"80\"}",
),
("nested.json", "{\"a\": {\"$schema\": \"schema.json\"}}"),
]);
for file in ["conf/a.json", "own/b.json"] {
let given = jsonvet(&dir, &["--schema", "schema.json", file]);
assert_eq!(given.0, 1, "{file}");
assert!(given.1.contains("error[schema]"), "{file}: {}", given.1);
let at = |(code, out): &(i32, String)| {
let lines = out
.lines()
.map(|l| l.split(" error").next().map(str::to_owned));
(*code, lines.collect::<Vec<_>>())
};
let own = jsonvet(&dir, &[file]);
assert_eq!(at(&own), at(&given), "{file}");
assert!(
own.1.contains("value fails \"required\""),
"{file}: {}",
own.1
);
}
assert_eq!(
jsonvet(&dir, &["plain.json", "remote.json", "nested.json"]),
(0, String::new())
);
let (code, out) = jsonvet(&dir, &["."]);
assert_eq!(code, 1);
let out = out.replace(std::path::MAIN_SEPARATOR, "/");
assert_eq!(
out.lines().map(|l| l.split(':').next()).collect::<Vec<_>>(),
[
Some("./conf/a.json"),
Some("./conf/a.json"),
Some("./conf/a.json"),
Some("./own/b.json"),
Some("./own/b.json"),
Some("./own/b.json"),
],
"{out}"
);
}
#[test]
fn a_schema_that_cannot_be_used_is_reported_unless_a_file_names_a_missing_one() {
let dir = workdir(&[
("gone.json", "{\"$schema\": \"missing.json\", \"a\": 1}"),
("broken.json", "{\"type\": "),
("uses-broken.json", "{\n \"$schema\": \"broken.json\"\n}"),
("ok.json", "{}"),
(
"bad.toml",
"schemas = [{ files = \"*.json\", schema = \"broken.json\" }]\n",
),
(
"typo.toml",
"schemas = [{ files = \"*.json\", schemas = \"broken.json\" }]\n",
),
]);
let (code, out, err) = jsonvet_stderr(&dir, &["gone.json", "ok.json", "gone.json"]);
assert_eq!((code, out.as_str()), (0, ""), "{err}");
assert_eq!(
err,
"jsonvet: note: the schema missing.json that a `$schema` names does not exist, so the \
files that name it are checked without it\n"
);
let (code, out, err) = jsonvet_stderr(&dir, &["gone.json", "uses-broken.json", "ok.json"]);
assert_eq!(code, 1, "{err}");
let lines: Vec<&str> = out.lines().collect();
assert_eq!(lines.len(), 1, "{out}");
assert_eq!(
lines[0],
"uses-broken.json:2:14: error[schema] the schema broken.json:1:10 cannot be used"
);
let (code, out, err) = jsonvet_stderr(&dir, &["--config", "bad.toml", "ok.json"]);
assert_eq!((code, out.as_str()), (2, ""));
assert!(err.contains("broken.json:1:10: unclosed `{`"), "{err}");
let (code, _, err) = jsonvet_stderr(&dir, &["--config", "typo.toml", "ok.json"]);
assert_eq!(code, 2);
assert!(
err.ends_with(
"typo.toml:1:32: unknown key `schemas` (did you mean `schema`?) in a `schemas` \
entry; expected files and schema\n"
),
"{err}"
);
}
fn picked_by(dir: &Path, args: &[&str]) -> String {
let (code, out, err) = jsonvet_stderr(dir, args);
assert_eq!(code, 1, "{out}{err}");
[
("ka", ".json:1:14)"),
("kb", ".json:1:15)"),
("kc", ".json:1:16)"),
]
.into_iter()
.filter(|(key, at)| out.contains(key) || out.contains(at))
.map(|(key, _)| key)
.collect()
}
const SB: &str = "{ \"required\": [\"kb\"]}";
fn keyed_schemas() -> Vec<(&'static str, &'static str)> {
vec![
("sa.json", "{\"required\": [\"ka\"]}"),
("sb.json", SB),
("sc.json", "{ \"required\": [\"kc\"]}"),
]
}
#[test]
fn schema_option_beats_config_beats_a_files_own_dollar_schema() {
let mut files = keyed_schemas();
files.push((
"jsonvet.toml",
"schemas = [{ files = \"d/*.json\", schema = \"sb.json\" }]\n",
));
files.push(("d/f.json", "{\"$schema\": \"../sc.json\"}"));
let dir = workdir(&files);
assert_eq!(picked_by(&dir, &["--schema", "sa.json", "d/f.json"]), "ka");
assert_eq!(picked_by(&dir, &["d/f.json"]), "kb");
assert_eq!(picked_by(&dir, &["--no-config", "d/f.json"]), "kc");
}
#[test]
fn the_last_matching_schemas_entry_wins() {
let mut files = keyed_schemas();
files.push((
"jsonvet.toml",
"schemas = [{ files = \"d/*.json\", schema = \"sa.json\" }, \
{ files = \"d/f.json\", schema = \"sb.json\" }]\n",
));
files.push((
"other.toml",
"schemas = [{ files = \"d/f.json\", schema = \"sb.json\" }, \
{ files = \"d/*.json\", schema = \"sa.json\" }]\n",
));
files.push(("d/f.json", "{}"));
let dir = workdir(&files);
assert_eq!(picked_by(&dir, &["d/f.json"]), "kb");
assert_eq!(
picked_by(&dir, &["--config", "other.toml", "d/f.json"]),
"ka"
);
}
#[test]
fn a_nested_config_file_takes_its_own_schemas_and_replaces_the_roots() {
let mut files = keyed_schemas();
files.push((
"jsonvet.toml",
"schemas = [{ files = \"*.json\", schema = \"sa.json\" }]\n",
));
files.push(("top.json", "{}"));
files.push(("sub/sb.json", SB));
files.push((
"sub/jsonvet.toml",
"schemas = [{ files = \"*.json\", schema = \"sb.json\" }]\n",
));
files.push(("sub/f.json", "{}"));
files.push(("bare/jsonvet.toml", "exclude = [\"nothing\"]\n"));
files.push(("bare/f.json", "{}"));
let dir = workdir(&files);
assert_eq!(picked_by(&dir, &["top.json"]), "ka");
assert_eq!(picked_by(&dir, &["sub/f.json"]), "kb");
assert_eq!(jsonvet(&dir, &["bare/f.json"]), (0, String::new()));
}
#[test]
fn an_unquoted_json5_dollar_schema_key_is_found() {
let mut files = keyed_schemas();
files.push(("f.json5", "{$schema: 'sc.json'}"));
let dir = workdir(&files);
assert_eq!(picked_by(&dir, &["f.json5"]), "kc");
}
#[test]
fn a_dollar_schema_may_end_in_an_empty_fragment() {
let mut files = keyed_schemas();
files.push(("f.json", "{\"$schema\": \"sc.json#\"}"));
let dir = workdir(&files);
assert_eq!(picked_by(&dir, &["f.json"]), "kc");
}
#[test]
fn a_dollar_schema_key_written_with_an_escape_is_found() {
let mut files = keyed_schemas();
files.push(("f.json", r#"{"\u0024schema": "sc.json"}"#));
files.push(("g.json", r#"{"$sch\u0065ma": "sc.json"}"#));
let dir = workdir(&files);
assert_eq!(picked_by(&dir, &["f.json"]), "kc");
assert_eq!(picked_by(&dir, &["g.json"]), "kc");
}
#[test]
fn a_config_schema_path_is_relative_to_the_config_file_not_the_working_directory() {
let mut files = keyed_schemas();
files.push(("sub/sa.json", SB));
files.push((
"sub/jsonvet.toml",
"schemas = [{ files = \"deep/*.json\", schema = \"sa.json\" }]\n",
));
files.push(("sub/deep/f.json", "{}"));
let dir = workdir(&files);
assert_eq!(picked_by(&dir, &["sub/deep/f.json"]), "kb");
assert_eq!(
picked_by(&dir, &["--config", "sub/jsonvet.toml", "sub/deep/f.json"]),
"kb"
);
}
#[test]
fn only_a_top_level_string_dollar_schema_names_a_schema() {
let dir = workdir(&[
("fail.json", "{\"not\": {}}"),
("key.json", "{\"x\": \"fail.json\"}"),
("number.json", "{\"$schema\": 5}"),
("empty.json", "{\"$schema\": \"\"}"),
("fragment.json", "{\"$schema\": \"#\"}"),
("array.json", "[{\"$schema\": \"fail.json\"}]"),
("nested.json", "{\"a\": {\"$schema\": \"fail.json\"}}"),
("lines.jsonl", "{\"$schema\": \"fail.json\"}\n"),
]);
for file in [
"key.json",
"number.json",
"empty.json",
"fragment.json",
"array.json",
"nested.json",
"lines.jsonl",
] {
assert_eq!(jsonvet(&dir, &[file]), (0, String::new()), "{file}");
}
let dir = workdir(&[
("fail.json", "{\"not\": {}}"),
(
"late.json",
"{\"a\": {\"$schema\": 1, \"b\": [{}]}, \"c\": [[]], \"$schema\": \"fail.json\"}",
),
]);
let (code, out) = jsonvet(&dir, &["late.json"]);
assert_eq!(code, 1, "{out}");
assert!(out.contains("error[schema]"), "{out}");
}
#[test]
fn a_file_with_a_local_dollar_schema_nested_past_512_levels_has_a_syntax_error() {
let text = format!(
"{{\"$schema\": \"schema.json\", \"a\": {}0{}}}",
"[".repeat(600),
"]".repeat(600)
);
let dir = workdir(&[("schema.json", "{}"), ("deep.json", &text)]);
let (code, out) = jsonvet(&dir, &["deep.json"]);
assert_eq!(code, 1, "{out}");
assert!(out.contains("nested deeper than 512 levels"), "{out}");
}
#[test]
fn a_config_file_outside_the_project_named_by_config_may_name_a_schema_beside_it() {
let dir = workdir(&[]);
let shared = outside(&dir, "s.json", "{\"required\": [\"kx\"]}");
let shared = shared.parent().expect("the outside folder");
fs::write(
shared.join("shared.toml"),
"schemas = [{ files = \"*.json\", schema = \"s.json\" }]\n",
)
.expect("write config");
fs::write(shared.join("f.json"), "{}").expect("write file");
let config = shared.join("shared.toml");
let file = shared.join("f.json");
let (code, out, err) = jsonvet_stderr(
&dir,
&[
"--config",
&config.to_string_lossy(),
&file.to_string_lossy(),
],
);
assert_eq!(code, 1, "{out}{err}");
assert!(out.contains("kx"), "{out}");
}
#[cfg(unix)]
#[test]
fn a_dollar_schema_that_cannot_be_looked_up_is_an_error() {
let dir = workdir(&[("f.json", "{\"$schema\": \"f.json/s.json\"}")]);
let (code, out) = jsonvet(&dir, &["f.json"]);
assert_eq!(code, 1, "{out}");
assert!(
out.starts_with("f.json:1:13: error[schema] cannot read the schema f.json/s.json: "),
"{out}"
);
}
#[test]
fn a_rooted_dollar_schema_is_an_error_and_a_missing_one_is_not() {
for (schema, expected) in [
("/schemas/v1.json", 1),
("//host/x.json", 1),
("C:/x.json", 1),
("C:x.json", 1),
(r"\\\\host\\share\\x.json", 1),
("missing.json", 0),
] {
let dir = workdir(&[("f.json", &format!("{{\"$schema\": \"{schema}\"}}"))]);
let (code, out) = jsonvet(&dir, &["f.json"]);
assert_eq!(code, expected, "{schema}: {out}");
if expected == 1 {
assert!(out.contains("is outside the project"), "{schema}: {out}");
}
}
}
#[test]
fn a_config_schema_is_named_from_the_working_directory() {
let config =
|schema| format!("schemas = [{{ files = \"*.json\", schema = \"{schema}\" }}]\n");
let dir = workdir(&[
("jsonvet.toml", &config("s.json")),
("s.json", "{\"enum\": [1]}"),
("a.json", "2"),
("sub/jsonvet.toml", &config("../s.json")),
("sub/b.json", "2"),
]);
let sub = format!("sub{}../s.json", std::path::MAIN_SEPARATOR);
for (file, name) in [("a.json", "s.json"), ("sub/b.json", sub.as_str())] {
let (code, out) = jsonvet(&dir, &[file]);
assert_eq!(code, 1, "{out}");
assert_eq!(
out,
format!(
"{file}:1:1: error[schema] value fails \"enum\" (the schema {name}:1:10) (at $)\n"
)
);
}
}
#[test]
fn one_dollar_schema_from_two_folders_gets_two_verdicts() {
let own = "{\"$schema\": \"../s.json\", \"a\": 1}";
let names: Vec<String> = (0..40).map(|i| format!("sub/f{i}.json")).collect();
let mut files = vec![("s.json", "{\"required\": [\"b\"]}"), ("h.json", own)];
files.extend(names.iter().map(|name| (name.as_str(), own)));
let dir = workdir(&files);
let mut args: Vec<&str> = names.iter().map(String::as_str).collect();
args.push("h.json");
let (code, out) = jsonvet(&dir, &args);
assert_eq!(code, 1, "{out}");
let lines: Vec<&str> = out.lines().collect();
assert_eq!(lines.len(), 41, "{out}");
for (line, name) in lines.iter().zip(&names) {
let fails = format!("{name}:1:1: error[schema] value fails \"required\"");
assert!(line.starts_with(&fails), "{out}");
}
assert!(
lines[40].starts_with("h.json:1:13: error[schema] the schema ../s.json is outside"),
"{out}"
);
}
fn outside(dir: &Path, name: &str, text: &str) -> PathBuf {
let folder = dir.with_extension("outside");
fs::create_dir_all(&folder).expect("create the outside folder");
let path = folder.join(name);
fs::write(&path, text).expect("write the outside file");
path
}
#[test]
fn a_schema_outside_the_project_is_refused_and_never_quoted() {
let dir = workdir(&[("ok.json", "{}"), ("sub/ok.json", "{}")]);
let folder = dir
.file_name()
.expect("a name")
.to_string_lossy()
.into_owned();
let text = outside(&dir, "secret.json", "TOPSECRET:x:0:0\n");
let values = outside(&dir, "values.json", "{\"type\": \"TOPSECRET\"}");
let escaped = |path: &Path| path.to_string_lossy().replace('\\', "\\\\");
let mut cases = vec![
("absolute.json", escaped(&text)),
("values.json", escaped(&values)),
("up.json", format!("../{folder}.outside/secret.json")),
("up-values.json", format!("../{folder}.outside/values.json")),
(
"up-missing.json",
format!("../{folder}.outside/missing.json"),
),
];
#[cfg(unix)]
{
std::os::unix::fs::symlink(&text, dir.join("link.json")).expect("symlink");
std::os::unix::fs::symlink(&values, dir.join("link-values.json")).expect("symlink");
cases.push(("linked.json", "link.json".to_owned()));
cases.push(("linked-values.json", "link-values.json".to_owned()));
let folder = text.parent().expect("the outside folder");
for (link, target) in [
("dangling.json", folder.join("missing.json")),
("out-dir", folder.to_owned()),
("dead-dir", folder.join("missing")),
] {
std::os::unix::fs::symlink(target, dir.join(link)).expect("symlink");
}
cases.push(("to-dangling.json", "dangling.json".to_owned()));
cases.push(("through-dir.json", "out-dir/missing.json".to_owned()));
cases.push(("through-dead-dir.json", "dead-dir/s.json".to_owned()));
}
cases.push(("back.json", format!(r"..\\{folder}.outside\\secret.json")));
cases.push((
"back-sub.json",
format!(r"sub\\..\\..\\{folder}.outside\\x.json"),
));
for (file, schema) in &cases {
fs::write(dir.join(file), format!("{{\"$schema\": \"{schema}\"}}"))
.expect("write file");
for format in ["text", "json", "sarif"] {
let (code, out, err) = jsonvet_stderr(&dir, &["--format", format, file]);
assert_eq!(code, 1, "{file} {format}: {out}{err}");
assert!(!out.contains("TOPSECRET"), "{file} {format}: {out}");
assert!(
out.contains("outside the project"),
"{file} {format}: {out}"
);
}
let schema = if Path::new(schema).is_absolute() {
schema.clone()
} else {
format!("../{schema}")
};
fs::write(
dir.join("sub/jsonvet.toml"),
format!("schemas = [{{ files = \"*.json\", schema = \"{schema}\" }}]\n"),
)
.expect("write config");
let (code, out, err) = jsonvet_stderr(&dir, &["sub/ok.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{file}: {err}");
assert!(!err.contains("TOPSECRET"), "{file}: {err}");
assert!(err.contains("jsonvet.toml:1:"), "{file}: {err}");
assert!(err.contains("outside the project"), "{file}: {err}");
}
#[cfg(unix)]
{
std::os::unix::fs::symlink(dir.join("nothing.json"), dir.join("dead.json"))
.expect("symlink");
fs::write(dir.join("inside.json"), "{\"$schema\": \"dead.json\"}").expect("write");
assert_eq!(jsonvet(&dir, &["inside.json"]), (0, String::new()));
}
let (code, out) = jsonvet(&dir, &["--schema", &values.to_string_lossy(), "ok.json"]);
assert_eq!(code, 2, "{out}");
}
#[test]
fn a_file_in_another_repository_takes_schemas_from_its_own_project() {
let a = workdir(&[]);
let config = "schemas = [{ files = \"x.json\", schema = \"s.json\" }]\n";
for (repo, file, text) in [
("configured", "x.json", "{}"),
("own", "y.json", "{\"$schema\": \"s.json\"}"),
] {
let b = a.with_extension(repo);
fs::create_dir_all(b.join(".git")).expect("the other repository");
fs::write(b.join("s.json"), "{\"type\": \"array\"}").expect("write schema");
fs::write(b.join(file), text).expect("write file");
if repo == "configured" {
fs::write(b.join("jsonvet.toml"), config).expect("write config");
}
let name = b
.file_name()
.expect("a name")
.to_string_lossy()
.into_owned();
let path = format!("../{name}/{file}");
let (code, out, err) = jsonvet_stderr(&a, &[&path]);
assert_eq!(code, 1, "{file}: {out}{err}");
assert!(out.contains("error[schema]"), "{file}: {out}");
assert!(!out.contains("outside"), "{file}: {out}");
}
}
#[test]
fn a_broken_schema_named_by_the_file_or_a_config_shows_no_text_from_it() {
let dir = workdir(&[
("ok.json", "{}"),
("text.json", "TOPSECRET\n"),
("values.json", "{\n \"type\": \"TOPSECRET\"\n}"),
("a.json", "{\"$schema\": \"text.json\"}"),
("b.json", "{\"$schema\": \"values.json\"}"),
(
"c.toml",
"schemas = [{ files = \"*.json\", schema = \"values.json\" }]\n",
),
(
"sub/jsonvet.toml",
"schemas = [{ files = \"*.json\", schema = \"../values.json\" }]\n",
),
("sub/ok.json", "{}"),
]);
for format in ["text", "json", "sarif"] {
let (code, out, err) = jsonvet_stderr(&dir, &["--format", format, "a.json", "b.json"]);
assert_eq!(code, 1, "{format}: {err}");
assert!(!out.contains("TOPSECRET"), "{format}: {out}");
}
let (_, out) = jsonvet(&dir, &["a.json", "b.json"]);
assert_eq!(
out,
"a.json:1:13: error[schema] the schema text.json:1:1 cannot be used\n\
b.json:1:13: error[schema] the schema values.json:2:11 cannot be used\n"
);
let (code, _, err) = jsonvet_stderr(&dir, &["sub/ok.json"]);
assert_eq!(code, 2);
assert!(!err.contains("TOPSECRET"), "{err}");
assert!(err.contains("values.json:2:11 cannot be used"), "{err}");
let (code, _, err) = jsonvet_stderr(&dir, &["--config", "c.toml", "ok.json"]);
assert_eq!(code, 2);
assert!(err.contains("TOPSECRET"), "{err}");
}
#[test]
fn a_ref_outside_a_schema_named_by_the_file_is_not_quoted() {
let dir = workdir(&[
(
"remote.json",
r#"{"$ref": "https://topsecret.example/a.json"}"#,
),
("a.json", r#"{"$schema": "remote.json"}"#),
]);
let (code, out) = jsonvet(&dir, &["a.json"]);
assert_eq!(code, 1, "{out}");
assert!(!out.contains("topsecret"), "{out}");
assert!(
out.contains("the schema remote.json has a `$ref` outside the file"),
"{out}"
);
}
#[test]
fn a_schema_named_by_the_file_or_a_config_quotes_nothing_from_it_in_its_errors() {
let secret = r#"{
"enum": ["SECRET-ENUM-VALUE"],
"const": "SECRET-CONST",
"pattern": "^SECRET-PATTERN$",
"required": ["SECRET_KEY_NAME"],
"properties": {
"p": {"pattern": "^SECRET-P$"},
"d": {"not": {"type": "string", "description": "SECRET-NOT"}},
"k": {"maxLength": 1, "minimum": 2},
"c": {"pattern": "^(a|a)*\\1SECRET"}
},
"patternProperties": {"^(a|a)*\\1SECRET": {"type": "string"}},
"dependentRequired": {"p": ["SECRET_DEP"]},
"propertyNames": {"pattern": "^([$a-z]+|SECRET-NAMES)$"}
}"#;
let costly = "a".repeat(30) + "!";
let members = format!(
"\"p\": \"x\", \"d\": \"y\", \"k\": \"zz\", \"c\": \"{costly}\", \"Q\": 1, \"{costly}\": 1}}"
);
let a = format!("{{\"$schema\": \"./secret.json\", {members}");
let b = format!("{{{members}");
let dir = workdir(&[
("secret.json", secret),
("a.json", &a),
(
"sub/jsonvet.toml",
"[[schemas]]\nfiles = \"*.json\"\nschema = \"../secret.json\"\n",
),
("sub/b.json", &b),
]);
for file in ["a.json", "sub/b.json"] {
for format in ["text", "json", "sarif"] {
let (code, out, err) = jsonvet_stderr(&dir, &["--format", format, file]);
assert_eq!(code, 1, "{file} {format}: {err}");
assert!(!out.contains("SECRET"), "{file} {format}: {out}");
assert!(
out.contains("fails \\\"enum\\\"") || out.contains("fails \"enum\""),
"{out}"
);
}
}
let (_, out) = jsonvet(&dir, &["a.json"]);
for line in [
"a.json:1:1: error[schema] value fails \"required\" (the schema ./secret.json:5:15) (at $)\n",
"error[schema] this string could not be checked against a pattern: matching it takes more than 5000 backtracking steps (at $.c)\n",
"error[schema] this key could not be checked against a pattern: matching it",
] {
assert!(out.contains(line), "{out}");
}
let (_, out) = jsonvet(&dir, &["--schema", "secret.json", "sub/b.json"]);
assert!(out.contains("\"SECRET-CONST\" was expected"), "{out}");
assert!(out.contains("the pattern \"^(a|a)*\\1SECRET\""), "{out}");
}
#[test]
fn a_quiet_schema_error_under_an_embedded_id_does_not_point_at_the_root() {
let schema = r#"{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"maxProperties": 9,
"properties": { "a": { "$ref": "https://ex.com/sub" } },
"$defs": { "s": { "$id": "https://ex.com/sub", "maxProperties": 1 } }
}"#;
let dir = workdir(&[
("s.json", schema),
("a.json", r#"{"$schema":"./s.json","a":{"x":1,"y":2}}"#),
(
"b.json",
r#"{"$schema":"./s.json","b":1,"c":1,"d":1,"e":1,"f":1,"g":1,"h":1,"i":1,"j":1}"#,
),
]);
let (code, out) = jsonvet(&dir, &["a.json"]);
assert_eq!(code, 1, "{out}");
assert!(!out.contains("s.json:4:20"), "{out}");
assert!(
out.contains("value fails \"maxProperties\" (the schema ./s.json) (at $.a)"),
"{out}"
);
let (code, out) = jsonvet(&dir, &["b.json"]);
assert_eq!(code, 1, "{out}");
assert!(
out.contains("value fails \"maxProperties\" (the schema ./s.json:4:20) (at $)"),
"{out}"
);
}
#[test]
fn a_quiet_schema_error_under_the_roots_own_id_points_at_the_root() {
let schema = r#"{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://ex.com/root",
"maxProperties": 1
}"#;
let dir = workdir(&[
("s.json", schema),
("a.json", r#"{"$schema":"./s.json","a":1}"#),
]);
let (code, out) = jsonvet(&dir, &["a.json"]);
assert_eq!(code, 1, "{out}");
assert!(
out.contains("value fails \"maxProperties\" (the schema ./s.json:4:20) (at $)"),
"{out}"
);
}
#[test]
fn a_quiet_schema_property_named_like_a_keyword_still_has_places() {
let schema = "{\"properties\":{\"e\":{\"properties\":{\"enum\":{\"minimum\":7}}}}}";
let dir = workdir(&[
("s.json", schema),
("a.json", r#"{"$schema":"./s.json","e":{"enum":1}}"#),
]);
let (code, out) = jsonvet(&dir, &["a.json"]);
assert_eq!(code, 1, "{out}");
assert!(
out.contains("value fails \"minimum\" (the schema ./s.json:1:53)"),
"{out}"
);
}
#[test]
fn a_configured_schema_that_no_file_matches_is_still_read() {
let dir = workdir(&[
("ok.json", "{}"),
("broken.json", "{\"type\": "),
(
"gone.toml",
"schemas = [{ files = \"nothing/*.json\", schema = \"missing.json\" }]\n",
),
(
"broken.toml",
"schemas = [{ files = \"nothing/*.json\", schema = \"broken.json\" }]\n",
),
]);
let (code, out, err) = jsonvet_stderr(&dir, &["--config", "gone.toml", "ok.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{err}");
assert!(err.contains("missing.json"), "{err}");
let (code, _, err) = jsonvet_stderr(&dir, &["--config", "broken.toml", "ok.json"]);
assert_eq!(code, 2, "{err}");
assert!(err.contains("broken.json:1:10: unclosed `{`"), "{err}");
let (code, _, err) = jsonvet_stderr(
&dir,
&["--config", "gone.toml", "--schema", "ok.json", "ok.json"],
);
assert_eq!(code, 0, "{err}");
}
#[test]
fn a_schema_that_cannot_be_used_is_a_usage_error() {
let dir = workdir(&[
("ok.json", "{}"),
("type.json", "{\n \"type\": 5\n}"),
("broken.json", "{\"type\": "),
(
"remote.json",
"{\"properties\": {\"a\": {\"$ref\": \"https://example.com/a.json\"}}}",
),
("relative.json", "{\"$ref\": \"other.json\"}"),
(
"local.json",
"{\"$defs\": {\"s\": {\"type\": \"string\"}}, \"$ref\": \"#/$defs/s\"}",
),
]);
let cases = [
(
"type.json",
"jsonvet: the schema type.json:2:11: 5 is not valid under any of the schemas listed in the 'anyOf' keyword\n",
),
(
"broken.json",
"jsonvet: the schema broken.json:1:10: unclosed `{`: expected a value, found the end of the file\n",
),
(
"remote.json",
"jsonvet: the schema remote.json has a `$ref` to https://example.com/a.json, outside the file; external references are not fetched\n",
),
(
"relative.json",
"jsonvet: the schema relative.json has a `$ref` to other.json, outside the file; external references are not fetched\n",
),
("gone.json", "jsonvet: cannot read the schema gone.json: "),
];
for (schema, message) in cases {
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", schema, "ok.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{schema}");
assert!(err.starts_with(message), "{schema}: {err}");
}
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "local.json", "ok.json"]);
assert_eq!(
(code, out.as_str()),
(
1,
"ok.json:1:1: error[schema] {} is not of type \"string\" (at $)\n"
)
);
}
fn chain(hops: usize, base: &str, last: &str) -> String {
let mut text = "{\n\"$id\": \"https://example.com/x.json\",\n\"$defs\": {\n".to_owned();
for i in 0..hops {
let _ = writeln!(
text,
"\"d{i}\": {{\"$ref\": \"{base}#/$defs/d{}\"}},",
i + 1
);
}
let _ = write!(
text,
"\"d{hops}\": {last}\n}},\n\"$ref\": \"#/$defs/d0\"\n}}"
);
text
}
#[test]
fn each_json_lines_document_places_its_own_errors() {
let strings = format!("[{}]", vec!["\"s\""; 101].join(","));
let text = format!("[]\n\"a\"\n{strings}\n");
let dir = workdir(&[
(
"schema.json",
"{\"type\": \"array\", \"items\": {\"type\": \"integer\"}}",
),
("l.jsonl", &text),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "l.jsonl"]);
assert_eq!(code, 1);
let lines: Vec<&str> = out.lines().collect();
assert_eq!(lines.len(), 102, "{out}");
assert_eq!(
lines[0],
"l.jsonl:2:1: error[schema] \"a\" is not of type \"array\" (at $)"
);
assert_eq!(
lines[101],
"l.jsonl:3:1: error[schema] ... and more schema errors in this document"
);
}
#[test]
fn schema_load_errors_show_control_characters_escaped() {
let dir = workdir(&[
("ok.json", "{}"),
("nl.json", r#"{"$ref": "urn:\n::error title=pwned::x"}"#),
(
"esc.json",
r#"{"$ref": "x:\u001b]0;TITLE\u0007\u001b[31mRED"}"#,
),
]);
let mut schemas = vec!["nl.json", "esc.json", "gone\u{1b}[31m.json"];
if cfg!(unix) {
fs::write(dir.join("two\n::error::x.json"), "{} {}").expect("write file");
schemas.push("two\n::error::x.json");
}
for schema in schemas {
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", schema, "ok.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{schema:?}");
assert_escaped(&err);
}
}
#[test]
fn a_long_ref_chain_is_a_usage_error() {
let dir = workdir(&[
("ok.json", "[[[1]]]"),
("local.json", &chain(1_000, "", "{}")),
(
"by-id.json",
&chain(1_000, "https://example.com/x.json", "{}"),
),
(
"cycle.json",
&chain(100, "", "{\"items\": {\"$ref\": \"#\"}}"),
),
]);
for schema in ["local.json", "by-id.json"] {
let start = std::time::Instant::now();
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", schema, "ok.json"]);
no_hang(start);
assert_eq!((code, out.as_str()), (2, ""), "{schema}");
assert_eq!(
err,
format!(
"jsonvet: the schema {schema}:129:10: this `$ref` leads more than 128 levels \
deep, counting each `$ref` followed as a level\n"
),
);
}
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "cycle.json", "ok.json"]);
assert_eq!((code, out.as_str()), (0, ""));
}
#[test]
fn a_schema_whose_refs_double_at_each_level_is_a_usage_error() {
let defs: Vec<String> = (0..24)
.map(|i| {
let next = format!("{{\"$ref\": \"#/$defs/d{}\"}}", i + 1);
format!("\"d{i}\": {{\"anyOf\": [{next}, {next}]}}")
})
.collect();
let schema = format!(
"{{\"$ref\": \"#/$defs/d0\", \"$defs\": {{{}, \"d24\": {{\"type\": \"string\"}}}}}}",
defs.join(", ")
);
let dir = workdir(&[("ok.json", "{}"), ("fan.json", &schema)]);
let start = std::time::Instant::now();
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "fan.json", "ok.json"]);
no_hang(start);
assert_eq!((code, out.as_str()), (2, ""));
assert!(
err.ends_with(
"this `$ref` takes the schema past 5000000 objects and arrays, counting what each \
`$ref` points at every time it is followed\n"
),
"{err}"
);
}
#[test]
fn a_schema_whose_regexes_cost_too_much_to_compile_is_a_usage_error() {
let costly = r#"{"properties": {"a": {"pattern": "(?:[a-z]{1000}){100}N"}}}"#;
let many = |n: usize| {
let regexes: Vec<String> = (0..n)
.map(|i| format!("\"p{i}\": {{\"pattern\": \"^k{i}$\"}}"))
.collect();
format!("{{\"properties\": {{{}}}}}", regexes.join(", "))
};
let dir = workdir(&[
("ok.json", "{}"),
("costly.json", costly),
("1000.json", &many(1000)),
("1001.json", &many(1001)),
]);
let start = std::time::Instant::now();
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "costly.json", "ok.json"]);
assert!(
start.elapsed().as_secs_f64() < 1.0,
"took {:?}",
start.elapsed()
);
assert_eq!((code, out.as_str()), (2, ""));
assert_eq!(
err,
"jsonvet: the schema costly.json:1:34: \"(?:[a-z]{1000}){100}N\" is not a \"regex\", \
or it takes more than 2 MB compiled\n"
);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "1000.json", "ok.json"]);
assert_eq!((code, out.as_str()), (0, ""));
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "1001.json", "ok.json"]);
assert_eq!((code, out.as_str()), (2, ""));
let column = many(1001).find("\"p999\"").expect("p999") + "\"p999\": {".len() + 1;
assert_eq!(
err,
format!(
"jsonvet: the schema 1001.json:1:{column}: this regex takes the schema past 1000 \
distinct regexes, each allowed 2 MB once compiled\n"
)
);
}
#[test]
fn schemas_whose_regexes_together_cost_too_much_are_a_usage_error() {
let many = |name: &str| {
let regexes: Vec<String> = (0..400).map(|i| format!("\"^{name}{i}$\": {{}}")).collect();
format!("{{\"patternProperties\": {{{}}}}}", regexes.join(", "))
};
let dir = workdir(&[
("a.json", &many("a")),
("b.json", &many("b")),
("c.json", &many("c")),
("use-a.json", "{\"$schema\": \"a.json\"}"),
("use-b.json", "{\"$schema\": \"b.json\"}"),
("use-c.json", "{\"$schema\": \"c.json\"}"),
]);
assert_eq!(
jsonvet(&dir, &["use-a.json", "use-b.json"]),
(0, String::new())
);
let (code, out, err) = jsonvet_stderr(&dir, &["use-a.json", "use-b.json", "use-c.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{err}");
assert!(
err.ends_with(
".json takes the schemas this run reads past 1000 regexes between them, each \
allowed 2 MB once compiled\n"
),
"{err}"
);
fs::write(
dir.join("jsonvet.toml"),
"schemas = [\n { files = \"a*.json\", schema = \"a.json\" },\n \
{ files = \"b*.json\", schema = \"b.json\" },\n \
{ files = \"c*.json\", schema = \"c.json\" },\n]\n",
)
.expect("write config");
let (code, out, err) = jsonvet_stderr(&dir, &["use-a.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{err}");
assert!(err.starts_with("jsonvet: the schema c.json takes"), "{err}");
}
#[test]
fn schema_errors_in_a_large_file() {
let mut text = String::from("[\n");
for i in 0..100_000 {
let port = if i % 10_000 == 0 {
"\"80\"".to_owned()
} else {
i.to_string()
};
let _ = writeln!(text, " {{\"name\": \"n{i}\", \"port\": {port}}},");
}
text.push_str(" {\"name\": \"last\"}\n]\n");
let schema = "{\"items\": {\"properties\": {\"port\": {\"type\": \"integer\"}}}}";
let dir = workdir(&[
("schema.json", schema),
("object.json", "{\"type\": \"object\"}"),
("big.json", &text),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "big.json"]);
assert_eq!(code, 1);
let lines: Vec<&str> = out.lines().collect();
assert_eq!(lines.len(), 10);
assert_eq!(
lines[0],
"big.json:2:26: error[schema] \"80\" is not of type \"integer\" (at $[0].port)"
);
assert_eq!(
lines[9],
"big.json:90002:30: error[schema] \"80\" is not of type \"integer\" (at $[90000].port)"
);
assert_eq!(
jsonvet_stderr(&dir, &["--schema", "object.json", "big.json"]).1,
"big.json:1:1: error[schema] value is not of type \"object\" (at $)\n"
);
}
const RECURSIVE: &str = r##"{
"allOf": [{"$ref": "#/$defs/a"}],
"$defs": {
"a": {"allOf": [{"$ref": "#/$defs/o"}]},
"o": {"additionalProperties": {"$ref": "#"}, "type": "object"}
}
}"##;
#[test]
fn a_deep_file_with_long_keys_is_placed_quickly() {
let key = "k".repeat(100);
let mut text = String::new();
for i in 0..512 {
let _ = write!(text, "{{\"{key}{i}\":");
}
text.push('0');
text.push_str(&"}".repeat(512));
let dir = workdir(&[("schema.json", RECURSIVE), ("deep.json", &text)]);
let start = std::time::Instant::now();
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "deep.json"]);
no_hang(start);
assert_eq!(code, 1);
let column = text.len() - 512;
assert!(
out.starts_with(&format!(
"deep.json:1:{column}: error[schema] 0 is not of type \"object\" (at $.{key}0…"
)),
"{out}"
);
assert!(out.ends_with("...\n"), "{out}");
assert_eq!(out.lines().count(), 1);
}
#[test]
fn a_file_nested_past_512_levels_has_a_syntax_error_under_schema_only() {
let text = format!("{}0{}", "{\"a\":".repeat(9_990), "}".repeat(9_990));
let dir = workdir(&[("schema.json", RECURSIVE), ("deep.json", &text)]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "deep.json"]);
assert_eq!(
(code, out.as_str()),
(
1,
"deep.json:1:2561: error[syntax] nested deeper than 512 levels\n"
)
);
assert_eq!(jsonvet(&dir, &["deep.json"]), (0, String::new()));
}
#[test]
fn a_deep_schema_is_a_usage_error() {
let schema = format!("{}{{}}{}", "{\"not\":".repeat(5_000), "}".repeat(5_000));
let dir = workdir(&[("schema.json", &schema), ("ok.json", "{}")]);
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "schema.json", "ok.json"]);
assert_eq!((code, out.as_str()), (2, ""));
assert_eq!(
err,
"jsonvet: the schema schema.json:1:897: nested deeper than 128 levels\n"
);
}
#[test]
fn a_backtracking_pattern_is_cut_short_and_reported() {
let strings: Vec<String> = (0..500)
.map(|i| format!("\"{}!{i}\"", "a".repeat(30)))
.collect();
let dir = workdir(&[
("schema.json", r#"{"items": {"pattern": "^(a|a)*\\1b"}}"#),
("r.json", &format!("[{}]", strings.join(", "))),
]);
let start = std::time::Instant::now();
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "r.json"]);
no_hang(start);
assert_eq!(code, 1);
assert_eq!(out.lines().count(), 101);
assert!(
out.ends_with(
"r.json:1:1: error[schema] ... and more schema errors in this document\n"
)
);
assert!(out.starts_with(
"r.json:1:2: error[schema] this string could not be checked against the pattern \"^(a|a)*\\1b\": matching it takes more than 5000 backtracking steps (at $[0])\n"
), "{out}");
}
#[test]
fn a_key_too_costly_to_match_against_pattern_properties_fails_the_file() {
let key = format!("{}!", "a".repeat(30));
let dir = workdir(&[
(
"slow.json",
r#"{"patternProperties": {"^(a|a)*\\1b|!": false}}"#,
),
(
"linear.json",
r#"{"patternProperties": {"^(a+)+$": false}}"#,
),
(
"normal.json",
r#"{"patternProperties": {"^a": {"type": "integer"}, "^x": {"type": "string"}}}"#,
),
(
"names.json",
r#"{"propertyNames": {"not": {"pattern": "^(a|a)*\\1b|!"}}}"#,
),
("k.json", &format!("{{\"x\": \"s\", \"{key}\": 1}}")),
]);
for schema in ["slow.json", "names.json"] {
let start = std::time::Instant::now();
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", schema, "k.json"]);
no_hang(start);
assert_eq!(code, 1, "{schema}");
assert_eq!(
out,
format!(
"k.json:1:12: error[schema] this key could not be checked against the pattern \
\"^(a|a)*\\1b|!\": matching it takes more than 5000 backtracking steps \
(at $[\"{key}\"])\n"
),
"{schema}"
);
}
for schema in ["linear.json", "normal.json"] {
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", schema, "k.json"]);
assert_eq!((code, out.as_str()), (0, ""), "{schema}");
}
}
#[test]
fn costly_patterns_are_found_in_arrays_and_tried_on_keys_only_under_property_names() {
let slow = format!("{}!", "a".repeat(30));
let pattern = r#""^(a|a)*\\1b|!""#;
let dir = workdir(&[
(
"names.json",
&format!("{{\"propertyNames\": {{\"pattern\": {pattern}}}}}"),
),
(
"all.json",
&format!("{{\"allOf\": [{{\"pattern\": {pattern}}}]}}"),
),
(
"items.json",
&format!("{{\"items\": {{\"pattern\": {pattern}}}}}"),
),
("k.json", &format!("{{\"{slow}\": 1}}")),
("s.json", &format!("\"{slow}\"")),
]);
let message = |what: &str, at: &str| {
format!(
"error[schema] this {what} could not be checked against the pattern \
\"^(a|a)*\\1b|!\": matching it takes more than 5000 backtracking steps (at {at})\n"
)
};
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "names.json", "k.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
format!("k.json:1:2: {}", message("key", &format!("$[\"{slow}\"]")))
);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "all.json", "s.json"]);
assert_eq!(code, 1);
assert_eq!(out, format!("s.json:1:1: {}", message("string", "$")));
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "items.json", "k.json"]);
assert_eq!((code, out.as_str()), (0, ""));
}
#[test]
fn a_costly_pattern_error_takes_one_of_the_100_places() {
let numbers: Vec<String> = (0..150).map(|i| i.to_string()).collect();
let dir = workdir(&[
(
"schema.json",
r#"{"items": {"type": "string", "pattern": "^(a|a)*\\1b"}}"#,
),
(
"n.json",
&format!("[\"{}!\", {}]", "a".repeat(30), numbers.join(",")),
),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "n.json"]);
assert_eq!(code, 1);
assert_eq!(out.lines().count(), 101);
assert!(
out.ends_with(
"n.json:1:1: error[schema] ... and more schema errors in this document\n"
),
"{out}"
);
}
#[test]
fn past_100_errors_text_says_there_are_more_and_sarif_and_json_leave_that_out() {
let numbers: Vec<String> = (0..101).map(|i| i.to_string()).collect();
let dir = workdir(&[
("schema.json", r#"{"items": {"type": "string"}}"#),
("n.json", &format!("[{}]", numbers.join(","))),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "n.json"]);
assert_eq!(code, 1);
let lines: Vec<&str> = out.lines().collect();
assert_eq!(lines.len(), 101);
assert_eq!(
lines[99],
"n.json:1:289: error[schema] 99 is not of type \"string\" (at $[99])"
);
assert_eq!(
lines[100],
"n.json:1:1: error[schema] ... and more schema errors in this document"
);
let (code, out, _) = jsonvet_stderr(
&dir,
&["--format", "sarif", "--schema", "schema.json", "n.json"],
);
assert_eq!(code, 1);
let sarif: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let results = sarif["runs"][0]["results"].as_array().expect("results");
assert_eq!(results.len(), 100);
let (code, out, _) = jsonvet_stderr(
&dir,
&["--format", "json", "--schema", "schema.json", "n.json"],
);
assert_eq!(code, 1);
let json: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let diagnostics = json["diagnostics"].as_array().expect("diagnostics");
assert_eq!(diagnostics.len(), 100);
assert_eq!(diagnostics[99]["offset"], 288);
}
#[test]
fn a_long_key_in_the_path_is_cut_with_the_message() {
let key = "\u{e9}".repeat(100_000);
let ones = vec!["1"; 150].join(",");
let dir = workdir(&[
(
"schema.json",
r#"{"additionalProperties": {"items": {"type": "string"}}}"#,
),
("k.json", &format!("{{\"{key}\": [{ones}]}}")),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "k.json"]);
assert_eq!(code, 1);
assert_eq!(out.lines().count(), 101);
for line in out.lines() {
assert!(line.len() < 300, "{line}");
}
}
#[test]
fn a_json_lines_dialect_for_every_file_leaves_the_schema_one_value() {
let dir = workdir(&[
("schema.json", "{\n \"type\": \"object\"\n}\n"),
("d.jsonl", "{}\n[]\n"),
]);
let (code, out, _) = jsonvet_stderr(
&dir,
&["--dialect", "jsonl", "--schema", "schema.json", "d.jsonl"],
);
assert_eq!(code, 1);
assert_eq!(
out,
"d.jsonl:2:1: error[schema] [] is not of type \"object\" (at $)\n"
);
}
#[test]
fn a_schema_value_json_schema_cannot_hold_is_a_usage_error() {
let dir = workdir(&[
("nan.json5", "{enum: [NaN, 1]}"),
(
"keys.json",
r#"{"properties": {"\ud800": true, "\ud801": false}}"#,
),
("null.json", "null"),
]);
let cases = [
(
"nan.json5",
"jsonvet: the schema nan.json5:1:9: NaN, Infinity and numbers beyond a double's range have no JSON Schema value\n",
),
(
"keys.json",
"jsonvet: the schema keys.json:1:33: this key and an earlier one in the object are the same once lone surrogates become U+FFFD\n",
),
];
for (schema, message) in cases {
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", schema, "null.json"]);
assert_eq!((code, out.as_str(), err.as_str()), (2, "", message));
}
}
#[test]
fn keys_that_differ_only_in_lone_surrogates_skip_the_document() {
let dir = workdir(&[
(
"schema.json",
r#"{"additionalProperties": {"type": "string"}}"#,
),
("s.json", r#"{"\ud800": 1, "\ud801": "s"}"#),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "s.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
"s.json:1:15: error[schema] this key and an earlier one in the object are the same once \
lone surrogates become U+FFFD, so this document is not checked against the schema\n"
);
}
#[test]
fn many_keys_with_lone_surrogates_take_linear_time() {
let keys: Vec<String> = (0..20_000)
.map(|i| format!("\"\\ud800{i}\": {i}"))
.collect();
let dir = workdir(&[
("schema.json", r#"{"type": "object"}"#),
("k.json", &format!("{{{}}}", keys.join(","))),
]);
let start = std::time::Instant::now();
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "k.json"]);
no_hang(start);
assert_eq!((code, out.as_str()), (0, ""));
}
#[test]
fn a_lone_surrogate_key_has_one_path_in_schema_and_duplicate_key_errors() {
let dir = workdir(&[
(
"schema.json",
r#"{"properties": {"\ud800": {"type": "string"}}}"#,
),
("one.json", r#"{"\ud800": 1}"#),
("two.json", r#"{"\ud800":1,"\ud800":2}"#),
]);
let (code, out, _) =
jsonvet_stderr(&dir, &["--schema", "schema.json", "one.json", "two.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
"one.json:1:12: error[schema] 1 is not of type \"string\" (at $[\"\\ud800\"])\n\
two.json:1:13: error[duplicate-key] duplicate key $[\"\\ud800\"] (first at 1:2)\n\
two.json:1:22: error[schema] 2 is not of type \"string\" (at $[\"\\ud800\"])\n"
);
}
#[test]
fn each_unexpected_key_is_one_error_up_to_the_cap() {
let members: Vec<String> = (0..1_000).map(|i| format!("\"k{i}\": {i}")).collect();
let file = format!("{{{}}}", members.join(", "));
for schema in [
r#"{"properties": {"a": true}, "additionalProperties": false}"#,
r#"{"additionalProperties": false}"#,
] {
let dir = workdir(&[("schema.json", schema), ("many.json", &file)]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "many.json"]);
assert_eq!(code, 1);
let lines: Vec<&str> = out.lines().collect();
assert_eq!(lines.len(), 101, "{schema}");
assert_eq!(
lines[0],
"many.json:1:2: error[schema] Additional properties are not allowed ('k0' was unexpected) (at $.k0)"
);
assert_eq!(
lines[100],
"many.json:1:1: error[schema] ... and more schema errors in this document"
);
}
}
#[test]
fn control_characters_in_keys_are_escaped_in_the_output() {
let dir = workdir(&[
(
"schema.json",
r#"{"additionalProperties": {"type": "string"}}"#,
),
("k.json", r#"{"x\u001b]0;t\u0007\r\n::error::y": 1}"#),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "k.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
"k.json:1:37: error[schema] 1 is not of type \"string\" (at $[\"x\\u001b]0;t\\u0007\\r\\n::error::y\"])\n"
);
}
#[test]
fn schema_paths_are_written_as_duplicate_key_paths_are() {
let key = "a/b~c.d\\\"\u{e9}";
let schema = format!(
r#"{{"required": ["z"], "properties": {{"": {{"type": "string"}}, "~1": {{"type": "string"}}, "{key}": {{"items": {{"type": "integer"}}}}}}}}"#
);
let file = format!(r#"{{"": 1, "": 2, "{key}": [0], "{key}": [0, "x"], "~1": 3}}"#);
let dir = workdir(&[("schema.json", &schema), ("e.json", &file)]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "e.json"]);
assert_eq!(code, 1);
let quoted = "[\"a/b~c.d\\\"é\"]";
assert_eq!(
out,
format!(
"e.json:1:9: error[duplicate-key] duplicate key $[\"\"] (first at 1:2)\n\
e.json:1:35: error[duplicate-key] duplicate key ${quoted} (first at 1:16)\n\
e.json:1:1: error[schema] \"z\" is a required property (at $)\n\
e.json:1:13: error[schema] 2 is not of type \"string\" (at $[\"\"])\n\
e.json:1:53: error[schema] \"x\" is not of type \"integer\" (at ${quoted}[1])\n\
e.json:1:65: error[schema] 3 is not of type \"string\" (at $[\"~1\"])\n"
)
);
}
#[test]
fn file_and_schema_errors_cannot_be_reconfigured() {
let dir = workdir(&[("schema.json", "{}"), ("ok.json", "{}")]);
for rule in ["schema", "unreadable"] {
fs::write(
dir.join("jsonvet.toml"),
format!("[rules]\n{rule} = \"warn\"\n"),
)
.expect("config");
let message = format!("the `{rule}` rule is always an error and cannot be set\n");
for flag in ["--error", "--warn", "--ignore"] {
let (code, out, err) =
jsonvet_stderr(&dir, &[flag, rule, "--schema", "schema.json", "ok.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{flag}");
assert!(
err.starts_with(&format!("jsonvet: {message}")),
"{flag}: {err}"
);
}
let (code, _, err) = jsonvet_stderr(&dir, &["--schema", "schema.json", "ok.json"]);
assert_eq!(code, 2);
assert!(
err.contains(&format!("jsonvet.toml:2:1: {message}")),
"{err}"
);
}
}
#[test]
fn a_document_with_nan_is_reported_at_the_first_one() {
let dir = workdir(&[
("schema.json", r#"{"items": {"type": "string"}}"#),
("n.json5", "[1,\n NaN, Infinity]"),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "n.json5"]);
assert_eq!(code, 1);
assert_eq!(
out,
"n.json5:2:2: error[schema] NaN, Infinity and numbers beyond a double's range have no \
JSON Schema value, so this document is not checked against the schema\n"
);
}
#[test]
fn unexpected_and_named_keys_point_at_the_key() {
let dir = workdir(&[
(
"additional.json",
r#"{"properties": {"a": true}, "additionalProperties": false}"#,
),
(
"unevaluated.json",
r#"{"properties": {"a": true}, "unevaluatedProperties": false}"#,
),
("names.json", r#"{"propertyNames": {"maxLength": 1}}"#),
("closed.json", r#"{"additionalProperties": false}"#),
(
"patterns.json",
r#"{"patternProperties": {"^[ab]$": true}, "additionalProperties": false}"#,
),
("o.json", "{\"z\": 1,\n \"a\": 0, \"b\": 2}"),
("n.json", "{\"a\": 0,\n \"long\": 1}"),
]);
let cases = [
(
"additional.json",
"o.json",
"o.json:1:2: error[schema] Additional properties are not allowed ('z' was unexpected) (at $.z)\n\
o.json:2:10: error[schema] Additional properties are not allowed ('b' was unexpected) (at $.b)\n",
),
(
"closed.json",
"o.json",
"o.json:1:2: error[schema] Additional properties are not allowed ('z' was unexpected) (at $.z)\n\
o.json:2:2: error[schema] Additional properties are not allowed ('a' was unexpected) (at $.a)\n\
o.json:2:10: error[schema] Additional properties are not allowed ('b' was unexpected) (at $.b)\n",
),
(
"patterns.json",
"o.json",
"o.json:1:2: error[schema] Additional properties are not allowed ('z' was unexpected) (at $.z)\n",
),
(
"unevaluated.json",
"o.json",
"o.json:1:2: error[schema] Unevaluated properties are not allowed ('b', 'z' were unexpected) (at $)\n",
),
(
"names.json",
"n.json",
"n.json:2:2: error[schema] \"long\" is longer than 1 character (at $)\n",
),
];
for (schema, file, expected) in cases {
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", schema, file]);
assert_eq!((code, out.as_str()), (1, expected), "{schema}");
}
}
#[test]
fn ignored_duplicate_keys_still_place_schema_errors() {
let schema = r#"{"properties": {"a": {"properties": {"b": {"type": "integer"}}, "additionalProperties": false}}}"#;
let dir = workdir(&[
("schema.json", schema),
(
"d.json",
"{\"a\": {\"b\": 1},\n \"a\": {\"b\": \"x\", \"c\": 0}}",
),
]);
let (code, out, _) = jsonvet_stderr(
&dir,
&[
"--ignore",
"duplicate-key",
"--schema",
"schema.json",
"d.json",
],
);
assert_eq!(code, 1);
assert_eq!(
out,
"d.json:2:13: error[schema] \"x\" is not of type \"integer\" (at $.a.b)\n\
d.json:2:18: error[schema] Additional properties are not allowed ('c' was unexpected) (at $.a.c)\n"
);
let dir = workdir(&[
(
"schema.json",
r#"{"properties": {}, "additionalProperties": false}"#,
),
("d.json", r#"{"b":1,"a":1,"b":2}"#),
]);
let args = [
"--ignore",
"duplicate-key",
"--schema",
"schema.json",
"d.json",
];
assert_eq!(
jsonvet(&dir, &args),
(
1,
"d.json:1:2: error[schema] Additional properties are not allowed ('b' was unexpected) (at $.b)\n\
d.json:1:8: error[schema] Additional properties are not allowed ('a' was unexpected) (at $.a)\n"
.to_owned()
)
);
}
#[test]
fn a_schema_file_with_more_than_one_value_is_a_usage_error() {
let dir = workdir(&[("schema.jsonl", "{}\n{}\n"), ("ok.json", "{}")]);
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "schema.jsonl", "ok.json"]);
assert_eq!(
(code, out.as_str(), err.as_str()),
(
2,
"",
"jsonvet: the schema schema.jsonl must hold one value\n"
)
);
}
#[cfg(unix)]
#[test]
fn a_deep_file_is_checked_against_a_recursive_schema_on_a_small_main_stack() {
let text = format!("{}0{}", "[".repeat(512), "]".repeat(512));
let dir = workdir(&[
(
"schema.json",
r##"{"items": {"$ref": "#"}, "type": "array"}"##,
),
("deep.json", &text),
]);
let out = command("sh")
.arg("-c")
.arg("ulimit -s 256 && exec \"$0\" --schema schema.json deep.json")
.arg(env!("CARGO_BIN_EXE_jsonvet"))
.current_dir(&dir)
.output()
.expect("run jsonvet");
let stdout = String::from_utf8_lossy(&out.stdout);
assert_eq!(out.status.code(), Some(1), "{stdout}");
assert!(
stdout.starts_with("deep.json:1:513: error[schema] 0 is not of type \"array\""),
"{stdout}"
);
}
#[cfg(target_os = "linux")]
#[test]
fn an_array_failing_everywhere_is_checked_in_bounded_memory() {
let text = format!("[{}]", vec!["0"; 1_000_000].join(","));
let dir = workdir(&[
("schema.json", r#"{"items": {"type": "string"}}"#),
("many.json", &text),
]);
let out = command("sh")
.arg("-c")
.arg("ulimit -v 300000 && exec \"$0\" --schema schema.json many.json")
.arg(env!("CARGO_BIN_EXE_jsonvet"))
.current_dir(&dir)
.output()
.expect("run jsonvet");
let stdout = String::from_utf8_lossy(&out.stdout);
assert_eq!(
out.status.code(),
Some(1),
"{}",
String::from_utf8_lossy(&out.stderr)
);
assert!(
stdout.ends_with(
"many.json:1:1: error[schema] ... and more schema errors in this document\n"
),
"{stdout}"
);
}
fn doubling(levels: usize, last: &str) -> String {
let defs: Vec<String> = (0..levels)
.map(|i| {
let next = format!("{{\"$ref\": \"#/$defs/l{}\"}}", i + 1);
format!("\"l{i}\": {{\"anyOf\": [{next}, {next}]}}")
})
.collect();
format!("{}, \"l{levels}\": {last}", defs.join(", "))
}
#[cfg(target_os = "linux")]
fn capped(dir: &Path, kilobytes: usize, file: &str) -> (Option<i32>, String) {
let out = command("sh")
.arg("-c")
.arg(format!(
"ulimit -v {kilobytes} && exec \"$0\" --schema schema.json {file}"
))
.arg(env!("CARGO_BIN_EXE_jsonvet"))
.current_dir(dir)
.output()
.expect("run jsonvet");
(
out.status.code(),
String::from_utf8_lossy(&out.stdout).into_owned(),
)
}
#[cfg(target_os = "linux")]
#[test]
fn errors_that_hold_much_of_the_schema_are_dropped_as_they_come() {
let enum_ = (1..=20_000).map(|n| n.to_string()).collect::<Vec<_>>();
let members: Vec<String> = (0..2_000).map(|i| format!("\"k{i}\": 0")).collect();
let cases = [
(
format!(
"{{\"$defs\": {{{}}}, \"items\": {{\"$ref\": \"#/$defs/l0\"}}}}",
doubling(14, "{\"type\": \"string\"}")
),
format!("[{}]", vec!["0"; 40].join(",")),
40,
),
(
format!(
"{{\"additionalProperties\": {{\"enum\": [{}]}}}}",
enum_.join(",")
),
format!("{{{}}}", members.join(",")),
101,
),
];
for (schema, text, lines) in cases {
let dir = workdir(&[("schema.json", &schema), ("many.json", &text)]);
let (code, out) = capped(&dir, 300_000, "many.json");
assert_eq!(code, Some(1), "{out}");
assert_eq!(out.lines().count(), lines, "{out}");
}
}
#[test]
fn errors_past_the_first_100_are_not_looked_for() {
let schema = format!(
"{{\"$defs\": {{{}}}, \"items\": {{\"$ref\": \"#/$defs/l0\"}}}}",
doubling(10, "{\"type\": \"string\"}")
);
let text = format!("[{}]", vec!["0"; 5_000].join(","));
let dir = workdir(&[("schema.json", &schema), ("many.json", &text)]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "many.json"]);
assert_eq!(code, 1);
let lines: Vec<&str> = out.lines().collect();
assert_eq!(lines.len(), 101);
assert_eq!(
lines[100],
"many.json:1:1: error[schema] ... and more schema errors in this document"
);
}
#[test]
fn each_costly_key_is_reported_once_and_a_match_that_decides_nothing_not_at_all() {
let slow = format!("{}!", "a".repeat(30));
let dir = workdir(&[
(
"false.json",
r#"{"patternProperties": {"^(a|a)*\\1b|!": false, "^(a|a)*\\1c|!": false}}"#,
),
(
"true.json",
r#"{"patternProperties": {"^(a|a)*\\1b|!": true}, "anyOf": [{"pattern": "^(a|a)*\\1b|!"}, {"type": "object"}]}"#,
),
("k.json", &format!("{{\"{slow}\": 1, \"{slow}{slow}\": 2}}")),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "false.json", "k.json"]);
assert_eq!(code, 1);
assert_eq!(out.lines().count(), 2, "{out}");
assert!(
out.lines()
.all(|line| line.contains("this key could not be checked against the pattern")),
"{out}"
);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "true.json", "k.json"]);
assert_eq!((code, out.as_str()), (0, ""));
}
#[test]
fn a_costly_key_is_reported_only_for_the_pattern_jsonschema_names() {
let dir = workdir(&[
(
"schema.json",
r#"{"allOf": [{"patternProperties": {"^(a|a)*\\1b": false, "^(c|c)*\\1d": true}}]}"#,
),
(
"k.json",
&format!("{{\"{}!\": 1, \"{}!\": 2}}", "a".repeat(30), "c".repeat(30)),
),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "k.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
format!(
"k.json:1:2: error[schema] this key could not be checked against the pattern \
\"^(a|a)*\\1b\": matching it takes more than 5000 backtracking steps \
(at $[\"{}!\"])\n",
"a".repeat(30)
)
);
}
#[test]
fn a_costly_string_is_named_with_the_pattern_the_schema_applies_there() {
let dir = workdir(&[
(
"schema.json",
r#"{"$defs": {"unused": {"pattern": "^(a|a)*\\1b"}}, "properties": {"x": {"pattern": "^(a|a)*\\1c"}}}"#,
),
("x.json", &format!("{{\"x\": \"{}!\"}}", "a".repeat(30))),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "x.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
"x.json:1:7: error[schema] this string could not be checked against the pattern \
\"^(a|a)*\\1c\": matching it takes more than 5000 backtracking steps (at $.x)\n"
);
}
#[test]
fn costly_keys_in_one_pass_stop_at_the_error_cap() {
let slow = format!("{}!", "a".repeat(30));
let keys: Vec<String> = (0..101).map(|i| format!("\"{slow}{i}\": {i}")).collect();
let dir = workdir(&[
(
"schema.json",
r#"{"allOf": [{"patternProperties": {"^(a|a)*\\1b|!": false}}]}"#,
),
("k.json", &format!("{{{}}}", keys.join(", "))),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "k.json"]);
assert_eq!(code, 1);
assert_eq!(out.lines().count(), 101, "{out}");
assert!(
out.ends_with("... and more schema errors in this document\n"),
"{out}"
);
}
#[test]
fn a_costly_string_the_schema_does_not_match_on_is_not_an_error() {
let dir = workdir(&[
(
"schema.json",
r#"{"properties": {"code": {"type": "string", "pattern": "(\\w)\\1"}, "age": {"type": "integer"}}}"#,
),
(
"d.json",
&format!("{{\"bio\": \"{}\", \"age\": \"old\"}}", "ab-".repeat(7_000)),
),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "d.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
"d.json:1:21020: error[schema] \"old\" is not of type \"integer\" (at $.age)\n"
);
}
#[test]
fn a_costly_pattern_behind_a_ref_to_its_own_id_is_still_named() {
let dir = workdir(&[
(
"schema.json",
r#"{"$id": "https://example.com/root", "$defs": {"s": {"$id": "s", "pattern": "^(a|a)*\\1b"}}, "properties": {"x": {"$ref": "s"}}}"#,
),
("x.json", &format!("{{\"x\": \"{}!\"}}", "a".repeat(30))),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "x.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
"x.json:1:7: error[schema] this string could not be checked against the pattern \
\"^(a|a)*\\1b\": matching it takes more than 5000 backtracking steps (at $.x)\n"
);
}
#[test]
fn a_costly_match_beside_an_error_every_outcome_shares_is_left_out() {
let dir = workdir(&[
(
"schema.json",
r#"{"allOf": [{"properties": {"x": {"pattern": "^(a|a)*\\1b"}, "y": {"type": "integer"}}}]}"#,
),
(
"d.json",
&format!("{{\"x\": \"{}!\", \"y\": \"s\"}}", "a".repeat(30)),
),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "d.json"]);
assert_eq!(code, 1);
assert_eq!(
out,
"d.json:1:47: error[schema] \"s\" is not of type \"integer\" (at $.y)\n"
);
}
#[test]
fn keys_too_costly_for_a_pattern_stop_at_the_error_cap() {
let slow = format!("{}!", "a".repeat(30));
let keys: Vec<String> = (0..150).map(|i| format!("\"{slow}{i}\": {i}")).collect();
let dir = workdir(&[
(
"schema.json",
r#"{"patternProperties": {"^(a|a)*\\1b|!": false}}"#,
),
("k.json", &format!("{{{}}}", keys.join(", "))),
]);
let (code, out, _) = jsonvet_stderr(&dir, &["--schema", "schema.json", "k.json"]);
assert_eq!(code, 1);
assert_eq!(out.lines().count(), 101, "{out}");
assert!(out.ends_with("... and more schema errors in this document\n"));
}
#[test]
fn an_id_that_is_not_a_uri_is_a_usage_error() {
let dir = workdir(&[
(
"schema.json",
r#"{"$defs": {"a": {"$id": "http://[", "type": "string"}}}"#,
),
("x.json", "{}"),
]);
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "schema.json", "x.json"]);
assert_eq!((code, out.as_str()), (2, ""), "{err}");
assert!(err.contains("Invalid URI reference 'http://['"), "{err}");
}
#[test]
fn nested_unevaluated_properties_are_a_usage_error() {
let mut level = "{\"properties\": {\"a\": true}}".to_owned();
for _ in 0..22 {
level = format!(
"{{\"anyOf\": [{level}, {{\"properties\": {{\"b\": true}}}}], \"unevaluatedProperties\": false}}"
);
}
let schema = format!(
"{{\"$schema\": \"https://json-schema.org/draft/2020-12/schema\", \"allOf\": [{level}]}}"
);
let dir = workdir(&[("schema.json", &schema), ("c.json", "{\"c\": 1}")]);
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "schema.json", "c.json"]);
assert_eq!((code, out.as_str()), (2, ""));
assert!(
err.ends_with(
"this `unevaluatedProperties` takes the schema past 5000000 objects and arrays, \
counting the schemas beside it twice, as jsonschema checks them again for it\n"
),
"{err}"
);
let defs: Vec<String> = (0..40)
.map(|i| {
format!(
"\"d{i}\": {{\"properties\": {{\"a\": {{\"$ref\": \"#/$defs/d{}\"}}}}, \"unevaluatedProperties\": false}}",
i + 1
)
})
.collect();
let schema = format!(
"{{\"$defs\": {{{}, \"d40\": {{}}}}, \"$ref\": \"#/$defs/d0\"}}",
defs.join(", ")
);
let dir = workdir(&[("schema.json", &schema), ("c.json", "{\"c\": 1}")]);
let (code, _, err) = jsonvet_stderr(&dir, &["--schema", "schema.json", "c.json"]);
assert_eq!(code, 1, "{err}");
}
#[test]
fn a_ref_resolves_against_its_own_base_not_a_like_named_id_elsewhere() {
let (levels, decoys, chain) = (21, &mut Vec::new(), &mut Vec::new());
for i in 0..=levels {
decoys.push(format!(
"\"t{i}\": {{\"$id\": \"l{i}\", \"type\": \"string\"}}"
));
chain.push(if i == levels {
format!("\"c{i}\": {{\"$id\": \"l{i}\", \"type\": \"string\"}}")
} else {
let next = format!("{{\"$ref\": \"l{}\"}}", i + 1);
format!("\"c{i}\": {{\"$id\": \"l{i}\", \"anyOf\": [{next}, {next}]}}")
});
}
let schema = format!(
"{{\"$id\": \"https://x.example/root\", \"$defs\": {{\n\
\"A\": {{\"$id\": \"https://x.example/a/\", \"$defs\": {{{}}}}},\n\
\"B\": {{\"$id\": \"https://x.example/b/\", \"$defs\": {{{}}}}}}},\n\
\"items\": {{\"$ref\": \"https://x.example/b/l0\"}}}}",
decoys.join(", "),
chain.join(", ")
);
let dir = workdir(&[
("schema.json", &schema),
("one.json", "[0]"),
(
"unresolved.json",
"{\n \"$defs\": {\"a\": {\"$ref\": \"#/$defs/none\"}}\n}",
),
]);
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "schema.json", "one.json"]);
assert_eq!((code, out.as_str()), (2, ""));
assert!(
err.starts_with("jsonvet: the schema schema.json:3:"),
"{err}"
);
assert!(err.contains("takes the schema past 5000000"), "{err}");
let (code, out, err) = jsonvet_stderr(&dir, &["--schema", "unresolved.json", "one.json"]);
assert_eq!((code, out.as_str()), (2, ""));
assert_eq!(
err,
"jsonvet: the schema unresolved.json:2:19: this `$ref` cannot be resolved: Pointer \
'/$defs/none' does not exist\n"
);
}
}
mod directories {
use std::fs;
use super::{jsonvet, jsonvet_stderr, workdir};
const DUP: &str = "{\"a\": 1, \"a\": 2}";
fn dup(path: &str) -> String {
format!("{path}:1:10: error[duplicate-key] duplicate key $.a (first at 1:2)\n")
}
fn walked(path: &str) -> String {
dup(&path.replace('/', std::path::MAIN_SEPARATOR_STR))
}
#[test]
fn a_directory_is_walked_in_sorted_order_for_known_file_types_only() {
let dir = workdir(&[
("b.json", DUP),
("a/z.jsonc", DUP),
("a/y.json5", DUP),
("notes.txt", "not json"),
("README.md", "# not json"),
("Makefile", "all:"),
]);
assert_eq!(
jsonvet(&dir, &["."]),
(
1,
[
walked("./a/y.json5"),
walked("./a/z.jsonc"),
walked("./b.json")
]
.concat()
)
);
assert_eq!(
jsonvet(&dir, &["a"]),
(1, [walked("a/y.json5"), walked("a/z.jsonc")].concat())
);
}
#[test]
fn a_walk_takes_each_folders_nearest_exclude() {
let dir = workdir(&[
("jsonvet.toml", "exclude = [\"gen\"]\n"),
("gen/d.json", DUP),
("fixtures/d.json", DUP),
("packages/a/jsonvet.toml", "exclude = [\"fixtures\"]\n"),
("packages/a/fixtures/d.json", DUP),
("packages/a/gen/d.json", DUP),
]);
assert_eq!(
jsonvet(&dir, &["."]),
(
1,
[
walked("./fixtures/d.json"),
walked("./packages/a/gen/d.json")
]
.concat()
)
);
}
#[test]
fn a_walk_skips_ignored_and_hidden_files_but_a_named_file_is_checked() {
let dir = workdir(&[
(".gitignore", "build/\nskip.json\n"),
(".ignore", "also-skip.json\n"),
("build/out.json", DUP),
("skip.json", DUP),
("also-skip.json", DUP),
(".hidden/x.json", DUP),
(".babelrc", DUP),
("kept.json", DUP),
]);
assert_eq!(jsonvet(&dir, &["."]), (1, walked("./kept.json")));
assert_eq!(
jsonvet(&dir, &["skip.json", "build/out.json", ".babelrc"]),
(
1,
[dup("skip.json"), dup("build/out.json"), dup(".babelrc")].concat()
)
);
}
#[test]
fn ignore_files_apply_as_git_applies_them() {
let dir = workdir(&[
(".gitignore", "/pkg/gen/\n!.keep.json\n"),
(".git/info/exclude", "excluded.json\n"),
(".ignore", "ignored.json\n"),
(".user/git/ignore", "global.json\n"),
(".keep.json", DUP),
("global.json", DUP),
("pkg/gen/a.json", DUP),
("pkg/excluded.json", DUP),
("pkg/kept.json", DUP),
("pkg/bad.json", DUP),
("nested/.git/HEAD", ""),
("nested/excluded.json", DUP),
("nested/ignored.json", DUP),
("gitdirs/wt/commondir", "../../common\n"),
("common/info/exclude", "wt-excluded.json\n"),
("wt/wt-excluded.json", DUP),
("wt/kept.json", DUP),
("modules/sub/HEAD", ""),
("sub/kept.json", DUP),
]);
let gitdir = dir.join("gitdirs/wt");
fs::write(
dir.join("wt/.git"),
format!("gitdir: {}\n", gitdir.display()),
)
.expect("write");
fs::write(dir.join("pkg/.ignore"), b"bad.json\n\xff\nkept.json\n").expect("write");
fs::write(dir.join("sub/.git"), "gitdir: ../modules/sub\n").expect("write");
let user = dir.join(".user");
let run = |cwd: &std::path::Path, arg: &str| {
let out = super::command(env!("CARGO_BIN_EXE_jsonvet"))
.arg(arg)
.current_dir(cwd)
.env("HOME", &user)
.env("XDG_CONFIG_HOME", &user)
.output()
.expect("run jsonvet");
(
out.status.code().expect("an exit code"),
String::from_utf8(out.stdout).expect("UTF-8 output"),
)
};
assert_eq!(
run(&dir, "."),
(
1,
[
walked("./.keep.json"),
walked("./nested/excluded.json"),
walked("./pkg/kept.json"),
walked("./sub/kept.json"),
walked("./wt/kept.json"),
]
.concat()
)
);
assert_eq!(run(&dir, "pkg"), (1, walked("pkg/kept.json")));
assert_eq!(run(&dir.join("pkg"), "."), (1, walked("./kept.json")));
}
#[test]
fn exclude_applies_to_walked_files_not_named_ones() {
let dir = workdir(&[
(
"jsonvet.toml",
"exclude = [\"vendor\", \"data/*.jsonl\", \"gen-*.json\"]\n",
),
("vendor/x.json", DUP),
("sub/vendor/x.json", DUP),
("data/a.jsonl", "{\"a\": 1, \"a\": 2}\n"),
("data/a.json", DUP),
("sub/gen-1.json", DUP),
("sub/kept.json", DUP),
]);
assert_eq!(
jsonvet(&dir, &["."]),
(
1,
[walked("./data/a.json"), walked("./sub/kept.json")].concat()
)
);
assert_eq!(
jsonvet(&dir.join("sub"), &["."]),
(1, walked("./kept.json"))
);
assert_eq!(jsonvet(&dir, &["vendor/x.json"]), (1, dup("vendor/x.json")));
assert_eq!(jsonvet(&dir, &["vendor"]), (1, walked("vendor/x.json")));
}
#[test]
fn exclude_must_be_a_list_of_strings() {
let dir = workdir(&[
("a.toml", "exclude = \"vendor\"\n"),
("b.toml", "exclude = [1]\n"),
]);
let (code, out, err) = jsonvet_stderr(&dir, &["--config", "a.toml", "."]);
assert_eq!((code, out.as_str()), (2, ""));
assert_eq!(
err,
"jsonvet: a.toml:1:11: `exclude` must be an array of strings\n"
);
let (code, _, err) = jsonvet_stderr(&dir, &["--config", "b.toml", "."]);
assert_eq!(code, 2);
assert_eq!(
err,
"jsonvet: b.toml:1:12: `exclude` must be an array of strings\n"
);
}
#[test]
fn files_and_directories_mix_in_the_order_given() {
let dir = workdir(&[
("d/a.json", DUP),
("d/b.json", DUP),
("top.json", DUP),
("end.json", DUP),
]);
assert_eq!(
jsonvet(&dir, &["top.json", "d", "end.json"]),
(
1,
[
dup("top.json"),
walked("d/a.json"),
walked("d/b.json"),
dup("end.json")
]
.concat()
)
);
}
#[test]
fn an_empty_directory_checks_nothing_and_a_missing_path_is_reported() {
let dir = workdir(&[]);
fs::create_dir(dir.join("empty")).expect("create a folder");
assert_eq!(
jsonvet_stderr(&dir, &["empty"]),
(0, String::new(), String::new())
);
assert_eq!(
jsonvet(&dir, &["empty", "gone"]),
(1, "gone: error: no such file\n".to_owned())
);
}
}
mod file_reads {
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::{Duration, Instant};
use super::{command, workdir};
fn run(dir: &Path, user: &Path, args: &[&str]) -> (i32, String, String) {
run_for(dir, user, args, 10)
}
fn run_for(dir: &Path, user: &Path, args: &[&str], secs: u64) -> (i32, String, String) {
let mut child = command(env!("CARGO_BIN_EXE_jsonvet"))
.args(args)
.current_dir(dir)
.env("XDG_CONFIG_HOME", user)
.env("APPDATA", user)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("run jsonvet");
let start = Instant::now();
while child.try_wait().expect("wait").is_none() {
if start.elapsed() > Duration::from_secs(secs) {
let _ = child.kill();
panic!("jsonvet {args:?} still running after {secs} seconds");
}
std::thread::sleep(Duration::from_millis(20));
}
let out = child.wait_with_output().expect("output");
(
out.status.code().expect("an exit code"),
String::from_utf8(out.stdout).expect("UTF-8 output"),
String::from_utf8(out.stderr).expect("UTF-8 errors"),
)
}
fn bare(name: &str, files: &[(&str, &str)]) -> PathBuf {
let dir = std::env::temp_dir().join(format!("jsonvet-{name}-{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
for (file, text) in files {
let path = dir.join(file);
fs::create_dir_all(path.parent().expect("a parent")).expect("create dirs");
fs::write(path, text).expect("write file");
}
assert!(
!dir.ancestors().any(|d| d.join(".git").exists()),
"{} is in a repository",
dir.display()
);
dir
}
#[cfg(unix)]
fn fifo(path: &Path) {
let made = std::process::Command::new("mkfifo")
.arg(path)
.status()
.expect("run mkfifo");
assert!(made.success());
}
const DUP: &str = "{\"a\": 1, \"a\": 2}";
#[cfg(unix)]
const DUP_ERROR: &str = "x.json:1:10: error[duplicate-key] duplicate key $.a (first at 1:2)\n";
#[cfg(unix)]
#[test]
fn config_schema_and_ignore_files_that_are_not_regular_files_are_not_read() {
let dir = workdir(&[("a/x.json", DUP), ("b/x.json", DUP), ("c/x.json", DUP)]);
let user = dir.join("no-user");
fifo(&dir.join("a/pyproject.toml"));
assert_eq!(
run(&dir.join("a"), &user, &["x.json"]),
(1, DUP_ERROR.to_owned(), String::new())
);
fifo(&dir.join("schema.json"));
let (code, _, err) = run(&dir, &user, &["--schema", "schema.json", "a/x.json"]);
assert_eq!(
(code, err.as_str()),
(
2,
"jsonvet: cannot read the schema schema.json: not a regular file\n"
)
);
fifo(&dir.join("b/.gitignore"));
let (code, _, err) = run(&dir, &user, &["."]);
assert_eq!(
(code, err.as_str()),
(
2,
"jsonvet: cannot walk: ./b/.gitignore: not a regular file\n"
)
);
let (code, _, err) = run(&dir, &user, &["b"]);
assert_eq!(code, 2);
assert_eq!(
err,
"jsonvet: cannot walk: b/.gitignore: not a regular file\n"
);
fifo(&dir.join("c/.ignore"));
let (code, _, err) = run(&dir.join("c"), &user, &["."]);
assert_eq!(code, 2);
assert_eq!(err, "jsonvet: cannot walk: ./.ignore: not a regular file\n");
fs::write(dir.join("a/pyproject.toml.tmp"), b"\xff[tool.jsonvet]\n").expect("write");
fs::remove_file(dir.join("a/pyproject.toml")).expect("remove the FIFO");
fs::rename(
dir.join("a/pyproject.toml.tmp"),
dir.join("a/pyproject.toml"),
)
.expect("move");
assert_eq!(run(&dir.join("a"), &user, &["x.json"]).0, 1);
let big = dir.join("big");
fs::create_dir_all(&big).expect("create dir");
let sized = |name: &str, len: u64| {
fs::File::create(big.join(name))
.and_then(|file| file.set_len(len))
.expect("a large file");
};
fs::write(big.join("x.json"), "{}").expect("write");
sized(".gitignore", (16 << 20) + 1);
let (code, _, err) = run(&big, &user, &["."]);
assert_eq!(
(code, err.as_str()),
(2, "jsonvet: cannot walk: ./.gitignore: larger than 16 MB\n")
);
sized("pyproject.toml", (16 << 20) + 1);
let (code, _, err) = run(&big, &user, &["x.json"]);
assert_eq!(code, 2);
assert!(
err.ends_with("pyproject.toml: larger than 16 MB\n"),
"{err}"
);
let _ = fs::remove_dir_all(&dir);
}
#[cfg(unix)]
#[test]
fn info_exclude_files_that_are_not_regular_files_are_not_read() {
let dir = workdir(&[
("sub/x.json", DUP),
("sub/.git/info/HEAD", ""),
("wt/x.json", DUP),
("gitdirs/wt/commondir", "../../common\n"),
("common/info/HEAD", ""),
]);
let user = dir.join("no-user");
fifo(&dir.join("sub/.git/info/exclude"));
let (code, _, err) = run_for(&dir, &user, &["."], 5);
assert_eq!(
(code, err.as_str()),
(
2,
"jsonvet: cannot walk: ./sub/.git/info/exclude: not a regular file\n"
)
);
fs::remove_file(dir.join("sub/.git/info/exclude")).expect("remove the FIFO");
std::os::unix::fs::symlink("/dev/zero", dir.join("sub/.git/info/exclude")).expect("link");
let (code, _, err) = run_for(&dir, &user, &["sub"], 5);
assert_eq!(
(code, err.as_str()),
(
2,
"jsonvet: cannot walk: sub/.git/info/exclude: not a regular file\n"
)
);
fs::remove_file(dir.join("sub/.git/info/exclude")).expect("remove the link");
let gitdir = dir.join("gitdirs/wt");
fs::write(
dir.join("wt/.git"),
format!("gitdir: {}\n", gitdir.display()),
)
.expect("write");
fifo(&dir.join("common/info/exclude"));
let (code, _, err) = run_for(&dir.join("wt"), &user, &["."], 5);
assert_eq!(code, 2);
assert!(
err.ends_with("common/info/exclude: not a regular file\n"),
"{err}"
);
fs::remove_file(dir.join("common/info/exclude")).expect("remove the FIFO");
fs::remove_file(dir.join("gitdirs/wt/commondir")).expect("remove commondir");
fifo(&dir.join("gitdirs/wt/commondir"));
let (code, _, err) = run_for(&dir.join("wt"), &user, &["."], 5);
assert_eq!(code, 2);
assert!(err.ends_with("wt/commondir: not a regular file\n"), "{err}");
let _ = fs::remove_dir_all(&dir);
}
#[cfg(unix)]
#[test]
fn an_error_in_a_symlinked_config_file_shows_no_value_from_it() {
let dir = workdir(&[
("secret.toml", "[rules]\nsyntax = \"ghp_SECRETTOKEN123\"\n"),
("r/x.json", "{}"),
]);
let user = dir.join("no-user");
std::os::unix::fs::symlink("../secret.toml", dir.join("r/jsonvet.toml")).expect("link");
let (code, _, err) = run(&dir.join("r"), &user, &["x.json"]);
assert_eq!(code, 2);
assert!(
err.ends_with(
"/r/jsonvet.toml:2:10: invalid setting, not shown because the file is a symlink\n"
),
"{err}"
);
let (_, _, err) = run(&dir, &user, &["--config", "secret.toml", "r/x.json"]);
assert!(err.contains("ghp_SECRETTOKEN123"), "{err}");
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn without_git_schemas_are_confined_to_where_the_search_stops() {
let dir = bare(
"no-git-schema",
&[
("s.json", "{\"type\": \"array\"}"),
(
"jsonvet.toml",
"schemas = [{ files = \"x.json\", schema = \"s.json\" }]\n",
),
("x.json", "{}"),
("y.json", "{\"$schema\": \"s.json\"}"),
("sub/ok.json", "{}"),
],
);
let user = dir.join("no-user");
for file in ["../y.json", "../x.json"] {
let (code, out, err) = run(&dir.join("sub"), &user, &[file]);
assert_eq!(code, 1, "{file}: {out}{err}");
assert!(out.contains("error[schema]"), "{file}: {out}");
assert!(!out.contains("outside"), "{file}: {out}");
}
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn without_git_the_search_stops_at_the_working_directory() {
let off = "[rules]\nduplicate-key = \"off\"\n";
let dir = bare(
"no-git",
&[
("jsonvet.toml", off),
("job/x.json", DUP),
("job/sub/x.json", DUP),
("other/x.json", DUP),
("other/sub/x.json", DUP),
("own/jsonvet.toml", off),
("own/x.json", DUP),
],
);
let user = dir.join("no-user");
assert_eq!(run(&dir.join("job"), &user, &["x.json"]).0, 1);
assert_eq!(run(&dir.join("job"), &user, &["sub/x.json"]).0, 1);
assert_eq!(run(&dir, &user, &["job/sub/x.json"]).0, 0);
let job = dir.join("job");
assert_eq!(run(&job, &user, &["../other/x.json"]).0, 1);
assert_eq!(run(&job, &user, &["../other/sub/x.json"]).0, 1);
assert_eq!(run(&job, &user, &["../own/x.json"]).0, 0);
fs::create_dir_all(dir.join("other/.git")).expect("mark a repository");
fs::write(dir.join("other/jsonvet.toml"), off).expect("write");
assert_eq!(run(&job, &user, &["../other/sub/x.json"]).0, 0);
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn the_user_config_applies_only_without_a_project_config() {
let dir = workdir(&[
(
"user/jsonvet/jsonvet.toml",
"[rules]\nduplicate-key = \"warning\"\n\n[[dialects]]\nfiles = \"data/*.txt\"\ndialect = \"jsonl\"\n\n[[per-file-rules]]\nfiles = \"data/*.json\"\nduplicate-key = \"ignore\"\n",
),
("p/x.json", DUP),
("p/data/x.json", DUP),
("p/data/x.txt", "{}\n{}\n"),
("q/jsonvet.toml", ""),
("q/x.json", DUP),
]);
let user = dir.join("user");
let warning = "x.json:1:10: warning[duplicate-key] duplicate key $.a (first at 1:2)\n";
let p = dir.join("p");
assert_eq!(
run(&p, &user, &["x.json"]),
(0, warning.to_owned(), String::new())
);
assert_eq!(run(&p, &user, &["data/x.txt"]).0, 0);
assert_eq!(run(&p.join("data"), &user, &["x.txt"]).0, 1);
assert_eq!(
run(&p, &user, &["data/x.json"]),
(0, String::new(), String::new())
);
assert_eq!(
run(&p.join("data"), &user, &["x.json"]),
(0, warning.to_owned(), String::new())
);
let q = dir.join("q");
assert_eq!(run(&q, &user, &["x.json"]).0, 1);
assert_eq!(run(&p, &user, &["--no-config", "x.json"]).0, 1);
assert_eq!(
run(&p, &user, &["--config", "../q/jsonvet.toml", "x.json"]).0,
1
);
#[cfg(not(windows))]
assert_eq!(run(&dir, Path::new("user"), &["p/x.json"]).0, 1);
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn the_user_configs_exclude_is_relative_to_the_working_directory() {
let dir = workdir(&[
("user/jsonvet/jsonvet.toml", "exclude = [\"skip\"]\n"),
("p/x.json", DUP),
("p/skip/x.json", DUP),
("p/sub/x.json", DUP),
]);
let user = dir.join("user");
let (code, out, _) = run(&dir.join("p"), &user, &["."]);
assert_eq!(code, 1);
let walked: Vec<&str> = out
.lines()
.map(|l| l.split(':').next().unwrap_or(l))
.collect();
let sep = std::path::MAIN_SEPARATOR;
assert_eq!(
walked,
[format!(".{sep}sub{sep}x.json"), format!(".{sep}x.json")],
"{out}"
);
let _ = fs::remove_dir_all(&dir);
}
}
#[test]
fn a_panic_names_the_issue_tracker_and_exits_101() {
let out = command(env!("CARGO_BIN_EXE_jsonvet"))
.env("JSONVET_TEST_PANIC", "1")
.output()
.expect("run jsonvet");
let err = String::from_utf8(out.stderr).expect("UTF-8 errors");
assert_eq!(out.status.code(), Some(101));
assert_eq!(out.stdout, Vec::<u8>::new());
assert!(err.contains("test panic"), "{err}");
assert!(
err.contains("https://github.com/simonvanlierde/jsonvet/issues"),
"{err}"
);
}
#[test]
fn json_output_matches_its_published_schema() {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs/json-output.schema.json");
let schema: serde_json::Value =
serde_json::from_str(&fs::read_to_string(path).expect("the schema file"))
.expect("a JSON schema");
let validator = jsonschema::validator_for(&schema).expect("a valid schema");
let dir = workdir(&[
("schema.json", r#"{"type": "object"}"#),
("bad.json", "[1,"),
("dup.json", "{\n \"a\": 1,\n \"a\": 2\n}"),
("bom.json", "\u{FEFF}{}"),
("arr.json", "[]"),
]);
let full = [
"--format",
"json",
"--schema",
"schema.json",
"--dialect=json:*.json",
"bad.json",
"dup.json",
"bom.json",
"arr.json",
"gone.json",
];
for args in [&full[..], &["--format", "json"]] {
let (_, out, _) = jsonvet_stderr(&dir, args);
let json: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
let errors: Vec<String> = validator
.iter_errors(&json)
.map(|error| error.to_string())
.collect();
assert!(errors.is_empty(), "{errors:?}\n{out}");
}
let (_, out, _) = jsonvet_stdin(
&dir,
&["--format", "json", "-"],
"{\"$schema\":\"missing.json\"}",
);
let json: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
assert_eq!(json["notes"].as_array().map(Vec::len), Some(1), "{out}");
assert!(validator.is_valid(&json), "{out}");
let (_, out, _) = jsonvet_stderr(&dir, &full);
let mut json: serde_json::Value = serde_json::from_str(&out).expect("JSON output");
json["diagnostics"][0]["severity"] = "fatal".into();
assert!(!validator.is_valid(&json));
json["diagnostics"][0]["severity"] = "error".into();
json["diagnostics"][0]
.as_object_mut()
.expect("an object")
.remove("url");
assert!(!validator.is_valid(&json));
}
#[cfg(unix)]
#[test]
fn a_folder_reached_through_a_symlink_is_checked_once() {
let dir = workdir(&[("sub/b.json", "{\"b\":1,\"b\":2}\n")]);
std::os::unix::fs::symlink("sub", dir.join("alias")).expect("symlink");
let (code, out) = jsonvet(&dir, &["sub", "alias"]);
assert_eq!(code, 1, "{out}");
assert_eq!(
out.lines().filter(|l| l.contains("duplicate-key")).count(),
1,
"{out}"
);
}
#[test]
fn a_folder_inside_another_is_checked_once_whichever_comes_first() {
let dir = workdir(&[
("top/a.json", "{\"a\":1,\"a\":2}\n"),
("top/sub/b.json", "{\"b\":1,\"b\":2}\n"),
]);
for args in [["top", "top/sub"], ["top/sub", "top"]] {
let (code, out) = jsonvet(&dir, &args);
assert_eq!(code, 1, "{out}");
assert_eq!(
out.lines().filter(|l| l.contains("b.json:")).count(),
1,
"{out}"
);
}
}
fn lsp_message(body: &serde_json::Value) -> Vec<u8> {
let body = body.to_string();
format!("Content-Length: {}\r\n\r\n{body}", body.len()).into_bytes()
}
#[test]
fn lsp_checks_a_document_over_stdio_and_exits_0_after_shutdown() {
use serde_json::json;
use std::io::{BufRead, BufReader, Read, Write};
let dir = workdir(&[("jsonvet.toml", "")]);
let path = dir.join("a.json").to_string_lossy().replace('\\', "/");
let uri = format!("file:///{}", path.trim_start_matches('/'));
let mut child = command(env!("CARGO_BIN_EXE_jsonvet"))
.arg("lsp")
.current_dir(&dir)
.env("XDG_CONFIG_HOME", dir.join("no-user"))
.env("APPDATA", dir.join("no-user"))
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.expect("run jsonvet lsp");
let mut input = child.stdin.take().expect("stdin");
let text = "{\"a\": 1, \"a\": 2}";
let messages = [
json!({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"capabilities": {}}}),
json!({"jsonrpc": "2.0", "method": "initialized", "params": {}}),
json!({"jsonrpc": "2.0", "method": "textDocument/didOpen", "params": {"textDocument": {
"uri": uri, "languageId": "json", "version": 1, "text": text,
}}}),
json!({"jsonrpc": "2.0", "id": 2, "method": "shutdown"}),
json!({"jsonrpc": "2.0", "method": "exit"}),
];
for message in &messages {
input.write_all(&lsp_message(message)).expect("written");
}
let start = std::time::Instant::now();
let mut output = BufReader::new(child.stdout.take().expect("stdout"));
let (sender, receiver) = std::sync::mpsc::channel();
std::thread::spawn(move || {
loop {
let mut length = 0;
loop {
let mut line = String::new();
if output.read_line(&mut line).unwrap_or(0) == 0 {
return;
}
match line.trim_end().split_once(": ") {
Some(("Content-Length", n)) => length = n.parse().expect("a length"),
None if line.trim_end().is_empty() => break,
_ => {}
}
}
let mut body = vec![0; length];
output.read_exact(&mut body).expect("a body");
let message: serde_json::Value = serde_json::from_slice(&body).expect("JSON");
if sender.send(message).is_err() {
return;
}
}
});
let mut read = || match receiver.recv_timeout(std::time::Duration::from_secs(10)) {
Ok(message) => message,
Err(e) => {
let _ = child.kill();
panic!("no message from jsonvet lsp: {e}");
}
};
assert_eq!(read()["result"]["serverInfo"]["name"], "jsonvet");
let published = read();
assert_eq!(published["method"], "textDocument/publishDiagnostics");
let diagnostic = &published["params"]["diagnostics"][0];
assert_eq!(diagnostic["code"], "duplicate-key");
assert_eq!(read()["id"], 2);
drop(input);
assert_eq!(child.wait().expect("an exit").code(), Some(0));
no_hang(start);
}
#[test]
fn lsp_exits_after_exit_while_the_client_keeps_its_pipe_open() {
use serde_json::json;
use std::io::Write;
let dir = workdir(&[("jsonvet.toml", "")]);
let mut child = command(env!("CARGO_BIN_EXE_jsonvet"))
.arg("lsp")
.current_dir(&dir)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.spawn()
.expect("run jsonvet lsp");
let mut input = child.stdin.take().expect("stdin");
for message in [
json!({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"capabilities": {}}}),
json!({"jsonrpc": "2.0", "method": "initialized", "params": {}}),
json!({"jsonrpc": "2.0", "id": 2, "method": "shutdown"}),
json!({"jsonrpc": "2.0", "method": "exit"}),
] {
input.write_all(&lsp_message(&message)).expect("written");
}
input.flush().expect("flushed");
let start = std::time::Instant::now();
let code = loop {
if let Some(status) = child.try_wait().expect("a status") {
break status.code();
}
if start.elapsed() > std::time::Duration::from_secs(10) {
let _ = child.kill();
panic!("jsonvet lsp still running 10 s after exit");
}
std::thread::sleep(std::time::Duration::from_millis(20));
};
assert_eq!(code, Some(0));
drop(input);
}
#[test]
fn an_inline_suppression_hides_a_rule_on_the_next_line() {
let text = "{\n // jsonvet-ignore duplicate-key\n \"a\": 1, \"a\": 2,\n \"b\": 1,\n /* jsonvet-ignore duplicate-key, mixed-line-endings */\n\n // a note\n \"b\": 2,\n // jsonvet-ignore duplicate-keys\n \"c\": 1,\n // jsonvet-ignore syntax\n \"d\": 1,\n // jsonvet-ignore duplicate-key\n \"e\": 1,\n //jsonvet-ignore\n \"f\": 1\n}\n";
let dir = workdir(&[
(
"jsonvet.toml",
"per-file-rules = [{ files = \"quiet/*.jsonc\", unused-suppression = \"off\" }]\n",
),
("s.jsonc", text),
("quiet/s.jsonc", text),
(
"j.json",
"// jsonvet-ignore duplicate-key\n{\"a\": 1, \"a\": 2}\n",
),
]);
assert_eq!(
jsonvet(&dir, &["s.jsonc", "quiet/s.jsonc"]),
(
0,
"s.jsonc:5:36: warning[unused-suppression] `mixed-line-endings` is about the whole file: turn it off with `per-file-rules`\n\
s.jsonc:9:21: warning[unused-suppression] unknown rule `duplicate-keys` in a suppression; did you mean `duplicate-key`?\n\
s.jsonc:11:21: warning[unused-suppression] the `syntax` rule is always an error and cannot be suppressed\n\
s.jsonc:13:21: warning[unused-suppression] nothing to suppress: no `duplicate-key` problem on the next line\n\
s.jsonc:15:5: warning[unused-suppression] a suppression needs a rule, such as `jsonvet-ignore duplicate-key`\n"
.to_owned()
)
);
assert_eq!(
jsonvet(&dir, &["--ignore", "unused-suppression", "s.jsonc"]),
(0, String::new())
);
let (code, out) = jsonvet(&dir, &["--ignore", "duplicate-key", "s.jsonc"]);
assert_eq!(code, 0);
assert!(!out.contains("no `duplicate-key` problem"), "{out}");
assert_eq!(
jsonvet(&dir, &["--dialect", "json:j.json", "j.json"]),
(
1,
"j.json:1:1: error[syntax] comments are not allowed in json; the jsonc dialect allows this\n"
.to_owned()
)
);
}