Skip to content

Commit 4350cc6

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 8a79877 commit 4350cc6

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
@@ -405,9 +405,9 @@ pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref,
405405
last_block_hash: Mutex<BlockHash>,
406406
secp_ctx: Secp256k1<secp256k1::All>,
407407

408-
#[cfg(test)]
408+
#[cfg(any(test, feature = "_test_utils"))]
409409
pub(super) channel_state: Mutex<ChannelHolder<ChanSigner>>,
410-
#[cfg(not(test))]
410+
#[cfg(not(any(test, feature = "_test_utils")))]
411411
channel_state: Mutex<ChannelHolder<ChanSigner>>,
412412
our_network_key: SecretKey,
413413

lightning/src/ln/functional_test_utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ macro_rules! get_event_msg {
263263
}
264264
}
265265

266+
#[cfg(test)]
266267
macro_rules! get_htlc_update_msgs {
267268
($node: expr, $node_id: expr) => {
268269
{
@@ -279,6 +280,7 @@ macro_rules! get_htlc_update_msgs {
279280
}
280281
}
281282

283+
#[cfg(test)]
282284
macro_rules! get_feerate {
283285
($node: expr, $channel_id: expr) => {
284286
{
@@ -289,6 +291,7 @@ macro_rules! get_feerate {
289291
}
290292
}
291293

294+
#[cfg(test)]
292295
macro_rules! get_local_commitment_txn {
293296
($node: expr, $channel_id: expr) => {
294297
{
@@ -327,6 +330,8 @@ macro_rules! unwrap_send_err {
327330
}
328331
}
329332

333+
/// Check whether N channel monitor(s) have been added.
334+
#[macro_export]
330335
macro_rules! check_added_monitors {
331336
($node: expr, $count: expr) => {
332337
{
@@ -553,6 +558,9 @@ macro_rules! get_closing_signed_broadcast {
553558
}
554559
}
555560

561+
/// Check that a channel's closing channel update has been broadcasted, and optionally
562+
/// check whether an error message event has occurred.
563+
#[macro_export]
556564
macro_rules! check_closed_broadcast {
557565
($node: expr, $with_error_msg: expr) => {{
558566
let events = $node.node.get_and_clear_pending_msg_events();
@@ -779,6 +787,7 @@ macro_rules! expect_pending_htlcs_forwardable {
779787
}}
780788
}
781789

790+
#[cfg(test)]
782791
macro_rules! expect_payment_received {
783792
($node: expr, $expected_payment_hash: expr, $expected_recv_value: expr) => {
784793
let events = $node.node.get_and_clear_pending_events();
@@ -807,6 +816,7 @@ macro_rules! expect_payment_sent {
807816
}
808817
}
809818

819+
#[cfg(test)]
810820
macro_rules! expect_payment_failed {
811821
($node: expr, $expected_payment_hash: expr, $rejected_by_dest: expr $(, $expected_error_code: expr, $expected_error_data: expr)*) => {
812822
let events = $node.node.get_and_clear_pending_events();
@@ -1284,6 +1294,7 @@ pub fn get_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec<Node<'a, 'b,
12841294
}
12851295
}
12861296

1297+
#[cfg(test)]
12871298
macro_rules! get_channel_value_stat {
12881299
($node: expr, $channel_id: expr) => {{
12891300
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)