diff --git a/CHANGELOG.md b/CHANGELOG.md index 2da5980f82..98c8528e25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ incremented for features. ## [Unreleased] +## Features + +* cli: Add yarn flag to test command ([#267](https://github.com/project-serum/anchor/pull/267)). + ## [0.5.0] - 2021-05-07 ## Features diff --git a/Cargo.lock b/Cargo.lock index a223dd8b14..db6773c00d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -148,6 +148,7 @@ dependencies = [ "solana-sdk", "syn 1.0.67", "toml", + "which", ] [[package]] @@ -4227,6 +4228,16 @@ dependencies = [ "webpki", ] +[[package]] +name = "which" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b55551e42cbdf2ce2bedd2203d0cc08dba002c27510f86dab6d0ce304cba3dfe" +dependencies = [ + "either", + "libc", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 12c92f2e9c..09f87570e5 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -31,3 +31,4 @@ dirs = "3.0" heck = "0.3.1" flate2 = "1.0.19" rand = "0.7.3" +which = "4.1.0" diff --git a/cli/src/main.rs b/cli/src/main.rs index f31ed1a32b..b6f57603e6 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -76,6 +76,9 @@ pub enum Command { /// url is a localnet. #[clap(long)] skip_local_validator: bool, + /// Use this flag if you want to use yarn as your package manager. + #[clap(long)] + yarn: bool, file: Option, }, /// Creates a new program. @@ -230,8 +233,9 @@ fn main() -> Result<()> { Command::Test { skip_deploy, skip_local_validator, + yarn, file, - } => test(skip_deploy, skip_local_validator, file), + } => test(skip_deploy, skip_local_validator, yarn, file), #[cfg(feature = "dev")] Command::Airdrop { url } => airdrop(url), Command::Cluster { subcmd } => cluster(subcmd), @@ -909,7 +913,12 @@ enum OutFile { } // Builds, deploys, and tests all workspace programs in a single command. -fn test(skip_deploy: bool, skip_local_validator: bool, file: Option) -> Result<()> { +fn test( + skip_deploy: bool, + skip_local_validator: bool, + use_yarn: bool, + file: Option, +) -> Result<()> { with_workspace(|cfg, _path, _cargo| { // Bootup validator, if needed. let validator_handle = match cfg.cluster.url() { @@ -936,6 +945,11 @@ fn test(skip_deploy: bool, skip_local_validator: bool, file: Option) -> // Setup log reader. let log_streams = stream_logs(&cfg.cluster.url()); + // Check to see if yarn is installed, panic if not. + if use_yarn { + which::which("yarn").unwrap(); + } + // Run the tests. let test_result: Result<_> = { let ts_config_exist = Path::new("tsconfig.json").exists(); @@ -947,8 +961,28 @@ fn test(skip_deploy: bool, skip_local_validator: bool, file: Option) -> } else { args.push("tests/"); } - let exit = match ts_config_exist { - true => std::process::Command::new("ts-mocha") + let exit = match (ts_config_exist, use_yarn) { + (true, true) => std::process::Command::new("yarn") + .arg("ts-mocha") + .arg("-p") + .arg("./tsconfig.json") + .args(args) + .env("ANCHOR_PROVIDER_URL", cfg.cluster.url()) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output() + .map_err(anyhow::Error::from) + .with_context(|| "ts-mocha"), + (false, true) => std::process::Command::new("yarn") + .arg("mocha") + .args(args) + .env("ANCHOR_PROVIDER_URL", cfg.cluster.url()) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output() + .map_err(anyhow::Error::from) + .with_context(|| "mocha"), + (true, false) => std::process::Command::new("ts-mocha") .arg("-p") .arg("./tsconfig.json") .args(args) @@ -958,7 +992,7 @@ fn test(skip_deploy: bool, skip_local_validator: bool, file: Option) -> .output() .map_err(anyhow::Error::from) .with_context(|| "ts-mocha"), - false => std::process::Command::new("mocha") + (false, false) => std::process::Command::new("mocha") .args(args) .env("ANCHOR_PROVIDER_URL", cfg.cluster.url()) .stdout(Stdio::inherit())