Skip to content

Commit

Permalink
fix: testing added DeployKind to allow deploys to return various data
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed May 31, 2024
1 parent 0437ac6 commit a9996a5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
27 changes: 17 additions & 10 deletions cmd/crates/soroban-test/tests/it/integration/tx.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
use soroban_cli::commands::tx;
use soroban_sdk::xdr::{Limits, ReadXdr, TransactionEnvelope, TransactionV1Envelope, WriteXdr};
use soroban_sdk::xdr::{
Limits, ReadXdr, TransactionEnvelope, TransactionV1Envelope, VecM, WriteXdr,
};
use soroban_test::{AssertExt, TestEnv};

use crate::integration::util::{deploy_contract, HELLO_WORLD};
use crate::integration::util::{deploy_contract, DeployKind, HELLO_WORLD};

#[tokio::test]
async fn txn_simulate() {
let sandbox = &TestEnv::new();
let xdr_base64 = deploy_contract(sandbox, HELLO_WORLD, true).await;
println!("{xdr_base64}");
let xdr_base64_build_only = deploy_contract(sandbox, HELLO_WORLD, DeployKind::BuildOnly).await;
let xdr_base64_sim_only = deploy_contract(sandbox, HELLO_WORLD, DeployKind::SimOnly).await;
println!("{xdr_base64_build_only}");
let cmd = tx::simulate::Cmd::default();
let tx_env = TransactionEnvelope::from_xdr_base64(&xdr_base64, Limits::none()).unwrap();
let tx_env =
TransactionEnvelope::from_xdr_base64(&xdr_base64_build_only, Limits::none()).unwrap();
let TransactionEnvelope::Tx(TransactionV1Envelope { tx, .. }) = &tx_env else {
panic!("Only transaction v1 is supported")
};
let assembled = cmd.simulate(tx, &sandbox.client()).await.unwrap();
let assembled_str = sandbox
.new_assert_cmd("tx")
.arg("simulate")
.write_stdin(xdr_base64.as_bytes())
.write_stdin(xdr_base64_build_only.as_bytes())
.assert()
.success()
.stdout_as_str();
println!("{assembled_str}");
assert_eq!(xdr_base64_sim_only, assembled_str);
let txn_env = TransactionEnvelope::Tx(TransactionV1Envelope {
tx: assembled.transaction().clone(),
signatures: VecM::default(),
});

assert_eq!(
assembled
.transaction()
.to_xdr_base64(Limits::none())
.unwrap(),
txn_env.to_xdr_base64(Limits::none()).unwrap(),
assembled_str
);
}
37 changes: 29 additions & 8 deletions cmd/crates/soroban-test/tests/it/integration/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,39 @@ where

pub const TEST_SALT: &str = "f55ff16f66f43360266b95db6f8fec01d76031054306ae4a4b380598f6cfd114";

pub enum DeployKind {
BuildOnly,
Normal,
SimOnly,
}

impl Display for DeployKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DeployKind::BuildOnly => write!(f, "--build-only"),
DeployKind::Normal => write!(f, ""),
DeployKind::SimOnly => write!(f, "--sim-only"),
}
}
}

pub async fn deploy_hello(sandbox: &TestEnv) -> String {
deploy_contract(sandbox, HELLO_WORLD, false).await
deploy_contract(sandbox, HELLO_WORLD, DeployKind::Normal).await
}

pub async fn deploy_custom(sandbox: &TestEnv) -> String {
deploy_contract(sandbox, CUSTOM_TYPES, false).await
deploy_contract(sandbox, CUSTOM_TYPES, DeployKind::Normal).await
}

pub async fn deploy_swap(sandbox: &TestEnv) -> String {
deploy_contract(sandbox, SWAP).await
deploy_contract(sandbox, SWAP, DeployKind::Normal).await
}

pub async fn deploy_contract(sandbox: &TestEnv, wasm: &Wasm<'static>) -> String {
pub async fn deploy_contract(
sandbox: &TestEnv,
wasm: &Wasm<'static>,
deploy: DeployKind,
) -> String {
let cmd = sandbox.cmd_with_config::<_, commands::contract::deploy::wasm::Cmd>(&[
"--fee",
"1000000",
Expand All @@ -43,16 +63,17 @@ pub async fn deploy_contract(sandbox: &TestEnv, wasm: &Wasm<'static>) -> String
"--salt",
TEST_SALT,
"--ignore-checks",
build_only.then_some("--build-only").unwrap_or_default(),
deploy.to_string().as_str(),
]);
let res = sandbox.run_cmd_with(cmd, "test").await.unwrap();
if build_only {
match res.to_envelope() {
match deploy {
DeployKind::BuildOnly | DeployKind::SimOnly => match res.to_envelope() {
commands::txn_result::TxnEnvelopeResult::TxnEnvelope(e) => {
return e.to_xdr_base64(Limits::none()).unwrap()
}
commands::txn_result::TxnEnvelopeResult::Res(_) => todo!(),
}
},
DeployKind::Normal => (),
}
res.into_result().unwrap()
}
Expand Down

0 comments on commit a9996a5

Please sign in to comment.