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_all_of_a_processor_forces_a_rebuild() {
let dir = project();
let out = run_rsconstruct(dir.path(), &["clean", "all", "-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_all_dry_run_removes_nothing() {
let dir = project();
let out = run_rsconstruct(
dir.path(),
&["clean", "all", "-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_all_rejects_unknown_processors() {
let dir = project();
let out = run_rsconstruct(dir.path(), &["clean", "all", "-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:?}");
}
#[test]
fn clean_all_without_processors_removes_the_directories() {
let dir = project();
let out = run_rsconstruct(dir.path(), &["clean", "all", "--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 .rsconstruct/"), "{stdout}");
assert!(dir.path().join(".rsconstruct").exists());
let out = run_rsconstruct(dir.path(), &["clean", "all"]);
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
assert!(!dir.path().join(".rsconstruct").exists());
}
fn git(dir: &std::path::Path, args: &[&str]) {
let out = std::process::Command::new("git")
.current_dir(dir)
.args(args)
.output()
.expect("git invocation failed");
assert!(
out.status.success(),
"git {args:?} failed: {}",
String::from_utf8_lossy(&out.stderr)
);
}
fn git_init(dir: &std::path::Path) {
git(dir, &["init", "-q", "-b", "main"]);
git(dir, &["config", "user.email", "test@example.com"]);
git(dir, &["config", "user.name", "Test"]);
}
#[test]
fn clean_unknown_keeps_tracked_files_git_would_quote_and_nested_repositories() {
let dir = project();
let root = dir.path();
write_file(root, "café.txt", "tracked");
write_file(root, "sub/inner.txt", "owned by the nested repository");
git_init(&root.join("sub"));
git(&root.join("sub"), &["add", "-A"]);
git(&root.join("sub"), &["commit", "-q", "-m", "inner"]);
git_init(root);
git(root, &["config", "core.quotePath", "true"]);
git(
root,
&[
"add",
"rsconstruct.toml",
"tera.templates",
"café.txt",
"sub",
],
);
git(root, &["commit", "-q", "-m", "init"]);
write_file(root, "junk.txt", "untracked");
let out = run_rsconstruct(root, &["clean", "unknown"]);
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
assert!(!root.join("junk.txt").exists(), "untracked file must go");
assert!(root.join("café.txt").exists(), "tracked file must stay");
assert!(
root.join("sub/inner.txt").exists(),
"a nested repository's files must stay"
);
assert!(
root.join("sub/.git").exists(),
"a nested repository's .git must stay"
);
}