|
| 1 | +//! Integration test tasks for packages with bitcoind-tests or similar test packages. |
| 2 | +
|
| 3 | +use crate::environment::{get_crate_dirs, quiet_println, CONFIG_FILE_PATH}; |
| 4 | +use crate::quiet_cmd; |
| 5 | +use serde::Deserialize; |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | +use xshell::{cmd, Shell}; |
| 8 | + |
| 9 | +/// Integration test configuration loaded from rbmt.toml. |
| 10 | +#[derive(Debug, Deserialize, Default)] |
| 11 | +#[serde(default)] |
| 12 | +struct Config { |
| 13 | + integration: IntegrationConfig, |
| 14 | +} |
| 15 | + |
| 16 | +/// Integration-specific configuration. |
| 17 | +#[derive(Debug, Deserialize, Default)] |
| 18 | +#[serde(default)] |
| 19 | +struct IntegrationConfig { |
| 20 | + /// Package name containing integration tests (defaults to "bitcoind-tests"). |
| 21 | + package: Option<String>, |
| 22 | + |
| 23 | + /// Bitcoind versions to test (runs each individually). |
| 24 | + /// If not specified, discovers all version features from Cargo.toml. |
| 25 | + /// |
| 26 | + /// # Examples |
| 27 | + /// |
| 28 | + /// `["29_0", "28_2", "27_2"]` |
| 29 | + versions: Option<Vec<String>>, |
| 30 | +} |
| 31 | + |
| 32 | +impl IntegrationConfig { |
| 33 | + /// Load integration configuration from a crate directory. |
| 34 | + fn load(crate_dir: &Path) -> Result<Self, Box<dyn std::error::Error>> { |
| 35 | + let config_path = crate_dir.join(CONFIG_FILE_PATH); |
| 36 | + |
| 37 | + if !config_path.exists() { |
| 38 | + return Ok(IntegrationConfig::default()); |
| 39 | + } |
| 40 | + |
| 41 | + let contents = std::fs::read_to_string(&config_path)?; |
| 42 | + let config: Config = toml::from_str(&contents)?; |
| 43 | + Ok(config.integration) |
| 44 | + } |
| 45 | + |
| 46 | + /// Get the package name (defaults to "bitcoind-tests"). |
| 47 | + fn package_name(&self) -> &str { |
| 48 | + self.package.as_deref().unwrap_or("bitcoind-tests") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// Run integration tests for all crates with integration test packages. |
| 53 | +/// |
| 54 | +/// # Arguments |
| 55 | +/// |
| 56 | +/// * `packages` - Optional filter for specific package names. |
| 57 | +pub fn run(sh: &Shell, packages: &[String]) -> Result<(), Box<dyn std::error::Error>> { |
| 58 | + let crate_dirs = get_crate_dirs(sh, packages)?; |
| 59 | + quiet_println(&format!( |
| 60 | + "Looking for integration tests in {} crate(s)", |
| 61 | + crate_dirs.len() |
| 62 | + )); |
| 63 | + |
| 64 | + for crate_dir in &crate_dirs { |
| 65 | + let config = IntegrationConfig::load(Path::new(crate_dir))?; |
| 66 | + let integration_dir = PathBuf::from(crate_dir).join(config.package_name()); |
| 67 | + |
| 68 | + if !integration_dir.exists() { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + if !integration_dir.join("Cargo.toml").exists() { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + quiet_println(&format!( |
| 77 | + "Running integration tests for crate: {}", |
| 78 | + crate_dir |
| 79 | + )); |
| 80 | + |
| 81 | + let _dir = sh.push_dir(&integration_dir); |
| 82 | + |
| 83 | + let available_versions = discover_version_features(sh, &integration_dir)?; |
| 84 | + if available_versions.is_empty() { |
| 85 | + quiet_println(" No version features found in Cargo.toml"); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + let versions_to_test: Vec<String> = if let Some(config_versions) = &config.versions { |
| 90 | + // Filter available versions by config. |
| 91 | + let mut filtered = Vec::new(); |
| 92 | + for requested in config_versions { |
| 93 | + if available_versions.contains(requested) { |
| 94 | + filtered.push(requested.clone()); |
| 95 | + } else { |
| 96 | + return Err(format!( |
| 97 | + "Requested version '{}' not found in available versions: {}", |
| 98 | + requested, |
| 99 | + available_versions.join(", ") |
| 100 | + ) |
| 101 | + .into()); |
| 102 | + } |
| 103 | + } |
| 104 | + filtered |
| 105 | + } else { |
| 106 | + // No config, test all available versions. |
| 107 | + available_versions |
| 108 | + }; |
| 109 | + |
| 110 | + // Run tests for each version. |
| 111 | + for version in &versions_to_test { |
| 112 | + quiet_println(&format!(" Testing with version: {}", version)); |
| 113 | + quiet_cmd!(sh, "cargo test --features={version}").run()?; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + Ok(()) |
| 118 | +} |
| 119 | + |
| 120 | +/// Discover all features from the integration package using cargo metadata. |
| 121 | +fn discover_version_features( |
| 122 | + sh: &Shell, |
| 123 | + integration_dir: &Path, |
| 124 | +) -> Result<Vec<String>, Box<dyn std::error::Error>> { |
| 125 | + let _dir = sh.push_dir(integration_dir); |
| 126 | + let metadata = cmd!(sh, "cargo metadata --format-version 1 --no-deps").read()?; |
| 127 | + let json: serde_json::Value = serde_json::from_str(&metadata)?; |
| 128 | + |
| 129 | + let mut features = Vec::new(); |
| 130 | + |
| 131 | + // Find the package in the metadata and extract its features. |
| 132 | + if let Some(packages) = json["packages"].as_array() { |
| 133 | + // Should only be one package since we're in the integration test directory. |
| 134 | + if let Some(package) = packages.first() { |
| 135 | + if let Some(package_features) = package["features"].as_object() { |
| 136 | + for feature_name in package_features.keys() { |
| 137 | + features.push(feature_name.clone()); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Sort for consistent output. |
| 144 | + features.sort(); |
| 145 | + |
| 146 | + Ok(features) |
| 147 | +} |
0 commit comments