Skip to content

Commit

Permalink
fix: report config error on invalid artifact (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
agostbiro authored Oct 22, 2024
1 parent 3700823 commit 7aaf746
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
23 changes: 3 additions & 20 deletions crates/edr_napi/src/solidity_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ mod config;
mod runner;
mod test_results;

use std::{collections::BTreeMap, path::Path, sync::Arc};
use std::{path::Path, sync::Arc};

use artifact::Artifact;
use edr_solidity_tests::{contracts::ContractsByArtifact, TestFilter};
use edr_solidity_tests::TestFilter;
use napi::{
threadsafe_function::{
ErrorStrategy, ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode,
Expand Down Expand Up @@ -52,31 +52,14 @@ pub fn run_solidity_tests(
|ctx: ThreadSafeCallContext<napi::Error>| Ok(vec![ctx.value]),
)?;

let known_contracts: ContractsByArtifact = artifacts
.into_iter()
.map(|item| Ok((item.id.try_into()?, item.contract.try_into()?)))
.collect::<Result<
BTreeMap<
edr_solidity_tests::contracts::ArtifactId,
edr_solidity_tests::contracts::ContractData,
>,
napi::Error,
>>()?
.into();

let test_suites = test_suites
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()?;

let (tx_results, mut rx_results) = tokio::sync::mpsc::unbounded_channel::<(
edr_solidity_tests::contracts::ArtifactId,
edr_solidity_tests::result::SuiteResult,
)>();

let runtime = runtime::Handle::current();
runtime.spawn(async move {
let runner = match build_runner(known_contracts, test_suites, config_args).await {
let runner = match build_runner(artifacts, test_suites, config_args).await {
Ok(runner) => runner,
Err(error) => {
let call_status =
Expand Down
24 changes: 20 additions & 4 deletions crates/edr_napi/src/solidity_tests/runner.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
use std::collections::BTreeMap;

use edr_solidity_tests::{
contracts::{ArtifactId, ContractsByArtifact},
contracts::{ArtifactId, ContractData, ContractsByArtifact},
decode::RevertDecoder,
multi_runner::{TestContract, TestContracts},
MultiContractRunner, SolidityTestRunnerConfig,
};

use crate::solidity_tests::config::SolidityTestRunnerConfigArgs;
use crate::solidity_tests::{
artifact::{Artifact as JsArtifact, ArtifactId as JsArtifactId},
config::SolidityTestRunnerConfigArgs,
};

pub(super) async fn build_runner(
known_contracts: ContractsByArtifact,
test_suites: Vec<ArtifactId>,
artifacts: Vec<JsArtifact>,
test_suites: Vec<JsArtifactId>,
config_args: SolidityTestRunnerConfigArgs,
) -> napi::Result<MultiContractRunner> {
let known_contracts: ContractsByArtifact = artifacts
.into_iter()
.map(|item| Ok((item.id.try_into()?, item.contract.try_into()?)))
.collect::<Result<BTreeMap<ArtifactId, ContractData>, napi::Error>>()?
.into();

let test_suites = test_suites
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<ArtifactId>, _>>()?;

let config: SolidityTestRunnerConfig = config_args.try_into()?;

// Build revert decoder from ABIs of all artifacts.
Expand Down
19 changes: 19 additions & 0 deletions crates/edr_napi/test/solidity-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ describe("Solidity Tests", () => {
Error
);
});

it("error callback is called if contract bytecode is invalid", async function () {
const artifacts = [
loadContract("./artifacts/SetupConsistencyCheck.json"),
loadContract("./artifacts/PaymentFailureTest.json"),
];
// All artifacts are test suites.
const testSuites = artifacts.map((artifact) => artifact.id);
const config = {
projectRoot: __dirname,
};

artifacts[0].contract.bytecode = "invalid bytecode";

await assert.isRejected(
runAllSolidityTests(artifacts, testSuites, config),
"Invalid hex bytecode for contract"
);
});
});

// Load a contract built with Hardhat into a test suite
Expand Down

0 comments on commit 7aaf746

Please sign in to comment.