Skip to content

Commit

Permalink
refactor: builder accepts strings
Browse files Browse the repository at this point in the history
  • Loading branch information
aatifsyed committed Apr 10, 2024
1 parent 88f9967 commit f501cdb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ pub enum CargoOpt {
/// Run cargo with `--no-default-features`
NoDefaultFeatures,
/// Run cargo with `--features <FEATURES>`
SomeFeatures(Vec<FeatureName>),
SomeFeatures(Vec<String>),
}

/// A builder for configurating `cargo metadata` invocation.
Expand Down Expand Up @@ -877,9 +877,7 @@ impl MetadataCommand {
/// ```
pub fn features(&mut self, features: CargoOpt) -> &mut MetadataCommand {
match features {
CargoOpt::SomeFeatures(features) => self
.features
.extend(features.into_iter().map(FeatureName::into_inner)),
CargoOpt::SomeFeatures(features) => self.features.extend(features),
CargoOpt::NoDefaultFeatures => {
assert!(
!self.no_default_features,
Expand Down
27 changes: 17 additions & 10 deletions tests/test_samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ Evil proc macro was here!
fn advanced_feature_configuration() {
fn build_features<F: FnOnce(&mut MetadataCommand) -> &mut MetadataCommand>(
func: F,
) -> Vec<FeatureName> {
) -> Vec<String> {
let mut meta = MetadataCommand::new();
let meta = meta.manifest_path("tests/all/Cargo.toml");

Expand All @@ -617,30 +617,37 @@ fn advanced_feature_configuration() {
.find(|n| !n.features.is_empty())
.unwrap();

all.features.clone()
all.features
.clone()
.into_iter()
.map(FeatureName::into_inner)
.collect()
}

// Default behavior; tested above
let default_features = build_features(|meta| meta);
assert_eq!(
sorted!(default_features),
features!["bitflags", "default", "feat1"]
vec!["bitflags", "default", "feat1"]
);

// Manually specify the same default features
let manual_features = build_features(|meta| {
meta.features(CargoOpt::NoDefaultFeatures)
.features(CargoOpt::SomeFeatures(features!["feat1", "bitflags",]))
.features(CargoOpt::SomeFeatures(vec![
"feat1".into(),
"bitflags".into(),
]))
});
assert_eq!(sorted!(manual_features), features!["bitflags", "feat1"]);
assert_eq!(sorted!(manual_features), vec!["bitflags", "feat1"]);

// Multiple SomeFeatures is same as one longer SomeFeatures
let manual_features = build_features(|meta| {
meta.features(CargoOpt::NoDefaultFeatures)
.features(CargoOpt::SomeFeatures(features!["feat1"]))
.features(CargoOpt::SomeFeatures(features!["feat2"]))
.features(CargoOpt::SomeFeatures(vec!["feat1".into()]))
.features(CargoOpt::SomeFeatures(vec!["feat2".into()]))
});
assert_eq!(sorted!(manual_features), features!["feat1", "feat2"]);
assert_eq!(sorted!(manual_features), vec!["feat1", "feat2"]);

// No features + All features == All features
let all_features = build_features(|meta| {
Expand All @@ -649,12 +656,12 @@ fn advanced_feature_configuration() {
});
assert_eq!(
sorted!(all_features),
features!["bitflags", "default", "feat1", "feat2"]
vec!["bitflags", "default", "feat1", "feat2"]
);

// The '--all-features' flag supersedes other feature flags
let all_flag_variants = build_features(|meta| {
meta.features(CargoOpt::SomeFeatures(features!["feat2"]))
meta.features(CargoOpt::SomeFeatures(vec!["feat2".into()]))
.features(CargoOpt::NoDefaultFeatures)
.features(CargoOpt::AllFeatures)
});
Expand Down

0 comments on commit f501cdb

Please sign in to comment.