Skip to content

Commit

Permalink
feat: run cargo tests from go
Browse files Browse the repository at this point in the history
First example of using `cargo test` from the go code.
  • Loading branch information
willemneal committed Sep 14, 2023
1 parent 0356a78 commit bd707ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
31 changes: 21 additions & 10 deletions cmd/crates/soroban-test/tests/it/contract_sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ use std::path::PathBuf;

use crate::util::{
add_test_seed, DEFAULT_PUB_KEY, DEFAULT_PUB_KEY_1, DEFAULT_SECRET_KEY, DEFAULT_SEED_PHRASE,
HELLO_WORLD,
HELLO_WORLD, TEST_SALT,
};

#[test]
fn install_wasm_then_deploy_contract() {
let hash = HELLO_WORLD.hash().unwrap();
let sandbox = TestEnv::default();
assert_eq!(deploy_hello(&sandbox), TEST_CONTRACT_ID);
}

const TEST_CONTRACT_ID: &str = "CBVTIVBYWAO2HNPNGKDCZW4OZYYESTKNGD7IPRTDGQSFJS4QBDQQJX3T";

fn deploy_hello(sandbox: &TestEnv) -> String {
let hash = HELLO_WORLD.hash().unwrap();
sandbox
.new_assert_cmd("contract")
.arg("install")
Expand All @@ -23,15 +29,18 @@ fn install_wasm_then_deploy_contract() {
.success()
.stdout(format!("{hash}\n"));

sandbox
.new_assert_cmd("contract")
.arg("deploy")
.arg("--wasm-hash")
.arg(&format!("{hash}"))
.arg("--id=1")
.assert()
let mut cmd: &mut assert_cmd::Command = &mut sandbox.new_assert_cmd("contract");

cmd = cmd.arg("deploy").arg("--wasm-hash").arg(&format!("{hash}"));
if std::env::var("SOROBAN_RPC_URL").is_err() {
cmd = cmd.arg("--id").arg(TEST_CONTRACT_ID);
} else {
cmd = cmd.arg("--salt").arg(TEST_SALT);
}
cmd.assert()
.success()
.stdout("CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM\n");
.stdout(format!("{TEST_CONTRACT_ID}\n"));
TEST_CONTRACT_ID.to_string()
}

#[test]
Expand All @@ -53,6 +62,8 @@ fn invoke_hello_world_with_deploy_first() {
let res = sandbox
.new_assert_cmd("contract")
.arg("deploy")
.arg("--salt")
.arg(TEST_SALT)
.arg("--wasm")
.arg(HELLO_WORLD.path())
.assert()
Expand Down
1 change: 1 addition & 0 deletions cmd/crates/soroban-test/tests/it/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ pub const DEFAULT_PUB_KEY: &str = "GDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2
pub const DEFAULT_SECRET_KEY: &str = "SC36BWNUOCZAO7DMEJNNKFV6BOTPJP7IG5PSHLUOLT6DZFRU3D3XGIXW";

pub const DEFAULT_PUB_KEY_1: &str = "GCKZUJVUNEFGD4HLFBUNVYM2QY2P5WQQZMGRA3DDL4HYVT5MW5KG3ODV";
pub const TEST_SALT: &str = "f55ff16f66f43360266b95db6f8fec01d76031054306ae4a4b380598f6cfd114";
16 changes: 16 additions & 0 deletions cmd/soroban-rpc/internal/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ import (
"gotest.tools/v3/icmd"
)

func cargoTest(name string) *icmd.Result {
c := icmd.Command("cargo", "test", "--package", "soroban-test", "--test", "it", "--", name, "--exact", "--nocapture")
c.Env = append(os.Environ(),
fmt.Sprintf("SOROBAN_RPC_URL=http://localhost:%d/", sorobanRPCPort),
fmt.Sprintf("SOROBAN_NETWORK_PASSPHRASE=%s", StandaloneNetworkPassphrase),
)
return icmd.RunCmd(c)
}

func TestCLIWithCargo(t *testing.T) {
NewCLITest(t)
res := cargoTest("contract_sandbox::invoke_hello_world_with_deploy_first")
stdout, stderr := res.Stdout(), res.Stderr()
println(stdout, stderr)
}

func TestCLIContractInstall(t *testing.T) {
NewCLITest(t)
output := runSuccessfulCLICmd(t, "contract install --wasm "+helloWorldContractPath)
Expand Down

0 comments on commit bd707ca

Please sign in to comment.