Skip to content

Commit

Permalink
bug(tests): Fix local test scripts (#316)
Browse files Browse the repository at this point in the history
The test scripts in scripts didn't work due to missing environment variables needed by the tests.

The tests also relied on the working directory being set such that test artifacts in source could be read. This has been fixed with include_bytes! and include_str! isntead, and the the working directory for the test is no longer modified.
  • Loading branch information
shamsasari authored Oct 9, 2024
1 parent 4154165 commit 5758897
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next release

- fix(cleanup): Updated EditorConfig to 4-space indents
- fix(tests): Fixed local testing scripts
- fix: override chain config
- fix: estimate_fee should through an error if any txn fails
- fix: rejected transaction block production panic
Expand Down
18 changes: 3 additions & 15 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,14 @@ impl MadaraCmdBuilder {
}

pub fn run(self) -> MadaraCmd {
let output = std::process::Command::new("cargo")
.arg("locate-project")
.arg("--workspace")
.arg("--message-format=plain")
.output()
.expect("Failed to execute command");

let cargo_toml_path = String::from_utf8(output.stdout).expect("Invalid UTF-8");
let project_root = PathBuf::from(cargo_toml_path.trim()).parent().unwrap().to_path_buf();

env::set_current_dir(&project_root).expect("Failed to set working directory");
let target_bin = option_env!("COVERAGE_BIN").unwrap_or("./target/debug/madara");
let target_bin = PathBuf::from_str(target_bin).expect("target bin is not a path");
let target_bin = env::var("COVERAGE_BIN").expect("env COVERAGE_BIN to be set by script");
let target_bin = PathBuf::from_str(&target_bin).expect("COVERAGE_BIN to be a path");
if !target_bin.exists() {
panic!("No binary to run: {:?}", target_bin)
}

// This is an optional argument to sync faster from the FGW if gateway_key is set
let gateway_key_arg =
std::env::var("GATEWAY_KEY").ok().map(|gateway_key| ["--gateway-key".into(), gateway_key]);
let gateway_key_arg = env::var("GATEWAY_KEY").ok().map(|gateway_key| ["--gateway-key".into(), gateway_key]);

let process = Command::new(target_bin)
.envs(self.env)
Expand Down
16 changes: 4 additions & 12 deletions crates/tests/src/rpc/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ mod test_rpc_read_calls {
use starknet_providers::{JsonRpcClient, Provider};
use std::any::Any;
use std::fmt::Write;
use std::fs::File;
use std::io::BufReader;
use std::io::Read;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -1196,15 +1194,9 @@ mod test_rpc_read_calls {

let decompressed_program = decompress_to_string(contract_program.unwrap());

let mut class_program_file = File::open("crates/tests/src/rpc/test_utils/class_program.txt").unwrap();

let mut original_program = String::new();
class_program_file.read_to_string(&mut original_program).expect("issue while reading the file");

let contract_class_file = File::open("crates/tests/src/rpc/test_utils/contract_class.json").unwrap();
let reader = BufReader::new(contract_class_file);

let expected_contract_class: ContractClass = serde_json::from_reader(reader).unwrap();
let expected_program = include_str!("test_utils/class_program.txt");
let expected_contract_class: ContractClass =
serde_json::from_slice(include_bytes!("test_utils/contract_class.json")).unwrap();

let expected_contract_entry_points = match expected_contract_class.clone() {
ContractClass::Legacy(compressed) => Some(compressed.entry_points_by_type),
Expand All @@ -1216,7 +1208,7 @@ mod test_rpc_read_calls {
_ => None,
};

assert_eq!(decompressed_program, original_program);
assert_eq!(decompressed_program, expected_program);
assert_eq!(contract_entry_points, expected_contract_entry_points);
assert_eq!(contract_abi, expected_contract_abi);
}
Expand Down
10 changes: 10 additions & 0 deletions scripts/e2e-coverage.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
set -e

anvil --fork-url https://eth.merkle.io --fork-block-number 20395662 &

subshell() {
set -e
rm -f target/madara-* lcov.info
Expand All @@ -10,11 +12,19 @@ subshell() {
cargo build --bin madara --profile dev

export COVERAGE_BIN=$(realpath target/debug/madara)
export ETH_FORK_URL=https://eth.merkle.io

# wait for anvil
while ! nc -z localhost 8545; do
sleep 1
done

cargo test --profile dev

cargo llvm-cov report --lcov --output-path lcov.info
cargo llvm-cov report
}

(subshell && r=$?) || r=$?
pkill -P $$
exit $r
5 changes: 5 additions & 0 deletions scripts/e2e-tests.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ anvil --fork-url https://eth.merkle.io --fork-block-number 20395662 &
subshell() {
set -e
cargo build --bin madara --profile dev

export COVERAGE_BIN=$(realpath target/debug/madara)
export ETH_FORK_URL=https://eth.merkle.io

# wait for anvil
while ! nc -z localhost 8545; do
sleep 1
done

cargo test --profile dev $@
}

Expand Down

0 comments on commit 5758897

Please sign in to comment.