Skip to content

Commit

Permalink
Merge pull request #98 from Ackee-Blockchain/feat/dyn-templates
Browse files Browse the repository at this point in the history
✨ Support for dynamic templates.
  • Loading branch information
lukacan committed Sep 18, 2023
2 parents 8490568 + 602c4ac commit b33642a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
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 @@ -35,6 +35,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 @@ -127,7 +129,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 @@ -155,7 +164,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 @@ -166,11 +175,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 @@ -385,7 +394,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 @@ -399,7 +408,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

0 comments on commit b33642a

Please sign in to comment.