#![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;
use std::sync::{Mutex, MutexGuard};
use jsonvet_cli::Terminal;
use peak_alloc::PeakAlloc;
#[global_allocator]
static ALLOCATOR: PeakAlloc = PeakAlloc;
const FILE_BYTES: usize = 8 << 20;
const MAX_BYTES_PER_BYTE: usize = 8;
static COUNTING: Mutex<()> = Mutex::new(());
fn counting() -> MutexGuard<'static, ()> {
COUNTING
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn rows(bytes: usize) -> String {
let mut text = String::from("[\n");
let mut i = 0;
while text.len() < bytes {
if i > 0 {
text.push_str(",\n");
}
if i % 10 == 9 {
text.push_str("// row\n");
}
write!(text, "{{\"x\":{i},\"y\":[{i},\"s\"],\"t\":\"text\"}}").expect("write to a string");
i += 1;
}
text.push_str("\n]\n");
text
}
fn run(dir: &Path, file: &Path, size: usize) -> (usize, Result<u8, String>, String) {
let args = [
OsString::from("fmt"),
OsString::from("--no-config"),
OsString::from("--check"),
file.as_os_str().to_owned(),
];
let _ = dir;
let before = ALLOCATOR.current_usage();
ALLOCATOR.reset_peak_usage();
let (code, out) = std::thread::Builder::new()
.stack_size(64 << 20)
.spawn(move || {
let mut out = Vec::new();
let code = jsonvet_cli::run_with(args.to_vec(), false, Terminal::default(), &mut out);
(code, out)
})
.expect("a thread")
.join()
.expect("no panic");
let peak = ALLOCATOR.peak_usage() - before;
eprintln!(
"peak heap: {peak} bytes, {} per byte of the file",
peak / size
);
(peak, code, String::from_utf8_lossy(&out).into_owned())
}
fn folder(name: &str) -> std::path::PathBuf {
let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("format-memory-{name}"));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(dir.join(".git")).expect("create the directory");
dir
}
#[test]
fn format_memory_stays_under_its_bound_on_an_8_mb_file() {
let _counting = counting();
let dir = folder("rows");
let text = rows(FILE_BYTES);
let size = text.len();
let file = dir.join("rows.jsonc");
fs::write(&file, &text).expect("write the file");
drop(text);
let (peak, code, out) = run(&dir, &file, size);
assert_eq!(code, Ok(1), "{out}");
assert!(
peak <= MAX_BYTES_PER_BYTE * size,
"peak heap {peak} bytes is over {MAX_BYTES_PER_BYTE} per byte of the {size}-byte file"
);
}
#[test]
fn a_file_whose_output_would_be_huge_is_refused_without_building_it() {
let _counting = counting();
let dir = folder("deep");
let depth = 9_000;
let text = format!("{}1{}", "[".repeat(depth), "]".repeat(depth));
let file = dir.join("deep.json");
fs::write(&file, &text).expect("write the file");
let (peak, code, _) = run(&dir, &file, text.len());
assert_eq!(code, Ok(1));
assert!(peak < (64 << 20) / 2, "built {peak} bytes before refusing");
assert_eq!(fs::read_to_string(&file).expect("read"), text);
}