Skip to content

Commit

Permalink
Clean butane/.butane only during cargo test (Electron100#215)
Browse files Browse the repository at this point in the history
Fixes Electron100#56

Also only re-run build.rs if the tests/ change.
  • Loading branch information
jayvdb authored Mar 30, 2024
1 parent 80a1157 commit 2ca6075
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions butane/build.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
fn main() {
// This build.rs is only needed to help the tests
if std::env::var("CARGO_CFG_TEST").is_err() {
return;
}
println!("cargo:rerun-if-changed=tests");

// This cleans the .butane/ directory which is generated when compiling the tests so that
// the tests do not encounter side effects from previous test runs.
// Currently the only way to remove stale items from the generated .butane/ directory is to
// delete it before the code is compiled.
// This means we can not rely on `butane clean` or the code behind it, because it hasnt
// been compiled yet.
let dir = ".butane/";
println!("cargo:rerun-if-changed={dir}");
if std::path::Path::new(&dir).is_dir() {
match std::fs::remove_dir_all(dir) {
Ok(_) => {
// Re-create the directory. Only tests populate it and if it is left non-existent
// Cargo will detect it as changed and a no-op build will not in fact no-op
std::fs::create_dir(dir).unwrap();
}
Err(_) => eprintln!("Cannot delete .butane dir"),
println!("cargo:warning=Deleting .butane directory");
if std::fs::remove_dir_all(dir).is_err() {
println!("cargo:warning=Cannot delete .butane directory");
}
}
}

0 comments on commit 2ca6075

Please sign in to comment.