-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add CLI tests #216
Open
willemneal
wants to merge
5
commits into
stellar:main
Choose a base branch
from
AhaLabs:feat/tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add CLI tests #216
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2de3f37
feat: add tests for all CLI commands shown in docs
willemneal 12336bb
Merge remote-tracking branch 'origin/main' into feat/tests
willemneal 73033d7
Merge branch 'main' into feat/tests
chadoh 4878efb
update soroban_token_spec.wasm
chadoh ba17ec1
fix: cargo fmt
willemneal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "cli-tests" | ||
version = "0.0.0" | ||
authors = ["Stellar Development Foundation <info@stellar.org>"] | ||
license = "Apache-2.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
doctest = false | ||
|
||
[dev-dependencies] | ||
predicates = "2.1.5" | ||
soroban-test = "0.7.1" | ||
soroban-cli = "0.7.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Test CLI commands shown in the docs | ||
=================================== | ||
|
||
This directory contains tests for all CLI commands currently shown in | ||
https://soroban.stellar.org/docs/category/how-to-guides. | ||
|
||
Ideally, the docs themselves would have executable tests on all of their CLI | ||
examples. Perhaps in the future this can be facilitated by co-locating docs and | ||
examples in the same repo with git submodules. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// empty crate to collect all workspace tests into one binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//! Tests CLI commands from https://soroban.stellar.org/docs/how-to-guides/auth | ||
|
||
use soroban_cli::commands::config::identity::address; | ||
use soroban_test::{TestEnv, Wasm}; | ||
|
||
const WASM: &Wasm = &Wasm::Release("soroban_auth_contract"); | ||
|
||
const INCREMENT_1: &str = "2"; | ||
const INCREMENT_2: &str = "5"; | ||
|
||
fn account_pk(workspace: &TestEnv, hd_path: u8) -> String { | ||
workspace | ||
.cmd_arr::<address::Cmd>(&["--hd-path", &hd_path.to_string()]) | ||
.public_key() | ||
.unwrap() | ||
.to_string() | ||
} | ||
|
||
#[test] | ||
fn invoke_and_read() { | ||
TestEnv::with_default(|workspace| { | ||
let account_0_pk = account_pk(workspace, 0); | ||
let account_1_pk = account_pk(workspace, 1); | ||
|
||
assert_eq!( | ||
format!("{INCREMENT_1}"), | ||
workspace | ||
.invoke(&[ | ||
"--wasm", | ||
&WASM.path().to_string_lossy(), | ||
"--id", | ||
"1", | ||
"--", | ||
"increment", | ||
"--user", | ||
&account_0_pk, | ||
"--value", | ||
&INCREMENT_1, | ||
]) | ||
.unwrap(), | ||
); | ||
assert_eq!( | ||
format!("{INCREMENT_2}"), | ||
workspace | ||
.invoke(&[ | ||
"--hd-path", | ||
"1", | ||
"--wasm", | ||
&WASM.path().to_string_lossy(), | ||
"--id", | ||
"1", | ||
"--", | ||
"increment", | ||
"--user", | ||
&account_1_pk, | ||
"--value", | ||
&INCREMENT_2, | ||
]) | ||
.unwrap(), | ||
); | ||
|
||
// `read::Cmd`'s `run` returns `()` & dumps right to STDOUT; need to use assert_cmd | ||
workspace | ||
.new_assert_cmd("contract") | ||
.arg("read") | ||
.args(["--id", "1"]) | ||
.assert() | ||
.stderr("") | ||
.stdout(format!( | ||
r#""[""Counter"",""{account_0_pk}""]",{INCREMENT_1} | ||
"[""Counter"",""{account_1_pk}""]",{INCREMENT_2} | ||
"# | ||
)); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn invoke_auth_preview() { | ||
TestEnv::with_default(|workspace| { | ||
let account_0_pk = account_pk(workspace, 0); | ||
|
||
// `invoke::Cmd`'s `auth` option prints directly to STDERR; need to test with assert_cmd | ||
workspace.new_assert_cmd("contract") | ||
.arg("invoke") | ||
.arg("--auth") | ||
.arg("--wasm") | ||
.arg(&WASM.path()) | ||
.args(["--id", "1"]) | ||
.args(["--"]) | ||
.arg("increment") | ||
.args(["--user", &account_0_pk]) | ||
.args(["--value", &INCREMENT_1]) | ||
.assert() | ||
.stdout(format!("{INCREMENT_1}\n")) | ||
.stderr( | ||
r#"Contract auth: [{"address_with_nonce":null,"root_invocation":{"contract_id":"0000000000000000000000000000000000000000000000000000000000000001","function_name":"increment","args":[{"address":{"account":{"public_key_type_ed25519":"d18f0210ff6cc1f2dcf1301fbbd4c30ee11a075820684d471df89d0f1011ea28"}}},{"u32":"#.to_string() + &format!("{INCREMENT_1}") + r#"}],"sub_invocations":[]},"signature_args":[]}] | ||
"# | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//! Tests CLI commands from https://soroban.stellar.org/docs/how-to-guides/cross-contract-call | ||
|
||
use soroban_cli::commands::contract::{deploy, install}; | ||
use soroban_test::{TestEnv, Wasm}; | ||
|
||
const WASM_A: &Wasm = &Wasm::Release("soroban_cross_contract_a_contract"); | ||
const WASM_B: &Wasm = &Wasm::Release("soroban_cross_contract_b_contract"); | ||
|
||
#[test] | ||
fn invoke() { | ||
TestEnv::with_default(|workspace| { | ||
let hash_a = WASM_A.hash().unwrap(); | ||
workspace | ||
.cmd_arr::<install::Cmd>(&["--wasm", &WASM_A.path().to_string_lossy()]) | ||
.run_in_sandbox(WASM_A.bytes()) | ||
.unwrap(); | ||
workspace | ||
.cmd_arr::<deploy::Cmd>(&["--id", "a", "--wasm-hash", &format!("{hash_a}")]) | ||
.run_in_sandbox(hash_a) | ||
.unwrap(); | ||
|
||
let hash_b = WASM_B.hash().unwrap(); | ||
workspace | ||
.cmd_arr::<install::Cmd>(&["--wasm", &WASM_B.path().to_string_lossy()]) | ||
.run_in_sandbox(WASM_B.bytes()) | ||
.unwrap(); | ||
workspace | ||
.cmd_arr::<deploy::Cmd>(&["--id", "b", "--wasm-hash", &format!("{hash_b}")]) | ||
.run_in_sandbox(hash_b) | ||
.unwrap(); | ||
|
||
let res = workspace | ||
.invoke(&[ | ||
"--id", | ||
"b", | ||
"--", | ||
"add_with", | ||
"--contract_id", | ||
"a", | ||
"--x", | ||
"5", | ||
"--y", | ||
"7", | ||
]) | ||
.unwrap(); | ||
|
||
assert_eq!(res, "12"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! Tests CLI commands from https://soroban.stellar.org/docs/how-to-guides/custom-types | ||
|
||
use soroban_test::{TestEnv, Wasm}; | ||
|
||
const WASM: &Wasm = &Wasm::Release("soroban_custom_types_contract"); | ||
|
||
fn increment_by_5(workspace: &TestEnv) -> String { | ||
workspace | ||
.invoke(&[ | ||
"--wasm", | ||
&WASM.path().to_string_lossy(), | ||
"--id", | ||
"1", | ||
"--", | ||
"increment", | ||
"--incr", | ||
"5", | ||
]) | ||
.unwrap() | ||
} | ||
|
||
#[test] | ||
fn invoke() { | ||
TestEnv::with_default(|workspace| { | ||
assert_eq!("5", increment_by_5(workspace)); | ||
}); | ||
} | ||
|
||
const EXPECTED_READ: &str = r#"STATE,"{""count"":5,""last_incr"":5}" | ||
"#; | ||
|
||
#[test] | ||
fn read() { | ||
TestEnv::with_default(|workspace| { | ||
increment_by_5(workspace); | ||
|
||
// `read::Cmd`'s `run` returns `()` & dumps right to STDOUT; need to use assert_cmd | ||
workspace | ||
.new_assert_cmd("contract") | ||
.arg("read") | ||
.args(["--id", "1"]) | ||
.args(["--key", "STATE"]) | ||
.assert() | ||
.stderr("") | ||
.stdout(EXPECTED_READ); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! Tests CLI commands from https://soroban.stellar.org/docs/how-to-guides/deployer | ||
|
||
use soroban_cli::commands::contract::install; | ||
use soroban_test::{TestEnv, Wasm}; | ||
|
||
const WASM_DEPLOYER_TEST: &Wasm = &Wasm::Release("soroban_deployer_test_contract"); | ||
const WASM_DEPLOYER: &Wasm = &Wasm::Release("soroban_deployer_contract"); | ||
|
||
const INIT_VALUE: &str = "5"; | ||
|
||
#[test] | ||
fn invoke() { | ||
TestEnv::with_default(|workspace| { | ||
let hash = WASM_DEPLOYER_TEST.hash().unwrap(); | ||
|
||
// install (aka upload) the bytes | ||
let install_ret = workspace | ||
.cmd_arr::<install::Cmd>(&["--wasm", &WASM_DEPLOYER_TEST.path().to_string_lossy()]) | ||
.run_in_sandbox(WASM_DEPLOYER_TEST.bytes()) | ||
.unwrap(); | ||
assert_eq!(hash, install_ret); | ||
|
||
// now invoke a 2nd contract, which deploys an instance of the previously-uploaded bytes | ||
let new_contract_info_array = workspace | ||
.invoke(&[ | ||
"--wasm", | ||
&WASM_DEPLOYER.path().to_string_lossy(), | ||
"--id", | ||
"0", | ||
"--", | ||
"deploy", | ||
"--salt", | ||
"0000000000000000000000000000000000000000000000000000000000000000", | ||
"--wasm_hash", | ||
&hash.to_string(), | ||
"--init_fn", | ||
"init", | ||
"--init_args", | ||
&format!("[{{\"u32\":{INIT_VALUE}}}]"), | ||
]) | ||
.unwrap(); | ||
|
||
let contract_id = new_contract_info_array.split('"').nth(1).unwrap(); | ||
|
||
assert_eq!( | ||
format!("{INIT_VALUE}"), | ||
workspace | ||
.invoke(&["--id", contract_id, "--", "value",]) | ||
.unwrap() | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! Tests CLI commands from https://soroban.stellar.org/docs/how-to-guides/errors | ||
|
||
use soroban_cli::commands::contract::invoke; | ||
use soroban_test::{TestEnv, Wasm}; | ||
|
||
const WASM: &Wasm = &Wasm::Release("soroban_errors_contract"); | ||
|
||
const EXPECTED_ERROR_START: &str = r#"HostError | ||
Value: Status(ContractError(1)) | ||
"#; | ||
|
||
#[test] | ||
fn invoke() { | ||
TestEnv::with_default(|sandbox| { | ||
let increment = || { | ||
sandbox.invoke(&[ | ||
"--wasm", | ||
&WASM.path().to_string_lossy(), | ||
"--id", | ||
"1", | ||
"--", | ||
"increment", | ||
]) | ||
}; | ||
|
||
// works the first five times | ||
assert_eq!(increment().unwrap(), "1"); | ||
assert_eq!(increment().unwrap(), "2"); | ||
assert_eq!(increment().unwrap(), "3"); | ||
assert_eq!(increment().unwrap(), "4"); | ||
assert_eq!(increment().unwrap(), "5"); | ||
|
||
// then errors | ||
let res = increment(); | ||
|
||
assert!(matches!(res, Err(invoke::Error::Host(_)))); | ||
assert!(format!("{res:?}").contains(EXPECTED_ERROR_START)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Tests CLI commands from https://soroban.stellar.org/docs/how-to-guides/events | ||
|
||
use soroban_test::{TestEnv, Wasm}; | ||
|
||
const WASM: &Wasm = &Wasm::Release("soroban_events_contract"); | ||
|
||
const EXPECTED_EVENT: &str = r#"#0: event: {"ext":"v0","contract_id":"0000000000000000000000000000000000000000000000000000000000000001","type_":"contract","body":{"v0":{"topics":[{"symbol":"COUNTER"},{"symbol":"increment"}],"data":{"u32":1}}}} | ||
"#; | ||
|
||
#[test] | ||
fn invoke() { | ||
TestEnv::with_default(|workspace| { | ||
// events get dumped right to STDERR using eprintln; need to test with assert_cmd | ||
workspace | ||
.new_assert_cmd("contract") | ||
.arg("invoke") | ||
.arg("--wasm") | ||
.arg(&WASM.path()) | ||
.args(["--id", "1"]) | ||
.args(["--", "increment"]) | ||
.assert() | ||
.stdout("1\n") | ||
.stderr(EXPECTED_EVENT); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not an expert, but shouldn't this be 2023 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a 2023 edition (yet)? I don't see it listed in the edition guide.