Skip to content

Commit

Permalink
fix: add check-all-features-set in mithril-client and fix code un…
Browse files Browse the repository at this point in the history
…der features
  • Loading branch information
sfauvel committed Feb 20, 2025
1 parent e37957e commit 4e1913d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
17 changes: 17 additions & 0 deletions mithril-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
args = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}`

CARGO = cargo
FEATURES := fs unstable

all: test build

Expand All @@ -25,3 +26,19 @@ clean:

doc:
${CARGO} doc --no-deps --open --features full

# Compute the powerset of all the given features and save it to a file
.feature-sets:
powerset() { [ $$# -eq 0 ] && echo || (shift; powerset "$$@") | while read r ; do echo "$$1 $$r"; echo "$$r"; done };\
powerset $$(echo "$(FEATURES)") > .features-sets

check-all-features-set: .feature-sets
# Read the file to run cargo clippy on all those features sets
cat .features-sets | while read features_set; do \
echo "Clippy client with feature '$$features_set''"; \
${CARGO} clippy -p mithril-client --features "$$features_set"; \
done
echo "Clippy client without features"; \
${CARGO} clippy -p mithril-client

rm .features-sets
47 changes: 40 additions & 7 deletions mithril-client/src/cardano_database_client/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,34 @@ impl CardanoDatabaseClient {
pub(crate) mod test_dependency_injector {
use super::*;

use crate::{
aggregator_client::MockAggregatorClient,
feedback::FeedbackReceiver,
file_downloader::{FileDownloader, MockFileDownloaderBuilder},
test_utils,
};
use crate::aggregator_client::MockAggregatorClient;
#[cfg(feature = "fs")]
use crate::file_downloader::{FileDownloader, MockFileDownloaderBuilder};
#[cfg(feature = "fs")]
use crate::{feedback::FeedbackReceiver, test_utils};

/// Dependency injector for `CardanoDatabaseClient` for testing purposes.
pub(crate) struct CardanoDatabaseClientDependencyInjector {
aggregator_client: MockAggregatorClient,
#[cfg(feature = "fs")]
http_file_downloader: Arc<dyn FileDownloader>,
#[cfg(feature = "fs")]
feedback_receivers: Vec<Arc<dyn FeedbackReceiver>>,
}

impl CardanoDatabaseClientDependencyInjector {
pub(crate) fn new() -> Self {
Self {
aggregator_client: MockAggregatorClient::new(),
#[cfg(feature = "fs")]
http_file_downloader: Arc::new(
MockFileDownloaderBuilder::default()
.with_compression(None)
.with_success()
.with_times(0)
.build(),
),
#[cfg(feature = "fs")]
feedback_receivers: vec![],
}
}
Expand All @@ -153,6 +156,7 @@ pub(crate) mod test_dependency_injector {
self
}

#[cfg(feature = "fs")]
pub(crate) fn with_http_file_downloader(
self,
http_file_downloader: Arc<dyn FileDownloader>,
Expand All @@ -163,6 +167,7 @@ pub(crate) mod test_dependency_injector {
}
}

#[cfg(feature = "fs")]
pub(crate) fn with_feedback_receivers(
self,
feedback_receivers: &[Arc<dyn FeedbackReceiver>],
Expand All @@ -173,6 +178,7 @@ pub(crate) mod test_dependency_injector {
}
}

#[cfg(feature = "fs")]
pub(crate) fn build_cardano_database_client(self) -> CardanoDatabaseClient {
CardanoDatabaseClient::new(
Arc::new(self.aggregator_client),
Expand All @@ -181,15 +187,23 @@ pub(crate) mod test_dependency_injector {
test_utils::test_logger(),
)
}

#[cfg(not(feature = "fs"))]
pub(crate) fn build_cardano_database_client(self) -> CardanoDatabaseClient {
CardanoDatabaseClient::new(Arc::new(self.aggregator_client))
}
}

mod tests {
use mockall::predicate;

use crate::{aggregator_client::AggregatorRequest, feedback::StackFeedbackReceiver};
use crate::aggregator_client::AggregatorRequest;
#[cfg(feature = "fs")]
use crate::feedback::StackFeedbackReceiver;

use super::*;

#[cfg(feature = "fs")]
#[test]
fn test_cardano_database_client_dependency_injector_builds() {
let _ = CardanoDatabaseClientDependencyInjector::new()
Expand All @@ -214,5 +228,24 @@ pub(crate) mod test_dependency_injector {
.with_feedback_receivers(&[Arc::new(StackFeedbackReceiver::new())])
.build_cardano_database_client();
}

#[cfg(not(feature = "fs"))]
#[test]
fn test_cardano_database_client_dependency_injector_builds() {
let _ = CardanoDatabaseClientDependencyInjector::new()
.with_aggregator_client_mock_config(|http_client| {
let message = vec![CardanoDatabaseSnapshotListItem {
hash: "hash-123".to_string(),
..CardanoDatabaseSnapshotListItem::dummy()
}];
http_client
.expect_get_content()
.with(predicate::eq(
AggregatorRequest::ListCardanoDatabaseSnapshots,
))
.return_once(move |_| Ok(serde_json::to_string(&message).unwrap()));
})
.build_cardano_database_client();
}
}
}

0 comments on commit 4e1913d

Please sign in to comment.