use std::{
env,
path::{Path, PathBuf},
};
pub use ui_test;
use ui_test::{
bless_output_files,
color_eyre::eyre::eyre,
default_file_filter, default_per_file_config,
dependencies::DependencyBuilder,
ignore_output_conflict, run_tests_generic,
spanned::Spanned,
status_emitter::{Gha, StatusEmitter, Text},
Args, Config,
};
fn basic_config(root_dir: impl Into<PathBuf>, args: &Args) -> ui_test::Result<Config> {
let root_dir = root_dir.into();
match root_dir.try_exists() {
Ok(true) => { }
Ok(false) => {
return Err(eyre!("path does not exist: {}", root_dir.display()));
}
Err(error) => {
return Err(eyre!(
"failed to read path: {} ({})",
root_dir.display(),
error
));
}
}
let mut config = Config {
bless_command: Some(
"`cargo test` with the BLESS environment variable set to any non empty value"
.to_string(),
),
output_conflict_handling: if env::var_os("BLESS").is_some() {
bless_output_files
} else {
ignore_output_conflict
},
..Config::rustc(root_dir)
};
config.with_args(args);
let bevy_root = "..";
config.path_stderr_filter(Path::new(bevy_root), b"$BEVY_ROOT");
if let Some(path) = option_env!("RUSTUP_HOME") {
config.path_stderr_filter(Path::new(path), b"$RUSTUP_HOME");
}
config.stderr_filter(r"\/home\/[\pL\pN_@#\-\. ]+", "$HOME");
config.stderr_filter(
r"[a-zA-Z]:(?:\\|\/)users(?:\\|\/)[\pL\pN_@#\-\. ]+",
"$HOME",
);
config.comment_defaults.base().custom.insert(
"dependencies",
Spanned::dummy(vec![Box::new(DependencyBuilder::default())]),
);
Ok(config)
}
pub fn test(test_name: impl Into<String>, test_root: impl Into<PathBuf>) -> ui_test::Result<()> {
test_multiple(test_name, [test_root])
}
pub fn test_with_config(test_name: impl Into<String>, config: Config) -> ui_test::Result<()> {
test_with_multiple_configs(test_name, [Ok(config)])
}
pub fn test_multiple(
test_name: impl Into<String>,
test_roots: impl IntoIterator<Item = impl Into<PathBuf>>,
) -> ui_test::Result<()> {
let args = Args::test()?;
let configs = test_roots.into_iter().map(|root| basic_config(root, &args));
test_with_multiple_configs(test_name, configs)
}
pub fn test_with_multiple_configs(
test_name: impl Into<String>,
configs: impl IntoIterator<Item = ui_test::Result<Config>>,
) -> ui_test::Result<()> {
let configs = configs
.into_iter()
.collect::<ui_test::Result<Vec<Config>>>()?;
let emitter: Box<dyn StatusEmitter + Send> = if env::var_os("CI").is_some() {
Box::new((
Text::verbose(),
Gha {
group: true,
name: test_name.into(),
},
))
} else {
Box::new(Text::quiet())
};
run_tests_generic(
configs,
default_file_filter,
default_per_file_config,
emitter,
)
}