Skip to content

Commit

Permalink
governance: Remove dependency on ProcessInstructionWithContext (solan…
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque authored Nov 17, 2021
1 parent 7764a9d commit d5581db
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 56 deletions.
29 changes: 14 additions & 15 deletions governance/chat/program/tests/program_test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use solana_program::{program_error::ProgramError, pubkey::Pubkey};
use solana_program_test::processor;
use solana_program_test::{processor, ProgramTest};

use solana_sdk::{signature::Keypair, signer::Signer};
use spl_governance::{
Expand All @@ -21,7 +21,7 @@ use spl_governance_chat::{
processor::process_instruction,
state::{ChatMessage, GovernanceChatAccountType, MessageBody},
};
use spl_governance_test_sdk::{ProgramTestBench, TestBenchProgram};
use spl_governance_test_sdk::ProgramTestBench;

use crate::program_test::cookies::{ChatMessageCookie, ProposalCookie};

Expand All @@ -37,23 +37,22 @@ pub struct GovernanceChatProgramTest {

impl GovernanceChatProgramTest {
pub async fn start_new() -> Self {
let mut program_test = ProgramTest::default();
let program_id = Pubkey::from_str("GovernanceChat11111111111111111111111111111").unwrap();

let chat_program = TestBenchProgram {
program_name: "spl_governance_chat",
program_id: program_id,
process_instruction: processor!(process_instruction),
};
program_test.add_program(
"spl_governance_chat",
program_id,
processor!(process_instruction),
);

let governance_program_id =
Pubkey::from_str("Governance111111111111111111111111111111111").unwrap();
let governance_program = TestBenchProgram {
program_name: "spl_governance",
program_id: governance_program_id,
process_instruction: processor!(spl_governance::processor::process_instruction),
};

let bench = ProgramTestBench::start_new(&[chat_program, governance_program]).await;
program_test.add_program(
"spl_governance",
governance_program_id,
processor!(spl_governance::processor::process_instruction),
);
let bench = ProgramTestBench::start_new(program_test).await;

Self {
bench,
Expand Down
30 changes: 12 additions & 18 deletions governance/program/tests/program_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ use crate::program_test::cookies::{

use spl_governance_test_sdk::{
tools::{clone_keypair, NopOverride},
ProgramTestBench, TestBenchProgram,
ProgramTestBench,
};

use self::{
Expand Down Expand Up @@ -105,35 +105,29 @@ impl GovernanceProgramTest {

#[allow(dead_code)]
async fn start_impl(use_voter_weight_addin: bool) -> Self {
let mut programs = vec![];
let mut program_test = ProgramTest::default();

let program_id = Pubkey::from_str("Governance111111111111111111111111111111111").unwrap();

let program = TestBenchProgram {
program_name: "spl_governance",
program_test.add_program(
"spl_governance",
program_id,
process_instruction: processor!(process_instruction),
};

programs.push(program);
processor!(process_instruction),
);

let voter_weight_addin_id = if use_voter_weight_addin {
let voter_weight_addin_id =
Pubkey::from_str("VoterWeight11111111111111111111111111111111").unwrap();

let vote_weight_addin = TestBenchProgram {
program_name: "spl_governance_voter_weight_addin",
program_id: voter_weight_addin_id,
process_instruction: None,
};

programs.push(vote_weight_addin);
program_test.add_program(
"spl_governance_voter_weight_addin",
voter_weight_addin_id,
None,
);
Some(voter_weight_addin_id)
} else {
None
};

let bench = ProgramTestBench::start_new(&programs).await;
let bench = ProgramTestBench::start_new(program_test).await;

Self {
bench,
Expand Down
27 changes: 4 additions & 23 deletions governance/test-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use solana_program::{
system_instruction, sysvar,
};
use solana_program_test::{ProgramTest, ProgramTestContext};
use solana_sdk::{
account::Account, process_instruction::ProcessInstructionWithContext, signature::Keypair,
signer::Signer, transaction::Transaction,
};
use solana_sdk::{account::Account, signature::Keypair, signer::Signer, transaction::Transaction};

use bincode::deserialize;

Expand All @@ -22,14 +19,6 @@ use crate::tools::map_transaction_error;
pub mod cookies;
pub mod tools;

/// Specification of a program which is loaded into the test bench
#[derive(Clone)]
pub struct TestBenchProgram<'a> {
pub program_name: &'a str,
pub program_id: Pubkey,
pub process_instruction: Option<ProcessInstructionWithContext>,
}

/// Program's test bench which captures test context, rent and payer and common utility functions
pub struct ProgramTestBench {
pub context: ProgramTestContext,
Expand All @@ -39,17 +28,9 @@ pub struct ProgramTestBench {
}

impl ProgramTestBench {
pub async fn start_new(programs: &[TestBenchProgram<'_>]) -> Self {
let mut program_test = ProgramTest::default();

for program in programs {
program_test.add_program(
program.program_name,
program.program_id,
program.process_instruction,
)
}

/// Create new bench given a ProgramTest instance populated with all of the
/// desired programs.
pub async fn start_new(program_test: ProgramTest) -> Self {
let mut context = program_test.start_with_context().await;
let rent = context.banks_client.get_rent().await.unwrap();

Expand Down

0 comments on commit d5581db

Please sign in to comment.