use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use bloat_show::BloatJson;
fn binary() -> PathBuf {
let mut path = std::env::current_exe().unwrap();
path.pop();
if path.ends_with("deps") {
path.pop();
}
path.join(format!("bloat-show{}", std::env::consts::EXE_SUFFIX))
}
fn fixture() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/sample.json")
}
fn scratch(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("bloat-show-tests/{name}"));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn assert_matches_fixture(html: &str) {
let embedded = BloatJson::from_rendered_html(html).expect("report embeds its data");
let parsed = BloatJson::from_json_str(embedded).expect("embedded data is valid JSON");
let expected = BloatJson::from_json_str(&std::fs::read_to_string(fixture()).unwrap()).unwrap();
assert_eq!(parsed.functions.len(), expected.functions.len());
assert_eq!(parsed.text_section_size, expected.text_section_size);
assert_eq!(parsed.file_size, expected.file_size);
assert_eq!(parsed.functions[0].name, expected.functions[0].name);
assert!(!html.contains("__BLOAT_SHOW"), "unreplaced placeholder");
}
#[test]
fn renders_a_file_to_html() {
let out = scratch("renders_a_file").join("report.html");
let status = Command::new(binary())
.arg(fixture())
.arg("--output")
.arg(&out)
.status()
.unwrap();
assert!(status.success());
assert_matches_fixture(&std::fs::read_to_string(&out).unwrap());
}
#[test]
fn creates_missing_parent_directories() {
let out = scratch("creates_dirs").join("nested/deeper/report.html");
let status = Command::new(binary())
.arg(fixture())
.arg("-o")
.arg(&out)
.status()
.unwrap();
assert!(status.success());
assert!(out.is_file());
}
#[test]
fn reads_json_from_stdin() {
let out = scratch("stdin").join("report.html");
let mut child = Command::new(binary())
.arg("-")
.arg("-o")
.arg(&out)
.stdin(Stdio::piped())
.spawn()
.unwrap();
let json = std::fs::read(fixture()).unwrap();
child.stdin.take().unwrap().write_all(&json).unwrap();
assert!(child.wait().unwrap().success());
assert_matches_fixture(&std::fs::read_to_string(&out).unwrap());
}
#[test]
fn crates_mode_json_is_rejected_with_guidance() {
let input = scratch("crates_mode").join("crates.json");
std::fs::write(
&input,
r#"{"file-size":1,"text-section-size":1,"crates":[{"name":"wasmi","size":1}]}"#,
)
.unwrap();
let output = Command::new(binary())
.arg(&input)
.arg("-o")
.arg(input.with_extension("html"))
.output()
.unwrap();
assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("drop `--crates`"), "{stderr}");
}
#[test]
fn a_missing_input_file_fails_cleanly() {
let output = Command::new(binary())
.arg("definitely-not-here.json")
.arg("-o")
.arg(scratch("missing_input").join("report.html"))
.output()
.unwrap();
assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("definitely-not-here.json"), "{stderr}");
assert!(!stderr.contains("panicked"), "{stderr}");
}
#[test]
fn colour_follows_the_flag_and_the_pipe() {
let out = scratch("colour").join("report.html");
let run = |args: &[&str]| {
let mut command = Command::new(binary());
for var in ["NO_COLOR", "CLICOLOR", "CLICOLOR_FORCE"] {
command.env_remove(var);
}
let output = command
.arg(fixture())
.arg("-o")
.arg(&out)
.args(args)
.output()
.unwrap();
assert!(output.status.success());
String::from_utf8(output.stderr).unwrap()
};
let plain = run(&[]);
assert!(plain.starts_with(" Analyzed "), "{plain:?}");
assert!(!plain.contains('\u{1b}'), "escapes on a pipe: {plain:?}");
let coloured = run(&["--color=always"]);
assert!(coloured.contains('\u{1b}'), "{coloured:?}");
assert!(coloured.contains("Analyzed"), "{coloured:?}");
assert!(!run(&["--color", "never"]).contains('\u{1b}'));
}
#[test]
fn errors_are_reported_in_cargo_shape() {
let output = Command::new(binary())
.arg("definitely-not-here.json")
.arg("--color=always")
.output()
.unwrap();
assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("error\u{1b}[0m: "), "{stderr}");
assert!(stderr.contains("definitely-not-here.json"), "{stderr}");
}
#[test]
fn every_function_gets_its_own_rectangle() {
let out = scratch("no_grouping").join("report.html");
let status = Command::new(binary())
.arg(fixture())
.arg("-o")
.arg(&out)
.status()
.unwrap();
assert!(status.success());
let html = std::fs::read_to_string(&out).unwrap();
for gone in [
"smaller items",
"tm-remainder",
"applyRemainders",
"deepCloneTree",
] {
assert!(!html.contains(gone), "grouping came back: {gone}");
}
}