cli: Allow custom test commands (#550)

This commit is contained in:
Kirill Fomichev 2021-07-30 00:38:24 +03:00 committed by GitHub
parent ba6e15d557
commit 690c41315d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 99 additions and 26 deletions

View File

@ -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:

View File

@ -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)).

View File

@ -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/"
},
]);
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())

View File

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

View File

@ -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.
#

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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/"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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"

View File

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