Skip to content

Commit 6173159

Browse files
Put test utilities behind a feature flag.
The utilities will still be part of cfg(test). The purpose of this is to enable other crates in the workspace to use the test utilities.
1 parent f3ab31e commit 6173159

File tree

7 files changed

+28
-13
lines changed

7 files changed

+28
-13
lines changed

lightning/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Still missing tons of error-handling. See GitHub issues for suggested projects i
1212

1313
[features]
1414
fuzztarget = ["bitcoin/fuzztarget"]
15+
_test_utils = ["hex", "regex"]
1516
# Unlog messages superior at targeted level.
1617
max_level_off = []
1718
max_level_error = []
@@ -25,6 +26,9 @@ unsafe_revoked_tx_signing = []
2526
[dependencies]
2627
bitcoin = "0.24"
2728

29+
hex = { version = "0.3", optional = true }
30+
regex = { version = "0.1.80", optional = true }
31+
2832
[dev-dependencies.bitcoin]
2933
version = "0.24"
3034
features = ["bitcoinconsensus"]

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use std::io::Error;
5757

5858
/// An update generated by the underlying Channel itself which contains some new information the
5959
/// ChannelMonitor should be made aware of.
60-
#[cfg_attr(test, derive(PartialEq))]
60+
#[cfg_attr(any(test, feature = "_test_utils"), derive(PartialEq))]
6161
#[derive(Clone)]
6262
#[must_use]
6363
pub struct ChannelMonitorUpdate {
@@ -470,7 +470,7 @@ enum OnchainEvent {
470470
const SERIALIZATION_VERSION: u8 = 1;
471471
const MIN_SERIALIZATION_VERSION: u8 = 1;
472472

473-
#[cfg_attr(test, derive(PartialEq))]
473+
#[cfg_attr(any(test, feature = "_test_utils"), derive(PartialEq))]
474474
#[derive(Clone)]
475475
pub(crate) enum ChannelMonitorUpdateStep {
476476
LatestHolderCommitmentTXInfo {
@@ -696,7 +696,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
696696
secp_ctx: Secp256k1<secp256k1::All>, //TODO: dedup this a bit...
697697
}
698698

699-
#[cfg(any(test, feature = "fuzztarget"))]
699+
#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
700700
/// Used only in testing and fuzztarget to check serialization roundtrips don't change the
701701
/// underlying object
702702
impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {

lightning/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! generated/etc. This makes it a good candidate for tight integration into an existing wallet
1919
//! instead of having a rather-separate lightning appendage to a wallet.
2020
21-
#![cfg_attr(not(feature = "fuzztarget"), deny(missing_docs))]
21+
#![cfg_attr(not(any(feature = "fuzztarget", feature = "_test_utils")), deny(missing_docs))]
2222
#![forbid(unsafe_code)]
2323

2424
// In general, rust is absolutely horrid at supporting users doing things like,
@@ -28,8 +28,8 @@
2828
#![allow(ellipsis_inclusive_range_patterns)]
2929

3030
extern crate bitcoin;
31-
#[cfg(test)] extern crate hex;
32-
#[cfg(test)] extern crate regex;
31+
#[cfg(any(test, feature = "_test_utils"))] extern crate hex;
32+
#[cfg(any(test, feature = "_test_utils"))] extern crate regex;
3333

3434
#[macro_use]
3535
pub mod util;

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref,
404404
last_block_hash: Mutex<BlockHash>,
405405
secp_ctx: Secp256k1<secp256k1::All>,
406406

407-
#[cfg(test)]
407+
#[cfg(any(test, feature = "_test_utils"))]
408408
pub(super) channel_state: Mutex<ChannelHolder<ChanSigner>>,
409-
#[cfg(not(test))]
409+
#[cfg(not(any(test, feature = "_test_utils")))]
410410
channel_state: Mutex<ChannelHolder<ChanSigner>>,
411411
our_network_key: SecretKey,
412412

lightning/src/ln/functional_test_utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ macro_rules! get_event_msg {
260260
}
261261
}
262262

263+
#[allow(unused_macros)]
263264
macro_rules! get_htlc_update_msgs {
264265
($node: expr, $node_id: expr) => {
265266
{
@@ -276,6 +277,7 @@ macro_rules! get_htlc_update_msgs {
276277
}
277278
}
278279

280+
#[allow(unused_macros)]
279281
macro_rules! get_feerate {
280282
($node: expr, $channel_id: expr) => {
281283
{
@@ -286,6 +288,7 @@ macro_rules! get_feerate {
286288
}
287289
}
288290

291+
#[allow(unused_macros)]
289292
macro_rules! get_local_commitment_txn {
290293
($node: expr, $channel_id: expr) => {
291294
{
@@ -324,6 +327,8 @@ macro_rules! unwrap_send_err {
324327
}
325328
}
326329

330+
/// Check whether N channel monitor(s) have been added.
331+
#[macro_export]
327332
macro_rules! check_added_monitors {
328333
($node: expr, $count: expr) => {
329334
{
@@ -538,6 +543,9 @@ macro_rules! get_closing_signed_broadcast {
538543
}
539544
}
540545

546+
/// Check that a channel's closing channel update has been broadcasted, and optionally
547+
/// check whether an error message event has occurred.
548+
#[macro_export]
541549
macro_rules! check_closed_broadcast {
542550
($node: expr, $with_error_msg: expr) => {{
543551
let events = $node.node.get_and_clear_pending_msg_events();
@@ -764,6 +772,7 @@ macro_rules! expect_pending_htlcs_forwardable {
764772
}}
765773
}
766774

775+
#[allow(unused_macros)]
767776
macro_rules! expect_payment_received {
768777
($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
769778
let events = $node.node.get_and_clear_pending_events();
@@ -792,6 +801,7 @@ macro_rules! expect_payment_sent {
792801
}
793802
}
794803

804+
#[allow(unused_macros)]
795805
macro_rules! expect_payment_failed {
796806
($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr $(, $expected_error_code: expr, $expected_error_data: expr)*) => {
797807
let events = $node.node.get_and_clear_pending_events();
@@ -1269,6 +1279,7 @@ pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec<Node<'a, 'b,
12691279
}
12701280
}
12711281

1282+
#[allow(unused_macros)]
12721283
macro_rules! get_channel_value_stat {
12731284
($node: expr, $channel_id: expr) => {{
12741285
let chan_lock = $node.node.channel_state.lock().unwrap();

lightning/src/ln/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ mod wire;
3838
// without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
3939
// about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
4040

41-
#[cfg(test)]
41+
#[cfg(any(test, feature = "_test_utils"))]
4242
#[macro_use]
43-
pub(crate) mod functional_test_utils;
43+
pub mod functional_test_utils;
4444
#[cfg(test)]
4545
#[allow(unused_mut)]
4646
mod functional_tests;

lightning/src/util/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ pub(crate) mod macro_logger;
3232
pub mod logger;
3333
pub mod config;
3434

35-
#[cfg(test)]
36-
pub(crate) mod test_utils;
35+
#[cfg(any(test, feature = "_test_utils"))]
36+
pub mod test_utils;
3737

3838
/// impls of traits that add exra enforcement on the way they're called. Useful for detecting state
3939
/// machine errors and used in fuzz targets and tests.
40-
#[cfg(any(test, feature = "fuzztarget"))]
40+
#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
4141
pub mod enforcing_trait_impls;

0 commit comments

Comments
 (0)