From bd707cad2c47a0a2cdd0c0fe1677f2460717d109 Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Thu, 14 Sep 2023 16:00:57 -0400 Subject: [PATCH] feat: run cargo tests from go First example of using `cargo test` from the go code. --- .../soroban-test/tests/it/contract_sandbox.rs | 31 +++++++++++++------ cmd/crates/soroban-test/tests/it/util.rs | 1 + cmd/soroban-rpc/internal/test/cli_test.go | 16 ++++++++++ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/cmd/crates/soroban-test/tests/it/contract_sandbox.rs b/cmd/crates/soroban-test/tests/it/contract_sandbox.rs index 931d5e9e53..a5c5ede96a 100644 --- a/cmd/crates/soroban-test/tests/it/contract_sandbox.rs +++ b/cmd/crates/soroban-test/tests/it/contract_sandbox.rs @@ -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") @@ -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] @@ -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() diff --git a/cmd/crates/soroban-test/tests/it/util.rs b/cmd/crates/soroban-test/tests/it/util.rs index ecd95ef8a8..17df21e70b 100644 --- a/cmd/crates/soroban-test/tests/it/util.rs +++ b/cmd/crates/soroban-test/tests/it/util.rs @@ -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"; diff --git a/cmd/soroban-rpc/internal/test/cli_test.go b/cmd/soroban-rpc/internal/test/cli_test.go index e4b5de52a3..89e7050462 100644 --- a/cmd/soroban-rpc/internal/test/cli_test.go +++ b/cmd/soroban-rpc/internal/test/cli_test.go @@ -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)