Skip to content

Commit 53e30d4

Browse files
authored
Merge pull request #133 from quicknode/claude/anchor-v2-testing-harness
Move every example onto Anchor v2's LiteSVM test harness
2 parents a9245b5 + 8b5014b commit 53e30d4

118 files changed

Lines changed: 806 additions & 407 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/anchor.yml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,10 @@ jobs:
309309
return 1
310310
fi
311311
312-
# Run anchor test
313-
if ! anchor test; then
312+
# Run anchor test. It regenerates the IDL too, so the skip above
313+
# has to apply here as well or the projects it exempts fail at the
314+
# test step instead of the build step.
315+
if ! anchor test $idl_flag; then
314316
echo "::error::anchor test failed for $project"
315317
echo "$project: anchor test failed" >> $GITHUB_WORKSPACE/failed_projects.txt
316318
rm -rf target node_modules

‎basics/account-data/anchor/programs/anchor-program-example/Cargo.toml‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ idl-build = ["anchor-lang/idl-build"]
1818
anchor-debug = []
1919
custom-heap = []
2020
custom-panic = []
21+
# Forwards to the harness so `anchor test --profile` can turn tracing on.
22+
profile = ["anchor-v2-testing/profile"]
2123

2224
[dependencies]
2325
anchor-lang = "2.0.0-rc.1"
@@ -26,9 +28,16 @@ anchor-lang = "2.0.0-rc.1"
2628
wincode = { version = "0.5", features = ["derive"] }
2729

2830
[dev-dependencies]
29-
litesvm = "0.13.1"
30-
solana-signer = "3.0.0"
31-
solana-keypair = "3.0.1"
31+
# Anchor v2's own LiteSVM harness. `svm()` is `LiteSVM::new()` until the
32+
# `profile` feature is on, when it also records the SBF register traces that
33+
# `anchor test --profile`, `anchor debugger` and `anchor coverage` read. Tests
34+
# that call `LiteSVM::new()` directly still pass but produce no traces, so
35+
# those three tools see nothing.
36+
#
37+
# Published only from git: crates.io has no `anchor-v2-testing`. Pinned to a
38+
# revision rather than tracking `anchor-next`, so a CI run cannot change
39+
# behaviour because upstream moved between builds.
40+
anchor-v2-testing = { git = "https://github.com/otter-sec/anchor.git", rev = "3e38a121b969d1a77230baeb0056baf5cc9b3c1a" }
3241
borsh = "1.6.1"
3342
solana-kite = "0.4.0"
3443

‎basics/account-data/anchor/programs/anchor-program-example/tests/test_account_data.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use {
22
anchor_lang::{
33
solana_program::instruction::Instruction, system_program, InstructionData, ToAccountMetas,
44
},
5+
anchor_v2_testing::{Keypair, LiteSVM, Signer},
56
borsh::BorshDeserialize,
6-
litesvm::LiteSVM,
7-
solana_keypair::Keypair,
87
solana_kite::{create_wallet, send_transaction_from_instructions},
9-
solana_signer::Signer,
108
};
119

1210
/// Deserialize the AddressInfo account (8-byte discriminator + fields).
@@ -22,7 +20,7 @@ struct AddressInfoAccount {
2220
#[test]
2321
fn test_create_address_info() {
2422
let program_id = account_data_anchor_program::id();
25-
let mut svm = LiteSVM::new();
23+
let mut svm = anchor_v2_testing::svm();
2624
let bytes = include_bytes!("../../../target/deploy/account_data_anchor_program.so");
2725
svm.add_program(program_id, bytes).unwrap();
2826
let payer = create_wallet(&mut svm, 10_000_000_000).unwrap();

‎basics/checking-accounts/anchor/programs/anchor-program-example/Cargo.toml‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ idl-build = ["anchor-lang/idl-build"]
1818
anchor-debug = []
1919
custom-heap = []
2020
custom-panic = []
21+
# Forwards to the harness so `anchor test --profile` can turn tracing on.
22+
profile = ["anchor-v2-testing/profile"]
2123

2224
[dependencies]
2325
anchor-lang = "2.0.0-rc.1"
@@ -26,9 +28,16 @@ anchor-lang = "2.0.0-rc.1"
2628
wincode = { version = "0.5", features = ["derive"] }
2729

2830
[dev-dependencies]
29-
litesvm = "0.13.1"
30-
solana-signer = "3.0.0"
31-
solana-keypair = "3.0.1"
31+
# Anchor v2's own LiteSVM harness. `svm()` is `LiteSVM::new()` until the
32+
# `profile` feature is on, when it also records the SBF register traces that
33+
# `anchor test --profile`, `anchor debugger` and `anchor coverage` read. Tests
34+
# that call `LiteSVM::new()` directly still pass but produce no traces, so
35+
# those three tools see nothing.
36+
#
37+
# Published only from git: crates.io has no `anchor-v2-testing`. Pinned to a
38+
# revision rather than tracking `anchor-next`, so a CI run cannot change
39+
# behaviour because upstream moved between builds.
40+
anchor-v2-testing = { git = "https://github.com/otter-sec/anchor.git", rev = "3e38a121b969d1a77230baeb0056baf5cc9b3c1a" }
3241
borsh = "1.6.1"
3342
solana-kite = "0.4.0"
3443

‎basics/checking-accounts/anchor/programs/anchor-program-example/tests/test_checking_accounts.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ use {
33
solana_program::{instruction::Instruction, system_instruction},
44
system_program, InstructionData, ToAccountMetas,
55
},
6-
litesvm::LiteSVM,
7-
solana_keypair::Keypair,
6+
anchor_v2_testing::{Keypair, LiteSVM, Signer},
87
solana_kite::{create_wallet, send_transaction_from_instructions},
9-
solana_signer::Signer,
108
};
119

1210
#[test]
1311
fn test_check_accounts() {
1412
let program_id = checking_account_program::id();
15-
let mut svm = LiteSVM::new();
13+
let mut svm = anchor_v2_testing::svm();
1614
let bytes = include_bytes!("../../../target/deploy/checking_account_program.so");
1715
svm.add_program(program_id, bytes).unwrap();
1816
let payer = create_wallet(&mut svm, 10_000_000_000).unwrap();

‎basics/close-account/anchor/programs/close-account/Cargo.toml‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ idl-build = ["anchor-lang/idl-build"]
1818
anchor-debug = []
1919
custom-heap = []
2020
custom-panic = []
21+
# Forwards to the harness so `anchor test --profile` can turn tracing on.
22+
profile = ["anchor-v2-testing/profile"]
2123

2224
[dependencies]
2325
anchor-lang = "2.0.0-rc.1"
@@ -32,9 +34,16 @@ wincode = { version = "0.5", features = ["derive"] }
3234
solana-address = ">=2.6, <2.7"
3335

3436
[dev-dependencies]
35-
litesvm = "0.13.1"
36-
solana-signer = "3.0.0"
37-
solana-keypair = "3.0.1"
37+
# Anchor v2's own LiteSVM harness. `svm()` is `LiteSVM::new()` until the
38+
# `profile` feature is on, when it also records the SBF register traces that
39+
# `anchor test --profile`, `anchor debugger` and `anchor coverage` read. Tests
40+
# that call `LiteSVM::new()` directly still pass but produce no traces, so
41+
# those three tools see nothing.
42+
#
43+
# Published only from git: crates.io has no `anchor-v2-testing`. Pinned to a
44+
# revision rather than tracking `anchor-next`, so a CI run cannot change
45+
# behaviour because upstream moved between builds.
46+
anchor-v2-testing = { git = "https://github.com/otter-sec/anchor.git", rev = "3e38a121b969d1a77230baeb0056baf5cc9b3c1a" }
3847
solana-kite = "0.4.0"
3948

4049
[lints.rust]

‎basics/close-account/anchor/programs/close-account/tests/test_close_account.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ use {
33
solana_program::instruction::Instruction, system_program, Address, InstructionData,
44
ToAccountMetas,
55
},
6-
litesvm::LiteSVM,
7-
solana_keypair::Keypair,
6+
anchor_v2_testing::{Keypair, LiteSVM, Signer},
87
solana_kite::{create_wallet, send_transaction_from_instructions},
9-
solana_signer::Signer,
108
};
119

1210
fn setup() -> (LiteSVM, Keypair) {
1311
let program_id = close_account_program::id();
14-
let mut svm = LiteSVM::new();
12+
let mut svm = anchor_v2_testing::svm();
1513
let bytes = include_bytes!("../../../target/deploy/close_account_program.so");
1614
svm.add_program(program_id, bytes).unwrap();
1715
let payer = create_wallet(&mut svm, 10_000_000_000).unwrap();

‎basics/counter/anchor/programs/counter_anchor/Cargo.toml‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ cpi = ["no-entrypoint"]
1414
no-entrypoint = []
1515
no-idl = []
1616
no-log-ix-name = []
17+
# Forwards to the harness so `anchor test --profile` can turn tracing on.
18+
profile = ["anchor-v2-testing/profile"]
1719
idl-build = ["anchor-lang/idl-build"]
1820
anchor-debug = []
1921
custom-heap = []
@@ -26,9 +28,16 @@ anchor-lang = "2.0.0-rc.1"
2628
wincode = { version = "0.5", features = ["derive"] }
2729

2830
[dev-dependencies]
29-
litesvm = "0.13.1"
30-
solana-signer = "3.0.0"
31-
solana-keypair = "3.0.1"
31+
# Anchor v2's own LiteSVM harness. `svm()` is `LiteSVM::new()` until the
32+
# `profile` feature is on, when it also records the SBF register traces that
33+
# `anchor test --profile`, `anchor debugger` and `anchor coverage` read. Tests
34+
# that call `LiteSVM::new()` directly still pass but produce no traces, so
35+
# those three tools see nothing.
36+
#
37+
# It is published only from git: crates.io has no `anchor-v2-testing`. Pinned
38+
# to a revision rather than tracking `anchor-next`, so a CI run cannot change
39+
# behaviour because upstream moved between builds.
40+
anchor-v2-testing = { git = "https://github.com/otter-sec/anchor.git", rev = "3e38a121b969d1a77230baeb0056baf5cc9b3c1a" }
3241
borsh = "1.6.1"
3342
solana-kite = "0.4.0"
3443

‎basics/counter/anchor/programs/counter_anchor/tests/test_counter.rs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use {
22
anchor_lang::{
33
solana_program::instruction::Instruction, system_program, InstructionData, ToAccountMetas,
44
},
5+
anchor_v2_testing::{Keypair, LiteSVM, Signer},
56
borsh::BorshDeserialize,
6-
litesvm::LiteSVM,
7-
solana_keypair::Keypair,
87
solana_kite::{create_wallet, send_transaction_from_instructions},
9-
solana_signer::Signer,
108
};
119

1210
/// Minimal deserialization of the Counter account (8-byte discriminator + u64).
@@ -18,7 +16,7 @@ struct CounterAccount {
1816

1917
fn setup() -> (LiteSVM, anchor_lang::prelude::Address, Keypair) {
2018
let program_id = counter_anchor::id();
21-
let mut svm = LiteSVM::new();
19+
let mut svm = anchor_v2_testing::svm();
2220
let bytes = include_bytes!("../../../target/deploy/counter_anchor.so");
2321
svm.add_program(program_id, bytes).unwrap();
2422
let payer = create_wallet(&mut svm, 10_000_000_000).unwrap();

‎basics/create-account/anchor/programs/create-system-account/Cargo.toml‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ idl-build = ["anchor-lang/idl-build"]
1818
anchor-debug = []
1919
custom-heap = []
2020
custom-panic = []
21+
# Forwards to the harness so `anchor test --profile` can turn tracing on.
22+
profile = ["anchor-v2-testing/profile"]
2123

2224
[dependencies]
2325
anchor-lang = "2.0.0-rc.1"
@@ -26,9 +28,16 @@ anchor-lang = "2.0.0-rc.1"
2628
wincode = { version = "0.5", features = ["derive"] }
2729

2830
[dev-dependencies]
29-
litesvm = "0.13.1"
30-
solana-signer = "3.0.0"
31-
solana-keypair = "3.0.1"
31+
# Anchor v2's own LiteSVM harness. `svm()` is `LiteSVM::new()` until the
32+
# `profile` feature is on, when it also records the SBF register traces that
33+
# `anchor test --profile`, `anchor debugger` and `anchor coverage` read. Tests
34+
# that call `LiteSVM::new()` directly still pass but produce no traces, so
35+
# those three tools see nothing.
36+
#
37+
# Published only from git: crates.io has no `anchor-v2-testing`. Pinned to a
38+
# revision rather than tracking `anchor-next`, so a CI run cannot change
39+
# behaviour because upstream moved between builds.
40+
anchor-v2-testing = { git = "https://github.com/otter-sec/anchor.git", rev = "3e38a121b969d1a77230baeb0056baf5cc9b3c1a" }
3241
solana-kite = "0.4.0"
3342

3443
[lints.rust]

0 commit comments

Comments
 (0)