Skip to content

Commit

Permalink
Merge branch 'main' into remap-path
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored Oct 29, 2024
2 parents 1b9009b + 4c8c735 commit 734d357
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 28 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ jobs:
crate:
- name: stellar-cli
binary: stellar
- name: soroban-cli
binary: soroban
sys:
- os: ubuntu-20.04 # Use 20.04 to get an older version of glibc for increased compat
target: x86_64-unknown-linux-gnu
Expand Down
4 changes: 4 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ members = [
"cmd/crates/*",
"cmd/crates/soroban-test/tests/fixtures/test-wasms/*",
"cmd/crates/soroban-test/tests/fixtures/hello",
"cmd/crates/soroban-test/tests/fixtures/bye",
]
default-members = ["cmd/soroban-cli", "cmd/crates/soroban-spec-tools", "cmd/crates/soroban-test"]
exclude = ["cmd/crates/soroban-test/tests/fixtures/hello"]
exclude = ["cmd/crates/soroban-test/tests/fixtures/hello", "cmd/crates/soroban-test/tests/fixtures/bye"]

[workspace.package]
version = "21.5.0"
Expand Down
2 changes: 1 addition & 1 deletion FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ Creates and funds a new account with the specified starting balance
* `--destination <DESTINATION>` — Account Id to create, e.g. `GBX...`
* `--starting-balance <STARTING_BALANCE>` — Initial balance in stroops of the account, default 1 XLM

Default value: `10_000_000`
Default value: `10000000`



Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ install_rust: install
install:
cargo install --force --locked --path ./cmd/stellar-cli --debug
cargo install --force --locked --path ./cmd/crates/soroban-test/tests/fixtures/hello --root ./target --debug --quiet
cargo install --force --locked --path ./cmd/crates/soroban-test/tests/fixtures/bye --root ./target --debug --quiet

# regenerate the example lib in `cmd/crates/soroban-spec-typsecript/fixtures/ts`
build-snapshot: typescript-bindings-fixtures
Expand Down
7 changes: 7 additions & 0 deletions cmd/crates/soroban-test/tests/fixtures/bye/Cargo.lock

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

9 changes: 9 additions & 0 deletions cmd/crates/soroban-test/tests/fixtures/bye/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "stellar-bye"
version = "21.5.0"
edition = "2021"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions cmd/crates/soroban-test/tests/fixtures/bye/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Bye, world!");
}
52 changes: 52 additions & 0 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use assert_fs::TempDir;
use predicates::prelude::predicate;
use soroban_test::{AssertExt, TestEnv};
use std::{fs, path::Path};

Expand Down Expand Up @@ -288,3 +289,54 @@ fn use_env() {
.success()
.stdout("SDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCQYFD\n");
}

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

sandbox
.new_assert_cmd("keys")
.env(
"SOROBAN_SECRET_KEY",
"SC4ZPYELVR7S7EE7KZDZN3ETFTNQHHLTUL34NUAAWZG5OK2RGJ4V2U3Z",
)
.arg("add")
.arg("alice")
.assert()
.success();

fs::rename(
sandbox.dir().join(".stellar"),
sandbox.dir().join("_soroban"),
)
.unwrap();

sandbox
.new_assert_cmd("keys")
.env(
"SOROBAN_SECRET_KEY",
"SAQMV6P3OWM2SKCK3OEWNXSRYWK5RNNUL5CPHQGIJF2WVT4EI2BZ63GG",
)
.arg("add")
.arg("alice")
.assert()
.success();

fs::rename(
sandbox.dir().join("_soroban"),
sandbox.dir().join(".soroban"),
)
.unwrap();

sandbox
.new_assert_cmd("keys")
.arg("show")
.arg("alice")
.arg("--verbose")
.assert()
.success()
.stderr(predicate::str::contains(
"WARN soroban_cli::utils: the .stellar and .soroban config directories exist at path",
))
.stdout("SAQMV6P3OWM2SKCK3OEWNXSRYWK5RNNUL5CPHQGIJF2WVT4EI2BZ63GG\n");
}
4 changes: 2 additions & 2 deletions cmd/crates/soroban-test/tests/it/integration/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async fn invoke() {
.assert()
.stdout_as_str();
let dir = sandbox.dir();
let seed_phrase = std::fs::read_to_string(dir.join(".soroban/identity/test.toml")).unwrap();
let seed_phrase = std::fs::read_to_string(dir.join(".stellar/identity/test.toml")).unwrap();
let s = toml::from_str::<secret::Secret>(&seed_phrase).unwrap();
let secret::Secret::SeedPhrase { seed_phrase } = s else {
panic!("Expected seed phrase")
Expand Down Expand Up @@ -113,7 +113,7 @@ async fn invoke() {
},
)
.unwrap();
let sk_from_file = std::fs::read_to_string(dir.join(".soroban/identity/testone.toml")).unwrap();
let sk_from_file = std::fs::read_to_string(dir.join(".stellar/identity/testone.toml")).unwrap();

assert_eq!(sk_from_file, format!("secret_key = \"{secret_key_1}\"\n"));
let secret_key_1_readin = sandbox
Expand Down
34 changes: 24 additions & 10 deletions cmd/crates/soroban-test/tests/it/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This function calls the soroban executable via cargo and checks that the output
This function calls the stellar executable via cargo and checks that the output
is correct. The PATH environment variable is set to include the target/bin
directory, so that the soroban executable can be found.
directory, so that the stellar executable can be found.
*/

use std::{ffi::OsString, path::PathBuf};
Expand All @@ -11,31 +11,45 @@ fn soroban_hello() {
// Add the target/bin directory to the iterator of paths
let paths = get_paths();
// Call soroban with the PATH variable set to include the target/bin directory
assert_cmd::Command::cargo_bin("soroban")
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("soroban"))
.arg("hello")
.env("PATH", &paths)
.assert()
.stdout("Hello, world!\n");
}

#[test]
fn stellar_bye() {
// Add the target/bin directory to the iterator of paths
let paths = get_paths();
// Call soroban with the PATH variable set to include the target/bin directory
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("stellar"))
.arg("bye")
.env("PATH", &paths)
.assert()
.stdout("Bye, world!\n");
}

#[test]
fn list() {
// Call `soroban --list` with the PATH variable set to include the target/bin directory
assert_cmd::Command::cargo_bin("soroban")
.unwrap_or_else(|_| assert_cmd::Command::new("soroban"))
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("stellar"))
.arg("--list")
.env("PATH", get_paths())
.assert()
.stdout(predicates::str::contains("hello"));
.stdout(predicates::str::contains("hello"))
.stdout(predicates::str::contains("bye"));
}

#[test]
#[cfg(not(unix))]
fn has_no_path() {
// Call soroban with the PATH variable set to include just target/bin directory
assert_cmd::Command::cargo_bin("soroban")
.unwrap_or_else(|_| assert_cmd::Command::new("soroban"))
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("stellar"))
.arg("hello")
.env("PATH", target_bin())
.assert()
Expand All @@ -45,8 +59,8 @@ fn has_no_path() {
#[test]
fn has_no_path_failure() {
// Call soroban with the PATH variable set to include just target/bin directory
assert_cmd::Command::cargo_bin("soroban")
.unwrap_or_else(|_| assert_cmd::Command::new("soroban"))
assert_cmd::Command::cargo_bin("stellar")
.unwrap_or_else(|_| assert_cmd::Command::new("stellar"))
.arg("hello")
.assert()
.stderr(predicates::str::contains("error: no such command: `hello`"));
Expand Down
6 changes: 4 additions & 2 deletions cmd/soroban-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use clap::CommandFactory;
use dotenvy::dotenv;
use tracing_subscriber::{fmt, EnvFilter};

use crate::print::Print;
use crate::upgrade_check::upgrade_check;
use crate::{commands, print, Root};
use crate::{commands, Root};

#[tokio::main]
pub async fn main() {
Expand Down Expand Up @@ -42,6 +43,7 @@ pub async fn main() {
std::process::exit(1);
}
});

// Now use root to setup the logger
if let Some(level) = root.global_args.log_level() {
let mut e_filter = EnvFilter::from_default_env()
Expand Down Expand Up @@ -78,7 +80,7 @@ pub async fn main() {
upgrade_check(root.global_args.quiet).await;
});

let printer = print::Print::new(root.global_args.quiet);
let printer = Print::new(root.global_args.quiet);
if let Err(e) = root.run().await {
printer.errorln(format!("error: {e}"));
std::process::exit(1);
Expand Down
23 changes: 18 additions & 5 deletions cmd/soroban-cli/src/commands/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::process::Command;
use std::{path::PathBuf, process::Command};

use clap::CommandFactory;
use which::which;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn run() -> Result<(), Error> {
return Ok(());
}

let bin = which(format!("soroban-{name}")).map_err(|_| {
let bin = find_bin(&name).map_err(|_| {
let suggestion = if let Ok(bins) = list() {
let suggested_name = bins
.iter()
Expand All @@ -53,6 +53,7 @@ pub fn run() -> Result<(), Error> {
.min_by(|a, b| a.1.total_cmp(&b.1))
.map(|(a, _)| a.to_string())
.unwrap_or_default();

if suggested_name.is_empty() {
suggested_name
} else {
Expand All @@ -64,8 +65,10 @@ pub fn run() -> Result<(), Error> {
} else {
String::new()
};

Error::ExecutableNotFound(name, suggestion)
})?;

std::process::exit(
Command::new(bin)
.args(args)
Expand All @@ -78,19 +81,29 @@ pub fn run() -> Result<(), Error> {

const MAX_HEX_LENGTH: usize = 10;

fn find_bin(name: &str) -> Result<PathBuf, which::Error> {
if let Ok(path) = which(format!("stellar-{name}")) {
Ok(path)
} else {
which(format!("soroban-{name}"))
}
}

pub fn list() -> Result<Vec<String>, Error> {
let re_str = if cfg!(target_os = "windows") {
r"^soroban-.*.exe$"
r"^(soroban|stellar)-.*.exe$"
} else {
r"^soroban-.*"
r"^(soroban|stellar)-.*"
};

let re = regex::Regex::new(re_str)?;

Ok(which::which_re(re)?
.filter_map(|b| {
let s = b.file_name()?.to_str()?;
Some(s.strip_suffix(".exe").unwrap_or(s).to_string())
})
.filter(|s| !(utils::is_hex_string(s) && s.len() > MAX_HEX_LENGTH))
.map(|s| s.replace("soroban-", ""))
.map(|s| s.replace("soroban-", "").replace("stellar-", ""))
.collect())
}
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/tx/new/create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Cmd {
#[arg(long)]
pub destination: xdr::AccountId,
/// Initial balance in stroops of the account, default 1 XLM
#[arg(long, default_value = "10_000_000")]
#[arg(long, default_value = "10000000")]
pub starting_balance: i64,
}

Expand Down
24 changes: 21 additions & 3 deletions cmd/soroban-cli/src/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Args {

pub fn local_config(&self) -> Result<PathBuf, Error> {
let pwd = self.current_dir()?;
Ok(find_config_dir(pwd.clone()).unwrap_or_else(|_| pwd.join(".soroban")))
Ok(find_config_dir(pwd.clone()).unwrap_or_else(|_| pwd.join(".stellar")))
}

pub fn current_dir(&self) -> Result<PathBuf, Error> {
Expand Down Expand Up @@ -468,14 +468,32 @@ impl KeyType {
}

pub fn global_config_path() -> Result<PathBuf, Error> {
Ok(if let Ok(config_home) = std::env::var("XDG_CONFIG_HOME") {
let config_dir = if let Ok(config_home) = std::env::var("XDG_CONFIG_HOME") {
PathBuf::from_str(&config_home).map_err(|_| Error::XdgConfigHome(config_home))?
} else {
dirs::home_dir()
.ok_or(Error::HomeDirNotFound)?
.join(".config")
};

let soroban_dir = config_dir.join("soroban");
let stellar_dir = config_dir.join("stellar");
let soroban_exists = soroban_dir.exists();
let stellar_exists = stellar_dir.exists();

if stellar_exists && soroban_exists {
tracing::warn!("the .stellar and .soroban config directories exist at path {config_dir:?}, using the .stellar");
}

if stellar_exists {
return Ok(stellar_dir);
}
.join("soroban"))

if soroban_exists {
return Ok(soroban_dir);
}

Ok(stellar_dir)
}

impl Pwd for Args {
Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{

const TERMS: &[&str] = &["Apple_Terminal", "vscode"];

#[derive(Clone)]
pub struct Print {
pub quiet: bool,
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/soroban-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ pub fn find_config_dir(mut pwd: std::path::PathBuf) -> std::io::Result<std::path
if soroban_exists {
return Ok(soroban_dir);
}

if !pwd.pop() {
break;
}
}

Err(std::io::Error::new(
std::io::ErrorKind::Other,
"stellar directory not found",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Rust's output directory
target

# Local Soroban settings
# Local settings
.soroban
.stellar

0 comments on commit 734d357

Please sign in to comment.