Skip to content

Commit

Permalink
feat!: move config commands to top level (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored Jan 11, 2024
1 parent 54f1dc0 commit 8906a53
Show file tree
Hide file tree
Showing 45 changed files with 1,179 additions and 673 deletions.
17 changes: 12 additions & 5 deletions cmd/crates/soroban-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use assert_fs::{fixture::FixtureError, prelude::PathChild, TempDir};
use fs_extra::dir::CopyOptions;

use soroban_cli::{
commands::{config, contract, contract::invoke, global},
commands::{config, contract, contract::invoke, global, keys},
CommandParser, Pwd,
};

Expand Down Expand Up @@ -82,6 +82,13 @@ impl TestEnv {
pub fn new() -> Result<TestEnv, Error> {
let this = TempDir::new().map(|temp_dir| TestEnv { temp_dir })?;
std::env::set_var("XDG_CONFIG_HOME", this.temp_dir.as_os_str());
this.new_assert_cmd("keys")
.arg("generate")
.arg("test")
.arg("-d")
.arg("--no-fund")
.assert();
std::env::set_var("SOROBAN_ACCOUNT", "test");
Ok(this)
}

Expand Down Expand Up @@ -156,17 +163,17 @@ impl TestEnv {
&self.temp_dir
}

/// Returns the public key corresponding to the test identity's `hd_path`
/// Returns the public key corresponding to the test keys's `hd_path`
pub fn test_address(&self, hd_path: usize) -> String {
self.cmd::<config::identity::address::Cmd>(&format!("--hd-path={hd_path}"))
self.cmd::<keys::address::Cmd>(&format!("--hd-path={hd_path}"))
.public_key()
.unwrap()
.to_string()
}

/// Returns the private key corresponding to the test identity's `hd_path`
/// Returns the private key corresponding to the test keys's `hd_path`
pub fn test_show(&self, hd_path: usize) -> String {
self.cmd::<config::identity::show::Cmd>(&format!("--hd-path={hd_path}"))
self.cmd::<keys::show::Cmd>(&format!("--hd-path={hd_path}"))
.private_key()
.unwrap()
.to_string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ fn test_atomic_swap() {
let env = Env::default();
env.mock_all_auths();

let a = Address::random(&env);
let b = Address::random(&env);
let a = Address::generate(&env);
let b = Address::generate(&env);

let token_admin = Address::random(&env);
let token_admin = Address::generate(&env);

let (token_a, token_a_admin) = create_token_contract(&env, &token_admin);
let (token_b, token_b_admin) = create_token_contract(&env, &token_admin);
Expand Down
34 changes: 17 additions & 17 deletions cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ fn test() {
let e = Env::default();
e.mock_all_auths();

let admin1 = Address::random(&e);
let admin2 = Address::random(&e);
let user1 = Address::random(&e);
let user2 = Address::random(&e);
let user3 = Address::random(&e);
let admin1 = Address::generate(&e);
let admin2 = Address::generate(&e);
let user1 = Address::generate(&e);
let user2 = Address::generate(&e);
let user3 = Address::generate(&e);
let token = create_token(&e, &admin1);

token.mint(&user1, &1000);
Expand Down Expand Up @@ -142,9 +142,9 @@ fn test_burn() {
let e = Env::default();
e.mock_all_auths();

let admin = Address::random(&e);
let user1 = Address::random(&e);
let user2 = Address::random(&e);
let admin = Address::generate(&e);
let user1 = Address::generate(&e);
let user2 = Address::generate(&e);
let token = create_token(&e, &admin);

token.mint(&user1, &1000);
Expand Down Expand Up @@ -199,9 +199,9 @@ fn transfer_insufficient_balance() {
let e = Env::default();
e.mock_all_auths();

let admin = Address::random(&e);
let user1 = Address::random(&e);
let user2 = Address::random(&e);
let admin = Address::generate(&e);
let user1 = Address::generate(&e);
let user2 = Address::generate(&e);
let token = create_token(&e, &admin);

token.mint(&user1, &1000);
Expand All @@ -216,10 +216,10 @@ fn transfer_from_insufficient_allowance() {
let e = Env::default();
e.mock_all_auths();

let admin = Address::random(&e);
let user1 = Address::random(&e);
let user2 = Address::random(&e);
let user3 = Address::random(&e);
let admin = Address::generate(&e);
let user1 = Address::generate(&e);
let user2 = Address::generate(&e);
let user3 = Address::generate(&e);
let token = create_token(&e, &admin);

token.mint(&user1, &1000);
Expand All @@ -235,7 +235,7 @@ fn transfer_from_insufficient_allowance() {
#[should_panic(expected = "already initialized")]
fn initialize_already_initialized() {
let e = Env::default();
let admin = Address::random(&e);
let admin = Address::generate(&e);
let token = create_token(&e, &admin);

token.initialize(&admin, &10, &"name".into_val(&e), &"symbol".into_val(&e));
Expand All @@ -245,7 +245,7 @@ fn initialize_already_initialized() {
#[should_panic(expected = "Decimal must fit in a u8")]
fn decimal_is_over_max() {
let e = Env::default();
let admin = Address::random(&e);
let admin = Address::generate(&e);
let token = TokenClient::new(&e, &e.register_contract(None, Token {}));
token.initialize(
&admin,
Expand Down
74 changes: 24 additions & 50 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use assert_fs::TempDir;
use soroban_test::TestEnv;
use std::{fs, path::Path};

use crate::util::{add_identity, add_test_id, SecretKind, DEFAULT_SEED_PHRASE};
use soroban_cli::commands::config::network;
use crate::util::{add_key, add_test_id, SecretKind, DEFAULT_SEED_PHRASE};
use soroban_cli::commands::network;

const NETWORK_PASSPHRASE: &str = "Local Sandbox Stellar Network ; September 2022";

Expand Down Expand Up @@ -32,8 +32,7 @@ fn set_and_remove_network() {
// .assert()
// .stdout("");
sandbox
.new_assert_cmd("config")
.arg("network")
.new_assert_cmd("network")
.arg("ls")
.assert()
.stdout("\n");
Expand All @@ -42,8 +41,7 @@ fn set_and_remove_network() {

fn add_network(sandbox: &TestEnv, name: &str) {
sandbox
.new_assert_cmd("config")
.arg("network")
.new_assert_cmd("network")
.arg("add")
.args([
"--rpc-url=https://127.0.0.1",
Expand All @@ -59,9 +57,8 @@ fn add_network(sandbox: &TestEnv, name: &str) {

fn add_network_global(sandbox: &TestEnv, dir: &Path, name: &str) {
sandbox
.new_assert_cmd("config")
.new_assert_cmd("network")
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("network")
.arg("add")
.arg("--global")
.arg("--rpc-url")
Expand All @@ -81,28 +78,25 @@ fn set_and_remove_global_network() {
add_network_global(&sandbox, &dir, "global");

sandbox
.new_assert_cmd("config")
.new_assert_cmd("network")
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("network")
.arg("ls")
.arg("--global")
.assert()
.stdout("global\n");

sandbox
.new_assert_cmd("config")
.new_assert_cmd("network")
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("network")
.arg("rm")
.arg("--global")
.arg("global")
.assert()
.stdout("");

sandbox
.new_assert_cmd("config")
.new_assert_cmd("network")
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("network")
.arg("ls")
.assert()
.stdout("\n");
Expand Down Expand Up @@ -143,42 +137,41 @@ fn multiple_networks() {
}

#[test]
fn read_identity() {
fn read_key() {
let sandbox = TestEnv::default();
let dir = sandbox.dir().as_ref();
add_test_id(dir);
let ident_dir = dir.join(".soroban/identity");
assert!(ident_dir.exists());
sandbox
.new_assert_cmd("config")
.arg("identity")
.new_assert_cmd("keys")
.arg("ls")
.assert()
.stdout("test_id\n");
.stdout(predicates::str::contains("test_id\n"));
}

#[test]
fn generate_identity() {
fn generate_key() {
let sandbox = TestEnv::default();
sandbox
.new_assert_cmd("config")
.arg("identity")
.new_assert_cmd("keys")
.arg("generate")
.arg("--network=futurenet")
.arg("--no-fund")
.arg("--seed")
.arg("0000000000000000")
.arg("test")
.arg("test_2")
.assert()
.stdout("")
.success();

sandbox
.new_assert_cmd("config")
.arg("identity")
.new_assert_cmd("keys")
.arg("ls")
.assert()
.stdout("test\n");
.stdout(predicates::str::contains("test_2\n"));
let file_contents =
fs::read_to_string(sandbox.dir().join(".soroban/identity/test.toml")).unwrap();
fs::read_to_string(sandbox.dir().join(".soroban/identity/test_2.toml")).unwrap();
assert_eq!(
file_contents,
format!("seed_phrase = \"{DEFAULT_SEED_PHRASE}\"\n")
Expand All @@ -189,61 +182,42 @@ fn generate_identity() {
fn seed_phrase() {
let sandbox = TestEnv::default();
let dir = sandbox.dir();
add_identity(
add_key(
dir,
"test_seed",
SecretKind::Seed,
"one two three four five six seven eight nine ten eleven twelve",
);

sandbox
.new_assert_cmd("config")
.new_assert_cmd("keys")
.current_dir(dir)
.arg("identity")
.arg("ls")
.assert()
.stdout("test_seed\n");
}

#[test]
fn read_address() {
let sandbox = TestEnv::default();
for hd_path in 0..2 {
test_hd_path(&sandbox, hd_path);
}
.stdout(predicates::str::contains("test_seed\n"));
}

#[test]
fn use_env() {
let sandbox = TestEnv::default();

sandbox
.new_assert_cmd("config")
.new_assert_cmd("keys")
.env(
"SOROBAN_SECRET_KEY",
"SDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCQYFD",
)
.arg("identity")
.arg("add")
.arg("bob")
.assert()
.stdout("")
.success();

sandbox
.new_assert_cmd("config")
.arg("identity")
.new_assert_cmd("keys")
.arg("show")
.arg("bob")
.assert()
.success()
.stdout("SDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCQYFD\n");
}

fn test_hd_path(sandbox: &TestEnv, hd_path: usize) {
let seed_phrase = sep5::SeedPhrase::from_seed_phrase(DEFAULT_SEED_PHRASE).unwrap();
let key_pair = seed_phrase.from_path_index(hd_path, None).unwrap();
let pub_key = key_pair.public().to_string();
let test_address = sandbox.test_address(hd_path);
assert_eq!(pub_key, test_address);
}
12 changes: 1 addition & 11 deletions cmd/crates/soroban-test/tests/it/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use soroban_cli::commands::{
config::identity,
contract::{self, fetch},
};
use soroban_cli::commands::contract::{self, fetch};
use soroban_test::TestEnv;
use std::path::PathBuf;

Expand All @@ -11,13 +8,6 @@ use crate::util::{
TEST_SALT,
};








#[tokio::test]
async fn fetch() {
if !is_rpc() {
Expand Down
Loading

0 comments on commit 8906a53

Please sign in to comment.