Skip to content

Commit 6b6fa71

Browse files
authored
cli: Add test template for mollusk (otter-sec#3352)
1 parent 950af6e commit 6b6fa71

4 files changed

Lines changed: 80 additions & 7 deletions

File tree

.github/workflows/reusable-tests.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ jobs:
338338
name: Test Anchor Init
339339
runs-on: ubuntu-latest
340340
timeout-minutes: 30
341+
strategy:
342+
matrix:
343+
template: [mocha, jest, rust, mollusk]
341344
steps:
342345
- uses: actions/checkout@v3
343346
- uses: ./.github/actions/setup/
@@ -358,7 +361,7 @@ jobs:
358361
path: ~/.cargo/bin/
359362
- run: chmod +x ~/.cargo/bin/anchor
360363

361-
- run: cd "$(mktemp -d)" && anchor init hello-anchor && cd hello-anchor && yarn link @coral-xyz/anchor && yarn && anchor test && yarn lint:fix
364+
- run: cd "$(mktemp -d)" && anchor init --test-template ${{ matrix.template }} hello-anchor-${{ matrix.template }} && cd hello-anchor-${{ matrix.template }} && yarn link @coral-xyz/anchor && yarn && anchor test && yarn lint:fix
362365
- uses: ./.github/actions/git-diff/
363366

364367
test-programs:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ The minor version will be incremented upon a breaking change and the patch versi
5959
- avm: Add short alias for `install` and `list` commands ([#3326](https://github.com/coral-xyz/anchor/pull/3326)).
6060
- avm: Add Windows support for renaming anchor binary ([#3325](https://github.com/coral-xyz/anchor/pull/3325)).
6161
- cli: Add optional `package-manager` flag in `init` command to set package manager field in Anchor.toml ([#3328](https://github.com/coral-xyz/anchor/pull/3328)).
62+
- cli: Add test template for [Mollusk](https://github.com/buffalojoec/mollusk) ([#3352](https://github.com/coral-xyz/anchor/pull/3352)).
6263

6364
### Fixes
6465

cli/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,11 @@ fn init(
10361036
if solidity {
10371037
solidity_template::create_program(&project_name)?;
10381038
} else {
1039-
rust_template::create_program(&project_name, template)?;
1039+
rust_template::create_program(
1040+
&project_name,
1041+
template,
1042+
TestTemplate::Mollusk == test_template,
1043+
)?;
10401044
}
10411045

10421046
// Build the migrations directory.
@@ -1155,7 +1159,7 @@ fn new(
11551159
if solidity {
11561160
solidity_template::create_program(&name)?;
11571161
} else {
1158-
rust_template::create_program(&name, template)?;
1162+
rust_template::create_program(&name, template, false)?;
11591163
}
11601164

11611165
programs.insert(

cli/src/rust_template.rs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ pub enum ProgramTemplate {
2929
}
3030

3131
/// Create a program from the given name and template.
32-
pub fn create_program(name: &str, template: ProgramTemplate) -> Result<()> {
32+
pub fn create_program(name: &str, template: ProgramTemplate, with_mollusk: bool) -> Result<()> {
3333
let program_path = Path::new("programs").join(name);
3434
let common_files = vec![
3535
("Cargo.toml".into(), workspace_manifest().into()),
36-
(program_path.join("Cargo.toml"), cargo_toml(name)),
36+
(
37+
program_path.join("Cargo.toml"),
38+
cargo_toml(name, with_mollusk),
39+
),
3740
(program_path.join("Xargo.toml"), xargo_toml().into()),
3841
];
3942

@@ -171,7 +174,17 @@ codegen-units = 1
171174
"#
172175
}
173176

174-
fn cargo_toml(name: &str) -> String {
177+
fn cargo_toml(name: &str, with_mollusk: bool) -> String {
178+
let test_sbf_feature = if with_mollusk { r#"test-sbf = []"# } else { "" };
179+
let dev_dependencies = if with_mollusk {
180+
r#"
181+
[dev-dependencies]
182+
mollusk-svm = "=0.0.6-solana-1.18"
183+
"#
184+
} else {
185+
""
186+
};
187+
175188
format!(
176189
r#"[package]
177190
name = "{0}"
@@ -190,13 +203,17 @@ no-entrypoint = []
190203
no-idl = []
191204
no-log-ix-name = []
192205
idl-build = ["anchor-lang/idl-build"]
206+
{2}
193207
194208
[dependencies]
195-
anchor-lang = "{2}"
209+
anchor-lang = "{3}"
210+
{4}
196211
"#,
197212
name,
198213
name.to_snake_case(),
214+
test_sbf_feature,
199215
VERSION,
216+
dev_dependencies,
200217
)
201218
}
202219

@@ -610,6 +627,8 @@ pub enum TestTemplate {
610627
Jest,
611628
/// Generate template for Rust unit-test
612629
Rust,
630+
/// Generate template for Mollusk Rust unit-test
631+
Mollusk,
613632
}
614633

615634
impl TestTemplate {
@@ -636,6 +655,7 @@ impl TestTemplate {
636655
}
637656
}
638657
Self::Rust => "cargo test".to_owned(),
658+
Self::Mollusk => "cargo test-sbf".to_owned(),
639659
}
640660
}
641661

@@ -707,6 +727,19 @@ impl TestTemplate {
707727
));
708728
override_or_create_files(&files)?;
709729
}
730+
Self::Mollusk => {
731+
// Build the test suite.
732+
let tests_path_str = format!("programs/{}/tests", &project_name);
733+
let tests_path = Path::new(&tests_path_str);
734+
fs::create_dir_all(tests_path)?;
735+
736+
let mut files = Vec::new();
737+
files.extend(create_program_template_mollusk_test(
738+
project_name,
739+
tests_path,
740+
));
741+
override_or_create_files(&files)?;
742+
}
710743
}
711744

712745
Ok(())
@@ -778,3 +811,35 @@ fn test_initialize() {{
778811
),
779812
]
780813
}
814+
815+
/// Generate template for Mollusk Rust unit-test
816+
fn create_program_template_mollusk_test(name: &str, tests_path: &Path) -> Files {
817+
vec![(
818+
tests_path.join("test_initialize.rs"),
819+
format!(
820+
r#"#![cfg(feature = "test-sbf")]
821+
822+
use {{
823+
anchor_lang::{{solana_program::instruction::Instruction, InstructionData, ToAccountMetas}},
824+
mollusk_svm::{{result::Check, Mollusk}},
825+
}};
826+
827+
#[test]
828+
fn test_initialize() {{
829+
let program_id = {0}::id();
830+
831+
let mollusk = Mollusk::new(&program_id, "{0}");
832+
833+
let instruction = Instruction::new_with_bytes(
834+
program_id,
835+
&{0}::instruction::Initialize {{}}.data(),
836+
{0}::accounts::Initialize {{}}.to_account_metas(None),
837+
);
838+
839+
mollusk.process_and_validate_instruction(&instruction, &[], &[Check::success()]);
840+
}}
841+
"#,
842+
name.to_snake_case(),
843+
),
844+
)]
845+
}

0 commit comments

Comments
 (0)