Skip to content

Commit

Permalink
test(commands): add integration test (install/uninstall) (#190)
Browse files Browse the repository at this point in the history
* test(commands): add integration tests for uninstall

* test(commands): add integration test for config in foundry file

* test(commands): add test for remappings in foundry file

* test(commands): add uninstall test with foundry config
  • Loading branch information
beeb authored Sep 20, 2024
1 parent bf645f4 commit a716dd8
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
61 changes: 60 additions & 1 deletion crates/commands/tests/tests-install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use testdir::testdir;

fn check_install(dir: &Path, name: &str, version_req: &str) {
assert!(dir.join("dependencies").exists());
let deps = read_config_deps(dir.join("soldeer.toml")).unwrap();
let mut config_path = dir.join("soldeer.toml");
if !config_path.exists() {
config_path = dir.join("foundry.toml");
}
let deps = read_config_deps(config_path).unwrap();
assert_eq!(deps.first().unwrap().name(), name);
let remappings = fs::read_to_string(dir.join("remappings.txt")).unwrap();
assert!(remappings.contains(name));
Expand Down Expand Up @@ -196,6 +200,61 @@ async fn test_install_git_branch() {
);
}

#[tokio::test]
async fn test_install_foundry_config() {
let dir = testdir!();
fs::write(dir.join("foundry.toml"), "[dependencies]\n").unwrap();
let cmd: Command = Install {
dependency: Some("@openzeppelin-contracts~5".to_string()),
remote_url: None,
rev: None,
tag: None,
branch: None,
regenerate_remappings: false,
recursive_deps: false,
clean: false,
}
.into();
let res =
async_with_vars([("SOLDEER_PROJECT_ROOT", Some(dir.to_string_lossy().as_ref()))], run(cmd))
.await;
assert!(res.is_ok(), "{res:?}");
check_install(&dir, "@openzeppelin-contracts", "5");
}

#[tokio::test]
async fn test_install_foundry_remappings() {
let dir = testdir!();
let contents = r#"[profile.default]
[soldeer]
remappings_location = "config"
[dependencies]
"@openzeppelin-contracts" = "5"
"#;
fs::write(dir.join("foundry.toml"), contents).unwrap();
let cmd: Command = Install {
dependency: None,
remote_url: None,
rev: None,
tag: None,
branch: None,
regenerate_remappings: false,
recursive_deps: false,
clean: false,
}
.into();
let res =
async_with_vars([("SOLDEER_PROJECT_ROOT", Some(dir.to_string_lossy().as_ref()))], run(cmd))
.await;
assert!(res.is_ok(), "{res:?}");
let config = fs::read_to_string(dir.join("foundry.toml")).unwrap();
assert!(config.contains(
"remappings = [\"@openzeppelin-contracts-5/=dependencies/@openzeppelin-contracts-5.0.2/\"]"
));
}

#[tokio::test]
async fn test_install_missing_no_lock() {
let dir = testdir!();
Expand Down
86 changes: 86 additions & 0 deletions crates/commands/tests/tests-uninstall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use soldeer_commands::{
commands::{install::Install, uninstall::Uninstall},
run, Command,
};
use soldeer_core::{config::read_config_deps, lock::read_lockfile};
use std::{fs, path::PathBuf};
use temp_env::async_with_vars;
use testdir::testdir;

async fn setup(config_filename: &str) -> PathBuf {
let dir = testdir!();
let mut contents = r#"[dependencies]
"@openzeppelin-contracts" = "5.0.2"
solady = "0.0.238"
"#
.to_string();
if config_filename == "foundry.toml" {
contents = format!(
r#"[profile.default]
[soldeer]
remappings_location = "config"
{contents}"#
);
}
fs::write(dir.join(config_filename), contents).unwrap();
let cmd: Command = Install::default().into();
let res =
async_with_vars([("SOLDEER_PROJECT_ROOT", Some(dir.to_string_lossy().as_ref()))], run(cmd))
.await;
assert!(res.is_ok(), "{res:?}");
dir
}

#[tokio::test]
async fn test_uninstall_one() {
let dir = setup("soldeer.toml").await;
let cmd: Command = Uninstall { dependency: "solady".to_string() }.into();
let res =
async_with_vars([("SOLDEER_PROJECT_ROOT", Some(dir.to_string_lossy().as_ref()))], run(cmd))
.await;
assert!(res.is_ok(), "{res:?}");
let deps = read_config_deps(dir.join("soldeer.toml")).unwrap();
assert!(!deps.iter().any(|d| d.name() == "solady"));
let remappings = fs::read_to_string(dir.join("remappings.txt")).unwrap();
assert!(!remappings.contains("solady"));
let lock = read_lockfile(dir.join("soldeer.lock")).unwrap();
assert!(!lock.entries.iter().any(|d| d.name() == "solady"));
assert!(!dir.join("dependencies").join("solady-0.0.238").exists());
}

#[tokio::test]
async fn test_uninstall_all() {
let dir = setup("soldeer.toml").await;
let cmd: Command = Uninstall { dependency: "solady".to_string() }.into();
let res =
async_with_vars([("SOLDEER_PROJECT_ROOT", Some(dir.to_string_lossy().as_ref()))], run(cmd))
.await;
assert!(res.is_ok(), "{res:?}");
let cmd: Command = Uninstall { dependency: "@openzeppelin-contracts".to_string() }.into();
let res =
async_with_vars([("SOLDEER_PROJECT_ROOT", Some(dir.to_string_lossy().as_ref()))], run(cmd))
.await;
assert!(res.is_ok(), "{res:?}");

let deps = read_config_deps(dir.join("soldeer.toml")).unwrap();
assert!(deps.is_empty());
let remappings = fs::read_to_string(dir.join("remappings.txt")).unwrap();
assert_eq!(remappings, "");
assert!(!dir.join("soldeer.lock").exists());
}

#[tokio::test]
async fn test_uninstall_foundry_config() {
let dir = setup("foundry.toml").await;
let cmd: Command = Uninstall { dependency: "solady".to_string() }.into();
let res =
async_with_vars([("SOLDEER_PROJECT_ROOT", Some(dir.to_string_lossy().as_ref()))], run(cmd))
.await;
assert!(res.is_ok(), "{res:?}");
let deps = read_config_deps(dir.join("foundry.toml")).unwrap();
assert!(!deps.iter().any(|d| d.name() == "solady"));
let config = fs::read_to_string(dir.join("foundry.toml")).unwrap();
assert!(!config.contains("solady"));
}

0 comments on commit a716dd8

Please sign in to comment.