diff --git a/.travis.yml b/.travis.yml index ba8a8e39..b20b3a1e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ dist: bionic language: rust rust: - - stable + - nightly cache: cargo env: - NODE_VERSION="14.7.0" @@ -44,6 +44,7 @@ jobs: - <<: *examples name: Runs the examples script: + - pushd client/example && ./run-test.sh && popd - pushd examples/sysvars && anchor test && popd - pushd examples/composite && anchor test && popd - pushd examples/errors && anchor test && popd diff --git a/client/example/Cargo.toml b/client/example/Cargo.toml index 87a7b1d4..353b8bc6 100644 --- a/client/example/Cargo.toml +++ b/client/example/Cargo.toml @@ -7,9 +7,10 @@ edition = "2018" [workspace] [dependencies] -anchor-client = { path = "../" } +anchor-client = { git = "https://github.com/project-serum/anchor" } basic-2 = { path = "../../examples/tutorial/basic-2/programs/basic-2", features = ["no-entrypoint"] } composite = { path = "../../examples/composite/programs/composite", features = ["no-entrypoint"] } shellexpand = "2.1.0" anyhow = "1.0.32" -rand = "0.7.3" \ No newline at end of file +rand = "0.7.3" +clap = "3.0.0-beta.2" diff --git a/client/example/run-test.sh b/client/example/run-test.sh new file mode 100755 index 00000000..d26864e1 --- /dev/null +++ b/client/example/run-test.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +################################################################################ +# +# A script to run the example as an integration test. It starts up a localnet +# and executes the current directory's rust binary. +# +# Usage: +# +# ./run.sh +# +# Run this script from within the `example/` directory in which it is located. +# The anchor cli must be installed. +# +# cargo install --git https://github.com/project-serum/anchor anchor-cli --locked +# +################################################################################ + +set -euox pipefail + +main() { + # + # Bootup validator. + # + solana-test-validator > test-validator.log & + sleep 5 + + # + # Deploy programs. + # + pushd ../../examples/composite/ + anchor deploy + local composite_pid=$(cat target/idl/composite.json | jq -r .metadata.address) + popd + pushd ../../examples/tutorial/basic-2/ + anchor deploy + local basic_2_pid=$(cat target/idl/basic_2.json | jq -r .metadata.address) + popd + + # + # Run Test. + # + cargo run -- --composite-pid $composite_pid --basic-2-pid $basic_2_pid +} + +cleanup() { + pkill -P $$ || true + wait || true +} + +trap_add() { + trap_add_cmd=$1; shift || fatal "${FUNCNAME} usage error" + for trap_add_name in "$@"; do + trap -- "$( + extract_trap_cmd() { printf '%s\n' "${3:-}"; } + eval "extract_trap_cmd $(trap -p "${trap_add_name}")" + printf '%s\n' "${trap_add_cmd}" + )" "${trap_add_name}" \ + || fatal "unable to add to trap ${trap_add_name}" + done +} + +declare -f -t trap_add +trap_add 'cleanup' EXIT +main diff --git a/client/example/src/main.rs b/client/example/src/main.rs index d4ebc87e..cb4331dd 100644 --- a/client/example/src/main.rs +++ b/client/example/src/main.rs @@ -1,4 +1,5 @@ use anchor_client::solana_sdk::commitment_config::CommitmentConfig; +use anchor_client::solana_sdk::pubkey::Pubkey; use anchor_client::solana_sdk::signature::read_keypair_file; use anchor_client::solana_sdk::signature::{Keypair, Signer}; use anchor_client::solana_sdk::system_instruction; @@ -10,24 +11,36 @@ use basic_2::accounts as basic_2_accounts; use basic_2::instruction as basic_2_instruction; use basic_2::Counter; // The `accounts` and `instructions` modules are generated by the framework. +use clap::Clap; use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize}; use composite::instruction as composite_instruction; use composite::{DummyA, DummyB}; use rand::rngs::OsRng; +#[derive(Clap)] +pub struct Opts { + #[clap(long)] + composite_pid: Pubkey, + #[clap(long)] + basic_2_pid: Pubkey, +} + +// This example assumes a local validator is running with the programs +// deployed at the addresses given by the CLI args. fn main() -> Result<()> { + let opts = Opts::parse(); + // Wallet and cluster params. let payer = read_keypair_file(&shellexpand::tilde("~/.config/solana/id.json")) .expect("Example requires a keypair file"); let url = "http://localhost:8899"; - let opts = CommitmentConfig::recent(); // Client. - let client = Client::new_with_options(url, payer, opts); + let client = Client::new_with_options(url, payer, CommitmentConfig::recent()); // Run tests. - composite(&client)?; - basic_2(&client)?; + composite(&client, opts.composite_pid)?; + basic_2(&client, opts.basic_2_pid)?; // Success. Ok(()) @@ -36,12 +49,7 @@ fn main() -> Result<()> { // Runs a client for examples/tutorial/composite. // // Make sure to run a localnet with the program deploy to run this example. -fn composite(client: &Client) -> Result<()> { - // Deployed program to execute. - let pid = "CD4y4hpiqB9N3vo2bAmZofsZuFmCnScqDPXejZSTeCV9" - .parse() - .unwrap(); - +fn composite(client: &Client, pid: Pubkey) -> Result<()> { // Program client. let program = client.program(pid); @@ -113,13 +121,8 @@ fn composite(client: &Client) -> Result<()> { // Runs a client for examples/tutorial/basic-2. // // Make sure to run a localnet with the program deploy to run this example. -fn basic_2(client: &Client) -> Result<()> { - // Deployed program to execute. - let program_id = "DXfgYBD7A3DvFDJoCTcS81EnyxfwXyeYadH5VdKMhVEx" - .parse() - .unwrap(); - - let program = client.program(program_id); +fn basic_2(client: &Client, pid: Pubkey) -> Result<()> { + let program = client.program(pid); // `Create` parameters. let counter = Keypair::generate(&mut OsRng); @@ -133,7 +136,7 @@ fn basic_2(client: &Client) -> Result<()> { &counter.pubkey(), program.rpc().get_minimum_balance_for_rent_exemption(500)?, 500, - &program_id, + &pid, )) .signer(&counter) .accounts(basic_2_accounts::Create {