#![warn(missing_docs)]
mod boolean_expression;
pub mod config;
pub mod discovery;
mod filecheck;
pub mod formats;
mod integrated;
mod runner;
mod shell;
mod substitution;
pub mod test;
pub use config::LitConfig;
pub use libtest_mimic::{Arguments, Conclusion};
pub use test::{ResultCode, Test, TestResult};
use std::path::PathBuf;
#[derive(Debug, Default)]
pub struct Suite {
config: LitConfig,
}
impl Suite {
pub fn new(root: impl Into<PathBuf>) -> Self {
Self {
config: LitConfig::new(root),
}
}
pub fn with_root(mut self, root: impl Into<PathBuf>) -> Self {
self.config.root = config::anchor_path(root.into());
self.config.test_source_root = None;
self
}
pub fn tool(mut self, name: impl Into<String>, path: impl Into<PathBuf>) -> 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, pattern: impl Into<String>, value: impl Into<String>) -> Self {
self.config = self.config.substitution(pattern, 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, seconds: u64) -> Self {
self.config = self.config.max_individual_test_time(seconds);
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: Arguments) -> Conclusion {
let tests = match discovery::try_discover_tests(&self.config.root, &self.config) {
Ok(tests) if !tests.is_empty() => tests,
Ok(_) => {
let root = self
.config
.test_source_root
.as_deref()
.unwrap_or(&self.config.root)
.display()
.to_string();
let trial = libtest_mimic::Trial::test("litnik :: discovery", move || {
Err(format!("no tests discovered below '{root}'").into())
});
return libtest_mimic::run(&args, vec![trial]);
}
Err(error) => {
let message = error.to_string();
let trial = libtest_mimic::Trial::test("litnik :: discovery", move || {
Err(message.clone().into())
});
return libtest_mimic::run(&args, vec![trial]);
}
};
let mut trials = Vec::with_capacity(tests.len());
for mut test in tests {
let config = self.config.clone();
let name = test.get_full_name();
trials.push(libtest_mimic::Trial::ignorable_test(name, move || {
let result = 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()),
})
}
code if code.is_failure() => {
let message =
format!("{} ({})\n{}", code.name(), code.label(), result.output);
Err(message.into())
}
_ => Ok(libtest_mimic::Completion::Completed),
}
}));
}
libtest_mimic::run(&args, trials)
}
pub fn run_from_args(self) -> ! {
self.run(Arguments::from_args()).exit()
}
}
#[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);
}
}