Browse Source

Add yarn flag to the anchor test command (#267)

Michael Huang 4 years ago
parent
commit
425997a12d
4 changed files with 55 additions and 5 deletions
  1. 4 0
      CHANGELOG.md
  2. 11 0
      Cargo.lock
  3. 1 0
      cli/Cargo.toml
  4. 39 5
      cli/src/main.rs

+ 4 - 0
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

+ 11 - 0
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"

+ 1 - 0
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"

+ 39 - 5
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<String>,
     },
     /// 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<String>) -> Result<()> {
+fn test(
+    skip_deploy: bool,
+    skip_local_validator: bool,
+    use_yarn: bool,
+    file: Option<String>,
+) -> 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<String>) ->
         // 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<String>) ->
             } 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<String>) ->
                     .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())