|
| 1 | +//! Build and test tasks with feature matrix testing. |
| 2 | +
|
| 3 | +use crate::environment::{get_crate_dirs, quiet_println, CONFIG_FILE_PATH}; |
| 4 | +use crate::quiet_cmd; |
| 5 | +use crate::toolchain::{check_toolchain, Toolchain}; |
| 6 | +use serde::Deserialize; |
| 7 | +use std::path::Path; |
| 8 | +use xshell::Shell; |
| 9 | + |
| 10 | +/// Test configuration loaded from contrib/rbmt.toml. |
| 11 | +#[derive(Debug, Deserialize)] |
| 12 | +struct Config { |
| 13 | + test: TestConfig, |
| 14 | +} |
| 15 | + |
| 16 | +/// Test-specific configuration. |
| 17 | +#[derive(Debug, Deserialize)] |
| 18 | +struct TestConfig { |
| 19 | + /// Examples to run with the format "name:feature1 feature2". |
| 20 | + /// |
| 21 | + /// # Examples |
| 22 | + /// |
| 23 | + /// `["example1:serde", "example2:serde rand"]` |
| 24 | + examples: Vec<String>, |
| 25 | + |
| 26 | + /// List of individual features to test with the conventional `std` feature enabled. |
| 27 | + /// Automatically tests feature combinations, alone with `std`, all pairs, and all together. |
| 28 | + /// |
| 29 | + /// # Examples |
| 30 | + /// |
| 31 | + /// `["serde", "rand"]` tests `std+serde`, `std+rand`, `std+serde+rand`. |
| 32 | + features_with_std: Vec<String>, |
| 33 | + |
| 34 | + /// List of individual features to test without the `std` feature. |
| 35 | + /// Automatically tests features combinations, each feature alone, |
| 36 | + /// all pairs, and all together. |
| 37 | + /// |
| 38 | + /// # Examples |
| 39 | + /// |
| 40 | + /// `["serde", "rand"]` tests `serde`, `rand`, `serde+rand`. |
| 41 | + features_without_std: Vec<String>, |
| 42 | + |
| 43 | + /// Exact feature combinations to test. |
| 44 | + /// Use for crates that don't follow the conventional `std` feature pattern. |
| 45 | + /// Each inner vector is a list of features to test together. There is |
| 46 | + /// no automatic combinations of features tests. |
| 47 | + /// |
| 48 | + /// # Examples |
| 49 | + /// |
| 50 | + /// `[["serde", "rand"], ["rand"]]` tests exactly those two combinations. |
| 51 | + exact_features: Vec<Vec<String>>, |
| 52 | + |
| 53 | + /// List of individual features to test with the `no-std` feature enabled. |
| 54 | + /// Only use if your crate has an explicit `no-std` feature (rust-miniscript pattern). |
| 55 | + /// Automatically tests each feature alone with `no-std`, all pairs, and all together. |
| 56 | + /// |
| 57 | + /// # Examples |
| 58 | + /// |
| 59 | + /// `["serde", "rand"]` tests `no-std+serde`, `no-std+serde`, `no-std+serde+rand`. |
| 60 | + features_with_no_std: Vec<String>, |
| 61 | +} |
| 62 | + |
| 63 | +impl TestConfig { |
| 64 | + /// Load test configuration from a crate directory. |
| 65 | + fn load(crate_dir: &Path) -> Result<Self, Box<dyn std::error::Error>> { |
| 66 | + let config_path = crate_dir.join(CONFIG_FILE_PATH); |
| 67 | + |
| 68 | + if !config_path.exists() { |
| 69 | + // Return empty config if file doesn't exist. |
| 70 | + return Ok(TestConfig { |
| 71 | + examples: Vec::new(), |
| 72 | + features_with_std: Vec::new(), |
| 73 | + features_without_std: Vec::new(), |
| 74 | + exact_features: Vec::new(), |
| 75 | + features_with_no_std: Vec::new(), |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + let contents = std::fs::read_to_string(&config_path)?; |
| 80 | + let config: Config = toml::from_str(&contents)?; |
| 81 | + Ok(config.test) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Run build and test for all crates with the specified toolchain. |
| 86 | +pub fn run(sh: &Shell, toolchain: Toolchain) -> Result<(), Box<dyn std::error::Error>> { |
| 87 | + check_toolchain(sh, toolchain)?; |
| 88 | + |
| 89 | + let crate_dirs = get_crate_dirs(sh)?; |
| 90 | + quiet_println(&format!("Testing {} crates", crate_dirs.len())); |
| 91 | + |
| 92 | + for crate_dir in &crate_dirs { |
| 93 | + quiet_println(&format!("Testing crate: {}", crate_dir)); |
| 94 | + |
| 95 | + let _dir = sh.push_dir(crate_dir); |
| 96 | + let config = TestConfig::load(Path::new(crate_dir))?; |
| 97 | + |
| 98 | + do_test(sh, &config)?; |
| 99 | + do_feature_matrix(sh, &config)?; |
| 100 | + } |
| 101 | + |
| 102 | + Ok(()) |
| 103 | +} |
| 104 | + |
| 105 | +/// Run basic build, test, and examples. |
| 106 | +fn do_test(sh: &Shell, config: &TestConfig) -> Result<(), Box<dyn std::error::Error>> { |
| 107 | + quiet_println("Running basic tests"); |
| 108 | + |
| 109 | + // Basic build and test. |
| 110 | + quiet_cmd!(sh, "cargo build").run()?; |
| 111 | + quiet_cmd!(sh, "cargo test").run()?; |
| 112 | + |
| 113 | + // Run examples. |
| 114 | + for example in &config.examples { |
| 115 | + let parts: Vec<&str> = example.split(':').collect(); |
| 116 | + if parts.len() != 2 { |
| 117 | + return Err(format!( |
| 118 | + "Invalid example format: {}, expected 'name:features'", |
| 119 | + example |
| 120 | + ) |
| 121 | + .into()); |
| 122 | + } |
| 123 | + |
| 124 | + let name = parts[0]; |
| 125 | + let features = parts[1]; |
| 126 | + |
| 127 | + quiet_println(&format!( |
| 128 | + "Running example {} with features: {}", |
| 129 | + name, features |
| 130 | + )); |
| 131 | + quiet_cmd!(sh, "cargo run --example {name} --features={features}").run()?; |
| 132 | + } |
| 133 | + |
| 134 | + Ok(()) |
| 135 | +} |
| 136 | + |
| 137 | +/// Run feature matrix tests. |
| 138 | +fn do_feature_matrix(sh: &Shell, config: &TestConfig) -> Result<(), Box<dyn std::error::Error>> { |
| 139 | + quiet_println("Running feature matrix tests"); |
| 140 | + |
| 141 | + // Handle exact features (for unusual crates). |
| 142 | + if !config.exact_features.is_empty() { |
| 143 | + for features in &config.exact_features { |
| 144 | + let features_str = features.join(" "); |
| 145 | + quiet_println(&format!("Testing exact features: {}", features_str)); |
| 146 | + quiet_cmd!( |
| 147 | + sh, |
| 148 | + "cargo build --no-default-features --features={features_str}" |
| 149 | + ) |
| 150 | + .run()?; |
| 151 | + quiet_cmd!( |
| 152 | + sh, |
| 153 | + "cargo test --no-default-features --features={features_str}" |
| 154 | + ) |
| 155 | + .run()?; |
| 156 | + } |
| 157 | + return Ok(()); |
| 158 | + } |
| 159 | + |
| 160 | + // Handle no-std pattern (rust-miniscript). |
| 161 | + if !config.features_with_no_std.is_empty() { |
| 162 | + quiet_println("Testing no-std"); |
| 163 | + quiet_cmd!(sh, "cargo build --no-default-features --features=no-std").run()?; |
| 164 | + quiet_cmd!(sh, "cargo test --no-default-features --features=no-std").run()?; |
| 165 | + |
| 166 | + loop_features(sh, "no-std", &config.features_with_no_std)?; |
| 167 | + } else { |
| 168 | + quiet_println("Testing no-default-features"); |
| 169 | + quiet_cmd!(sh, "cargo build --no-default-features").run()?; |
| 170 | + quiet_cmd!(sh, "cargo test --no-default-features").run()?; |
| 171 | + } |
| 172 | + |
| 173 | + // Test all features. |
| 174 | + quiet_println("Testing all-features"); |
| 175 | + quiet_cmd!(sh, "cargo build --all-features").run()?; |
| 176 | + quiet_cmd!(sh, "cargo test --all-features").run()?; |
| 177 | + |
| 178 | + // Test features with std. |
| 179 | + if !config.features_with_std.is_empty() { |
| 180 | + loop_features(sh, "std", &config.features_with_std)?; |
| 181 | + } |
| 182 | + |
| 183 | + // Test features without std. |
| 184 | + if !config.features_without_std.is_empty() { |
| 185 | + loop_features(sh, "", &config.features_without_std)?; |
| 186 | + } |
| 187 | + |
| 188 | + Ok(()) |
| 189 | +} |
| 190 | + |
| 191 | +/// Test each feature individually and all combinations of two features. |
| 192 | +/// |
| 193 | +/// This implements three feature matrix testing strategies. |
| 194 | +/// 1. All features together. |
| 195 | +/// 2. Each feature individually (only if more than one feature). |
| 196 | +/// 3. All unique pairs of features. |
| 197 | +/// |
| 198 | +/// The pair testing catches feature interaction bugs (where two features work |
| 199 | +/// independently, but conflict when combined) while keeping test time manageable. |
| 200 | +fn loop_features( |
| 201 | + sh: &Shell, |
| 202 | + base: &str, |
| 203 | + features: &[String], |
| 204 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 205 | + let base_flag = if base.is_empty() { |
| 206 | + String::new() |
| 207 | + } else { |
| 208 | + format!("{} ", base) |
| 209 | + }; |
| 210 | + |
| 211 | + // Test all features together. |
| 212 | + let all_features = format!("{}{}", base_flag, features.join(" ")); |
| 213 | + quiet_println(&format!("Testing features: {}", all_features.trim())); |
| 214 | + quiet_cmd!( |
| 215 | + sh, |
| 216 | + "cargo build --no-default-features --features={all_features}" |
| 217 | + ) |
| 218 | + .run()?; |
| 219 | + quiet_cmd!( |
| 220 | + sh, |
| 221 | + "cargo test --no-default-features --features={all_features}" |
| 222 | + ) |
| 223 | + .run()?; |
| 224 | + |
| 225 | + // Test each feature individually and all pairs (only if more than one feature). |
| 226 | + if features.len() > 1 { |
| 227 | + for i in 0..features.len() { |
| 228 | + let feature_combo = format!("{}{}", base_flag, features[i]); |
| 229 | + quiet_println(&format!("Testing features: {}", feature_combo.trim())); |
| 230 | + quiet_cmd!( |
| 231 | + sh, |
| 232 | + "cargo build --no-default-features --features={feature_combo}" |
| 233 | + ) |
| 234 | + .run()?; |
| 235 | + quiet_cmd!( |
| 236 | + sh, |
| 237 | + "cargo test --no-default-features --features={feature_combo}" |
| 238 | + ) |
| 239 | + .run()?; |
| 240 | + |
| 241 | + // Test all pairs with features[i]. |
| 242 | + for j in (i + 1)..features.len() { |
| 243 | + let feature_combo = format!("{}{} {}", base_flag, features[i], features[j]); |
| 244 | + quiet_println(&format!("Testing features: {}", feature_combo.trim())); |
| 245 | + quiet_cmd!( |
| 246 | + sh, |
| 247 | + "cargo build --no-default-features --features={feature_combo}" |
| 248 | + ) |
| 249 | + .run()?; |
| 250 | + quiet_cmd!( |
| 251 | + sh, |
| 252 | + "cargo test --no-default-features --features={feature_combo}" |
| 253 | + ) |
| 254 | + .run()?; |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + Ok(()) |
| 260 | +} |
0 commit comments