Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
add providers feature guard in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Aug 5, 2023
1 parent 7693a01 commit 2a88d2c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
30 changes: 25 additions & 5 deletions ethers-contract/tests/it/abigen.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
//! Test cases to validate the `abigen!` macro

use ethers_contract::{abigen, ContractError, EthCall, EthError, EthEvent};
use ethers_contract::{abigen, EthEvent};
use ethers_core::{
abi::{AbiDecode, AbiEncode, Address, Tokenizable},
abi::{AbiDecode, AbiEncode, Tokenizable},
rand::thread_rng,
types::{Bytes, U256},
utils::Anvil,
};
use ethers_providers::{MockProvider, Provider};
use ethers_signers::{LocalWallet, Signer};
use std::{fmt::Debug, hash::Hash, str::FromStr, sync::Arc};
use std::{fmt::Debug, hash::Hash, str::FromStr};

#[cfg(feature = "providers")]
use ethers_contract::{ContractError, EthCall, EthError};
#[cfg(feature = "providers")]
use ethers_core::{abi::Address, utils::Anvil};
#[cfg(feature = "providers")]
use ethers_providers::{MockProvider, Provider};
#[cfg(feature = "providers")]
use std::sync::Arc;

const fn assert_codec<T: AbiDecode + AbiEncode>() {}
#[cfg(feature = "providers")]
const fn assert_tokenizeable<T: Tokenizable>() {}
const fn assert_call<T: AbiEncode + AbiDecode + Default + Tokenizable>() {}
const fn assert_event<T: EthEvent>() {}
Expand Down Expand Up @@ -148,6 +156,7 @@ fn can_generate_internal_structs_2() {
}

#[test]
#[cfg(feature = "providers")]
fn can_generate_internal_structs_multiple() {
// NOTE: nesting here is necessary due to how tests are structured...
use contract::*;
Expand Down Expand Up @@ -221,6 +230,7 @@ fn can_generate_return_struct() {
}

#[test]
#[cfg(feature = "providers")]
fn can_generate_human_readable_with_structs() {
abigen!(
SimpleContract,
Expand Down Expand Up @@ -268,6 +278,7 @@ fn can_generate_human_readable_with_structs() {
}

#[test]
#[cfg(feature = "providers")]
fn can_handle_overloaded_functions() {
abigen!(
SimpleContract,
Expand Down Expand Up @@ -383,6 +394,7 @@ fn can_handle_unique_underscore_functions() {
}

#[test]
#[cfg(feature = "providers")]
fn can_handle_underscore_numeric() {
abigen!(
Test,
Expand Down Expand Up @@ -426,6 +438,7 @@ fn can_abican_generate_console_sol() {
}

#[test]
#[cfg(feature = "providers")]
fn can_generate_nested_types() {
abigen!(
Test,
Expand Down Expand Up @@ -453,6 +466,7 @@ fn can_generate_nested_types() {
}

#[test]
#[cfg(feature = "providers")]
fn can_handle_different_calls() {
abigen!(
Test,
Expand All @@ -470,6 +484,7 @@ fn can_handle_different_calls() {
}

#[test]
#[cfg(feature = "providers")]
fn can_handle_case_sensitive_calls() {
abigen!(
StakedOHM,
Expand All @@ -487,6 +502,7 @@ fn can_handle_case_sensitive_calls() {
}

#[tokio::test]
#[cfg(feature = "providers")]
async fn can_deploy_greeter() {
abigen!(Greeter, "ethers-contract/tests/solidity-contracts/greeter.json",);
let anvil = Anvil::new().spawn();
Expand All @@ -505,6 +521,7 @@ async fn can_deploy_greeter() {
}

#[tokio::test]
#[cfg(feature = "providers")]
async fn can_abiencoderv2_output() {
abigen!(AbiEncoderv2Test, "ethers-contract/tests/solidity-contracts/Abiencoderv2Test.json");

Expand Down Expand Up @@ -568,6 +585,7 @@ fn can_handle_overloaded_events() {

#[tokio::test]
#[cfg(not(feature = "celo"))]
#[cfg(feature = "providers")]
async fn can_send_struct_param() {
abigen!(StructContract, "./tests/solidity-contracts/StructContract.json");

Expand All @@ -592,6 +610,7 @@ async fn can_send_struct_param() {
}

#[test]
#[cfg(feature = "providers")]
fn can_generate_seaport_1_0() {
abigen!(Seaport, "./tests/solidity-contracts/seaport_1_0.json");

Expand Down Expand Up @@ -768,6 +787,7 @@ fn can_handle_overloaded_function_with_array() {
}

#[test]
#[cfg(feature = "providers")]
#[allow(clippy::disallowed_names)]
fn convert_uses_correct_abi() {
abigen!(
Expand Down
14 changes: 12 additions & 2 deletions ethers-contract/tests/it/common.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use ethers_contract::{Contract, ContractFactory, EthEvent};
use ethers_contract::EthEvent;
use ethers_core::types::Address;

#[cfg(feature = "providers")]
use ethers_contract::{Contract, ContractFactory};
#[cfg(feature = "providers")]
use ethers_core::{
abi::{Abi, JsonAbi},
types::{Address, Bytes},
types::Bytes,
utils::AnvilInstance,
};
#[cfg(feature = "providers")]
use ethers_providers::{Http, Middleware, Provider};
#[cfg(feature = "providers")]
use std::{convert::TryFrom, fs, sync::Arc, time::Duration};

// Note: The `EthEvent` derive macro implements the necessary conversion between `Tokens` and
Expand All @@ -21,6 +28,7 @@ pub struct ValueChanged {
}

/// Gets the contract ABI and bytecode from a JSON file
#[cfg(feature = "providers")]
#[track_caller]
pub fn get_contract(filename: &str) -> (Abi, Bytes) {
let path = format!("./tests/solidity-contracts/{filename}");
Expand All @@ -34,6 +42,7 @@ pub fn get_contract(filename: &str) -> (Abi, Bytes) {
}

/// connects the private key to http://localhost:8545
#[cfg(feature = "providers")]
pub fn connect(anvil: &AnvilInstance, idx: usize) -> Arc<Provider<Http>> {
let sender = anvil.addresses()[idx];
let provider = Provider::<Http>::try_from(anvil.endpoint())
Expand All @@ -44,6 +53,7 @@ pub fn connect(anvil: &AnvilInstance, idx: usize) -> Arc<Provider<Http>> {
}

/// Launches a Anvil instance and deploys the SimpleStorage contract
#[cfg(feature = "providers")]
pub async fn deploy<M: Middleware>(client: Arc<M>, abi: Abi, bytecode: Bytes) -> Contract<M> {
let factory = ContractFactory::new(abi, bytecode, client);
let deployer = factory.deploy("initial value".to_string()).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion ethers-contract/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ mod abigen;

mod derive;

#[cfg(feature = "providers")]
mod contract_call;

mod eip712;

#[cfg(all(not(target_arch = "wasm32"), not(feature = "celo")))]
mod common;

#[cfg(all(not(target_arch = "wasm32"), not(feature = "celo")))]
#[cfg(all(feature = "providers", not(target_arch = "wasm32"), not(feature = "celo")))]
mod contract;

0 comments on commit 2a88d2c

Please sign in to comment.