litnik 0.1.0

A Rust-native, libtest-mimic integrated runner for LLVM/MLIR-style lit tests.
Documentation
use std::collections::{HashMap, HashSet};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ResultCode {
    pub name: &'static str,
    pub label: &'static str,
    pub is_failure: bool,
}

impl ResultCode {
    pub const EXCLUDED: Self = Self {
        name: "EXCLUDED",
        label: "Excluded",
        is_failure: false,
    };
    pub const SKIPPED: Self = Self {
        name: "SKIPPED",
        label: "Skipped",
        is_failure: false,
    };
    pub const UNSUPPORTED: Self = Self {
        name: "UNSUPPORTED",
        label: "Unsupported",
        is_failure: false,
    };
    pub const PASS: Self = Self {
        name: "PASS",
        label: "Passed",
        is_failure: false,
    };
    pub const FLAKYPASS: Self = Self {
        name: "FLAKYPASS",
        label: "Passed With Retry",
        is_failure: false,
    };
    pub const FIXED: Self = Self {
        name: "FIXED",
        label: "Passed After Update",
        is_failure: false,
    };
    pub const XFAIL: Self = Self {
        name: "XFAIL",
        label: "Expectedly Failed",
        is_failure: false,
    };
    pub const UNRESOLVED: Self = Self {
        name: "UNRESOLVED",
        label: "Unresolved",
        is_failure: true,
    };
    pub const TIMEOUT: Self = Self {
        name: "TIMEOUT",
        label: "Timed Out",
        is_failure: true,
    };
    pub const FAIL: Self = Self {
        name: "FAIL",
        label: "Failed",
        is_failure: true,
    };
    pub const XPASS: Self = Self {
        name: "XPASS",
        label: "Unexpectedly Passed",
        is_failure: true,
    };

    pub const ALL: [Self; 11] = [
        Self::EXCLUDED,
        Self::SKIPPED,
        Self::UNSUPPORTED,
        Self::PASS,
        Self::FLAKYPASS,
        Self::FIXED,
        Self::XFAIL,
        Self::UNRESOLVED,
        Self::TIMEOUT,
        Self::FAIL,
        Self::XPASS,
    ];
}

#[derive(Debug, Clone)]
pub struct TestResult {
    pub code: ResultCode,
    pub output: String,
    pub elapsed: Option<f64>,
    pub attempts: usize,
    pub max_allowed_attempts: Option<usize>,
    pub test_updater_outputs: Vec<String>,
    pub metrics: HashMap<String, String>,
    pub micro_results: HashMap<String, TestResult>,
}

impl TestResult {
    pub fn new(code: ResultCode, output: impl Into<String>) -> Self {
        Self {
            code,
            output: output.into(),
            elapsed: None,
            attempts: 1,
            max_allowed_attempts: None,
            test_updater_outputs: Vec::new(),
            metrics: HashMap::new(),
            micro_results: HashMap::new(),
        }
    }
}

impl TestResult {
    pub fn is_failure(&self) -> bool {
        self.code.is_failure
    }
}

#[derive(Debug, Clone)]
pub struct Test {
    pub suite_name: String,
    pub source_root: std::path::PathBuf,
    pub exec_root: std::path::PathBuf,
    pub relative_path: std::path::PathBuf,
    pub config: TestConfig,
    pub xfails: Vec<String>,
    pub requires: Vec<String>,
    pub unsupported: Vec<String>,
    pub allowed_retries: usize,
    pub exclude_xfail: bool,
    pub unsupported_not: bool,
    pub xfail_not: bool,
    pub result: Option<TestResult>,
    pub previous_failure: bool,
    pub previous_elapsed: f64,
}

impl Test {
    pub fn new(
        suite_name: impl Into<String>,
        source_root: std::path::PathBuf,
        exec_root: std::path::PathBuf,
        relative_path: impl Into<std::path::PathBuf>,
        config: TestConfig,
    ) -> Self {
        Self {
            suite_name: suite_name.into(),
            source_root,
            exec_root,
            relative_path: relative_path.into(),
            config,
            xfails: Vec::new(),
            requires: Vec::new(),
            unsupported: Vec::new(),
            allowed_retries: 0,
            exclude_xfail: false,
            unsupported_not: false,
            xfail_not: false,
            result: None,
            previous_failure: false,
            previous_elapsed: 0.0,
        }
    }

    pub fn source_path(&self) -> std::path::PathBuf {
        self.source_root.join(&self.relative_path)
    }

    pub fn exec_path(&self) -> std::path::PathBuf {
        self.exec_root.join(&self.relative_path)
    }

    pub fn get_full_name(&self) -> String {
        format!("{} :: {}", self.suite_name, self.relative_path.display())
    }

    pub fn set_result(&mut self, mut result: TestResult) {
        let is_expected_to_fail = self.is_expected_to_fail();
        if is_expected_to_fail {
            if result.code == ResultCode::PASS {
                result.code = ResultCode::XPASS;
            } else if result.code == ResultCode::FAIL {
                result.code = ResultCode::XFAIL;
            }
        }
        self.result = Some(result);
    }

    pub fn is_expected_to_fail(&self) -> bool {
        if self.exclude_xfail {
            return false;
        }
        if self.xfails.iter().any(|expr| expr == "*") {
            return true;
        }
        if self.xfail_not {
            return !self.matching_bool_expr_list(&self.xfails);
        }
        self.matching_bool_expr_list(&self.xfails)
    }

    pub fn missing_required_features(&self) -> Vec<String> {
        self.requires
            .iter()
            .filter_map(|expr| {
                if expr == "*" {
                    return None;
                }
                if !self.matching_bool_expr(expr).unwrap_or(false) {
                    Some(expr.clone())
                } else {
                    None
                }
            })
            .collect()
    }

    pub fn unsupported_features(&self) -> Vec<String> {
        if self.unsupported_not {
            return Vec::new();
        }
        self.unsupported
            .iter()
            .filter_map(|expr| {
                if expr == "*" || self.matching_bool_expr(expr).unwrap_or(false) {
                    Some(expr.clone())
                } else {
                    None
                }
            })
            .collect()
    }

    pub fn is_within_feature_limits(&self) -> bool {
        if self.config.limit_to_features.is_empty() {
            return true;
        }

        if !self.missing_required_features().is_empty() {
            return false;
        }

        let without_limits: HashSet<String> = self
            .config
            .available_features
            .iter()
            .filter(|f| !self.config.limit_to_features.contains((*f).as_str()))
            .cloned()
            .collect();

        let requires_without_limits = self.requires.iter().all(|expr| {
            if expr == "*" {
                false
            } else {
                self.matching_bool_expr_with_features(expr, &without_limits)
                    .unwrap_or(false)
            }
        });
        !requires_without_limits
    }

    fn matching_bool_expr_list(&self, list: &[String]) -> bool {
        list.iter()
            .any(|expr| self.matching_bool_expr(expr).unwrap_or(false))
    }

    fn matching_bool_expr(&self, expr: &str) -> Result<bool, String> {
        crate::boolean_expression::evaluate(expr, &self.config.available_features)
    }

    fn matching_bool_expr_with_features(
        &self,
        expr: &str,
        features: &HashSet<String>,
    ) -> Result<bool, String> {
        crate::boolean_expression::evaluate(expr, features)
    }
}

#[derive(Debug, Clone, Default)]
pub struct TestConfig {
    pub name: String,
    pub suffixes: HashSet<String>,
    pub exclusions: HashSet<String>,
    pub substitutions: Vec<(String, String)>,
    pub default_substitutions: Vec<(String, String)>,
    pub unsupported: bool,
    pub normalize_slashes: bool,
    pub test_exec_root: Option<std::path::PathBuf>,
    pub test_source_root: Option<std::path::PathBuf>,
    pub available_features: HashSet<String>,
    pub pipefail: bool,
    pub recursive_expansion_limit: Option<usize>,
    pub limit_to_features: HashSet<String>,
    pub max_individual_test_time: Option<std::time::Duration>,
    pub environment: HashMap<String, String>,
    pub no_execute: bool,
    pub test_retry_attempts: usize,
    pub run_with_external_shell: bool,
}

impl TestConfig {
    pub fn is_excluded_name(&self, name: &str) -> bool {
        self.exclusions.contains(name) || name == "Inputs" || name == ".git" || name == ".svn"
    }
}