cargo-feature-combinations 0.4.4

run cargo commands for all feature combinations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Package-level configuration discovery and the [`Package`] extension trait.

mod combinations;

pub use combinations::FeatureCombinationError;

use crate::config::patch::{FeatureSetVecPatch, StringSetPatch};
use crate::config::{Config, ResolvedFeatures, validate_package_metadata};
use crate::print_warning;
use crate::{DEFAULT_METADATA_KEY, find_metadata_value, pkg_metadata_section};
use color_eyre::eyre::{self, WrapErr};
use itertools::Itertools;
use std::collections::HashSet;

#[must_use]
pub(crate) fn has_lib_target(package: &cargo_metadata::Package) -> bool {
    package
        .targets
        .iter()
        .any(|target| target.kind.contains(&cargo_metadata::TargetKind::Lib))
}

/// Extension trait for [`cargo_metadata::Package`] used by this crate.
pub trait Package {
    /// Parse the configuration for this package if present.
    ///
    /// If the Cargo.toml manifest contains a configuration section,
    /// the latter is parsed.
    /// Otherwise, a default configuration is used.
    ///
    /// # Errors
    ///
    /// If the configuration in the manifest can not be parsed,
    /// an error is returned.
    ///
    fn config(&self) -> eyre::Result<Config>;
    /// Compute all feature combinations for this package based on the
    /// provided [`ResolvedFeatures`].
    ///
    /// # Errors
    ///
    /// Returns an error if the package declares too many features or its
    /// mutually exclusive feature groups are invalid.
    fn feature_combinations<'a>(
        &'a self,
        config: &ResolvedFeatures,
    ) -> eyre::Result<Vec<Vec<&'a String>>>;
    /// Convert [`Package::feature_combinations`] into a list of comma-separated
    /// feature strings suitable for passing to `cargo --features`.
    ///
    /// # Errors
    ///
    /// Returns an error if [`Package::feature_combinations`] fails.
    fn feature_matrix(&self, config: &ResolvedFeatures) -> eyre::Result<Vec<String>>;
}

impl Package for cargo_metadata::Package {
    fn config(&self) -> eyre::Result<Config> {
        let (mut config, key) = match find_metadata_value(&self.metadata) {
            Some((value, key)) => {
                validate_package_metadata(value, &pkg_metadata_section(key))?;
                (
                    serde_json::from_value(value.clone()).wrap_err_with(|| {
                        format!(
                            "invalid [{}] configuration in package `{}`",
                            pkg_metadata_section(key),
                            self.name
                        )
                    })?,
                    key,
                )
            }
            None => (Config::default(), DEFAULT_METADATA_KEY),
        };

        let section = pkg_metadata_section(key);

        if !config.deprecated.skip_feature_sets.is_empty() {
            print_warning!(
                "[{section}].skip_feature_sets in package `{}` is deprecated; use exclude_feature_sets instead",
                self.name,
            );
        }

        if !config.deprecated.denylist.is_empty() {
            print_warning!(
                "[{section}].denylist in package `{}` is deprecated; use exclude_features instead",
                self.name,
            );
        }

        if !config.deprecated.exact_combinations.is_empty() {
            print_warning!(
                "[{section}].exact_combinations in package `{}` is deprecated; use include_feature_sets instead",
                self.name,
            );
        }

        fold_deprecated_feature_sets(
            &mut config.base.settings.features.exclude_feature_sets,
            std::mem::take(&mut config.deprecated.skip_feature_sets),
        );
        fold_deprecated_string_set(
            &mut config.base.settings.features.exclude_features,
            std::mem::take(&mut config.deprecated.denylist),
        );
        fold_deprecated_feature_sets(
            &mut config.base.settings.features.include_feature_sets,
            std::mem::take(&mut config.deprecated.exact_combinations),
        );

        // After folding, so names from deprecated spellings are checked too.
        crate::config::validate_feature_names(&config, &self.features, &self.name, &section)?;

        Ok(config)
    }

    fn feature_combinations<'a>(
        &'a self,
        config: &ResolvedFeatures,
    ) -> eyre::Result<Vec<Vec<&'a String>>> {
        combinations::feature_combinations(self, config)
    }

    fn feature_matrix(&self, config: &ResolvedFeatures) -> eyre::Result<Vec<String>> {
        Ok(self
            .feature_combinations(config)?
            .into_iter()
            .map(|features| features.iter().join(","))
            .collect())
    }
}

fn fold_deprecated_string_set(target: &mut Option<StringSetPatch>, values: HashSet<String>) {
    if values.is_empty() {
        return;
    }
    match target {
        Some(
            StringSetPatch::Override(current)
            | StringSetPatch::Patch {
                r#override: Some(current),
                ..
            },
        ) => current.extend(values),
        Some(StringSetPatch::Patch { add, .. }) => add.extend(values),
        None => {
            *target = Some(StringSetPatch::Patch {
                r#override: None,
                add: values,
                remove: HashSet::new(),
            });
        }
    }
}

fn fold_deprecated_feature_sets(
    target: &mut Option<FeatureSetVecPatch>,
    mut values: Vec<HashSet<String>>,
) {
    if values.is_empty() {
        return;
    }
    match target {
        Some(
            FeatureSetVecPatch::Override(current)
            | FeatureSetVecPatch::Patch {
                r#override: Some(current),
                ..
            },
        ) => current.append(&mut values),
        Some(FeatureSetVecPatch::Patch { add, .. }) => add.append(&mut values),
        None => {
            *target = Some(FeatureSetVecPatch::Patch {
                r#override: None,
                add: values,
                remove: Vec::new(),
            });
        }
    }
}

#[cfg(test)]
pub(crate) mod test {
    use super::Package;
    use crate::config::ResolvedFeatures;
    use color_eyre::eyre;

    static INIT: std::sync::Once = std::sync::Once::new();

    pub(crate) fn init() {
        INIT.call_once(|| {
            color_eyre::install().ok();
        });
    }

    pub(crate) fn package(name: &str) -> eyre::Result<cargo_metadata::Package> {
        package_with_manifest_path(name, "")
    }

    pub(crate) fn package_with_manifest_path(
        name: &str,
        manifest_path: &str,
    ) -> eyre::Result<cargo_metadata::Package> {
        use cargo_metadata::{PackageBuilder, PackageId, PackageName};
        use semver::Version;
        use std::str::FromStr as _;

        Ok(PackageBuilder::new(
            PackageName::from_str(name)?,
            Version::parse("0.1.0")?,
            PackageId {
                repr: name.to_string(),
            },
            manifest_path,
        )
        .build()?)
    }

    pub(crate) fn effective_target(triple: &str) -> crate::target::EffectiveTarget {
        crate::target::EffectiveTarget {
            triple: crate::target::TargetTriple(triple.to_string()),
            source: crate::target::TargetSource::WorkspaceConfig,
        }
    }

    pub(crate) fn package_with_features(
        features: &[&str],
    ) -> eyre::Result<cargo_metadata::Package> {
        let mut package = package("test")?;
        package.features = features
            .iter()
            .map(|feature| ((*feature).to_string(), vec![]))
            .collect();
        Ok(package)
    }

    /// Build a package whose metadata contains a config under the given alias.
    pub(crate) fn package_with_metadata(
        features: &[&str],
        metadata_key: &str,
        config: &serde_json::Value,
    ) -> eyre::Result<cargo_metadata::Package> {
        let mut package = package_with_features(features)?;
        package.metadata = serde_json::json!({ metadata_key: config });
        Ok(package)
    }

    #[test]
    fn config_from_cargo_fc_alias() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo", "bar"],
            "cargo-fc",
            &serde_json::json!({ "exclude_features": ["foo"] }),
        )?;
        let config = package.config()?;
        let resolved = ResolvedFeatures::from_config(&config);
        assert!(resolved.exclude_features.contains("foo"));
        assert!(!resolved.exclude_features.contains("bar"));
        Ok(())
    }

    #[test]
    fn config_from_fc_alias() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo", "bar"],
            "fc",
            &serde_json::json!({ "exclude_features": ["bar"] }),
        )?;
        let config = package.config()?;
        let resolved = ResolvedFeatures::from_config(&config);
        assert!(resolved.exclude_features.contains("bar"));
        assert!(!resolved.exclude_features.contains("foo"));
        Ok(())
    }

    #[test]
    fn config_from_feature_combinations_alias() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["a", "b"],
            "feature-combinations",
            &serde_json::json!({ "no_empty_feature_set": true }),
        )?;
        let config = package.config()?;
        assert!(ResolvedFeatures::from_config(&config).no_empty_feature_set);
        Ok(())
    }

    #[test]
    fn config_from_cargo_feature_combinations_alias() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["a", "b"],
            "cargo-feature-combinations",
            &serde_json::json!({ "exclude_features": ["a"] }),
        )?;
        let config = package.config()?;
        assert!(
            ResolvedFeatures::from_config(&config)
                .exclude_features
                .contains("a")
        );
        Ok(())
    }

    #[test]
    fn config_rejects_unknown_feature_names() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo"],
            "cargo-fc",
            &serde_json::json!({ "exclude_features": ["bar-typo"] }),
        )?;

        let err = package
            .config()
            .expect_err("unknown feature names should fail config load");

        assert!(err.to_string().contains("bar-typo"), "{err}");
        Ok(())
    }

    #[test]
    fn config_rejects_unknown_feature_in_unmatched_target_section() -> eyre::Result<()> {
        init();
        // The feature list does not depend on the resolution target, so even a
        // section whose cfg never matches this host must reference real names.
        let package = package_with_metadata(
            &["foo"],
            "cargo-fc",
            &serde_json::json!({
                "target": {
                    "cfg(target_os = \"none\")": {
                        "mutually_exclusive_features": [["foo", "ghost"]],
                    },
                },
            }),
        )?;

        let err = package
            .config()
            .expect_err("unknown names in any target section should fail config load");

        assert!(err.to_string().contains("ghost"), "{err}");
        Ok(())
    }

    #[test]
    fn config_rejects_undeclared_default() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo"],
            "cargo-fc",
            &serde_json::json!({ "exclude_features": ["default"] }),
        )?;

        let err = package
            .config()
            .expect_err("excluding an undeclared default feature should fail config load");

        assert!(err.to_string().contains("`default`"), "{err}");
        Ok(())
    }

    #[test]
    fn config_validates_names_from_deprecated_keys() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo"],
            "cargo-fc",
            &serde_json::json!({ "denylist": ["typo"] }),
        )?;

        let err = package
            .config()
            .expect_err("deprecated spellings fold into checked keys");

        assert!(err.to_string().contains("typo"), "{err}");
        Ok(())
    }

    #[test]
    fn config_default_when_no_metadata() -> eyre::Result<()> {
        init();
        let package = package_with_features(&["foo"])?;
        let config = package.config()?;
        let resolved = ResolvedFeatures::from_config(&config);
        assert!(resolved.exclude_features.is_empty());
        assert!(!resolved.no_empty_feature_set);
        Ok(())
    }

    #[test]
    fn config_alias_affects_feature_matrix() -> eyre::Result<()> {
        init();
        let package = package_with_metadata(
            &["foo", "bar"],
            "cargo-fc",
            &serde_json::json!({ "exclude_features": ["foo"] }),
        )?;
        let config = package.config()?;
        let matrix = package.feature_combinations(&ResolvedFeatures::from_config(&config))?;

        // "foo" is excluded, so no combination should contain it
        assert!(
            !matrix.iter().any(|combo| combo.iter().any(|f| *f == "foo")),
            "expected no combination to contain 'foo', got: {matrix:?}"
        );
        // "bar" should still appear
        assert!(
            matrix.iter().any(|combo| combo.iter().any(|f| *f == "bar")),
            "expected 'bar' in at least one combination, got: {matrix:?}"
        );
        Ok(())
    }
}