Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/reusable-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ jobs:
- run: cd tests/bpf-upgradeable-state && yarn --frozen-lockfile
- run: cd tests/bpf-upgradeable-state
- run: cd tests/bpf-upgradeable-state && anchor build --skip-lint --ignore-keys
- run: cd tests/bpf-upgradeable-state && solana program deploy --program-id program_with_different_programdata.json target/deploy/bpf_upgradeable_state.so
- run: cd tests/bpf-upgradeable-state && solana program deploy --program-id program_with_different_programdata.json --use-rpc target/deploy/bpf_upgradeable_state.so
- run: cd tests/bpf-upgradeable-state && cp bpf_upgradeable_state-keypair.json target/deploy/bpf_upgradeable_state-keypair.json && anchor test --skip-local-validator --skip-build --skip-lint
- run: cd tests/bpf-upgradeable-state && npx tsc --noEmit
- uses: ./.github/actions/git-diff/
Expand Down Expand Up @@ -474,6 +474,8 @@ jobs:
path: tests/idl
- cmd: cd tests/lazy-account && anchor test --skip-lint
path: tests/lazy-account
- cmd: cd tests/multiple-scripts && anchor test --script test2
path: tests/multiple-scripts
- cmd: cd tests/test-instruction-validation && ./test.sh
path: tests/test-instruction-validation
- cmd: cd tests/signature-verification && anchor test
Expand Down
19 changes: 15 additions & 4 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ pub enum Command {
/// Run the test suites under the specified path
#[clap(long)]
run: Vec<String>,
/// Name of the script to run from [scripts] section (defaults to "test")
#[clap(long)]
script: Option<String>,
/// Validator type to use for local testing
#[clap(value_enum, long, default_value = "surfpool")]
validator: ValidatorType,
Expand Down Expand Up @@ -1251,6 +1254,7 @@ fn process_command(opts: Opts) -> Result<()> {
no_idl,
detach,
run,
script,
validator,
args,
env,
Expand All @@ -1267,6 +1271,7 @@ fn process_command(opts: Opts) -> Result<()> {
no_idl,
detach,
run,
script,
validator,
args,
env,
Expand Down Expand Up @@ -3172,6 +3177,7 @@ fn test(
no_idl: bool,
detach: bool,
tests_to_run: Vec<String>,
script_name: Option<String>,
validator_type: ValidatorType,
extra_args: Vec<String>,
env_vars: Vec<String>,
Expand Down Expand Up @@ -3231,7 +3237,8 @@ fn test(
cfg.run_hooks(HookType::PreTest)?;

let mut is_first_suite = true;
if let Some(test_script) = cfg.scripts.get_mut("test") {
let script_name_to_use = script_name.as_deref().unwrap_or("test");
if let Some(test_script) = cfg.scripts.get_mut(script_name_to_use) {
is_first_suite = false;

match program_name {
Expand All @@ -3254,7 +3261,8 @@ fn test(
}
}
_ => println!(
"\nFound a 'test' script in the Anchor.toml. Running it as a test suite!"
"\nFound a '{}' script in the Anchor.toml. Running it as a test suite!",
script_name_to_use
),
}

Expand All @@ -3268,6 +3276,7 @@ fn test(
validator_type,
&cfg.test_validator,
&cfg.scripts,
script_name_to_use,
&extra_args,
&cfg.surfpool_config,
)?;
Expand Down Expand Up @@ -3297,6 +3306,7 @@ fn test(
validator_type,
&test_suite.1.test,
&test_suite.1.scripts,
script_name_to_use,
&extra_args,
&cfg.surfpool_config,
)?;
Expand All @@ -3318,6 +3328,7 @@ fn run_test_suite(
validator_type: ValidatorType,
test_validator: &Option<TestValidator>,
scripts: &ScriptsConfig,
script_name: &str,
extra_args: &[String],
surfpool_config: &Option<SurfpoolConfig>,
) -> Result<()> {
Expand Down Expand Up @@ -3380,8 +3391,8 @@ fn run_test_suite(
// Run the tests.
let test_result = {
let cmd = scripts
.get("test")
.expect("Not able to find script for `test`")
.get(script_name)
.unwrap_or_else(|| panic!("Not able to find script for `{}`", script_name))
.clone();
let script_args = format!("{cmd} {}", extra_args.join(" "));

Expand Down
18 changes: 18 additions & 0 deletions tests/multiple-scripts/Anchor.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[toolchain]

[features]
resolution = true
skip-lint = false

[provider]
cluster = "localnet"
wallet = "~/.config/solana/id.json"

[programs.localnet]
example = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/test.ts"
test2 = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/test.ts"

[test]
16 changes: 16 additions & 0 deletions tests/multiple-scripts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[workspace]
members = [
"programs/*"
]
resolver = "2"

[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1

[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1

9 changes: 9 additions & 0 deletions tests/multiple-scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "multiple-scripts-example",
"version": "0.1.0",
"description": "Example demonstrating multiple test scripts with anchor test --script",
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
}
}
16 changes: 16 additions & 0 deletions tests/multiple-scripts/programs/example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "example"
version = "0.1.0"
description = "Example program demonstrating multiple test scripts"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "example"

[features]
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = { path = "../../../../lang" }

16 changes: 16 additions & 0 deletions tests/multiple-scripts/programs/example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod example {
use super::*;

pub fn initialize(_ctx: Context<Initialize>) -> Result<()> {
msg!("Program initialized!");
Ok(())
}
}

#[derive(Accounts)]
pub struct Initialize {}
27 changes: 27 additions & 0 deletions tests/multiple-scripts/tests/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Example } from "../target/types/example";
import { assert } from "chai";

describe("multiple-scripts", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());

const program = anchor.workspace.Example as Program<Example>;

it("Initializes successfully", async () => {
// Call the initialize instruction
const tx = await program.methods.initialize().rpc();

console.log("Transaction signature:", tx);
assert.ok(tx, "Transaction should have a signature");
});

it("Can be called multiple times", async () => {
// Call initialize again to demonstrate it can be called multiple times
const tx = await program.methods.initialize().rpc();

console.log("Second transaction signature:", tx);
assert.ok(tx, "Second transaction should also succeed");
});
});
11 changes: 11 additions & 0 deletions tests/multiple-scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"types": ["mocha", "chai"],
"typeRoots": ["./node_modules/@types"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"esModuleInterop": true
}
}

1 change: 1 addition & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"multiple-suites",
"multiple-suites-run-single",
"bpf-upgradeable-state",
"multiple-scripts",
"duplicate-mutable-accounts",
"signature-verification"
],
Expand Down