Skip to content

Commit

Permalink
automatically generate feature permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandros committed Oct 31, 2024
1 parent ff58205 commit e73716b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
run: echo RUSTFLAGS=${RUSTFLAGS}\ --cfg=thiserror_nightly_testing >> $GITHUB_ENV
if: matrix.rust == 'nightly'
- run: cargo xtask ci --no-extended
if: matrix.rust != '1.64.0'
- run: cargo xtask ci --no-extended -f tokio
if: matrix.rust == '1.64.0'
- uses: actions/upload-artifact@v4
if: matrix.rust == 'nightly' && always()
with:
Expand Down
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ workspace = true
xshell = "0.2"
anyhow = "1"
clap = { version = "=4.1.14", features = ["derive"] }
itertools = "0.13"
86 changes: 66 additions & 20 deletions xtask/src/ci.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![allow(clippy::exit)]
//! Xtask CI "emulation".
use std::iter;

use anyhow::Result;
use itertools::Itertools;
use xshell::{cmd, Shell};

/// How tests are run.
Expand All @@ -20,6 +23,10 @@ pub(super) struct CiArgs {
#[arg(short, long)]
testrunner: Option<TestRunner>,

/// Which features to **not** test.
#[arg(short, long)]
filter: Option<Vec<String>>,

/// Whether to skip extended tests, e.g., `clippy`, `cargo outdated`, and
/// `cargo-semver-checks`. Useful in CI, when these run as a separate task.
#[arg(long, default_value_t = false)]
Expand Down Expand Up @@ -61,26 +68,31 @@ pub(super) fn run(args: &CiArgs) -> Result<()> {
true
};

let test_matrix = [
("ferrunix", ""),
("ferrunix", "derive"),
("ferrunix", "multithread"),
("ferrunix", "tokio"),
("ferrunix", "tracing"),
("ferrunix", "multithread,tracing"),
("ferrunix", "tokio,tracing"),
("ferrunix", "multithread,tokio,tracing"),
("ferrunix", "derive,multithread,tokio,tracing"),
("ferrunix-core", ""),
("ferrunix-core", "multithread"),
("ferrunix-core", "tokio"),
("ferrunix-core", "tracing"),
("ferrunix-macros", ""),
("ferrunix-macros", "multithread"),
("ferrunix-macros", "development"),
("ferrunix-macros", "development,multithread"),
("doc-tests", ""),
];
let combinations_ferrunix =
feature_combinations(&["derive", "multithread", "tokio", "tracing"]);
let combinations_ferrunix_core =
feature_combinations(&["multithread", "tokio", "tracing"]);
let combinations_ferrunix_macros =
feature_combinations(&["multithread", "development"]);
let test_matrix = {
let ferrunix = iter::repeat("ferrunix").zip(combinations_ferrunix);
let ferrunix_core =
iter::repeat("ferrunix-core").zip(combinations_ferrunix_core);
let ferrunix_macros =
iter::repeat("ferrunix-macros").zip(combinations_ferrunix_macros);

let filter = args.filter.clone().unwrap_or_default();
ferrunix
.chain(ferrunix_core)
.chain(ferrunix_macros)
.chain(iter::once(("doc-tests", String::new())))
.filter(|(_, features)| {
filter
.iter()
.any(|filtered_feature| !features.contains(filtered_feature))
})
.collect_vec()
};

let testrunner: &[&str] = match args.testrunner {
Some(TestRunner::Nextest) => &["nextest", "run", "--profile", "ci"],
Expand Down Expand Up @@ -137,3 +149,37 @@ pub(super) fn run(args: &CiArgs) -> Result<()> {

Ok(())
}

/// Generate all possible combinations of `features`.
fn feature_combinations(features: &[&str]) -> Vec<String> {
let len = features.len();
let base_iter = features.iter().combinations(len);

let mut chains: Vec<Box<dyn Iterator<Item = Vec<&&str>>>> =
Vec::with_capacity(len);
for num_combinations in (1..len).rev() {
let iter = base_iter
.clone()
.chain(features.iter().combinations(num_combinations));
chains.push(Box::new(iter));
}

let mut ret = chains
.into_iter()
.map(Itertools::collect_vec)
.flat_map(|outer| {
outer
.into_iter()
.map(|el| {
let x = el.iter().join(",");
x
})
.collect_vec()
})
.unique()
.collect::<Vec<_>>();

ret.push(String::new());

ret
}
1 change: 1 addition & 0 deletions xtask/src/docs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Build documentation.
use xshell::{cmd, Shell};

/// All arguments for the `xtask docs` command.
Expand Down

0 comments on commit e73716b

Please sign in to comment.