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 all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- ts: Add IdlSeed Type for IDL PDA seeds ([#2752](https://github.com/coral-xyz/anchor/pull/2752)).
- cli: `idl close` accepts optional `--idl-address` parameter ([#2760](https://github.com/coral-xyz/anchor/pull/2760)).
- cli: Add support for simple wildcard patterns in Anchor.toml's `workspace.members` and `workspace.exclude`. ([#2785](https://github.com/coral-xyz/anchor/pull/2785)).
- cli: Add new `test-template` option in `init` command ([#2680](https://github.com/coral-xyz/anchor/issues/2680)).
- cli: `anchor test` is able to run multiple commands ([#2799](https://github.com/coral-xyz/anchor/pull/2799)).

### Fixes
Expand Down
103 changes: 47 additions & 56 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use heck::{ToKebabCase, ToSnakeCase};
use regex::{Regex, RegexBuilder};
use reqwest::blocking::multipart::{Form, Part};
use reqwest::blocking::Client;
use rust_template::ProgramTemplate;
use rust_template::{ProgramTemplate, TestTemplate};
use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value as JsonValue};
Expand Down Expand Up @@ -83,12 +83,12 @@ pub enum Command {
/// Don't initialize git
#[clap(long)]
no_git: bool,
/// Use `jest` instead of `mocha` for tests
#[clap(long)]
jest: bool,
/// Rust program template to use
#[clap(value_enum, short, long, default_value = "single")]
template: ProgramTemplate,
/// Test template to use
#[clap(value_enum, long, default_value = "mocha")]
test_template: TestTemplate,
/// Initialize even if there are files
#[clap(long, action)]
force: bool,
Expand Down Expand Up @@ -651,17 +651,17 @@ fn process_command(opts: Opts) -> Result<()> {
javascript,
solidity,
no_git,
jest,
template,
test_template,
force,
} => init(
&opts.cfg_override,
name,
javascript,
solidity,
no_git,
jest,
template,
test_template,
force,
),
Command::New {
Expand Down Expand Up @@ -824,8 +824,8 @@ fn init(
javascript: bool,
solidity: bool,
no_git: bool,
jest: bool,
template: ProgramTemplate,
test_template: TestTemplate,
force: bool,
) -> Result<()> {
if !force && Config::discover(cfg_override)?.is_some() {
Expand Down Expand Up @@ -861,27 +861,9 @@ fn init(
fs::create_dir_all("app")?;

let mut cfg = Config::default();
if jest {
cfg.scripts.insert(
"test".to_owned(),
if javascript {
"yarn run jest"
} else {
"yarn run jest --preset ts-jest"
}
.to_owned(),
);
} else {
cfg.scripts.insert(
"test".to_owned(),
if javascript {
"yarn run mocha -t 1000000 tests/"
} else {
"yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"
}
.to_owned(),
);
}
let test_script = test_template.get_test_script(javascript);
cfg.scripts
.insert("test".to_owned(), test_script.to_owned());

let mut localnet = BTreeMap::new();
let program_id = rust_template::get_or_create_program_id(&rust_name);
Expand Down Expand Up @@ -919,32 +901,15 @@ fn init(
rust_template::create_program(&project_name, template)?;
}

// Build the test suite.
fs::create_dir_all("tests")?;
// Build the migrations directory.
fs::create_dir_all("migrations")?;

let jest = TestTemplate::Jest == test_template;
if javascript {
// Build javascript config
let mut package_json = File::create("package.json")?;
package_json.write_all(rust_template::package_json(jest).as_bytes())?;

if jest {
let mut test = File::create(format!("tests/{}.test.js", &project_name))?;
if solidity {
test.write_all(solidity_template::jest(&project_name).as_bytes())?;
} else {
test.write_all(rust_template::jest(&project_name).as_bytes())?;
}
} else {
let mut test = File::create(format!("tests/{}.js", &project_name))?;
if solidity {
test.write_all(solidity_template::mocha(&project_name).as_bytes())?;
} else {
test.write_all(rust_template::mocha(&project_name).as_bytes())?;
}
}

let mut deploy = File::create("migrations/deploy.js")?;

deploy.write_all(rust_template::deploy_script().as_bytes())?;
Expand All @@ -958,15 +923,15 @@ fn init(

let mut deploy = File::create("migrations/deploy.ts")?;
deploy.write_all(rust_template::ts_deploy_script().as_bytes())?;

let mut mocha = File::create(format!("tests/{}.ts", &project_name))?;
if solidity {
mocha.write_all(solidity_template::ts_mocha(&project_name).as_bytes())?;
} else {
mocha.write_all(rust_template::ts_mocha(&project_name).as_bytes())?;
}
}

test_template.create_test_files(
&project_name,
javascript,
solidity,
&program_id.to_string(),
)?;

let yarn_result = install_node_modules("yarn")?;
if !yarn_result.status.success() {
println!("Failed yarn install will attempt to npm install");
Expand Down Expand Up @@ -1093,6 +1058,32 @@ pub fn create_files(files: &Files) -> Result<()> {
Ok(())
}

/// Override or create files from the given (path, content) tuple array.
///
/// # Example
///
/// ```ignore
/// override_or_create_files(vec![("programs/my_program/src/lib.rs".into(), "// Content".into())])?;
/// ```
pub fn override_or_create_files(files: &Files) -> Result<()> {
for (path, content) in files {
let path = Path::new(path);
if path.exists() {
let mut f = fs::OpenOptions::new()
.write(true)
.truncate(true)
.open(path)?;
f.write_all(content.as_bytes())?;
f.flush()?;
} else {
fs::create_dir_all(path.parent().unwrap())?;
fs::write(path, content)?;
}
}

Ok(())
}

pub fn expand(
cfg_override: &ConfigOverride,
program_name: Option<String>,
Expand Down Expand Up @@ -4541,8 +4532,8 @@ mod tests {
true,
false,
false,
false,
ProgramTemplate::default(),
TestTemplate::default(),
false,
)
.unwrap();
Expand All @@ -4560,8 +4551,8 @@ mod tests {
true,
false,
false,
false,
ProgramTemplate::default(),
TestTemplate::default(),
false,
)
.unwrap();
Expand All @@ -4579,8 +4570,8 @@ mod tests {
true,
false,
false,
false,
ProgramTemplate::default(),
TestTemplate::default(),
false,
)
.unwrap();
Expand Down
Loading
Loading