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

✨ Support for dynamic templates. #98

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions crates/client/src/templates/trdelnik-tests/fuzz_target.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use assert_matches::*;
use trdelnik_client::fuzzing::*;

const PROGRAM_NAME: &str = "###PROGRAM_NAME###";

#[derive(Arbitrary)]
pub struct FuzzData {
param1: u8,
Expand Down
6 changes: 4 additions & 2 deletions crates/client/src/templates/trdelnik-tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use fehler::throws;
use program_client;
use program_client::*;
use trdelnik_client::{anyhow::Result, *};
// @todo: do not forget to import your program crate (also in the ../Cargo.toml)

// @todo: create and deploy your fixture
#[throws]
Expand Down Expand Up @@ -41,5 +40,8 @@ impl Fixture {
self.client
.airdrop(self.client.payer().pubkey(), 5_000_000_000)
.await?;
self.client
.deploy_by_name(&self.program.clone(), "###PROGRAM_NAME###")
.await?;
}
}
22 changes: 15 additions & 7 deletions crates/client/src/test_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum Error {
Commander(#[from] CommanderError),
#[error("Cannot find the Anchor.toml file to locate the root folder")]
BadWorkspace,
#[error("The Anchor project does not contain any programs")]
NoProgramsFound,
}

pub struct TestGenerator;
Expand Down Expand Up @@ -123,7 +125,14 @@ impl TestGenerator {
env!("CARGO_MANIFEST_DIR"),
"/src/templates/trdelnik-tests/test.rs"
));
self.create_file(&test_path, TESTS_FILE_NAME, test_content)
let program_libs = self.get_program_lib_names(root).await?;
let program_name = if let Some(name) = program_libs.first() {
name
} else {
throw!(Error::NoProgramsFound)
};
let test_content = test_content.replace("###PROGRAM_NAME###", program_name);
self.create_file(&test_path, TESTS_FILE_NAME, &test_content)
.await?;

let cargo_toml_path = workspace_path.join(CARGO_TOML);
Expand Down Expand Up @@ -151,7 +160,7 @@ impl TestGenerator {
self.create_directory_all(&fuzzer_path, FUZZ_DIRECTORY)
.await?;

let libs = self.get_libs(root).await?;
let libs = self.get_program_lib_names(root).await?;

let fuzzer_test_path = fuzzer_path.join(FUZZ_TEST_FILE_NAME);
let fuzz_test_content = include_str!(concat!(
Expand All @@ -162,11 +171,11 @@ impl TestGenerator {

let fuzz_test_content = if let Some(lib) = libs.first() {
let use_entry = format!("use {}::entry;\n", lib);
let prog_name = format!("const PROGRAM_NAME: &str = \"{lib}\";\n");
let use_instructions = format!("use program_client::{}_instruction::*;\n", lib);
format!("{use_entry}{use_instructions}{prog_name}{fuzz_test_content}")
let template = format!("{use_entry}{use_instructions}{fuzz_test_content}");
template.replace("###PROGRAM_NAME###", lib)
} else {
fuzz_test_content
throw!(Error::NoProgramsFound)
};

self.create_file(&fuzzer_test_path, FUZZ_TEST_FILE_NAME, &fuzz_test_content)
Expand Down Expand Up @@ -358,7 +367,7 @@ impl TestGenerator {
}

/// Scans `programs` directory and returns a list of names of libraries
async fn get_libs(&self, root: &Path) -> Result<Vec<String>, Error> {
async fn get_program_lib_names(&self, root: &Path) -> Result<Vec<String>, Error> {
let programs = root.join("programs");
if !programs.exists() {
println!("Programs folder does not exist.");
Expand All @@ -372,7 +381,6 @@ impl TestGenerator {
if file.path().is_dir() {
let path = file.path().join(CARGO_TOML);
if path.exists() {
// let dir = file_name.to_str().unwrap();
let content: Value = fs::read_to_string(&path).await?.parse()?;
let name = content
.get("lib")
Expand Down
3 changes: 2 additions & 1 deletion examples/escrow/trdelnik-tests/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anchor_spl::token;
use fehler::throws;
use program_client::escrow_instruction;
use program_client::*;
// use program_client::escrow_instruction;
use trdelnik_client::{anyhow::Result, *};

#[throws]
Expand Down
Loading