litnik 0.1.0

A Rust-native, libtest-mimic integrated runner for LLVM/MLIR-style lit tests.
Documentation
pub mod boolean_expression;
pub mod config;
pub mod discovery;
pub mod formats;
pub mod integrated;
pub mod runner;
pub mod shell;
pub mod substitution;
pub mod test;

pub use config::LitConfig;
pub use shell::{execute_script_node, Lexer, ShParser, ShellEnv};
pub use test::{ResultCode, Test, TestResult};

use std::path::PathBuf;

#[derive(Debug)]
pub struct Suite {
    root: PathBuf,
    config: LitConfig,
}

impl Suite {
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self {
            root: root.into(),
            config: LitConfig::new("."),
        }
    }

    pub fn with_root(mut self, root: impl Into<PathBuf>) -> Self {
        self.root = root.into();
        self
    }

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

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

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

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

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

    pub fn substitution(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.config = self.config.substitution(key, value);
        self
    }

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

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

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

    pub fn source_root(mut self, root: impl Into<PathBuf>) -> Self {
        self.config = self.config.source_root(root);
        self
    }

    pub fn exec_root(mut self, root: impl Into<PathBuf>) -> Self {
        self.config = self.config.exec_root(root);
        self
    }

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

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

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

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

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

    pub fn normalize_slashes(mut self, value: bool) -> Self {
        self.config = self.config.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.config = self.config.limit_to_features(values);
        self
    }

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

    pub fn run(self, args: libtest_mimic::Arguments) -> libtest_mimic::Conclusion {
        let tests = discovery::discover_tests(&self.root, &self.config);

        let mut trials = Vec::with_capacity(tests.len());
        for mut test in tests {
            let config = self.config.clone();
            let full_name = test.get_full_name();
            trials.push(libtest_mimic::Trial::ignorable_test(full_name, move || {
                let result = crate::formats::run_shtest(&mut test, &config);
                match result.code {
                    ResultCode::EXCLUDED | ResultCode::SKIPPED | ResultCode::UNSUPPORTED => {
                        Ok(libtest_mimic::Completion::Ignored {
                            reason: Some(result.code.label.to_string()),
                        })
                    }
                    _ if result.code.is_failure => {
                        let msg = if result.output.is_empty() {
                            result.code.label.to_string()
                        } else {
                            result.output
                        };
                        Err(msg.into())
                    }
                    _ => Ok(libtest_mimic::Completion::Completed),
                }
            }));
        }

        libtest_mimic::run(&args, trials)
    }
}

impl Default for Suite {
    fn default() -> Self {
        Self {
            root: PathBuf::from("."),
            config: LitConfig::default(),
        }
    }
}

#[cfg(test)]
mod tests {
    use libtest_mimic::Arguments;

    #[test]
    fn config_builder_is_chainable() {
        let suite = crate::Suite::new("/tmp")
            .tool("butcher-opt", "/bin/true")
            .suffix(".mlir")
            .feature("linux")
            .excludes(vec!["Ignore"])
            .test_retry_attempts(2)
            .limit_to_features(vec!["linux"]);

        let cfg = suite.config;
        assert!(cfg.tools.contains_key("butcher-opt"));
        assert!(cfg.suffixes.contains(".mlir"));
        assert!(cfg.available_features.contains("linux"));
    }

    #[test]
    fn suite_runs_with_empty_tests() {
        let suite = super::Suite::new(".").suffix(".does-not-exist");
        let args = Arguments::from_args();
        let _ = suite.run(args);
    }
}