Skip to content

Commit 205890f

Browse files
committed
fix: address PR issues
- fix incorrect paths in tests - fix making pub(crate) previously internal fields for test extension traits by implementing the trait alongside the type (at the cost of mixing tests tools code with production code, but we keep the benefit of the trait import tagged as a test tool) - update `mithril_common::test` module doc - fix `FakeCertificaterRetriever` async trait use in wasm - remove `cfg_test_tools` macro to limit additional uses until the `test_tools` feature is removed
1 parent d9b1851 commit 205890f

File tree

8 files changed

+87
-86
lines changed

8 files changed

+87
-86
lines changed

mithril-common/src/crypto_helper/cardano/key_certification.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub enum ProtocolInitializerErrorWrapper {
101101
#[derive(Debug, Clone, Serialize, Deserialize)]
102102
pub struct StmInitializerWrapper {
103103
/// The Initializer
104-
pub(crate) stm_initializer: Initializer,
104+
stm_initializer: Initializer,
105105

106106
/// The KES signature over the Mithril key
107107
///
@@ -292,6 +292,19 @@ impl KeyRegWrapper {
292292
}
293293
}
294294

295+
#[cfg(any(test, feature = "test_tools"))]
296+
mod test_extensions {
297+
use crate::test::crypto_helper::ProtocolInitializerTestExtension;
298+
299+
use super::*;
300+
301+
impl ProtocolInitializerTestExtension for StmInitializerWrapper {
302+
fn override_protocol_parameters(&mut self, protocol_parameters: &ProtocolParameters) {
303+
self.stm_initializer.params = protocol_parameters.to_owned();
304+
}
305+
}
306+
}
307+
295308
#[cfg(test)]
296309
mod test {
297310
use crate::crypto_helper::cardano::kes::KesSignerStandard;

mithril-common/src/entities/block_range.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub type BlockRangeLength = BlockNumber;
1919
/// BlockRange for the Cardano chain
2020
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Debug, Hash)]
2121
pub struct BlockRange {
22-
pub(crate) inner_range: Range<BlockNumber>,
22+
inner_range: Range<BlockNumber>,
2323
}
2424

2525
impl BlockRange {
@@ -201,6 +201,38 @@ impl ExactSizeIterator for BlockRangesSequence {
201201
}
202202
}
203203

204+
#[cfg(any(test, feature = "test_tools"))]
205+
mod test_extensions {
206+
use crate::test::entities_extensions::BlockRangeTestExtension;
207+
208+
use super::*;
209+
210+
impl BlockRangeTestExtension for BlockRange {
211+
fn new(start: u64, end: u64) -> Self {
212+
Self {
213+
inner_range: BlockNumber(start)..BlockNumber(end),
214+
}
215+
}
216+
217+
fn try_add(&self, other: &BlockRange) -> StdResult<BlockRange> {
218+
if self.inner_range.end.max(other.inner_range.end)
219+
< self.inner_range.start.min(other.inner_range.start)
220+
{
221+
return Err(anyhow!(
222+
"BlockRange cannot be added as they don't strictly overlap"
223+
));
224+
}
225+
226+
Ok(Self {
227+
inner_range: Range {
228+
start: self.inner_range.start.min(other.inner_range.start),
229+
end: self.inner_range.end.max(other.inner_range.end),
230+
},
231+
})
232+
}
233+
}
234+
}
235+
204236
#[cfg(test)]
205237
mod tests {
206238
use std::ops::Not;
@@ -234,6 +266,22 @@ mod tests {
234266
assert!(BlockRange::new(1, 11) < BlockRange::new(2, 10));
235267
}
236268

269+
#[test]
270+
fn test_block_range_try_add() {
271+
assert_eq!(
272+
BlockRange::new(1, 10).try_add(&BlockRange::new(1, 10)).unwrap(),
273+
BlockRange::new(1, 10)
274+
);
275+
assert_eq!(
276+
BlockRange::new(1, 10).try_add(&BlockRange::new(1, 11)).unwrap(),
277+
BlockRange::new(1, 11)
278+
);
279+
assert_eq!(
280+
BlockRange::new(1, 10).try_add(&BlockRange::new(2, 10)).unwrap(),
281+
BlockRange::new(1, 10)
282+
);
283+
}
284+
237285
#[test]
238286
fn test_block_range_start() {
239287
assert_eq!(BlockRange::start(BlockNumber(0)), 0);

mithril-common/src/lib.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@
99
//! - [certificate chain][certificate_chain] used to validate the Certificate Chain created by an aggregator
1010
//! - The [entities] used by, and exchanged between, the aggregator, signers and client.
1111
12-
macro_rules! cfg_test_tools {
13-
($($item:item)*) => {
14-
$(
15-
#[cfg(any(test, feature = "test_tools"))]
16-
#[cfg_attr(docsrs, doc(cfg(feature = "test_tools")))]
17-
$item
18-
)*
19-
}
20-
}
21-
2212
pub mod api_version;
2313
pub mod certificate_chain;
2414
pub mod crypto_helper;
@@ -28,9 +18,9 @@ pub mod messages;
2818
pub mod protocol;
2919
pub mod signable_builder;
3020

31-
cfg_test_tools! {
32-
pub mod test;
33-
}
21+
#[cfg(any(test, feature = "test_tools"))]
22+
#[cfg_attr(docsrs, doc(cfg(feature = "test_tools")))]
23+
pub mod test;
3424

3525
pub use entities::{CardanoNetwork, MagicId};
3626

mithril-common/src/test/crypto_helper/cardano/extensions.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
use anyhow::Context;
22

33
use crate::StdResult;
4-
use crate::crypto_helper::{
5-
MKProof, MKTree, MKTreeNode, MKTreeStoreInMemory, ProtocolInitializer, ProtocolParameters,
6-
};
4+
use crate::crypto_helper::{MKProof, MKTree, MKTreeNode, MKTreeStoreInMemory, ProtocolParameters};
75

8-
/// Extension trait adding test utilities to [ProtocolInitializer]
6+
/// Extension trait adding test utilities to [ProtocolInitializer][crate::crypto_helper::ProtocolInitializer]
97
pub trait ProtocolInitializerTestExtension {
108
/// `TEST ONLY` - Override the protocol parameters of the `Initializer`
119
fn override_protocol_parameters(&mut self, protocol_parameters: &ProtocolParameters);
1210
}
1311

14-
impl ProtocolInitializerTestExtension for ProtocolInitializer {
15-
fn override_protocol_parameters(&mut self, protocol_parameters: &ProtocolParameters) {
16-
self.stm_initializer.params = protocol_parameters.to_owned();
17-
}
18-
}
19-
2012
/// Extension trait adding test utilities to [MKProof]
2113
pub trait MKProofTestExtension {
2214
/// `TEST ONLY` - Build a [MKProof] based on the given leaves

mithril-common/src/test/double/certificate_retriever.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ impl FakeCertificaterRetriever {
2727
}
2828
}
2929

30-
#[async_trait]
30+
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
31+
#[cfg_attr(not(target_family = "wasm"), async_trait)]
3132
impl CertificateRetriever for FakeCertificaterRetriever {
3233
async fn get_certificate_details(
3334
&self,

mithril-common/src/test/entities_extensions.rs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! A set of extension traits to add test utilities to this crate `entities`
22
33
use std::collections::HashMap;
4-
use std::ops::Range;
5-
6-
use anyhow::anyhow;
74

85
use crate::StdResult;
96
use crate::crypto_helper::{
@@ -24,31 +21,6 @@ pub trait BlockRangeTestExtension {
2421
fn try_add(&self, other: &BlockRange) -> StdResult<BlockRange>;
2522
}
2623

27-
impl BlockRangeTestExtension for BlockRange {
28-
fn new(start: u64, end: u64) -> Self {
29-
Self {
30-
inner_range: BlockNumber(start)..BlockNumber(end),
31-
}
32-
}
33-
34-
fn try_add(&self, other: &BlockRange) -> StdResult<BlockRange> {
35-
if self.inner_range.end.max(other.inner_range.end)
36-
< self.inner_range.start.min(other.inner_range.start)
37-
{
38-
return Err(anyhow!(
39-
"BlockRange cannot be added as they don't strictly overlap"
40-
));
41-
}
42-
43-
Ok(Self {
44-
inner_range: Range {
45-
start: self.inner_range.start.min(other.inner_range.start),
46-
end: self.inner_range.end.max(other.inner_range.end),
47-
},
48-
})
49-
}
50-
}
51-
5224
/// Extension trait adding test utilities to [CardanoTransactionsSetProof]
5325
pub trait CardanoTransactionsSetProofTestExtension {
5426
/// `TEST ONLY` - Helper to create a proof from a list of leaves
@@ -126,24 +98,3 @@ impl SingleSignatureTestExtension for SingleSignature {
12698
}
12799
}
128100
}
129-
130-
#[cfg(test)]
131-
mod tests {
132-
use super::*;
133-
134-
#[test]
135-
fn test_block_range_try_add() {
136-
assert_eq!(
137-
BlockRange::new(1, 10).try_add(&BlockRange::new(1, 10)).unwrap(),
138-
BlockRange::new(1, 10)
139-
);
140-
assert_eq!(
141-
BlockRange::new(1, 10).try_add(&BlockRange::new(1, 11)).unwrap(),
142-
BlockRange::new(1, 11)
143-
);
144-
assert_eq!(
145-
BlockRange::new(1, 10).try_add(&BlockRange::new(2, 10)).unwrap(),
146-
BlockRange::new(1, 10)
147-
);
148-
}
149-
}

mithril-common/src/test/mod.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
//! Test utilities
22
//!
3-
//! They contains:
4-
//! * A Open Api Spec tester
5-
//! * Some precomputed fake data and keys
6-
//! * A builder of [MithrilFixture] to generate signers alongside a stake distribution
3+
//! A collection of testing utilities and helpers:
4+
//!
5+
//! * `assert`: Custom assertions for comparing directory structures, iterables, JSON strings and more
6+
//! * `builder`: Test data builders and fixtures
7+
//! * `crypto_helper`: Cryptographic utilities for testing
8+
//! * `double`: Test doubles (mocks, fakes, dummies, ...)
9+
//! * `entities_extensions`: Extension traits adding test-specific methods for [crate::entities]
10+
//! * `logging`: Test logging infrastructure
11+
//! * `mock_extensions`: Additional mocking utilities
12+
//! * `temp_dir`: Temporary directory management for tests
713
//!
814
915
pub mod builder;
@@ -89,7 +95,7 @@ mod utils {
8995
#[test]
9096
fn test_format_function_path_from_given_function() {
9197
assert_eq!(
92-
"mithril_common/test_utils/utils/test_format_function_path_from_given_function",
98+
"mithril_common/test/utils/test_format_function_path_from_given_function",
9399
format_current_function_path(test_format_function_path_from_given_function)
94100
);
95101
}
@@ -98,7 +104,7 @@ mod utils {
98104
fn test_format_function_path_from_given_pseudo_function_f() {
99105
fn f() {}
100106
assert_eq!(
101-
"mithril_common/test_utils/utils/test_format_function_path_from_given_pseudo_function_f",
107+
"mithril_common/test/utils/test_format_function_path_from_given_pseudo_function_f",
102108
format_current_function_path(f)
103109
);
104110
}
@@ -107,7 +113,7 @@ mod utils {
107113
async fn test_format_function_path_from_given_async_function_f() {
108114
fn f() {}
109115
assert_eq!(
110-
"mithril_common/test_utils/utils/test_format_function_path_from_given_async_function_f",
116+
"mithril_common/test/utils/test_format_function_path_from_given_async_function_f",
111117
format_current_function_path(f)
112118
);
113119
}
@@ -116,7 +122,7 @@ mod utils {
116122
fn test_build_current_function_path_using_macros() {
117123
assert_eq!(
118124
Path::new("mithril_common")
119-
.join("test_utils")
125+
.join("test")
120126
.join("utils")
121127
.join("test_build_current_function_path_using_macros"),
122128
current_function_path!()
@@ -127,7 +133,7 @@ mod utils {
127133
async fn test_build_current_async_function_path_using_macros() {
128134
assert_eq!(
129135
Path::new("mithril_common")
130-
.join("test_utils")
136+
.join("test")
131137
.join("utils")
132138
.join("test_build_current_async_function_path_using_macros"),
133139
current_function_path!()

mithril-common/src/test/temp_dir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ mod tests {
258258
std::env::temp_dir()
259259
.join(TEMP_DIR_ROOT_NAME)
260260
.join("mithril_common")
261-
.join("test_utils")
261+
.join("test")
262262
.join("temp_dir")
263263
.join("creating_temp_dir_base_on_current_function"),
264264
temp_dir!(),
@@ -271,7 +271,7 @@ mod tests {
271271
std::env::temp_dir()
272272
.join(TEMP_DIR_ROOT_NAME)
273273
.join("mithril_common")
274-
.join("test_utils")
274+
.join("test")
275275
.join("temp_dir")
276276
.join("creating_temp_dir_base_on_current_async_function"),
277277
temp_dir!(),

0 commit comments

Comments
 (0)