From 6814ce646e9fa3277969c7d30fef9b73c9fcc1bc Mon Sep 17 00:00:00 2001 From: skrrb Date: Tue, 6 Dec 2022 22:42:10 +0100 Subject: [PATCH] cli: run a subset of the test suites (#1864) * cli: run subset of tests with --run * changelog * test * fix run test path * test: added missing package.json * cli: run subset of tests with --run * changelog * test * fix run test path * test: added missing package.json Co-authored-by: Armani Ferrante Co-authored-by: henrye --- .github/workflows/tests.yaml | 2 ++ CHANGELOG.md | 1 + cli/src/config.rs | 17 ++++++++++++----- cli/src/lib.rs | 17 ++++++++++++++++- tests/multiple-suites-run-single/Anchor.toml | 14 ++++++++++++++ tests/multiple-suites-run-single/Cargo.toml | 4 ++++ tests/multiple-suites-run-single/package.json | 19 +++++++++++++++++++ .../multiple-suites-run-single/Cargo.toml | 19 +++++++++++++++++++ .../multiple-suites-run-single/Xargo.toml | 2 ++ .../multiple-suites-run-single/src/lib.rs | 15 +++++++++++++++ .../tests/should-not-run/Test.toml | 4 ++++ .../tests/should-not-run/shouldNotRun.ts | 5 +++++ .../tests/should-run/Test.toml | 4 ++++ .../tests/should-run/shouldRun.ts | 17 +++++++++++++++++ .../multiple-suites-run-single/tsconfig.json | 11 +++++++++++ tests/package.json | 1 + 16 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 tests/multiple-suites-run-single/Anchor.toml create mode 100644 tests/multiple-suites-run-single/Cargo.toml create mode 100644 tests/multiple-suites-run-single/package.json create mode 100644 tests/multiple-suites-run-single/programs/multiple-suites-run-single/Cargo.toml create mode 100644 tests/multiple-suites-run-single/programs/multiple-suites-run-single/Xargo.toml create mode 100644 tests/multiple-suites-run-single/programs/multiple-suites-run-single/src/lib.rs create mode 100644 tests/multiple-suites-run-single/tests/should-not-run/Test.toml create mode 100644 tests/multiple-suites-run-single/tests/should-not-run/shouldNotRun.ts create mode 100644 tests/multiple-suites-run-single/tests/should-run/Test.toml create mode 100644 tests/multiple-suites-run-single/tests/should-run/shouldRun.ts create mode 100644 tests/multiple-suites-run-single/tsconfig.json diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 678b1173f..859a2c688 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -401,6 +401,8 @@ jobs: path: tests/cpi-returns - cmd: cd tests/multiple-suites && anchor test --skip-lint && npx tsc --noEmit path: tests/multiple-suites + - cmd: cd tests/multiple-suites-run-single && anchor test --skip-lint --run tests/should-run && npx tsc --noEmit + path: tests/multiple-suites-run-single - cmd: cd tests/pda-derivation && anchor test --skip-lint && npx tsc --noEmit path: tests/pda-derivation - cmd: cd tests/relations-derivation && anchor test --skip-lint && npx tsc --noEmit diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c91eb4f3..52f18ce64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The minor version will be incremented upon a breaking change and the patch versi ### Features +- cli: Add `--run` to `anchor test` for running a subset of test suites ([#1828](https://github.com/project-serum/anchor/issues/1828)). - client: Add `transaction` functions to RequestBuilder ([#1958](https://github.com/coral-xyz/anchor/pull/1958)). - spl: Add `create_metadata_accounts_v3` and `set_collection_size` wrappers ([#2119](https://github.com/coral-xyz/anchor/pull/2119)) - spl: Add `MetadataAccount` account deserialization. ([#2014](https://github.com/coral-xyz/anchor/pull/2014)). diff --git a/cli/src/config.rs b/cli/src/config.rs index e7b4f663e..7672dc2e1 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -341,8 +341,12 @@ pub struct BuildConfig { } impl Config { - pub fn add_test_config(&mut self, root: impl AsRef) -> Result<()> { - self.test_config = TestConfig::discover(root)?; + pub fn add_test_config( + &mut self, + root: impl AsRef, + test_paths: Vec, + ) -> Result<()> { + self.test_config = TestConfig::discover(root, test_paths)?; Ok(()) } @@ -662,14 +666,17 @@ impl Deref for TestConfig { } impl TestConfig { - pub fn discover(root: impl AsRef) -> Result> { + pub fn discover(root: impl AsRef, test_paths: Vec) -> Result> { let walker = WalkDir::new(root).into_iter(); let mut test_suite_configs = HashMap::new(); for entry in walker.filter_entry(|e| !is_hidden(e)) { let entry = entry?; if entry.file_name() == "Test.toml" { - let test_toml = TestToml::from_path(entry.path())?; - test_suite_configs.insert(entry.path().into(), test_toml); + let entry_path = entry.path(); + let test_toml = TestToml::from_path(entry_path)?; + if test_paths.is_empty() || test_paths.iter().any(|p| entry_path.starts_with(p)) { + test_suite_configs.insert(entry.path().into(), test_toml); + } } } diff --git a/cli/src/lib.rs b/cli/src/lib.rs index c7ba3009e..6b5574cc3 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -167,6 +167,9 @@ pub enum Command { /// to be able to check the transactions. #[clap(long)] detach: bool, + /// Run the test suites under the specified path + #[clap(long)] + run: Vec, args: Vec, /// Arguments to pass to the underlying `cargo build-bpf` command. #[clap(required = false, last = true)] @@ -426,6 +429,7 @@ pub fn entry(opts: Opts) -> Result<()> { skip_local_validator, skip_build, detach, + run, args, cargo_args, skip_lint, @@ -436,6 +440,7 @@ pub fn entry(opts: Opts) -> Result<()> { skip_build, skip_lint, detach, + run, args, cargo_args, ), @@ -1863,9 +1868,19 @@ fn test( skip_build: bool, skip_lint: bool, detach: bool, + tests_to_run: Vec, extra_args: Vec, cargo_args: Vec, ) -> Result<()> { + let test_paths = tests_to_run + .iter() + .map(|path| { + PathBuf::from(path) + .canonicalize() + .map_err(|_| anyhow!("Wrong path {}", path)) + }) + .collect::, _>>()?; + with_workspace(cfg_override, |cfg| { // Build if needed. if !skip_build { @@ -1887,7 +1902,7 @@ fn test( } let root = cfg.path().parent().unwrap().to_owned(); - cfg.add_test_config(root)?; + cfg.add_test_config(root, test_paths)?; // Run the deploy against the cluster in two cases: // diff --git a/tests/multiple-suites-run-single/Anchor.toml b/tests/multiple-suites-run-single/Anchor.toml new file mode 100644 index 000000000..7962da68e --- /dev/null +++ b/tests/multiple-suites-run-single/Anchor.toml @@ -0,0 +1,14 @@ +[features] +seeds = false +[programs.localnet] +multiple_suites_run_single = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS" + +[registry] +url = "https://anchor.projectserum.com" + +[provider] +cluster = "localnet" +wallet = "~/.config/solana/id.json" + +[test] +startup_wait = 20000 diff --git a/tests/multiple-suites-run-single/Cargo.toml b/tests/multiple-suites-run-single/Cargo.toml new file mode 100644 index 000000000..a60de986d --- /dev/null +++ b/tests/multiple-suites-run-single/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +members = [ + "programs/*" +] diff --git a/tests/multiple-suites-run-single/package.json b/tests/multiple-suites-run-single/package.json new file mode 100644 index 000000000..8841f8ab3 --- /dev/null +++ b/tests/multiple-suites-run-single/package.json @@ -0,0 +1,19 @@ +{ + "name": "multiple-suites-run-single", + "version": "0.24.2", + "license": "(MIT OR Apache-2.0)", + "homepage": "https://github.com/project-serum/anchor#readme", + "bugs": { + "url": "https://github.com/project-serum/anchor/issues" + }, + "repository": { + "type": "git", + "url": "https://github.com/project-serum/anchor.git" + }, + "engines": { + "node": ">=11" + }, + "scripts": { + "test": "anchor test" + } + } diff --git a/tests/multiple-suites-run-single/programs/multiple-suites-run-single/Cargo.toml b/tests/multiple-suites-run-single/programs/multiple-suites-run-single/Cargo.toml new file mode 100644 index 000000000..5b5e10292 --- /dev/null +++ b/tests/multiple-suites-run-single/programs/multiple-suites-run-single/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "multiple-suites-run-single" +version = "0.1.0" +description = "Created with Anchor" +edition = "2018" + +[lib] +crate-type = ["cdylib", "lib"] +name = "multiple_suites_run_single" + +[features] +no-entrypoint = [] +no-idl = [] +no-log-ix-name = [] +cpi = ["no-entrypoint"] +default = [] + +[dependencies] +anchor-lang = { path = "../../../../lang" } diff --git a/tests/multiple-suites-run-single/programs/multiple-suites-run-single/Xargo.toml b/tests/multiple-suites-run-single/programs/multiple-suites-run-single/Xargo.toml new file mode 100644 index 000000000..475fb71ed --- /dev/null +++ b/tests/multiple-suites-run-single/programs/multiple-suites-run-single/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] diff --git a/tests/multiple-suites-run-single/programs/multiple-suites-run-single/src/lib.rs b/tests/multiple-suites-run-single/programs/multiple-suites-run-single/src/lib.rs new file mode 100644 index 000000000..14d1daddf --- /dev/null +++ b/tests/multiple-suites-run-single/programs/multiple-suites-run-single/src/lib.rs @@ -0,0 +1,15 @@ +use anchor_lang::prelude::*; + +declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); + +#[program] +pub mod multiple_suites_run_single { + use super::*; + + pub fn initialize(_ctx: Context) -> Result<()> { + Ok(()) + } +} + +#[derive(Accounts)] +pub struct Initialize {} diff --git a/tests/multiple-suites-run-single/tests/should-not-run/Test.toml b/tests/multiple-suites-run-single/tests/should-not-run/Test.toml new file mode 100644 index 000000000..1d248395f --- /dev/null +++ b/tests/multiple-suites-run-single/tests/should-not-run/Test.toml @@ -0,0 +1,4 @@ +extends = ["../../Anchor.toml"] + +[scripts] +test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/should-not-run/**/*.ts" diff --git a/tests/multiple-suites-run-single/tests/should-not-run/shouldNotRun.ts b/tests/multiple-suites-run-single/tests/should-not-run/shouldNotRun.ts new file mode 100644 index 000000000..1b7dee1b9 --- /dev/null +++ b/tests/multiple-suites-run-single/tests/should-not-run/shouldNotRun.ts @@ -0,0 +1,5 @@ +describe("multiple-suites-run-single", () => { + it("Should not be executed", async () => { + throw new Error("This test has to be skipped"); + }); +}); diff --git a/tests/multiple-suites-run-single/tests/should-run/Test.toml b/tests/multiple-suites-run-single/tests/should-run/Test.toml new file mode 100644 index 000000000..9387a1476 --- /dev/null +++ b/tests/multiple-suites-run-single/tests/should-run/Test.toml @@ -0,0 +1,4 @@ +extends = ["../../Anchor.toml"] + +[scripts] +test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/should-run/**/*.ts" diff --git a/tests/multiple-suites-run-single/tests/should-run/shouldRun.ts b/tests/multiple-suites-run-single/tests/should-run/shouldRun.ts new file mode 100644 index 000000000..2924162a5 --- /dev/null +++ b/tests/multiple-suites-run-single/tests/should-run/shouldRun.ts @@ -0,0 +1,17 @@ +import * as anchor from "@project-serum/anchor"; +import { Program } from "@project-serum/anchor"; +import { assert } from "chai"; +import { MultipleSuitesRunSingle } from "../../target/types/multiple_suites_run_single"; + +describe("multiple-suites-run-single", () => { + // Configure the client to use the local cluster. + anchor.setProvider(anchor.AnchorProvider.env()); + + const program = anchor.workspace + .MultipleSuitesRunSingle as Program; + + it("Is initialized!", async () => { + const tx = await program.methods.initialize().rpc(); + console.log("Your transaction signature", tx); + }); +}); diff --git a/tests/multiple-suites-run-single/tsconfig.json b/tests/multiple-suites-run-single/tsconfig.json new file mode 100644 index 000000000..b3b6656d3 --- /dev/null +++ b/tests/multiple-suites-run-single/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "types": ["mocha", "chai"], + "typeRoots": ["./node_modules/@types"], + "lib": ["es2015"], + "module": "commonjs", + "target": "es6", + "esModuleInterop": true, + "skipLibCheck": true + } +} diff --git a/tests/package.json b/tests/package.json index 10a1f04e5..210c3e58f 100644 --- a/tests/package.json +++ b/tests/package.json @@ -37,6 +37,7 @@ "declare-id", "cpi-returns", "multiple-suites", + "multiple-suites-run-single", "bpf-upgradeable-state" ], "dependencies": {