Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test templates: add mollusk #3352

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion .github/workflows/reusable-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ jobs:
name: Test Anchor Init
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
matrix:
template: [mocha, jest, rust, mollusk]
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup/
Expand All @@ -358,7 +361,7 @@ jobs:
path: ~/.cargo/bin/
- run: chmod +x ~/.cargo/bin/anchor

- run: cd "$(mktemp -d)" && anchor init hello-anchor && cd hello-anchor && yarn link @coral-xyz/anchor && yarn && anchor test && yarn lint:fix
- 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
- uses: ./.github/actions/git-diff/

test-programs:
Expand Down
8 changes: 6 additions & 2 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,11 @@ fn init(
if solidity {
solidity_template::create_program(&project_name)?;
} else {
rust_template::create_program(&project_name, template)?;
rust_template::create_program(
&project_name,
template,
TestTemplate::Mollusk == test_template,
)?;
}

// Build the migrations directory.
Expand Down Expand Up @@ -1155,7 +1159,7 @@ fn new(
if solidity {
solidity_template::create_program(&name)?;
} else {
rust_template::create_program(&name, template)?;
rust_template::create_program(&name, template, false)?;
}

programs.insert(
Expand Down
73 changes: 69 additions & 4 deletions cli/src/rust_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ pub enum ProgramTemplate {
}

/// Create a program from the given name and template.
pub fn create_program(name: &str, template: ProgramTemplate) -> Result<()> {
pub fn create_program(name: &str, template: ProgramTemplate, with_mollusk: bool) -> Result<()> {
let program_path = Path::new("programs").join(name);
let common_files = vec![
("Cargo.toml".into(), workspace_manifest().into()),
(program_path.join("Cargo.toml"), cargo_toml(name)),
(
program_path.join("Cargo.toml"),
cargo_toml(name, with_mollusk),
),
(program_path.join("Xargo.toml"), xargo_toml().into()),
];

Expand Down Expand Up @@ -171,7 +174,17 @@ codegen-units = 1
"#
}

fn cargo_toml(name: &str) -> String {
fn cargo_toml(name: &str, with_mollusk: bool) -> String {
let test_sbf_feature = if with_mollusk { r#"test-sbf = []"# } else { "" };
let dev_dependencies = if with_mollusk {
r#"
[dev-dependencies]
mollusk-svm = "=0.0.6-solana-1.18"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason to use fixed versions? We're also on Solana v2 now (I see mollusk-svm 0.0.7-solana-2.0 available), but I guess it doesn't matter much.

This is not a blocker for getting this in, but please let me me know if we can relax this requirement to avoid repetitive bumps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, maybe I wasn't understanding how the versioning worked with master. It seemed like the tip was generating a template for Anchor 0.30, which should be running 1.18, right? My lockfile was pulling in two versions when I used a version of Mollusk compatible with >2.0.

But yeah, there's a 2.0-compat release line as well as the main release line which supports 2.1.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anchor init creates templates with anchor-lang 0.30.1 because that's what the current master version is. However, master branch is already on Solana v2.1, so when we bump the version to v0.31.0 (#3259), it will use the latest Solana version of v2.

In short, the init command is not up-to-date with master, as it always uses the last released version rather than directly depending on the master branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does that mean we'd adjust the version for mollusk-svm after the next Anchor release? Because trying to do it now causes the test job to fail.
https://github.com/coral-xyz/anchor/actions/runs/11814814004/job/32914710753?pr=3361

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think that would make sense, since people would have the incompatible version with the new release that way.

The init test is a bit weird because it's testing the past release compared to all other tests using the latest master. It's probably best to bump mollusk-svm version during Anchor version bumps e.g. in #3259. We could also bump it beforehand, but then we'd have CI failures until the next version is released.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh cool, yeah that makes sense to me. Do you want to add it to #3259? Version 0.0.8 is compatible with Solana 2.1.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, will add it to #3259

"#
} else {
""
};

format!(
r#"[package]
name = "{0}"
Expand All @@ -190,13 +203,17 @@ no-entrypoint = []
no-idl = []
no-log-ix-name = []
idl-build = ["anchor-lang/idl-build"]
{2}

[dependencies]
anchor-lang = "{2}"
anchor-lang = "{3}"
{4}
"#,
name,
name.to_snake_case(),
test_sbf_feature,
VERSION,
dev_dependencies,
)
}

Expand Down Expand Up @@ -611,6 +628,8 @@ pub enum TestTemplate {
Jest,
/// Generate template for Rust unit-test
Rust,
/// Generate template for Mollusk Rust unit-test
Mollusk,
}

impl TestTemplate {
Expand All @@ -637,6 +656,7 @@ impl TestTemplate {
}
}
Self::Rust => "cargo test".to_owned(),
Self::Mollusk => "cargo test-sbf".to_owned(),
}
}

Expand Down Expand Up @@ -708,6 +728,19 @@ impl TestTemplate {
));
override_or_create_files(&files)?;
}
Self::Mollusk => {
// Build the test suite.
let tests_path_str = format!("programs/{}/tests", &project_name);
let tests_path = Path::new(&tests_path_str);
fs::create_dir_all(tests_path)?;

let mut files = Vec::new();
files.extend(create_program_template_mollusk_test(
project_name,
tests_path,
));
override_or_create_files(&files)?;
}
}

Ok(())
Expand Down Expand Up @@ -779,3 +812,35 @@ fn test_initialize() {{
),
]
}

/// Generate template for Mollusk Rust unit-test
fn create_program_template_mollusk_test(name: &str, tests_path: &Path) -> Files {
vec![(
tests_path.join("test_initialize.rs"),
format!(
r#"#![cfg(feature = "test-sbf")]

use {{
anchor_lang::{{solana_program::instruction::Instruction, InstructionData, ToAccountMetas}},
mollusk_svm::{{result::Check, Mollusk}},
}};

#[test]
fn test_initialize() {{
let program_id = {0}::id();

let mollusk = Mollusk::new(&program_id, "{0}");

let instruction = Instruction::new_with_bytes(
program_id,
&{0}::instruction::Initialize {{}}.data(),
{0}::accounts::Initialize {{}}.to_account_metas(None),
);

mollusk.process_and_validate_instruction(&instruction, &[], &[Check::success()]);
}}
"#,
name.to_snake_case(),
),
)]
}
Loading