use crate::common::{make_executable, run_rsconstruct};
use std::fs;
use std::path::Path;
use tempfile::TempDir;
const SITE_PACKAGES: &str = ".venv/lib/python3.14/site-packages";
fn venv_project(processor_config: &str) -> TempDir {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let root = temp_dir.path();
fs::create_dir_all(root.join(SITE_PACKAGES)).unwrap();
fs::create_dir_all(root.join(SITE_PACKAGES).join("pyyaml-6.0.2.dist-info")).unwrap();
fs::write(
root.join(".venv/pyvenv.cfg"),
"home = /usr/bin\nimplementation = CPython\nversion_info = 3.14.0\n",
)
.unwrap();
fs::create_dir_all(root.join(".venv/bin")).unwrap();
fs::write(root.join(".venv/bin/python"), "#!/bin/sh\nexit 0\n").unwrap();
make_executable(&root.join(".venv/bin/python"));
fs::create_dir_all(root.join("tools")).unwrap();
fs::write(root.join("tools/plain.sh"), "#!/bin/sh\nexit 0\n").unwrap();
make_executable(&root.join("tools/plain.sh"));
fs::create_dir_all(root.join("src")).unwrap();
fs::write(root.join("src/a.txt"), "content\n").unwrap();
fs::write(
root.join("rsconstruct.toml"),
format!(
"[processor.checker.script]\nsrc_dirs = [\"src\"]\nsrc_extensions = [\".txt\"]\n{processor_config}"
),
)
.unwrap();
temp_dir
}
fn upgrade_package(root: &Path) {
let sp = root.join(SITE_PACKAGES);
fs::remove_dir(sp.join("pyyaml-6.0.2.dist-info")).unwrap();
fs::create_dir(sp.join("pyyaml-6.0.3.dist-info")).unwrap();
}
fn reruns_after_upgrade(root: &Path) -> bool {
for round in ["first", "second"] {
let out = run_rsconstruct(root, &["build", "-v"]);
assert!(
out.status.success(),
"{round} build failed: {}",
String::from_utf8_lossy(&out.stderr)
);
if round == "second" {
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
!stdout.contains("Processing:"),
"nothing changed, so nothing may re-run: {stdout}"
);
}
}
upgrade_package(root);
let out = run_rsconstruct(root, &["build", "-v"]);
assert!(
out.status.success(),
"build after the upgrade failed: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).contains("Processing:")
}
#[test]
fn venv_python_command_reruns_after_package_upgrade() {
let temp_dir = venv_project("command = \"python\"\n");
assert!(
reruns_after_upgrade(temp_dir.path()),
"a package upgrade must re-run a processor that runs the venv's python"
);
}
#[test]
fn non_python_command_is_not_keyed_by_the_venv() {
let temp_dir = venv_project("command = \"tools/plain.sh\"\n");
assert!(
!reruns_after_upgrade(temp_dir.path()),
"a package upgrade must not re-run a processor whose tool is not Python"
);
}
#[test]
fn explicit_true_keys_a_non_python_command() {
let temp_dir = venv_project("command = \"tools/plain.sh\"\nhash_python_env = true\n");
assert!(
reruns_after_upgrade(temp_dir.path()),
"hash_python_env = true must key the processor by the venv"
);
}
#[test]
fn explicit_false_overrides_detection() {
let temp_dir = venv_project("command = \"python\"\nhash_python_env = false\n");
assert!(
!reruns_after_upgrade(temp_dir.path()),
"hash_python_env = false must keep the venv out of the key"
);
}
#[test]
fn build_switch_disables_python_env_keys() {
let temp_dir = venv_project("command = \"python\"\nhash_python_env = true\n");
let root = temp_dir.path();
let config = fs::read_to_string(root.join("rsconstruct.toml")).unwrap();
fs::write(
root.join("rsconstruct.toml"),
format!("[build]\nhash_python_env = false\n\n{config}"),
)
.unwrap();
assert!(
!reruns_after_upgrade(root),
"[build] hash_python_env = false must keep the venv out of every key"
);
}
#[test]
fn python_checkers_default_to_hash_python_env() {
for pname in [
"processor.checker.ruff",
"processor.checker.pyrefly",
"processor.checker.pytest",
"processor.mass_generator.sphinx",
] {
let defconfig = crate::common::processor_defconfig(pname);
assert_eq!(
defconfig.get("hash_python_env"),
Some(&serde_json::Value::Bool(true)),
"{pname} must default hash_python_env to true: {defconfig}"
);
}
let defconfig = crate::common::processor_defconfig("processor.checker.shellcheck");
assert_eq!(
defconfig.get("hash_python_env"),
None,
"a non-Python checker leaves hash_python_env unset (detected): {defconfig}"
);
}