Эх сурвалжийг харах

cli: Allow custom test commands (#550)

Kirill Fomichev 4 жил өмнө
parent
commit
690c41315d

+ 1 - 1
.travis.yml

@@ -69,7 +69,7 @@ jobs:
         - pushd examples/chat && yarn && anchor test && popd
         - pushd examples/ido-pool && yarn && anchor test && popd
         - pushd examples/swap/deps/serum-dex/dex && cargo build-bpf && cd ../../../ && anchor test && popd
-        - pushd examples/cfo && anchor run test && popd
+        - pushd examples/cfo && anchor run test-with-build && popd
     - <<: *examples
       name: Runs the examples 3
       script:

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@ incremented for features.
 ### Features
 
 * cli: Add keys `members` / `exclude` in config `programs` section ([#546](https://github.com/project-serum/anchor/pull/546)).
+* cli: Allow to use custom command for test through scripts instead mocha runner ([#550](https://github.com/project-serum/anchor/pull/550)).
 * cli: Allow program address configuration for test command through `clusters.localnet` ([#554](https://github.com/project-serum/anchor/pull/554)).
 * lang: IDLs are now parsed from the entire crate ([#517](https://github.com/project-serum/anchor/pull/517)).
 

+ 27 - 25
cli/src/main.rs

@@ -96,7 +96,8 @@ pub enum Command {
         /// use this to save time when running test and the program code is not altered.
         #[clap(long)]
         skip_build: bool,
-        file: Option<String>,
+        #[clap(multiple = true)]
+        args: Vec<String>,
     },
     /// Creates a new program.
     New { name: String },
@@ -255,13 +256,13 @@ fn main() -> Result<()> {
             skip_deploy,
             skip_local_validator,
             skip_build,
-            file,
+            args,
         } => test(
             &opts.cfg_override,
             skip_deploy,
             skip_local_validator,
             skip_build,
-            file,
+            args,
         ),
         #[cfg(feature = "dev")]
         Command::Airdrop => airdrop(cfg_override),
@@ -282,7 +283,16 @@ fn init(cfg_override: &ConfigOverride, name: String, typescript: bool) -> Result
     std::env::set_current_dir(&name)?;
     fs::create_dir("app")?;
 
-    let cfg = Config::default();
+    let mut cfg = Config::default();
+    cfg.scripts.insert(
+        "test".to_owned(),
+        if typescript {
+            "ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
+        } else {
+            "mocha -t 1000000 tests/"
+        }
+        .to_owned(),
+    );
     let toml = cfg.to_string();
     let mut file = File::create("Anchor.toml")?;
     file.write_all(toml.as_bytes())?;
@@ -972,7 +982,7 @@ fn test(
     skip_deploy: bool,
     skip_local_validator: bool,
     skip_build: bool,
-    file: Option<String>,
+    extra_args: Vec<String>,
 ) -> Result<()> {
     with_workspace(cfg_override, |cfg, _path, _cargo| {
         // Build if needed.
@@ -1005,26 +1015,18 @@ fn test(
 
         // Run the tests.
         let test_result: Result<_> = {
-            let ts_config_exist = Path::new("tsconfig.json").exists();
-            let cmd = if ts_config_exist { "ts-mocha" } else { "mocha" };
-            let mut args = if ts_config_exist {
-                vec![cmd, "-p", "./tsconfig.json"]
-            } else {
-                vec![cmd]
-            };
-            args.extend_from_slice(&[
-                "-t",
-                "1000000",
-                if let Some(ref file) = file {
-                    file
-                } else if ts_config_exist {
-                    "tests/**/*.ts"
-                } else {
-                    "tests/"
-                },
-            ]);
-
-            std::process::Command::new("npx")
+            let cmd = cfg
+                .scripts
+                .get("test")
+                .expect("Not able to find command for `test`")
+                .clone();
+            let mut args: Vec<&str> = cmd
+                .split(' ')
+                .chain(extra_args.iter().map(|arg| arg.as_str()))
+                .collect();
+            let program = args.remove(0);
+
+            std::process::Command::new(program)
                 .args(args)
                 .env("ANCHOR_PROVIDER_URL", cfg.provider.cluster.url())
                 .stdout(Stdio::inherit())

+ 3 - 0
examples/cashiers-check/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 2 - 1
examples/cfo/Anchor.toml

@@ -10,7 +10,8 @@ lockup = { address = "6ebQNeTPZ1j7k3TtkCCtEPRvG7GQsucQrZ7sSEDQi9Ks", idl = "./de
 #
 # Testing.
 #
-test = "anchor run build && anchor test --skip-build"
+test = "mocha -t 1000000 tests/"
+test-with-build = "anchor run build && anchor test --skip-build"
 #
 # Build the program and all CPI dependencies.
 #

+ 3 - 0
examples/chat/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/composite/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/errors/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/escrow/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/events/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/ido-pool/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/interface/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/lockup/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/misc/Anchor.toml

@@ -8,3 +8,6 @@ program = "./target/deploy/misc.so"
 
 [workspace]
 exclude = ["shared"]
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/multisig/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/pyth/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

+ 3 - 0
examples/spl/token-proxy/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/swap/Anchor.toml

@@ -5,3 +5,6 @@ wallet = "~/.config/solana/id.json"
 [[test.genesis]]
 address = "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"
 program = "./deps/serum-dex/dex/target/deploy/serum_dex.so"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/sysvars/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/tutorial/basic-0/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/tutorial/basic-1/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/tutorial/basic-2/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/tutorial/basic-3/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/tutorial/basic-4/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/tutorial/basic-5/Anchor.toml

@@ -1,3 +1,6 @@
 [provider]
 cluster = "localnet"
 wallet = "~/.config/solana/id.json"
+
+[scripts]
+test = "mocha -t 1000000 tests/"

+ 3 - 0
examples/typescript/Anchor.toml

@@ -5,3 +5,6 @@ wallet = "~/.config/solana/id.json"
 [workspace]
 members = ["typescript"]
 exclude = ["typescript"]
+
+[scripts]
+test = "ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

+ 3 - 0
examples/zero-copy/Anchor.toml

@@ -4,3 +4,6 @@ wallet = "~/.config/solana/id.json"
 
 [workspace]
 members = ["zero-copy"]
+
+[scripts]
+test = "mocha -t 1000000 tests/"