Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: manually add install commands #7090

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions crates/forge/tests/cli/ext_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ fn solmate() {
#[test]
#[cfg_attr(windows, ignore = "Windows cannot find installed programs")]
fn prb_math() {
ExtTester::new("PaulRBerg", "prb-math", "5b6279a0cf7c1b1b6a5cc96082811f7ef620cf60").run();
ExtTester::new("PaulRBerg", "prb-math", "5b6279a0cf7c1b1b6a5cc96082811f7ef620cf60")
.install_command(&["bun", "install", "--prefer-offline"])
.run();
}

#[test]
#[cfg_attr(windows, ignore = "Windows cannot find installed programs")]
fn prb_proxy() {
ExtTester::new("PaulRBerg", "prb-proxy", "fa13cf09fbf544a2d575b45884b8e94a79a02c06").run();
ExtTester::new("PaulRBerg", "prb-proxy", "fa13cf09fbf544a2d575b45884b8e94a79a02c06")
.install_command(&["bun", "install", "--prefer-offline"])
.run();
}

#[test]
Expand All @@ -25,6 +29,7 @@ fn sablier_v2() {
.args(["--nmc", "Fork"])
// run tests without optimizations
.env("FOUNDRY_PROFILE", "lite")
.install_command(&["bun", "install", "--prefer-offline"])
.run();
}

Expand All @@ -49,7 +54,9 @@ fn stringutils() {

#[test]
fn lootloose() {
ExtTester::new("gakonst", "lootloose", "7b639efe97836155a6a6fc626bf1018d4f8b2495").run();
ExtTester::new("gakonst", "lootloose", "7b639efe97836155a6a6fc626bf1018d4f8b2495")
.install_command(&["make", "install"])
.run();
}

#[test]
Expand Down Expand Up @@ -93,7 +100,7 @@ fn gunilev() {
}

#[test]
fn convex() {
fn convex_shutdown_simulation() {
ExtTester::new(
"mds1",
"convex-shutdown-simulation",
Expand Down
101 changes: 43 additions & 58 deletions crates/test-utils/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct ExtTester {
pub fork_block: Option<u64>,
pub args: Vec<String>,
pub envs: Vec<(String, String)>,
pub install_command: Vec<String>,
}

impl ExtTester {
Expand All @@ -73,6 +74,7 @@ impl ExtTester {
fork_block: None,
args: vec![],
envs: vec![],
install_command: vec![],
}
}

Expand Down Expand Up @@ -121,6 +123,15 @@ impl ExtTester {
self
}

/// Adds a command to run after the project is cloned.
///
/// Note that the command is run in the project's root directory, and it won't fail the test if
/// it fails.
pub fn install_command(mut self, command: &[&str]) -> Self {
self.install_command.extend(command.iter().map(|s| s.to_string()));
self
}

/// Runs the test.
pub fn run(&self) {
// Skip fork tests if the RPC url is not set.
Expand All @@ -129,7 +140,7 @@ impl ExtTester {
return;
}

let (prj, mut cmd) = setup_forge(self.name, self.style.clone());
let (prj, mut test_cmd) = setup_forge(self.name, self.style.clone());

// Wipe the default structure.
prj.wipe();
Expand All @@ -141,42 +152,55 @@ impl ExtTester {

// Checkout the revision.
if self.rev.is_empty() {
let mut cmd = Command::new("git");
cmd.current_dir(root).args(["log", "-n", "1"]);
eprintln!("$ {cmd:?}");
let output = cmd.output().unwrap();
let mut git = Command::new("git");
git.current_dir(root).args(["log", "-n", "1"]);
eprintln!("$ {git:?}");
let output = git.output().unwrap();
if !output.status.success() {
panic!("git log failed: {output:?}");
}
let stdout = String::from_utf8(output.stdout).unwrap();
let commit = stdout.lines().next().unwrap().split_whitespace().nth(1).unwrap();
panic!("pin to latest commit: {commit}");
} else {
let mut cmd = Command::new("git");
cmd.current_dir(root).args(["checkout", self.rev]);
eprintln!("$ {cmd:?}");
let status = cmd.status().unwrap();
let mut git = Command::new("git");
git.current_dir(root).args(["checkout", self.rev]);
eprintln!("$ {git:?}");
let status = git.status().unwrap();
if !status.success() {
panic!("git checkout failed: {status}");
}
}

// Run common installation commands.
run_install_commands(prj.root());
// Run installation command.
if !self.install_command.is_empty() {
let mut install_cmd = Command::new(&self.install_command[0]);
install_cmd.args(&self.install_command[1..]).current_dir(root);
eprintln!("cd {root}; {install_cmd:?}");
match install_cmd.status() {
Ok(s) => {
eprintln!("\n\n{install_cmd:?}: {s}");
}
Err(e) => {
eprintln!("\n\n{install_cmd:?}: {e}");
}
}
}

// Run the tests.
cmd.arg("test");
cmd.args(&self.args);
cmd.args(["--fuzz-runs=256", "--ffi", "-vvvvv"]);
test_cmd.arg("test");
test_cmd.args(&self.args);
test_cmd.args(["--fuzz-runs=256", "--ffi", "-vvvvv"]);

cmd.envs(self.envs.iter().map(|(k, v)| (k, v)));
cmd.env("FOUNDRY_FUZZ_RUNS", "1");
test_cmd.envs(self.envs.iter().map(|(k, v)| (k, v)));
test_cmd.env("FOUNDRY_FUZZ_RUNS", "1");
if let Some(fork_block) = self.fork_block {
cmd.env("FOUNDRY_ETH_RPC_URL", foundry_common::rpc::next_http_archive_rpc_endpoint());
cmd.env("FOUNDRY_FORK_BLOCK_NUMBER", fork_block.to_string());
test_cmd
.env("FOUNDRY_ETH_RPC_URL", foundry_common::rpc::next_http_archive_rpc_endpoint());
test_cmd.env("FOUNDRY_FORK_BLOCK_NUMBER", fork_block.to_string());
}

cmd.assert_non_empty_stdout();
test_cmd.assert_non_empty_stdout();
}
}

Expand Down Expand Up @@ -252,45 +276,6 @@ pub fn clone_remote(repo_url: &str, target_dir: &str) {
eprintln!();
}

/// Runs common installation commands, such as `make` and `npm`. Continues if any command fails.
pub fn run_install_commands(root: &Path) {
let root_files =
std::fs::read_dir(root).unwrap().flatten().map(|x| x.path()).collect::<Vec<_>>();
let contains = |path: &str| root_files.iter().any(|p| p.to_str().unwrap().contains(path));
let run = |args: &[&str]| {
let mut cmd = Command::new(args[0]);
cmd.args(&args[1..]).current_dir(root);
eprintln!("cd {}; {cmd:?}", root.display());
match cmd.status() {
Ok(s) => {
eprintln!("\n\n{cmd:?}: {s}");
s.success()
}
Err(e) => {
eprintln!("\n\n{cmd:?}: {e}");
false
}
}
};
let maybe_run = |path: &str, args: &[&str]| {
if contains(path) {
run(args)
} else {
false
}
};

maybe_run("Makefile", &["make", "install"]);

// Only run one of these for `node_modules`.
let mut nm = false;
nm = nm || maybe_run("bun.lockb", &["bun", "install", "--prefer-offline"]);
nm = nm || maybe_run("pnpm-lock.yaml", &["pnpm", "install", "--prefer-offline"]);
nm = nm || maybe_run("yarn.lock", &["yarn", "install", "--prefer-offline"]);
nm = nm || maybe_run("package.json", &["npm", "install", "--prefer-offline"]);
let _ = nm;
}

/// Setup an empty test project and return a command pointing to the forge
/// executable whose CWD is set to the project's root.
///
Expand Down
Loading