#![allow(
clippy::expect_used,
reason = "a broken test setup should fail the test loudly"
)]
use std::ffi::OsString;
use std::fmt::Write;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard};
use jsonvet_cli::Terminal;
use peak_alloc::PeakAlloc;
#[global_allocator]
static ALLOCATOR: PeakAlloc = PeakAlloc;
const FILE_BYTES: usize = 10 << 20;
const MAX_BYTES_PER_BYTE: usize = 24;
const MAX_JSON_LINES_BYTES_PER_BYTE: usize = 4;
static COUNTING: Mutex<()> = Mutex::new(());
fn counting() -> MutexGuard<'static, ()> {
COUNTING
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn folder(name: &str) -> PathBuf {
let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("schema-memory-{name}"));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(dir.join(".git")).expect("create the directory");
dir
}
fn rows(bytes: usize, open: &str, between: &str, close: &str) -> String {
let mut text = String::from(open);
let mut i = 0;
while text.len() < bytes {
if i > 0 {
text.push_str(between);
}
write!(text, "{{\"x\": {i}, \"y\": [{i}, \"s\"], \"t\": \"text\"}}")
.expect("write to a string");
i += 1;
}
text.push_str(close);
text
}
fn peak(name: &str, schema: &str, lines: bool) -> (usize, Result<u8, String>, String) {
let _counting = counting();
let dir = folder(name);
let text = if lines {
rows(FILE_BYTES, "", "\n", "\n")
} else {
rows(FILE_BYTES, "[", ", ", "]")
};
let file = dir.join(if lines { "file.jsonl" } else { "file.json" });
fs::write(&file, &text).expect("write the file");
drop(text);
fs::write(dir.join("schema.json"), schema).expect("write the schema");
run(&[
OsString::from("--no-config"),
OsString::from("--schema"),
OsString::from(dir.join("schema.json")),
OsString::from(file),
])
}
fn run(args: &[OsString]) -> (usize, Result<u8, String>, String) {
let before = ALLOCATOR.current_usage();
ALLOCATOR.reset_peak_usage();
let mut out = Vec::new();
let code = jsonvet_cli::run_with(args.to_vec(), false, Terminal::default(), &mut out);
let peak = ALLOCATOR.peak_usage() - before;
eprintln!(
"peak heap: {peak} bytes, {} per byte of the file",
peak / FILE_BYTES
);
(peak, code, String::from_utf8_lossy(&out).into_owned())
}
fn assert_under(peak: usize, per_byte: usize) {
assert!(
peak <= per_byte * FILE_BYTES,
"peak heap {peak} bytes is over {per_byte} per byte of the {FILE_BYTES}-byte file"
);
}
#[test]
fn schema_memory_stays_under_its_bound_on_a_valid_10_mb_file() {
let schema =
r#"{"type": "array", "minItems": 1, "items": {"type": "object", "required": ["x"]}}"#;
let (peak, code, out) = peak("valid", schema, false);
assert_eq!(code, Ok(0), "{out}");
assert_under(peak, MAX_BYTES_PER_BYTE);
}
#[test]
fn schema_memory_stays_under_its_bound_placing_an_error_in_a_10_mb_file() {
let schema = r#"{"minItems": 1, "items": {"properties": {"x": {"not": {"const": 7}}}}}"#;
let (peak, code, out) = peak("error", schema, false);
assert_eq!(code, Ok(1), "{out}");
assert!(out.contains("(at $[7].x)"), "{out}");
assert_under(peak, MAX_BYTES_PER_BYTE);
}
#[test]
fn schema_memory_stays_under_its_bound_on_a_split_10_mb_json_lines_file() {
let schema = r#"{"type": "object", "properties": {"x": {"not": {"const": 7}}}}"#;
let (peak, code, out) = peak("lines", schema, true);
assert_eq!(code, Ok(1), "{out}");
assert!(out.contains(":8:7: error[schema]"), "{out}");
assert_under(peak, MAX_JSON_LINES_BYTES_PER_BYTE);
}
const ROWS: &str = r#"{"properties": {"rows": {"items": {"type": "object", "required": ["x"]}}}}"#;
fn own_schema_files(
name: &str,
own: &str,
count: usize,
bytes: usize,
) -> (Vec<OsString>, Vec<OsString>) {
let dir = folder(name);
fs::write(dir.join("s.json"), ROWS).expect("write the schema");
let text = rows(
bytes,
&format!("{{\"$schema\": \"{own}\", \"rows\": ["),
", ",
"]}",
);
let files: Vec<OsString> = (0..count)
.map(|i| {
let file = dir.join(format!("f{i}.json"));
fs::write(&file, &text).expect("write a file");
file.into_os_string()
})
.collect();
let own = ["--no-config".into()]
.into_iter()
.chain(files.clone())
.collect();
let given = [
"--no-config".into(),
"--schema".into(),
dir.join("s.json").into_os_string(),
]
.into_iter()
.chain(files)
.collect();
(own, given)
}
#[test]
fn a_local_dollar_schema_takes_what_schema_or_a_plain_check_does() {
let _counting = counting();
let (own, given) = own_schema_files("own", "s.json", 1, FILE_BYTES);
let (with_schema, code, out) = run(&given);
assert_eq!(code, Ok(0), "{out}");
let (peak, code, out) = run(&own);
assert_eq!(code, Ok(0), "{out}");
assert!(
peak <= with_schema + with_schema / 4,
"a local `$schema` took {peak} bytes, `--schema` {with_schema}"
);
let (missing, _) = own_schema_files("own-missing", "node_modules/s.json", 1, FILE_BYTES);
let (peak, code, out) = run(&missing);
assert_eq!(code, Ok(0), "{out}");
assert_under(peak, MAX_JSON_LINES_BYTES_PER_BYTE);
}
#[test]
fn files_with_a_local_dollar_schema_are_checked_as_few_at_a_time_as_with_schema() {
let _counting = counting();
let (count, bytes) = (12, 2 << 20);
let (own, given) = own_schema_files("own-many", "s.json", count, bytes);
let (with_schema, code, out) = run(&given);
assert_eq!(code, Ok(0), "{out}");
let (peak, code, out) = run(&own);
assert_eq!(code, Ok(0), "{out}");
assert!(
peak <= with_schema + count * bytes,
"local `$schema`s took {peak} bytes, `--schema` {with_schema}"
);
}
#[test]
fn a_regex_with_lookaround_compiles_the_schema_once() {
let _counting = counting();
let schema = |first: &str| {
let regexes: Vec<String> = (0..100)
.map(|i| format!("\"{first}(?:[a-z]{{20}}){{100}}N{i}\": {{}}"))
.collect();
format!("{{\"patternProperties\": {{{}}}}}", regexes.join(", "))
};
let dir = folder("lookaround");
fs::write(dir.join("file.json"), "{}").expect("write the file");
let mut peaks = Vec::new();
for (name, first) in [("linear.json", ""), ("fancy.json", "(?=a)")] {
fs::write(dir.join(name), schema(first)).expect("write the schema");
let (peak, code, out) = run(&[
OsString::from("--no-config"),
OsString::from("--schema"),
OsString::from(dir.join(name)),
OsString::from(dir.join("file.json")),
]);
assert_eq!(code, Ok(0), "{out}");
peaks.push(peak);
}
let (linear, fancy) = (peaks[0], peaks[1]);
assert!(
fancy <= linear + linear / 5,
"with lookaround {fancy} bytes, without {linear}"
);
}