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

cli: init rust test #2805

Merged
merged 29 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f3d1ad0
feat: add rust option in init
aoikurokawa Jan 31, 2024
e020739
feat: modify init command
aoikurokawa Feb 1, 2024
30278d2
feat: add some new templates for rust tests
aoikurokawa Feb 1, 2024
247c703
feat: use template instead of flag
aoikurokawa Feb 4, 2024
5e81fbc
fix: remove each templates for rust test
aoikurokawa Feb 4, 2024
98462cf
fix: add program files
aoikurokawa Feb 4, 2024
750a3dc
chore: remove warnings
aoikurokawa Feb 4, 2024
2b3912a
chore: fix clippy problem
aoikurokawa Feb 4, 2024
97b7e62
feat: define enum for TestTemplate
aoikurokawa Feb 7, 2024
6962a06
refact: divide test module
aoikurokawa Feb 7, 2024
f340657
refact: divide test module
aoikurokawa Feb 7, 2024
5a33468
feat: test template module
aoikurokawa Feb 7, 2024
dbcaedc
feat: remove code that create test files
aoikurokawa Feb 8, 2024
175382b
feat: takes multiple program template
aoikurokawa Feb 8, 2024
47abcdb
chore: fix clippy
aoikurokawa Feb 8, 2024
06351ee
chore: remove comments
aoikurokawa Feb 8, 2024
e1d42c7
feat: when rust-test, choose single or multiple
aoikurokawa Feb 8, 2024
1980a8e
chore: remove test_template.rs and move logic into rust_template
aoikurokawa Feb 11, 2024
567d5ff
fix: fix unit test error
aoikurokawa Feb 11, 2024
a483615
chore: remove comments and modify cli explanation
aoikurokawa Feb 11, 2024
6769c82
chore: remove comments
aoikurokawa Feb 11, 2024
466f2fc
fix: modify variable name
aoikurokawa Feb 11, 2024
a5f6c86
doc: add new log
aoikurokawa Feb 11, 2024
98aaf63
fix: remove tests_mod variable from program creation
aoikurokawa Feb 13, 2024
b769ce4
fix: create rust tests module inside test creation
aoikurokawa Feb 13, 2024
293aca9
chore: revert unnecessary modification
aoikurokawa Feb 13, 2024
3d6b6af
doc: change breaking to features
aoikurokawa Feb 13, 2024
1a61596
fix: do not initialize git repo
aoikurokawa Feb 13, 2024
7cf0785
fix: conflicts
aoikurokawa Feb 13, 2024
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
12 changes: 8 additions & 4 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ fn init(
}
.to_owned(),
);
} else if template == ProgramTemplate::RustTest {
cfg.scripts
.insert("test".to_owned(), "cargo test".to_owned());
} else {
cfg.scripts.insert(
"test".to_owned(),
Expand All @@ -886,7 +889,7 @@ fn init(
let mut localnet = BTreeMap::new();
let program_id = rust_template::get_or_create_program_id(&rust_name);
localnet.insert(
rust_name,
rust_name.clone(),
ProgramDeployment {
address: program_id,
path: None,
Expand Down Expand Up @@ -916,7 +919,7 @@ 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, &program_id.to_string())?;
}

// Build the test suite.
Expand Down Expand Up @@ -1039,16 +1042,17 @@ fn new(
)?;
}

let program_id = rust_template::get_or_create_program_id(&name);
if solidity {
solidity_template::create_program(&name)?;
} else {
rust_template::create_program(&name, template)?;
rust_template::create_program(&name, template, &program_id.to_string())?;
}

programs.insert(
name.clone(),
ProgramDeployment {
address: rust_template::get_or_create_program_id(&name),
address: program_id,
path: None,
idl: None,
},
Expand Down
103 changes: 95 additions & 8 deletions cli/src/rust_template.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::VERSION;
use crate::{config::ProgramWorkspace, create_files, Files};
use crate::{config::ProgramWorkspace, create_files, Files, VERSION};
use anchor_syn::idl::types::Idl;
use anyhow::Result;
use clap::{Parser, ValueEnum};
Expand All @@ -19,20 +18,39 @@ pub enum ProgramTemplate {
Single,
/// Program with multiple files for instructions, state...
Multiple,
/// Generate template for Rust unit-test
RustTest,
}
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved

/// 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, program_id: &str) -> Result<()> {
let program_path = Path::new("programs").join(name);
let tests = if template == ProgramTemplate::RustTest {
"tests"
} else {
""
};
let common_files = vec![
("Cargo.toml".into(), workspace_manifest().into()),
("Cargo.toml".into(), workspace_manifest(tests)),
(program_path.join("Cargo.toml"), cargo_toml(name)),
(program_path.join("Xargo.toml"), xargo_toml().into()),
];

let template_files = match template {
ProgramTemplate::Single => create_program_template_single(name, &program_path),
ProgramTemplate::Multiple => create_program_template_multiple(name, &program_path),
ProgramTemplate::RustTest => {
let mut files = create_program_template_single(name, &program_path);
let tests_path = Path::new("tests");
files.extend(vec![(
tests_path.join("Cargo.toml"),
tests_cargo_toml(name),
)]);
files.extend(create_program_template_rust_test(
name, tests_path, program_id,
));
files
}
};
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved

create_files(&[common_files, template_files].concat())
Expand Down Expand Up @@ -144,10 +162,61 @@ pub fn handler(ctx: Context<Initialize>) -> Result<()> {
]
}

const fn workspace_manifest() -> &'static str {
r#"[workspace]
/// Generate template for Rust unit-test
fn create_program_template_rust_test(name: &str, tests_path: &Path, program_id: &str) -> Files {
let src_path = tests_path.join("src");
vec![
(
src_path.join("lib.rs"),
r#"#[cfg(test)]
mod test_initialize;
"#
.into(),
),
(
src_path.join("test_initialize.rs"),
format!(
r#"use std::str::FromStr;

use anchor_client::{{
solana_sdk::{{
commitment_config::CommitmentConfig, pubkey::Pubkey, signature::read_keypair_file,
}},
Client, Cluster,
}};

#[test]
fn test_initialize() {{
let program_id = "{0}";
let anchor_wallet = std::env::var("ANCHOR_WALLET").expect("set ANCHOR_WALLET");
let payer = read_keypair_file(&anchor_wallet).expect("");

let client = Client::new_with_options(Cluster::Localnet, &payer, CommitmentConfig::confirmed());
let program_id = Pubkey::from_str(program_id).expect("parse program_id to Pubkey");
let program = client.program(program_id).expect("");

let tx = program
.request()
.accounts({1}::accounts::Initialize {{}})
.args({1}::instruction::Initialize {{}})
.send()
.expect("");

println!("Your transaction signature {{}}", tx);
}}
"#,
program_id, name,
),
),
]
}

fn workspace_manifest(tests: &str) -> String {
format!(
r#"[workspace]
members = [
"programs/*"
"programs/*",
"{}"
]
resolver = "2"

Expand All @@ -159,7 +228,9 @@ codegen-units = 1
opt-level = 3
incremental = false
codegen-units = 1
"#
"#,
tests
)
}

fn cargo_toml(name: &str) -> String {
Expand Down Expand Up @@ -606,3 +677,19 @@ anchor.workspace.{} = new anchor.Program({}, new PublicKey("{}"), provider);

Ok(eval_string)
}

pub fn tests_cargo_toml(name: &str) -> String {
format!(
r#"[package]
name = "tests"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[dependencies]
anchor-client = "{0}"
{1} = {{ version = "0.1.0", path = "../programs/{1}" }}
"#,
VERSION, name,
)
}
Loading