Skip to content

Commit

Permalink
feat(CLI): add dotenv so directories can now set CLI args
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Aug 24, 2023
1 parent 06bcd7b commit 2c741e0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

73 changes: 73 additions & 0 deletions cmd/crates/soroban-test/tests/it/dotenv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use soroban_test::TestEnv;

use crate::util::HELLO_WORLD;

const SOROBAN_CONTRACT_ID: &str = "SOROBAN_CONTRACT_ID=1";

fn deploy(e: &TestEnv, id: &str) {
e.new_assert_cmd("contract")
.arg("deploy")
.arg("--wasm")
.arg(HELLO_WORLD.path())
.arg("--id")
.arg(id)
.assert()
.success();
}

fn write_env_file(e: &TestEnv, contents: &str) {
let env_file = e.dir().join(".env");
std::fs::write(&env_file, contents).unwrap();
assert_eq!(contents, std::fs::read_to_string(env_file).unwrap());
}

#[test]
fn can_read_file() {
TestEnv::with_default(|e| {
deploy(e, "1");
write_env_file(e, SOROBAN_CONTRACT_ID);
e.new_assert_cmd("contract")
.arg("invoke")
.arg("--")
.arg("hello")
.arg("--world=world")
.assert()
.stdout("[\"Hello\",\"world\"]\n")
.success();
});
}

#[test]
fn current_env_not_overwritten() {
TestEnv::with_default(|e| {
deploy(e, "1");
write_env_file(e, SOROBAN_CONTRACT_ID);

e.new_assert_cmd("contract")
.env("SOROBAN_CONTRACT_ID", "2")
.arg("invoke")
.arg("--")
.arg("hello")
.arg("--world=world")
.assert()
.stderr("error: parsing contract spec: contract spec not found\n");
});
}

#[test]
fn cli_args_have_priority() {
TestEnv::with_default(|e| {
deploy(e, "2");
write_env_file(e, SOROBAN_CONTRACT_ID);
e.new_assert_cmd("contract")
.env("SOROBAN_CONTRACT_ID", "3")
.arg("invoke")
.arg("--id")
.arg("2")
.arg("--")
.arg("hello")
.arg("--world=world")
.assert()
.stdout("[\"Hello\",\"world\"]\n");
});
}
1 change: 1 addition & 0 deletions cmd/crates/soroban-test/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod arg_parsing;
mod config;

mod custom_types;
mod dotenv;
mod invoke_sandbox;
mod plugin;
mod util;
1 change: 1 addition & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
cargo_metadata = "0.15.4"
pathdiff = "0.2.1"
dotenvy = "0.15.7"
# For hyper-tls
[target.'cfg(unix)'.dependencies]
openssl = { version = "0.10.55", features = ["vendored"] }
Expand Down
5 changes: 3 additions & 2 deletions cmd/soroban-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use clap::{CommandFactory, Parser};
use tracing_subscriber::{fmt, EnvFilter};

use dotenvy::dotenv;
use soroban_cli::{commands::plugin, Root};
use tracing_subscriber::{fmt, EnvFilter};

#[tokio::main]
async fn main() {
let _ = dotenv().unwrap_or_default();
let mut root = Root::try_parse().unwrap_or_else(|e| {
use clap::error::ErrorKind;
match e.kind() {
Expand Down

0 comments on commit 2c741e0

Please sign in to comment.