|
| 1 | +use std::{ |
| 2 | + env, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +// Re-export ui_test so all the tests use the same version. |
| 7 | +pub use ui_test; |
| 8 | + |
| 9 | +use ui_test::{ |
| 10 | + default_file_filter, default_per_file_config, run_tests_generic, |
| 11 | + status_emitter::{Gha, StatusEmitter, Text}, |
| 12 | + Args, Config, OutputConflictHandling, |
| 13 | +}; |
| 14 | + |
| 15 | +/// Use this instead of hand rolling configs. |
| 16 | +/// |
| 17 | +/// `root_dir` is the directory your tests are contained in. Needs to be a path from crate root. |
| 18 | +fn basic_config(root_dir: impl Into<PathBuf>, args: &Args) -> Config { |
| 19 | + let mut config = Config { |
| 20 | + dependencies_crate_manifest_path: Some("Cargo.toml".into()), |
| 21 | + bless_command: Some("`cargo test` with the BLESS environment variable set to any non empty value".to_string()), |
| 22 | + output_conflict_handling: if env::var_os("BLESS").is_some() { |
| 23 | + OutputConflictHandling::Bless |
| 24 | + } else { |
| 25 | + // stderr output changes between rust versions so we just rely on annotations |
| 26 | + OutputConflictHandling::Ignore |
| 27 | + }, |
| 28 | + ..Config::rustc(root_dir) |
| 29 | + }; |
| 30 | + |
| 31 | + config.with_args(args); |
| 32 | + |
| 33 | + let bevy_root = ".."; |
| 34 | + |
| 35 | + // Don't leak contributor filesystem paths |
| 36 | + config.path_stderr_filter(Path::new(bevy_root), b"$BEVY_ROOT"); |
| 37 | + config.path_stderr_filter(Path::new(env!("RUSTUP_HOME")), b"$RUSTUP_HOME"); |
| 38 | + |
| 39 | + // ui_test doesn't compile regex with perl character classes. |
| 40 | + // \pL = unicode class for letters, \pN = unicode class for numbers |
| 41 | + config.stderr_filter(r"\/home\/[\pL\pN_@#\-\. ]+", "$HOME"); |
| 42 | + // Paths in .stderr seem to always be normalized to use /. Handle both anyway. |
| 43 | + config.stderr_filter( |
| 44 | + r"[a-zA-Z]:(?:\\|\/)users(?:\\|\/)[\pL\pN_@#\-\. ]+", // NOTE: [\pL\pN_@#\-\. ] is a poor attempt at handling usernames |
| 45 | + "$HOME", |
| 46 | + ); |
| 47 | + |
| 48 | + config |
| 49 | +} |
| 50 | + |
| 51 | +/// Runs ui tests for a single directory. |
| 52 | +/// |
| 53 | +/// `root_dir` is the directory your tests are contained in. Needs to be a path from crate root. |
| 54 | +pub fn test(test_root: impl Into<PathBuf>) -> ui_test::Result<()> { |
| 55 | + test_multiple([test_root]) |
| 56 | +} |
| 57 | + |
| 58 | +/// Run ui tests with the given config |
| 59 | +pub fn test_with_config(config: Config) -> ui_test::Result<()> { |
| 60 | + test_with_multiple_configs([config]) |
| 61 | +} |
| 62 | + |
| 63 | +/// Runs ui tests for a multiple directories. |
| 64 | +/// |
| 65 | +/// `root_dirs` paths need to come from crate root. |
| 66 | +pub fn test_multiple( |
| 67 | + test_roots: impl IntoIterator<Item = impl Into<PathBuf>>, |
| 68 | +) -> ui_test::Result<()> { |
| 69 | + let args = Args::test()?; |
| 70 | + |
| 71 | + let configs = test_roots.into_iter().map(|root| basic_config(root, &args)); |
| 72 | + |
| 73 | + test_with_multiple_configs(configs) |
| 74 | +} |
| 75 | + |
| 76 | +/// Run ui test with the given configs. |
| 77 | +/// |
| 78 | +/// Tests for configs are run in parallel. |
| 79 | +pub fn test_with_multiple_configs( |
| 80 | + configs: impl IntoIterator<Item = Config>, |
| 81 | +) -> ui_test::Result<()> { |
| 82 | + let configs = configs.into_iter().collect(); |
| 83 | + |
| 84 | + let emitter: Box<dyn StatusEmitter + Send> = if env::var_os("CI").is_some() { |
| 85 | + Box::new(( |
| 86 | + Text::verbose(), |
| 87 | + Gha::<true> { |
| 88 | + name: env!("CARGO_PKG_NAME").to_string(), |
| 89 | + }, |
| 90 | + )) |
| 91 | + } else { |
| 92 | + Box::new(Text::quiet()) |
| 93 | + }; |
| 94 | + |
| 95 | + run_tests_generic( |
| 96 | + configs, |
| 97 | + default_file_filter, |
| 98 | + default_per_file_config, |
| 99 | + emitter, |
| 100 | + ) |
| 101 | +} |
0 commit comments