Skip to content

Commit 733ec30

Browse files
cli: Embed workspace programs into genesis when testing
1 parent b243486 commit 733ec30

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ incremented for features.
1111

1212
## [Unreleased]
1313

14+
* cli: Embed workspace programs into local validator genesis when testing.
15+
1416
## [0.2.0] - 2021-02-08
1517

1618
### Features

cli/src/main.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,22 @@ fn test(skip_deploy: bool) -> Result<()> {
587587
with_workspace(|cfg, _path, _cargo| {
588588
// Bootup validator, if needed.
589589
let validator_handle = match cfg.cluster.url() {
590-
"http://127.0.0.1:8899" => Some(start_test_validator()?),
591-
_ => None,
590+
"http://127.0.0.1:8899" => {
591+
build(None)?;
592+
let flags = match skip_deploy {
593+
true => None,
594+
false => Some(genesis_flags()?),
595+
};
596+
Some(start_test_validator(flags)?)
597+
}
598+
_ => {
599+
if !skip_deploy {
600+
deploy(None, None)?;
601+
}
602+
None
603+
}
592604
};
593605

594-
// Deploy programs.
595-
if !skip_deploy {
596-
deploy(None, None)?;
597-
}
598-
599606
// Run the tests.
600607
if let Err(e) = std::process::Command::new("mocha")
601608
.arg("-t")
@@ -619,12 +626,38 @@ fn test(skip_deploy: bool) -> Result<()> {
619626
})
620627
}
621628

629+
// Returns the solana-test-validator flags to embed the workspace programs
630+
// in the genesis block. This allows us to run tests without every deploying.
631+
fn genesis_flags() -> Result<Vec<String>> {
632+
let mut flags = Vec::new();
633+
for mut program in read_all_programs()? {
634+
let binary_path = program.binary_path().display().to_string();
635+
636+
let kp = Keypair::generate(&mut OsRng);
637+
let address = kp.pubkey().to_string();
638+
639+
flags.push("--bpf-program".to_string());
640+
flags.push(address.clone());
641+
flags.push(binary_path);
642+
643+
// Add program address to the IDL.
644+
program.idl.metadata = Some(serde_json::to_value(IdlTestMetadata { address })?);
645+
646+
// Persist it.
647+
let idl_out = PathBuf::from("target/idl")
648+
.join(&program.idl.name)
649+
.with_extension("json");
650+
write_idl(&program.idl, OutFile::File(idl_out))?;
651+
}
652+
Ok(flags)
653+
}
654+
622655
#[derive(Debug, Serialize, Deserialize)]
623656
pub struct IdlTestMetadata {
624657
address: String,
625658
}
626659

627-
fn start_test_validator() -> Result<Child> {
660+
fn start_test_validator(flags: Option<Vec<String>>) -> Result<Child> {
628661
fs::create_dir_all(".anchor")?;
629662
let test_ledger_filename = ".anchor/test-ledger";
630663
let test_ledger_log_filename = ".anchor/test-ledger-log.txt";
@@ -642,6 +675,7 @@ fn start_test_validator() -> Result<Child> {
642675
let validator_handle = std::process::Command::new("solana-test-validator")
643676
.arg("--ledger")
644677
.arg(test_ledger_filename)
678+
.args(flags.unwrap_or(Vec::new()))
645679
.stdout(Stdio::from(test_validator_stdout))
646680
.stderr(Stdio::from(test_validator_stderr))
647681
.spawn()

0 commit comments

Comments
 (0)