litnik 0.1.0

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

#[derive(Debug, Clone)]
pub struct LitConfig {
    pub name: String,
    pub root: PathBuf,
    pub tools: HashMap<String, String>,
    pub suffixes: HashSet<String>,
    pub exclusions: HashSet<String>,
    pub substitutions: Vec<(String, String)>,
    pub environment: HashMap<String, String>,
    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_seconds: Option<u64>,
    pub use_external_shell: bool,
    pub no_execute: bool,
    pub normalize_slashes: bool,
    pub test_source_root: Option<PathBuf>,
    pub test_exec_root: Option<PathBuf>,
    pub default_substitutions: Vec<(String, String)>,
    pub test_retry_attempts: usize,
}

impl LitConfig {
    pub fn new(root: impl Into<PathBuf>) -> Self {
        let root = root.into();
        let mut environment = HashMap::new();
        if let Ok(path) = std::env::var("PATH") {
            environment.insert("PATH".into(), path);
        }

        Self {
            name: "litnik".into(),
            root,
            tools: HashMap::new(),
            suffixes: HashSet::new(),
            exclusions: HashSet::new(),
            substitutions: Vec::new(),
            environment,
            available_features: HashSet::new(),
            pipefail: true,
            recursive_expansion_limit: None,
            limit_to_features: HashSet::new(),
            max_individual_test_time_seconds: None,
            use_external_shell: false,
            no_execute: false,
            normalize_slashes: false,
            test_source_root: None,
            test_exec_root: None,
            default_substitutions: Vec::new(),
            test_retry_attempts: 0,
        }
    }

    pub fn suite_name(mut self, value: impl Into<String>) -> Self {
        self.name = value.into();
        self
    }

    pub fn tool(mut self, name: impl Into<String>, path: impl Into<String>) -> Self {
        self.tools.insert(name.into(), path.into());
        self
    }

    pub fn suffix(mut self, suffix: impl Into<String>) -> Self {
        self.suffixes.insert(suffix.into());
        self
    }

    pub fn suffixes<I, S>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.suffixes.extend(values.into_iter().map(Into::into));
        self
    }

    pub fn exclude(mut self, value: impl Into<String>) -> Self {
        self.exclusions.insert(value.into());
        self
    }

    pub fn excludes<I, S>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.exclusions.extend(values.into_iter().map(Into::into));
        self
    }

    pub fn substitution(mut self, pattern: impl Into<String>, value: impl Into<String>) -> Self {
        self.substitutions.push((pattern.into(), value.into()));
        self
    }

    pub fn substitutions<I, K, V>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        self.substitutions
            .extend(values.into_iter().map(|(k, v)| (k.into(), v.into())));
        self
    }

    pub fn default_substitution(
        mut self,
        pattern: impl Into<String>,
        value: impl Into<String>,
    ) -> Self {
        self.default_substitutions
            .push((pattern.into(), value.into()));
        self
    }

    pub fn default_substitutions<I, K, V>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        self.default_substitutions
            .extend(values.into_iter().map(|(k, v)| (k.into(), v.into())));
        self
    }

    pub fn feature(mut self, value: impl Into<String>) -> Self {
        self.available_features.insert(value.into());
        self
    }

    pub fn features<I, S>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.available_features
            .extend(values.into_iter().map(Into::into));
        self
    }

    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.environment.insert(key.into(), value.into());
        self
    }

    pub fn source_root(mut self, value: impl Into<PathBuf>) -> Self {
        self.test_source_root = Some(value.into());
        self
    }

    pub fn exec_root(mut self, value: impl Into<PathBuf>) -> Self {
        self.test_exec_root = Some(value.into());
        self
    }

    pub fn recursive_expansion_limit(mut self, value: Option<usize>) -> Self {
        self.recursive_expansion_limit = value;
        self
    }

    pub fn max_individual_test_time(mut self, secs: u64) -> Self {
        self.max_individual_test_time_seconds = Some(secs);
        self
    }

    pub fn use_external_shell(mut self, value: bool) -> Self {
        self.use_external_shell = value;
        self
    }

    pub fn no_execute(mut self, value: bool) -> Self {
        self.no_execute = value;
        self
    }

    pub fn with_pipefail(mut self, value: bool) -> Self {
        self.pipefail = value;
        self
    }

    pub fn normalize_slashes(mut self, value: bool) -> Self {
        self.normalize_slashes = value;
        self
    }

    pub fn limit_to_features<I, S>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.limit_to_features = values.into_iter().map(Into::into).collect();
        self
    }

    pub fn test_retry_attempts(mut self, attempts: usize) -> Self {
        self.test_retry_attempts = attempts;
        self
    }

    pub fn tool_path(&self, name: &str) -> Option<&String> {
        self.tools.get(name)
    }

    pub fn to_test_config(&self) -> crate::test::TestConfig {
        crate::test::TestConfig {
            name: self.name.clone(),
            suffixes: self.suffixes.clone(),
            exclusions: self.exclusions.clone(),
            substitutions: self.substitutions.clone(),
            default_substitutions: self.default_substitutions.clone(),
            unsupported: false,
            test_exec_root: self.test_exec_root.clone(),
            test_source_root: self.test_source_root.clone(),
            available_features: self.available_features.clone(),
            normalize_slashes: self.normalize_slashes,
            pipefail: self.pipefail,
            recursive_expansion_limit: self.recursive_expansion_limit,
            limit_to_features: self.limit_to_features.clone(),
            max_individual_test_time: self
                .max_individual_test_time_seconds
                .map(std::time::Duration::from_secs),
            environment: self.environment.clone(),
            no_execute: self.no_execute,
            run_with_external_shell: self.use_external_shell,
            test_retry_attempts: self.test_retry_attempts,
        }
    }
}

impl Default for LitConfig {
    fn default() -> Self {
        Self::new(".")
    }
}

#[cfg(test)]
mod tests {
    use super::LitConfig;

    #[test]
    fn builder_roundtrip() {
        let cfg = LitConfig::new("/tmp/root")
            .suite_name("x")
            .suffix(".mlir")
            .exclude("Inputs")
            .tool("clang", "/bin/clang")
            .feature("linux");
        assert_eq!(cfg.name, "x");
        assert!(cfg.suffixes.contains(".mlir"));
        assert!(cfg.exclusions.contains("Inputs"));
        assert_eq!(cfg.tool_path("clang"), Some(&"/bin/clang".to_string()));
        assert!(cfg.available_features.contains("linux"));
    }
}