use crate::common::*;
use tempfile::TempDir;
fn project() -> TempDir {
let temp_dir = setup_test_project();
write_file(temp_dir.path(), "tera.templates/hello.txt.tera", "hello");
let build = run_rsconstruct(temp_dir.path(), &["build"]);
assert!(
build.status.success(),
"{}",
String::from_utf8_lossy(&build.stderr)
);
assert!(temp_dir.path().join("hello.txt").exists());
temp_dir
}
#[test]
fn clean_products_forces_a_rebuild() {
let dir = project();
let out = run_rsconstruct(dir.path(), &["clean", "products", "-p", "generator.tera"]);
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("1 file(s) and 1 cache entry removed"),
"{stdout}"
);
assert!(!dir.path().join("hello.txt").exists());
assert!(
dir.path().join(".rsconstruct").exists(),
"the rest of the cache stays"
);
let result = run_rsconstruct_json(dir.path(), &["build"]);
assert!(result.exit_success);
assert_eq!(result.success, 1, "the product must be built again");
assert_eq!(result.restored, 0, "nothing to restore from: {result:?}");
}
#[test]
fn clean_outputs_keeps_the_cache() {
let dir = project();
let out = run_rsconstruct(dir.path(), &["clean", "outputs", "-p", "@generator"]);
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
assert!(!dir.path().join("hello.txt").exists());
let result = run_rsconstruct_json(dir.path(), &["build"]);
assert_eq!(result.restored, 1, "{result:?}");
assert_eq!(result.success, 0, "{result:?}");
}
#[test]
fn clean_products_dry_run_removes_nothing() {
let dir = project();
let out = run_rsconstruct(
dir.path(),
&["clean", "products", "-p", "@generator", "--dry-run"],
);
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("Would remove 1 output file(s), 0 output dir(s) and 1 cache entry"),
"{stdout}"
);
assert!(dir.path().join("hello.txt").exists());
let result = run_rsconstruct_json(dir.path(), &["build"]);
assert_eq!(result.skipped, 1, "still cached and present: {result:?}");
}
#[test]
fn clean_products_requires_known_processors() {
let dir = project();
let out = run_rsconstruct(dir.path(), &["clean", "products"]);
assert!(!out.status.success());
assert!(
String::from_utf8_lossy(&out.stderr).contains("--processor"),
"{}",
String::from_utf8_lossy(&out.stderr)
);
let out = run_rsconstruct(dir.path(), &["clean", "products", "-p", "nope"]);
assert_eq!(out.status.code(), Some(2));
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("Unknown processor(s)") && stderr.contains("processor.generator.tera"),
"{stderr}"
);
assert!(dir.path().join("hello.txt").exists(), "nothing was removed");
}
#[test]
fn build_accepts_prefixless_processor_names() {
let dir = project();
let result = run_rsconstruct_json(dir.path(), &["build", "-p", "generator.tera"]);
assert!(result.exit_success, "{result:?}");
assert_eq!(result.total_products, 1, "{result:?}");
}