From d60c5003bf8b324727fa31c8d104a53e17d7a8c3 Mon Sep 17 00:00:00 2001 From: LLFourn Date: Wed, 19 May 2021 13:04:32 +1000 Subject: [PATCH] Merge testutils crate into the main crate This avoids having to keep the apis in sync between the macros and the main project. --- .github/workflows/cont_integration.yml | 2 +- Cargo.toml | 16 +++++----- src/blockchain/electrum.rs | 5 ++- src/blockchain/esplora.rs | 5 ++- src/lib.rs | 9 +++--- .../src => src/testutils}/blockchain_tests.rs | 25 ++++++++------- testutils/src/lib.rs => src/testutils/mod.rs | 32 +++++++++++-------- src/wallet/address_validator.rs | 2 +- src/wallet/mod.rs | 1 + testutils/.gitignore | 2 -- testutils/Cargo.toml | 26 --------------- 11 files changed, 50 insertions(+), 75 deletions(-) rename {testutils/src => src/testutils}/blockchain_tests.rs (97%) rename testutils/src/lib.rs => src/testutils/mod.rs (95%) delete mode 100644 testutils/.gitignore delete mode 100644 testutils/Cargo.toml diff --git a/.github/workflows/cont_integration.yml b/.github/workflows/cont_integration.yml index dd899cd4..9d6fe420 100644 --- a/.github/workflows/cont_integration.yml +++ b/.github/workflows/cont_integration.yml @@ -120,7 +120,7 @@ jobs: - name: start ${{ matrix.blockchain.name }} run: nohup ${{ matrix.blockchain.start }} & sleep 5 - name: Test - run: $HOME/.cargo/bin/cargo test --features test-${{ matrix.blockchain.name }} --no-default-features ${{ matrix.blockchain.name }}::bdk_blockchain_tests + run: $HOME/.cargo/bin/cargo test --features ${{ matrix.blockchain.name }},test-blockchains --no-default-features ${{ matrix.blockchain.name }}::bdk_blockchain_tests check-wasm: name: Check WASM diff --git a/Cargo.toml b/Cargo.toml index 44a5623c..dda31216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,10 @@ socks = { version = "0.3", optional = true } lazy_static = { version = "1.4", optional = true } tiny-bip39 = { version = "^0.8", optional = true } +# Needed by bdk_blockchain_tests macro +bitcoincore-rpc = { version = "0.13", optional = true } +serial_test = { version = "0.4", optional = true } + # Platform-specific dependencies [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tokio = { version = "1", features = ["rt"] } @@ -54,17 +58,16 @@ all-keys = ["keys-bip39"] keys-bip39 = ["tiny-bip39"] # Debug/Test features -test-electrum = ["electrum"] -test-esplora = ["esplora"] +testutils = [] +test-blockchains = ["testutils", "bitcoincore-rpc", "electrum-client"] test-md-docs = ["electrum"] [dev-dependencies] -bdk-testutils = { path = "./testutils" } -serial_test = "0.4" lazy_static = "1.4" env_logger = "0.7" base64 = "^0.11" clap = "2.33" +serial_test = "0.4" [[example]] name = "address_validator" @@ -78,10 +81,7 @@ path = "examples/compiler.rs" required-features = ["compiler"] [workspace] -members = ["macros", "testutils"] - -# Generate docs with nightly to add the "features required" badge -# https://stackoverflow.com/questions/61417452/how-to-get-a-feature-requirement-tag-in-the-documentation-generated-by-cargo-do +members = ["macros"] [package.metadata.docs.rs] features = ["compiler", "electrum", "esplora", "compact_filters", "key-value-db", "all-keys"] # defines the configuration attribute `docsrs` diff --git a/src/blockchain/electrum.rs b/src/blockchain/electrum.rs index 2e103307..a37f0556 100644 --- a/src/blockchain/electrum.rs +++ b/src/blockchain/electrum.rs @@ -169,9 +169,8 @@ impl ConfigurableBlockchain for ElectrumBlockchain { } } -#[cfg(all(feature = "test-electrum", test))] -testutils::bdk_blockchain_tests! { - bdk => crate, +#[cfg(feature = "test-blockchains")] +crate::bdk_blockchain_tests! { fn test_instance() -> ElectrumBlockchain { ElectrumBlockchain::from(Client::new(&testutils::get_electrum_url()).unwrap()) } diff --git a/src/blockchain/esplora.rs b/src/blockchain/esplora.rs index 5214fcec..ff85f22f 100644 --- a/src/blockchain/esplora.rs +++ b/src/blockchain/esplora.rs @@ -415,9 +415,8 @@ impl_error!(std::num::ParseIntError, Parsing, EsploraError); impl_error!(consensus::encode::Error, BitcoinEncoding, EsploraError); impl_error!(bitcoin::hashes::hex::Error, Hex, EsploraError); -#[cfg(all(feature = "test-esplora", test))] -testutils::bdk_blockchain_tests! { - bdk => crate, +#[cfg(feature = "test-blockchains")] +crate::bdk_blockchain_tests! { fn test_instance() -> EsploraBlockchain { EsploraBlockchain::new(std::env::var("BDK_ESPLORA_URL").unwrap_or("127.0.0.1:3002".into()).as_str(), None) } diff --git a/src/lib.rs b/src/lib.rs index a4670b5c..722f680a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -228,16 +228,12 @@ pub extern crate reqwest; #[cfg(feature = "key-value-db")] pub extern crate sled; -#[allow(unused_imports)] -#[cfg(test)] -#[macro_use] -extern crate testutils; #[allow(unused_imports)] #[cfg(test)] #[allow(unused_imports)] #[cfg(test)] #[macro_use] -extern crate serial_test; +pub extern crate serial_test; #[macro_use] pub(crate) mod error; @@ -265,3 +261,6 @@ pub use wallet::Wallet; pub fn version() -> &'static str { env!("CARGO_PKG_VERSION", "unknown") } + +#[cfg(any(feature = "testutils", test))] +pub mod testutils; diff --git a/testutils/src/blockchain_tests.rs b/src/testutils/blockchain_tests.rs similarity index 97% rename from testutils/src/blockchain_tests.rs rename to src/testutils/blockchain_tests.rs index 07953a3d..1c470f05 100644 --- a/testutils/src/blockchain_tests.rs +++ b/src/testutils/blockchain_tests.rs @@ -3,18 +3,19 @@ /// the setup required to run the tests yourself. #[macro_export] macro_rules! bdk_blockchain_tests { - (bdk => $bdk:ident, + ( fn test_instance() -> $blockchain:ty $block:block) => { + #[cfg(test)] mod bdk_blockchain_tests { - use $bdk::bitcoin::Network; - use $bdk::miniscript::Descriptor; - use $crate::{TestClient, serial}; - use $bdk::blockchain::{Blockchain, noop_progress}; - use $bdk::descriptor::ExtendedDescriptor; - use $bdk::database::MemoryDatabase; - use $bdk::types::KeychainKind; - use $bdk::{Wallet, TxBuilder, FeeRate}; - use $bdk::wallet::AddressIndex::New; + use $crate::bitcoin::Network; + use $crate::testutils::{TestClient}; + use $crate::blockchain::noop_progress; + use $crate::database::MemoryDatabase; + use $crate::types::KeychainKind; + use $crate::{Wallet, FeeRate}; + use $crate::wallet::AddressIndex::New; + use $crate::testutils; + use $crate::serial_test::serial; use super::*; @@ -195,7 +196,7 @@ macro_rules! bdk_blockchain_tests { // FIXME: I would like this to be cfg_attr(not(feature = "test-esplora"), ignore) but it // doesn't work for some reason. - #[cfg(not(feature = "test-esplora"))] + #[cfg(not(feature = "esplora"))] #[test] #[serial] fn test_sync_reorg_block() { @@ -478,7 +479,7 @@ macro_rules! bdk_blockchain_tests { #[test] #[serial] fn test_sync_receive_coinbase() { - let (wallet, descriptors, mut test_client) = init_single_sig(); + let (wallet, _, mut test_client) = init_single_sig(); let wallet_addr = wallet.get_address(New).unwrap(); wallet.sync(noop_progress(), None).unwrap(); diff --git a/testutils/src/lib.rs b/src/testutils/mod.rs similarity index 95% rename from testutils/src/lib.rs rename to src/testutils/mod.rs index 333af54f..4771c946 100644 --- a/testutils/src/lib.rs +++ b/src/testutils/mod.rs @@ -8,12 +8,11 @@ // , at your option. // You may not use this file except in accordance with one or both of these // licenses. +#![allow(missing_docs)] -#[macro_use] -extern crate serde_json; mod blockchain_tests; -pub use serial_test::serial; +// pub use serial_test::serial; use std::collections::HashMap; use std::env; @@ -138,13 +137,14 @@ impl TranslateDescriptor for Descriptor { } } +#[doc(hidden)] #[macro_export] macro_rules! testutils { ( @external $descriptors:expr, $child:expr ) => ({ use bitcoin::secp256k1::Secp256k1; use miniscript::descriptor::{Descriptor, DescriptorPublicKey, DescriptorTrait}; - use $crate::TranslateDescriptor; + use $crate::testutils::TranslateDescriptor; let secp = Secp256k1::new(); @@ -155,7 +155,7 @@ macro_rules! testutils { use bitcoin::secp256k1::Secp256k1; use miniscript::descriptor::{Descriptor, DescriptorPublicKey, DescriptorTrait}; - use $crate::TranslateDescriptor; + use $crate::testutils::TranslateDescriptor; let secp = Secp256k1::new(); @@ -167,25 +167,27 @@ macro_rules! testutils { ( @tx ( $( ( $( $addr:tt )* ) => $amount:expr ),+ ) $( ( @locktime $locktime:expr ) )* $( ( @confirmations $confirmations:expr ) )* $( ( @replaceable $replaceable:expr ) )* ) => ({ let mut outs = Vec::new(); - $( outs.push(testutils::TestIncomingOutput::new($amount, testutils!( $($addr)* ))); )+ - + $( outs.push($crate::testutils::TestIncomingOutput::new($amount, testutils!( $($addr)* ))); )+ + #[allow(unused_mut)] let mut locktime = None::; $( locktime = Some($locktime); )* + #[allow(unused_assignments, unused_mut)] let mut min_confirmations = None::; $( min_confirmations = Some($confirmations); )* + #[allow(unused_assignments, unused_mut)] let mut replaceable = None::; $( replaceable = Some($replaceable); )* - testutils::TestIncomingTx::new(outs, min_confirmations, locktime, replaceable) + $crate::testutils::TestIncomingTx::new(outs, min_confirmations, locktime, replaceable) }); ( @literal $key:expr ) => ({ let key = $key.to_string(); (key, None::, None::) }); - ( @generate_xprv $( $external_path:expr )* $( ,$internal_path:expr )* ) => ({ + ( @generate_xprv $( $external_path:expr )? $( ,$internal_path:expr )? ) => ({ use rand::Rng; let mut seed = [0u8; 32]; @@ -196,11 +198,13 @@ macro_rules! testutils { &seed, ); + #[allow(unused_assignments)] let mut external_path = None::; - $( external_path = Some($external_path.to_string()); )* + $( external_path = Some($external_path.to_string()); )? + #[allow(unused_assignments)] let mut internal_path = None::; - $( internal_path = Some($internal_path.to_string()); )* + $( internal_path = Some($internal_path.to_string()); )? (key.unwrap().to_string(), external_path, internal_path) }); @@ -230,11 +234,10 @@ macro_rules! testutils { ( @descriptors ( $external_descriptor:expr ) $( ( $internal_descriptor:expr ) )* $( ( @keys $( $keys:tt )* ) )* ) => ({ use std::str::FromStr; use std::collections::HashMap; - use std::convert::TryInto; - - use miniscript::descriptor::{Descriptor, DescriptorPublicKey}; + use miniscript::descriptor::Descriptor; use miniscript::TranslatePk; + #[allow(unused_assignments, unused_mut)] let mut keys: HashMap<&'static str, (String, Option, Option)> = HashMap::new(); $( keys = testutils!{ @keys $( $keys )* }; @@ -257,6 +260,7 @@ macro_rules! testutils { }); let external = external.to_string(); + #[allow(unused_assignments, unused_mut)] let mut internal = None::; $( let string_internal: Descriptor = FromStr::from_str($internal_descriptor).unwrap(); diff --git a/src/wallet/address_validator.rs b/src/wallet/address_validator.rs index 4e20ef5e..36e39be1 100644 --- a/src/wallet/address_validator.rs +++ b/src/wallet/address_validator.rs @@ -146,7 +146,7 @@ mod test { let (mut wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); wallet.add_address_validator(Arc::new(TestValidator)); - let addr = testutils!(@external descriptors, 10); + let addr = crate::testutils!(@external descriptors, 10); let mut builder = wallet.build_tx(); builder.add_recipient(addr.script_pubkey(), 25_000); builder.finish().unwrap(); diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 71fc5d7d..28d954e4 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -1515,6 +1515,7 @@ pub(crate) mod test { use crate::types::KeychainKind; use super::*; + use crate::testutils; use crate::wallet::AddressIndex::{LastUnused, New, Peek, Reset}; #[test] diff --git a/testutils/.gitignore b/testutils/.gitignore deleted file mode 100644 index 2c96eb1b..00000000 --- a/testutils/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target/ -Cargo.lock diff --git a/testutils/Cargo.toml b/testutils/Cargo.toml deleted file mode 100644 index 8f7f2618..00000000 --- a/testutils/Cargo.toml +++ /dev/null @@ -1,26 +0,0 @@ -[package] -name = "bdk-testutils" -version = "0.4.0" -authors = ["Alekos Filini "] -edition = "2018" -homepage = "https://bitcoindevkit.org" -repository = "https://github.com/bitcoindevkit/bdk" -documentation = "https://docs.rs/bdk-testutils" -description = "Supporting testing utilities for `bdk`" -keywords = ["bdk"] -license = "MIT OR Apache-2.0" - -[lib] -name = "testutils" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -log = "0.4.8" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -serial_test = "0.4" -bitcoin = "0.26" -bitcoincore-rpc = "0.13" -miniscript = "5.1" -electrum-client = "0.6.0"