From 690c41315d47f91dfb7080239da4fddf13b473af Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Fri, 30 Jul 2021 00:38:24 +0300 Subject: [PATCH] cli: Allow custom test commands (#550) --- .travis.yml | 2 +- CHANGELOG.md | 1 + cli/src/main.rs | 50 ++++++++++++++------------- examples/cashiers-check/Anchor.toml | 3 ++ examples/cfo/Anchor.toml | 3 +- examples/chat/Anchor.toml | 3 ++ examples/composite/Anchor.toml | 3 ++ examples/errors/Anchor.toml | 3 ++ examples/escrow/Anchor.toml | 3 ++ examples/events/Anchor.toml | 3 ++ examples/ido-pool/Anchor.toml | 3 ++ examples/interface/Anchor.toml | 3 ++ examples/lockup/Anchor.toml | 3 ++ examples/misc/Anchor.toml | 3 ++ examples/multisig/Anchor.toml | 3 ++ examples/pyth/Anchor.toml | 3 ++ examples/spl/token-proxy/Anchor.toml | 3 ++ examples/swap/Anchor.toml | 3 ++ examples/sysvars/Anchor.toml | 3 ++ examples/tutorial/basic-0/Anchor.toml | 3 ++ examples/tutorial/basic-1/Anchor.toml | 3 ++ examples/tutorial/basic-2/Anchor.toml | 3 ++ examples/tutorial/basic-3/Anchor.toml | 3 ++ examples/tutorial/basic-4/Anchor.toml | 3 ++ examples/tutorial/basic-5/Anchor.toml | 3 ++ examples/typescript/Anchor.toml | 3 ++ examples/zero-copy/Anchor.toml | 3 ++ 27 files changed, 99 insertions(+), 26 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3b192c0ed..25f9e1f7a 100644 --- a/.travis.yml +++ b/.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: diff --git a/CHANGELOG.md b/CHANGELOG.md index d01cbfe34..abb9f281a 100644 --- a/CHANGELOG.md +++ b/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)). diff --git a/cli/src/main.rs b/cli/src/main.rs index 388d26536..96b81c4c6 100644 --- a/cli/src/main.rs +++ b/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, + #[clap(multiple = true)] + args: Vec, }, /// 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, + extra_args: Vec, ) -> 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/" - }, - ]); + 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("npx") + std::process::Command::new(program) .args(args) .env("ANCHOR_PROVIDER_URL", cfg.provider.cluster.url()) .stdout(Stdio::inherit()) diff --git a/examples/cashiers-check/Anchor.toml b/examples/cashiers-check/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/cashiers-check/Anchor.toml +++ b/examples/cashiers-check/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/cfo/Anchor.toml b/examples/cfo/Anchor.toml index dc33b5ea2..c84f0eaa6 100644 --- a/examples/cfo/Anchor.toml +++ b/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. # diff --git a/examples/chat/Anchor.toml b/examples/chat/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/chat/Anchor.toml +++ b/examples/chat/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/composite/Anchor.toml b/examples/composite/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/composite/Anchor.toml +++ b/examples/composite/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/errors/Anchor.toml b/examples/errors/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/errors/Anchor.toml +++ b/examples/errors/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/escrow/Anchor.toml b/examples/escrow/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/escrow/Anchor.toml +++ b/examples/escrow/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/events/Anchor.toml b/examples/events/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/events/Anchor.toml +++ b/examples/events/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/ido-pool/Anchor.toml b/examples/ido-pool/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/ido-pool/Anchor.toml +++ b/examples/ido-pool/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/interface/Anchor.toml b/examples/interface/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/interface/Anchor.toml +++ b/examples/interface/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/lockup/Anchor.toml b/examples/lockup/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/lockup/Anchor.toml +++ b/examples/lockup/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/misc/Anchor.toml b/examples/misc/Anchor.toml index f1fbaa1e5..9afebcc1a 100644 --- a/examples/misc/Anchor.toml +++ b/examples/misc/Anchor.toml @@ -8,3 +8,6 @@ program = "./target/deploy/misc.so" [workspace] exclude = ["shared"] + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/multisig/Anchor.toml b/examples/multisig/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/multisig/Anchor.toml +++ b/examples/multisig/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/pyth/Anchor.toml b/examples/pyth/Anchor.toml index b9705f038..0355825da 100644 --- a/examples/pyth/Anchor.toml +++ b/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" diff --git a/examples/spl/token-proxy/Anchor.toml b/examples/spl/token-proxy/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/spl/token-proxy/Anchor.toml +++ b/examples/spl/token-proxy/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/swap/Anchor.toml b/examples/swap/Anchor.toml index 959ea7b4c..c9d9af0cc 100644 --- a/examples/swap/Anchor.toml +++ b/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/" diff --git a/examples/sysvars/Anchor.toml b/examples/sysvars/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/sysvars/Anchor.toml +++ b/examples/sysvars/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/tutorial/basic-0/Anchor.toml b/examples/tutorial/basic-0/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/tutorial/basic-0/Anchor.toml +++ b/examples/tutorial/basic-0/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/tutorial/basic-1/Anchor.toml b/examples/tutorial/basic-1/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/tutorial/basic-1/Anchor.toml +++ b/examples/tutorial/basic-1/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/tutorial/basic-2/Anchor.toml b/examples/tutorial/basic-2/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/tutorial/basic-2/Anchor.toml +++ b/examples/tutorial/basic-2/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/tutorial/basic-3/Anchor.toml b/examples/tutorial/basic-3/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/tutorial/basic-3/Anchor.toml +++ b/examples/tutorial/basic-3/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/tutorial/basic-4/Anchor.toml b/examples/tutorial/basic-4/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/tutorial/basic-4/Anchor.toml +++ b/examples/tutorial/basic-4/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/tutorial/basic-5/Anchor.toml b/examples/tutorial/basic-5/Anchor.toml index b9705f038..5b26d40cf 100644 --- a/examples/tutorial/basic-5/Anchor.toml +++ b/examples/tutorial/basic-5/Anchor.toml @@ -1,3 +1,6 @@ [provider] cluster = "localnet" wallet = "~/.config/solana/id.json" + +[scripts] +test = "mocha -t 1000000 tests/" diff --git a/examples/typescript/Anchor.toml b/examples/typescript/Anchor.toml index 2c7f889ba..98c23964e 100644 --- a/examples/typescript/Anchor.toml +++ b/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" diff --git a/examples/zero-copy/Anchor.toml b/examples/zero-copy/Anchor.toml index 2f62faf56..721554fe3 100644 --- a/examples/zero-copy/Anchor.toml +++ b/examples/zero-copy/Anchor.toml @@ -4,3 +4,6 @@ wallet = "~/.config/solana/id.json" [workspace] members = ["zero-copy"] + +[scripts] +test = "mocha -t 1000000 tests/"