Skip to content
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

#1682: fix unit tests #1734

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ generate-full-help-doc:
cargo run --bin doc-gen --features clap-markdown

test: build-test
cargo test --workspace
cargo test --workspace --exclude soroban-test
cargo test -p soroban-test -- --skip integration::

e2e-test:
cargo test --features it --test it -- integration
Expand Down
1 change: 1 addition & 0 deletions cmd/crates/soroban-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ assert_fs = "1.0.7"
predicates = { workspace = true }
fs_extra = "1.3.0"
toml = { workspace = true }
home = "0.5.9"

[dev-dependencies]
serde_json = "1.0.93"
Expand Down
2 changes: 1 addition & 1 deletion cmd/crates/soroban-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl TestEnv {
let cmd = self.cmd_with_config::<I, invoke::Cmd>(command_str, None);
self.run_cmd_with(cmd, source)
.await
.map(|r| r.into_result().unwrap())
.map(|tx| tx.into_result().unwrap())
}

/// A convenience method for using the invoke command.
Expand Down
55 changes: 33 additions & 22 deletions cmd/crates/soroban-test/tests/it/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use predicates::prelude::predicate;
use soroban_cli::xdr::{Limited, Limits, ReadXdr, ScMetaEntry, ScMetaV0};
use soroban_spec_tools::contract::Spec;
use soroban_test::TestEnv;
use std::env;
use std::io::Cursor;

#[test]
Expand All @@ -16,11 +17,9 @@ fn build_all() {
.arg("--print-commands-only")
.assert()
.success()
.stdout(predicate::eq("\
cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
.stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
cargo rustc --manifest-path=contracts/call/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
cargo rustc --manifest-path=contracts/add/add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
"));
cargo rustc --manifest-path=contracts/add/add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release")));
}

#[test]
Expand All @@ -36,9 +35,7 @@ fn build_package_by_name() {
.arg("--package=add")
.assert()
.success()
.stdout(predicate::eq("\
cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
"));
.stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release")));
}

#[test]
Expand All @@ -54,9 +51,7 @@ fn build_package_by_current_dir() {
.assert()
.success()
.stdout(predicate::eq(
"\
cargo rustc --manifest-path=Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
",
with_flags("cargo rustc --manifest-path=Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"),
));
}

Expand Down Expand Up @@ -85,20 +80,17 @@ fn build_all_when_in_non_package_directory() {
let sandbox = TestEnv::default();
let cargo_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let fixture_path = cargo_dir.join("tests/fixtures/workspace/contracts/add/src/");

sandbox
.new_assert_cmd("contract")
.current_dir(fixture_path)
.arg("build")
.arg("--print-commands-only")
.assert()
.success()
.stdout(predicate::eq(
"\
cargo rustc --manifest-path=../Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
cargo rustc --manifest-path=../../call/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
cargo rustc --manifest-path=../add2/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
",
));
.stdout(predicate::eq(with_flags(
"cargo rustc --manifest-path=../Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release"
)));
}

#[test]
Expand All @@ -113,14 +105,11 @@ fn build_default_members() {
.arg("--print-commands-only")
.assert()
.success()
.stdout(predicate::eq(
"\
cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
",
));
.stdout(predicate::eq(with_flags("cargo rustc --manifest-path=contracts/add/Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release")));
}

#[test]
#[ignore] // TODO: unignore -- reproduces unfixed bug https://github.com/stellar/stellar-cli/issues/1694
fn build_with_metadata() {
let sandbox = TestEnv::default();
let cargo_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down Expand Up @@ -185,3 +174,25 @@ fn build_with_metadata() {

assert_eq!(entries, expected_entries);
}

fn with_flags(expected: &str) -> String {
let cargo_home = home::cargo_home().unwrap();
let cargo_home = format!("{}", cargo_home.display());
let registry_prefix = format!("{cargo_home}/registry/src/");

let vec: Vec<_> = if env::var("RUSTFLAGS").is_ok() {
expected.split('\n').map(ToString::to_string).collect()
} else {
expected
.split('\n')
.map(|x| format!("CARGO_BUILD_RUSTFLAGS=--remap-path-prefix={registry_prefix}= {x}",))
.collect()
};

format!(
"\
{}
",
vec.join("\n")
)
}
95 changes: 51 additions & 44 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,34 @@ fn ls(sandbox: &TestEnv) -> Vec<String> {
.collect::<Vec<_>>()
}

pub const NETWORKS: &str = r#"local
futurenet
mainnet
testnet
"#;

#[test]
fn set_and_remove_network() {
TestEnv::with_default(|sandbox| {
add_network(sandbox, "local");
let dir = sandbox.dir().join(".soroban").join("network");
add_network(sandbox, "custom");
let dir = sandbox.dir().join(".stellar").join("network");
let mut read_dir = std::fs::read_dir(dir).unwrap();
let file = read_dir.next().unwrap().unwrap();
assert_eq!(file.file_name().to_str().unwrap(), "local.toml");
assert_eq!(file.file_name().to_str().unwrap(), "custom.toml");
let res = ls(sandbox);
assert_eq!(res[0], "local");
assert_eq!(res[0], "custom");
sandbox
.new_assert_cmd("network")
.arg("rm")
.arg("local")
.arg("custom")
.assert()
.success();

sandbox
.new_assert_cmd("network")
.arg("ls")
.assert()
.stdout("\n");
});
}

#[test]
fn use_default_futurenet() {
TestEnv::with_default(|sandbox| {
sandbox
.new_assert_cmd("keys")
.args(["generate", "alice", "--network", "futurenet"])
.assert()
.success();
let dir = sandbox.dir().join(".soroban").join("network");
let mut read_dir = std::fs::read_dir(dir).unwrap();
let file = read_dir.next().unwrap().unwrap();
assert_eq!(file.file_name().to_str().unwrap(), "futurenet.toml");
});
}

#[test]
fn use_default_testnet() {
TestEnv::with_default(|sandbox| {
sandbox
.new_assert_cmd("keys")
.args(["generate", "alice", "--network", "testnet"])
.assert()
.success();
let dir = sandbox.dir().join(".soroban").join("network");
let mut read_dir = std::fs::read_dir(dir).unwrap();
let file = read_dir.next().unwrap().unwrap();
assert_eq!(file.file_name().to_str().unwrap(), "testnet.toml");
.stdout(NETWORKS);
});
}

Expand Down Expand Up @@ -118,7 +94,7 @@ fn set_and_remove_global_network() {
.arg("ls")
.arg("--global")
.assert()
.stdout("global\n");
.stdout(format!("global\n{NETWORKS}"));

sandbox
.new_assert_cmd("network")
Expand All @@ -134,23 +110,43 @@ fn set_and_remove_global_network() {
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("ls")
.assert()
.stdout("\n");
.stdout(NETWORKS);
}

#[test]
fn multiple_networks() {
let sandbox = TestEnv::default();
let ls = || -> Vec<String> { ls(&sandbox) };

add_network(&sandbox, "local");
println!("{:#?}", ls());
add_network(&sandbox, "custom");
println!("{:#?}", ls());
add_network(&sandbox, "local2");

assert_eq!(ls().as_slice(), ["local".to_owned(), "local2".to_owned()]);
assert_eq!(
ls().as_slice(),
[
"custom".to_owned(),
"local2".to_owned(),
"local".to_owned(),
"futurenet".to_owned(),
"mainnet".to_owned(),
"testnet".to_owned()
]
);

sandbox.cmd::<network::rm::Cmd>("local").run().unwrap();
sandbox.cmd::<network::rm::Cmd>("custom").run().unwrap();

assert_eq!(ls().as_slice(), ["local2".to_owned()]);
assert_eq!(
ls().as_slice(),
[
"local2".to_owned(),
"local".to_owned(),
"futurenet".to_owned(),
"mainnet".to_owned(),
"testnet".to_owned()
]
);

let sub_dir = sandbox.dir().join("sub_directory");
fs::create_dir(&sub_dir).unwrap();
Expand All @@ -168,7 +164,17 @@ fn multiple_networks() {
.run()
.unwrap();

assert_eq!(ls().as_slice(), ["local2".to_owned(), "local3".to_owned()]);
assert_eq!(
ls().as_slice(),
[
"local2".to_owned(),
"local3".to_owned(),
"local".to_owned(),
"futurenet".to_owned(),
"mainnet".to_owned(),
"testnet".to_owned()
]
);
}

#[test]
Expand Down Expand Up @@ -205,7 +211,7 @@ fn generate_key() {
.assert()
.stdout(predicates::str::contains("test_2\n"));
let file_contents =
fs::read_to_string(sandbox.dir().join(".soroban/identity/test_2.toml")).unwrap();
fs::read_to_string(sandbox.dir().join(".stellar/identity/test_2.toml")).unwrap();
assert_eq!(
file_contents,
format!("seed_phrase = \"{DEFAULT_SEED_PHRASE}\"\n")
Expand Down Expand Up @@ -368,6 +374,7 @@ fn set_default_identity() {

sandbox
.new_assert_cmd("env")
.env_remove("SOROBAN_ACCOUNT")
.assert()
.stdout(predicate::str::contains("STELLAR_ACCOUNT=alice"))
.success();
Expand Down
9 changes: 8 additions & 1 deletion cmd/crates/soroban-test/tests/it/help.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use soroban_cli::commands::contract::arg_parsing::Error::HelpMessage;
use soroban_cli::commands::contract::invoke::Error::ArgParsing;
use soroban_cli::commands::contract::{self, arg_parsing};
use soroban_test::TestEnv;

use crate::util::{invoke_custom as invoke, CUSTOM_TYPES, DEFAULT_CONTRACT_ID};

async fn invoke_custom(func: &str, args: &str) -> Result<String, contract::invoke::Error> {
let e = &TestEnv::default();
invoke(e, DEFAULT_CONTRACT_ID, func, args, &CUSTOM_TYPES.path()).await
let r = invoke(e, DEFAULT_CONTRACT_ID, func, args, &CUSTOM_TYPES.path()).await;
if let Err(ArgParsing(HelpMessage(e))) = r {
return Ok(e);
}
r
}

#[tokio::test]
Expand Down Expand Up @@ -35,6 +41,7 @@ async fn tuple_help() {
#[tokio::test]
async fn strukt_help() {
let output = invoke_custom("strukt", "--help").await.unwrap();
println!("{output}");
assert!(output.contains("--strukt '{ \"a\": 1, \"b\": true, \"c\": \"hello\" }'",));
assert!(output.contains("This is from the rust doc above the struct Test",));
}
Expand Down
Loading
Loading