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

✨ Fix case conversion for program packages #224

Merged
merged 1 commit into from
Nov 14, 2024
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
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
Loading