Skip to content

Commit

Permalink
✨ Fix case conversion for program packages
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Nov 14, 2024
1 parent 5d39766 commit 8cd1a0e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
11 changes: 3 additions & 8 deletions crates/client/src/anchor_idl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anchor_lang_idl_spec::Idl;
use cargo_metadata::Package;
use convert_case::{Case, Casing};

use std::error::Error;
use std::fs::{self, File};
Expand All @@ -13,13 +12,9 @@ pub fn load_idls(
) -> Result<Vec<Idl>, Box<dyn Error>> {
let mut idls = Vec::new();

let package_names: Vec<String> = program_packages
let mut package_names = program_packages
.iter()
.map(|package| {
let name = &package.name;
name.to_case(Case::Snake)
})
.collect();
.map(|package| package.name.replace("-", "_"));

// Read the directory and iterate over each entry
for entry in fs::read_dir(dir_path)? {
Expand All @@ -34,7 +29,7 @@ pub fn load_idls(
let package_name = idl_name_str.trim_end_matches(".json");

// Check if the package name is in the list of known packages
if package_names.iter().any(|name| name == package_name) {
if package_names.any(|package| package == package_name) {
// Open the file in read-only mode
let mut file = File::open(&path)?;

Expand Down
11 changes: 5 additions & 6 deletions crates/client/src/source_code_generators/test_fuzz_generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anchor_lang_idl_spec::Idl;
use convert_case::{Case, Casing};
use quote::{format_ident, ToTokens};
use syn::parse_quote;

Expand Down Expand Up @@ -63,7 +62,7 @@ fn get_program_names(idl_instructions: &[Idl]) -> Vec<syn::Stmt> {
.iter()
.map(|idl| {
let program_name = &idl.metadata.name;
let program_name_upper = idl.metadata.name.to_case(Case::UpperSnake);
let program_name_upper = &idl.metadata.name;
let program_name_ident = format_ident!("PROGRAM_NAME_{}", program_name_upper);

parse_quote!(const #program_name_ident: &str = #program_name;)
Expand All @@ -75,8 +74,8 @@ fn get_program_imports(idl_instructions: &[Idl]) -> Vec<syn::ItemUse> {
idl_instructions
.iter()
.flat_map(|idl| {
let program_name = idl.metadata.name.to_case(Case::Snake);
let program_name_upper = idl.metadata.name.to_case(Case::UpperSnake);
let program_name = &idl.metadata.name;
let program_name_upper = &idl.metadata.name;
let program_name_ident = format_ident!("{}", program_name);
let program_entry_ident = format_ident!("entry_{}", program_name);
let program_id_name_ident = format_ident!("PROGRAM_ID_{}", program_name_upper);
Expand All @@ -97,8 +96,8 @@ fn get_fuzzing_programs(idl_instructions: &[Idl]) -> (Vec<syn::Stmt>, syn::ExprA
let fuzzing_programs: Vec<syn::Stmt> = idl_instructions
.iter()
.map(|idl| {
let program_name = idl.metadata.name.to_case(Case::Snake);
let program_name_upper = idl.metadata.name.to_case(Case::UpperSnake);
let program_name = &idl.metadata.name;
let program_name_upper = &idl.metadata.name;
let fuzzing_program_name_ident = format_ident!("fuzzing_program_{}", program_name);
let program_id_name_ident = format_ident!("PROGRAM_ID_{}", program_name_upper);
let program_name_ident = format_ident!("PROGRAM_NAME_{}", program_name_upper);
Expand Down
16 changes: 8 additions & 8 deletions crates/client/tests/expected_source_codes/expected_test_fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use trident_client::fuzzing::*;
mod fuzz_instructions;
use dummy_2::entry as entry_dummy_2;
use dummy_2::ID as PROGRAM_ID_DUMMY_2;
use dummy_2::ID as PROGRAM_ID_dummy_2;
use dummy_example::entry as entry_dummy_example;
use dummy_example::ID as PROGRAM_ID_DUMMY_EXAMPLE;
use dummy_example::ID as PROGRAM_ID_dummy_example;
use fuzz_instructions::FuzzInstruction;
const PROGRAM_NAME_DUMMY_2: &str = "dummy_2";
const PROGRAM_NAME_DUMMY_EXAMPLE: &str = "dummy_example";
const PROGRAM_NAME_dummy_2: &str = "dummy_2";
const PROGRAM_NAME_dummy_example: &str = "dummy_example";
struct InstructionsSequence;
/// Define instruction sequences for invocation.
/// `pre` runs at the start, `middle` in the middle, and `post` at the end.
Expand All @@ -27,13 +27,13 @@ fn fuzz_iteration<T: FuzzTestExecutor<U> + std::fmt::Display, U>(
config: &Config,
) {
let fuzzing_program_dummy_2 = FuzzingProgram::new(
PROGRAM_NAME_DUMMY_2,
&PROGRAM_ID_DUMMY_2,
PROGRAM_NAME_dummy_2,
&PROGRAM_ID_dummy_2,
processor!(convert_entry!(entry_dummy_2)),
);
let fuzzing_program_dummy_example = FuzzingProgram::new(
PROGRAM_NAME_DUMMY_EXAMPLE,
&PROGRAM_ID_DUMMY_EXAMPLE,
PROGRAM_NAME_dummy_example,
&PROGRAM_ID_dummy_example,
processor!(convert_entry!(entry_dummy_example)),
);
let mut client = ProgramTestClientBlocking::new(
Expand Down

0 comments on commit 8cd1a0e

Please sign in to comment.