Skip to content

Commit

Permalink
chore: fix race condition on test project initialization (#7415)
Browse files Browse the repository at this point in the history
* chore: fix race condition on test initialization

* fix for windows

* fmt
  • Loading branch information
klkvr authored Mar 17, 2024
1 parent 63066ab commit 42a9d34
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions crates/test-utils/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use regex::Regex;
use std::{
env,
ffi::OsStr,
fs,
fs::File,
io::{BufWriter, IsTerminal, Write},
fs::{self, File},
io::{BufWriter, IsTerminal, Read, Seek, Write},
path::{Path, PathBuf},
process::{ChildStdin, Command, Output, Stdio},
sync::{
Expand Down Expand Up @@ -237,21 +236,30 @@ pub fn initialize(target: &Path) {

// Release the read lock and acquire a write lock, initializing the lock file.
_read = None;

let mut write = lock.write().unwrap();
write.write_all(b"1").unwrap();

// Initialize and build.
let (prj, mut cmd) = setup_forge("template", foundry_compilers::PathStyle::Dapptools);
eprintln!("- initializing template dir in {}", prj.root().display());
let mut data = String::new();
write.read_to_string(&mut data).unwrap();

if data != "1" {
write.set_len(0).unwrap();
write.seek(std::io::SeekFrom::Start(0)).unwrap();
write.write_all(b"1").unwrap();

cmd.args(["init", "--force"]).assert_success();
cmd.forge_fuse().args(["build", "--use", SOLC_VERSION]).assert_success();
// Initialize and build.
let (prj, mut cmd) = setup_forge("template", foundry_compilers::PathStyle::Dapptools);
eprintln!("- initializing template dir in {}", prj.root().display());

// Remove the existing template, if any.
let _ = fs::remove_dir_all(tpath);
cmd.args(["init", "--force"]).assert_success();
cmd.forge_fuse().args(["build", "--use", SOLC_VERSION]).assert_success();

// Copy the template to the global template path.
pretty_err(tpath, copy_dir(prj.root(), tpath));
// Remove the existing template, if any.
let _ = fs::remove_dir_all(tpath);

// Copy the template to the global template path.
pretty_err(tpath, copy_dir(prj.root(), tpath));
}

// Release the write lock and acquire a new read lock.
drop(write);
Expand Down

0 comments on commit 42a9d34

Please sign in to comment.