From b232bd8a5d2801551bfed39db20ec04d64f442b0 Mon Sep 17 00:00:00 2001 From: PrivacyOnion Date: Tue, 24 Nov 2026 19:31:49 +0100 Subject: [PATCH 1/2] tests: add xtp-testkit crate with visible test skip helper --- .github/labeler.yml | 4 + Cargo.lock | 4 + Cargo.toml | 1 + crates/xtp-testkit/Cargo.toml | 11 +++ crates/xtp-testkit/src/lib.rs | 150 ++++++++++++++++++++++++++++++++++ xtask/src/conventions.rs | 1 + 6 files changed, 171 insertions(+) create mode 100644 crates/xtp-testkit/Cargo.toml create mode 100644 crates/xtp-testkit/src/lib.rs diff --git a/.github/labeler.yml b/.github/labeler.yml index 4b298202..aec23131 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -40,6 +40,10 @@ area/game: - changed-files: - any-glob-to-any-file: 'crates/xtp-game/**' +area/testkit: + - changed-files: + - any-glob-to-any-file: 'crates/xtp-testkit/**' + area/app: - changed-files: - any-glob-to-any-file: 'apps/**' diff --git a/Cargo.lock b/Cargo.lock index e40d88f6..955e9f15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -920,3 +920,7 @@ dependencies = [ "windows", "windows-sys", ] + +[[package]] +name = "xtp-testkit" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0870b212..9bd5eae2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ xtp-shader = { path = "crates/xtp-shader" } xtp-formats = { path = "crates/xtp-formats" } xtp-pipeline = { path = "crates/xtp-pipeline" } xtp-game = { path = "crates/xtp-game" } +xtp-testkit = { path = "crates/xtp-testkit" } # External crates anyhow = "1.0.104" diff --git a/crates/xtp-testkit/Cargo.toml b/crates/xtp-testkit/Cargo.toml new file mode 100644 index 00000000..0d3358f3 --- /dev/null +++ b/crates/xtp-testkit/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "xtp-testkit" +description = "Shared test helpers (dev-dependency only): visible, countable test skips." +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +publish.workspace = true + +[lints] +workspace = true diff --git a/crates/xtp-testkit/src/lib.rs b/crates/xtp-testkit/src/lib.rs new file mode 100644 index 00000000..2e582ee6 --- /dev/null +++ b/crates/xtp-testkit/src/lib.rs @@ -0,0 +1,150 @@ +//! Shared test helpers for XTP (dev-dependency only). +//! +//! Host- and sample-dependent tests call [`skip`] when a prerequisite is absent +//! instead of silently returning or panicking. Every skip emits a uniform line +//! (`SKIPPED : `) so test runners can count skips by reason, and +//! machines that must execute a category can set `XTP_REQUIRE=` to +//! turn matching skips into panics. + +/// Environment variable listing comma-separated skip reasons that must panic +/// instead of skipping (or `all` / `*` to forbid every skip). +pub const REQUIRE_ENV: &str = "XTP_REQUIRE"; + +/// Record that the current test is skipped for `reason`, or panic if +/// [`REQUIRE_ENV`] lists `reason`. +/// +/// Prints `SKIPPED : ` to stderr and returns when skipping is +/// allowed. +pub fn skip(reason: &str) { + let test = current_test_name(); + let require = std::env::var(REQUIRE_ENV).ok(); + if is_required(reason, require.as_deref()) { + panic!( + "test `{test}` cannot skip for `{reason}` ({REQUIRE_ENV}={})", + require.as_deref().unwrap_or_default() + ); + } + eprintln!("{}", format_skip(&test, reason)); +} + +/// Format the canonical skip marker for `test` and `reason`. +pub fn format_skip(test: &str, reason: &str) -> String { + format!("SKIPPED {test}: {reason}") +} + +/// Name of the running test from the current thread (set by `libtest`), or +/// `` outside a named thread. +pub fn current_test_name() -> String { + std::thread::current() + .name() + .filter(|n| !n.is_empty()) + .unwrap_or("") + .to_owned() +} + +/// Return `true` if `require_list` (a comma-separated `XTP_REQUIRE` value) +/// forbids skipping for `reason`. +pub fn is_required(reason: &str, require_list: Option<&str>) -> bool { + let Some(list) = require_list else { + return false; + }; + let reason = reason.trim(); + list.split(',') + .map(str::trim) + .filter(|entry| !entry.is_empty()) + .any(|entry| { + entry == "*" || entry.eq_ignore_ascii_case("all") || entry.eq_ignore_ascii_case(reason) + }) +} + +#[cfg(test)] +mod tests { + use std::process::Command; + + use super::*; + + const CHILD_ENV: &str = "XTP_TESTKIT_CHILD"; + + #[test] + fn marker_names_the_current_test() { + let name = current_test_name(); + let line = format_skip(&name, "no-gpu"); + assert!( + line.starts_with("SKIPPED ") && line.ends_with(": no-gpu"), + "unexpected marker: {line}" + ); + assert!( + line.contains("marker_names_the_current_test"), + "marker should include test name: {line}" + ); + } + + #[test] + fn require_list_matching() { + assert!(!is_required("no-gpu", None)); + assert!(!is_required("no-gpu", Some(""))); + assert!(!is_required("no-gpu", Some("no-dxc, no-sample"))); + assert!(is_required("no-gpu", Some("no-gpu"))); + assert!(is_required("no-gpu", Some("no-dxc, NO-GPU , no-sample"))); + assert!(is_required("no-gpu", Some("all"))); + assert!(is_required("no-gpu", Some("*"))); + } + + #[test] + fn child_skip_no_gpu() { + if std::env::var_os(CHILD_ENV).is_none() { + return; + } + skip("no-gpu"); + } + + #[test] + fn child_skip_other_reason() { + if std::env::var_os(CHILD_ENV).is_none() { + return; + } + skip("no-dxc"); + } + + #[test] + fn require_env_panics_only_for_listed_reason() { + let exe = std::env::current_exe().expect("test binary path"); + + let panic_run = Command::new(&exe) + .args(["--exact", "tests::child_skip_no_gpu", "--nocapture"]) + .env(CHILD_ENV, "1") + .env(REQUIRE_ENV, "no-gpu") + .output() + .expect("spawn child test for required reason"); + assert!( + !panic_run.status.success(), + "expected child to panic when XTP_REQUIRE=no-gpu" + ); + let combined = format!( + "{}\n{}", + String::from_utf8_lossy(&panic_run.stdout), + String::from_utf8_lossy(&panic_run.stderr) + ); + assert!( + combined.contains("no-gpu"), + "panic output should name reason: {combined}" + ); + + let skip_run = Command::new(&exe) + .args(["--exact", "tests::child_skip_other_reason", "--nocapture"]) + .env(CHILD_ENV, "1") + .env(REQUIRE_ENV, "no-gpu") + .output() + .expect("spawn child test for non-required reason"); + assert!( + skip_run.status.success(), + "unrelated reason should skip cleanly: stderr={}", + String::from_utf8_lossy(&skip_run.stderr) + ); + let stderr = String::from_utf8_lossy(&skip_run.stderr); + assert!( + stderr.contains("SKIPPED tests::child_skip_other_reason: no-dxc"), + "stderr should contain canonical skip marker: {stderr}" + ); + } +} diff --git a/xtask/src/conventions.rs b/xtask/src/conventions.rs index 033a34c8..068cb87b 100644 --- a/xtask/src/conventions.rs +++ b/xtask/src/conventions.rs @@ -25,6 +25,7 @@ pub const AREAS: &[(&str, &str)] = &[ ("formats", "crates/xtp-formats"), ("pipeline", "crates/xtp-pipeline"), ("game", "crates/xtp-game"), + ("testkit", "crates/xtp-testkit"), ("app", "apps/xtp"), ("xtask", "xtask"), ("tests", "tests/"), From 2e0aff4bd4ed4c4dfb492e06baaf55aa6e7a06fb Mon Sep 17 00:00:00 2001 From: PrivacyOnion Date: Tue, 24 Nov 2026 19:47:40 +0100 Subject: [PATCH 2/2] testkit, docs: write skips directly to stderr and update docs --- CONTRIBUTING.md | 15 ++++--- crates/xtp-testkit/src/lib.rs | 85 +++++++++++++++++++++++++---------- docs/architecture.md | 1 + docs/dev/START_HERE.md | 1 + 4 files changed, 72 insertions(+), 30 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index db766fb6..d1466ba9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -140,15 +140,18 @@ failure. CI also runs `actionlint` on the workflows. #[test] fn decodes_real_streams() { let Some(dir) = std::env::var_os("XTP_XMA_STREAMS") else { - eprintln!("skipped: set XTP_XMA_STREAMS to a folder of your own streams"); + xtp_testkit::skip("no-sample XTP_XMA_STREAMS"); return; }; // ... } ``` - Run the ignored ones with `cargo test -p -- --ignored`. Document a - new variable in the test's doc comment and in the pull request. + `xtp_testkit::skip` writes `SKIPPED : ` directly to stderr so + skips stay visible under default `cargo test` capture; setting + `XTP_REQUIRE=` turns matching skips into a panic. Run the ignored + ones with `cargo test -p -- --ignored`. Document a new variable in + the test's doc comment and in the pull request. - Such tests assert on properties (it decodes, sizes match, the hash of our output is stable for your input), never on snapshots of game content, and they write nothing below the repository (`xtp_core::ensure_outside_repo`). @@ -162,9 +165,9 @@ Every commit message and every pull request title starts with a header: ``` - `` is one of: `core`, `pe`, `loader`, `hle`, `gpu`, `shader`, - `formats`, `pipeline`, `game` (the crates `crates/xtp-`), `app` - (`apps/xtp`), `xtask`, `tests`, `ci` (`.github/`, `.githooks/`), `docs`, - `deps` (dependency updates), `build` (workspace manifest, toolchain, + `formats`, `pipeline`, `game`, `testkit` (the crates `crates/xtp-`), + `app` (`apps/xtp`), `xtask`, `tests`, `ci` (`.github/`, `.githooks/`), + `docs`, `deps` (dependency updates), `build` (workspace manifest, toolchain, `.cargo/`, repository settings). A topic may follow (`hle/audio`), and a change that spans areas names them all (`gpu, shader: ...`). - `` says what the change does, in the imperative ("clamp indirect diff --git a/crates/xtp-testkit/src/lib.rs b/crates/xtp-testkit/src/lib.rs index 2e582ee6..3bdf26ff 100644 --- a/crates/xtp-testkit/src/lib.rs +++ b/crates/xtp-testkit/src/lib.rs @@ -1,20 +1,52 @@ //! Shared test helpers for XTP (dev-dependency only). //! //! Host- and sample-dependent tests call [`skip`] when a prerequisite is absent -//! instead of silently returning or panicking. Every skip emits a uniform line -//! (`SKIPPED : `) so test runners can count skips by reason, and -//! machines that must execute a category can set `XTP_REQUIRE=` to -//! turn matching skips into panics. +//! instead of silently returning or panicking. Every skip writes a uniform line +//! (`SKIPPED : `) directly to stderr (bypassing `libtest` +//! capture) so test runners can count skips by reason, and machines that must +//! execute a category can set `XTP_REQUIRE=` to turn matching skips +//! into panics. (In backlog item B3, `XTP_REQUIRE_GPU` will fold into +//! `XTP_REQUIRE=no-gpu`.) +//! +//! # Example +//! +//! ```no_run +//! fn needs_gpu() { +//! let has_gpu = false; +//! if !has_gpu { +//! xtp_testkit::skip(xtp_testkit::reason::NO_GPU); +//! return; +//! } +//! } +//! ``` + +use std::io::Write; /// Environment variable listing comma-separated skip reasons that must panic /// instead of skipping (or `all` / `*` to forbid every skip). pub const REQUIRE_ENV: &str = "XTP_REQUIRE"; +/// Standard skip reasons shared across crates so call sites and `XTP_REQUIRE` +/// values do not drift apart. +pub mod reason { + /// No hardware D3D12 GPU adapter is available on this host. + pub const NO_GPU: &str = "no-gpu"; + /// The host GPU does not support Shader Model 6.6. + pub const NO_SM66: &str = "no-sm66"; + /// Host `dxcompiler.dll` / `dxc` is unavailable. + pub const NO_DXC: &str = "no-dxc"; + /// Host `dxil.dll` validator is unavailable. + pub const NO_DXIL: &str = "no-dxil"; + /// Console DXC compiler is unavailable. + pub const NO_CONSOLE_DXC: &str = "no-console-dxc"; +} + /// Record that the current test is skipped for `reason`, or panic if /// [`REQUIRE_ENV`] lists `reason`. /// -/// Prints `SKIPPED : ` to stderr and returns when skipping is -/// allowed. +/// Writes `SKIPPED : ` directly to stderr (so it stays visible +/// under default `libtest` output capture) and returns; callers must `return` +/// immediately after calling `skip`. pub fn skip(reason: &str) { let test = current_test_name(); let require = std::env::var(REQUIRE_ENV).ok(); @@ -24,7 +56,7 @@ pub fn skip(reason: &str) { require.as_deref().unwrap_or_default() ); } - eprintln!("{}", format_skip(&test, reason)); + let _ = writeln!(std::io::stderr().lock(), "{}", format_skip(&test, reason)); } /// Format the canonical skip marker for `test` and `reason`. @@ -68,7 +100,7 @@ mod tests { #[test] fn marker_names_the_current_test() { let name = current_test_name(); - let line = format_skip(&name, "no-gpu"); + let line = format_skip(&name, reason::NO_GPU); assert!( line.starts_with("SKIPPED ") && line.ends_with(": no-gpu"), "unexpected marker: {line}" @@ -81,13 +113,16 @@ mod tests { #[test] fn require_list_matching() { - assert!(!is_required("no-gpu", None)); - assert!(!is_required("no-gpu", Some(""))); - assert!(!is_required("no-gpu", Some("no-dxc, no-sample"))); - assert!(is_required("no-gpu", Some("no-gpu"))); - assert!(is_required("no-gpu", Some("no-dxc, NO-GPU , no-sample"))); - assert!(is_required("no-gpu", Some("all"))); - assert!(is_required("no-gpu", Some("*"))); + assert!(!is_required(reason::NO_GPU, None)); + assert!(!is_required(reason::NO_GPU, Some(""))); + assert!(!is_required(reason::NO_GPU, Some("no-dxc, no-sample"))); + assert!(is_required(reason::NO_GPU, Some("no-gpu"))); + assert!(is_required( + reason::NO_GPU, + Some("no-dxc, NO-GPU , no-sample") + )); + assert!(is_required(reason::NO_GPU, Some("all"))); + assert!(is_required(reason::NO_GPU, Some("*"))); } #[test] @@ -95,7 +130,7 @@ mod tests { if std::env::var_os(CHILD_ENV).is_none() { return; } - skip("no-gpu"); + skip(reason::NO_GPU); } #[test] @@ -103,7 +138,7 @@ mod tests { if std::env::var_os(CHILD_ENV).is_none() { return; } - skip("no-dxc"); + skip(reason::NO_DXC); } #[test] @@ -111,9 +146,9 @@ mod tests { let exe = std::env::current_exe().expect("test binary path"); let panic_run = Command::new(&exe) - .args(["--exact", "tests::child_skip_no_gpu", "--nocapture"]) + .args(["--exact", "tests::child_skip_no_gpu"]) .env(CHILD_ENV, "1") - .env(REQUIRE_ENV, "no-gpu") + .env(REQUIRE_ENV, reason::NO_GPU) .output() .expect("spawn child test for required reason"); assert!( @@ -126,14 +161,16 @@ mod tests { String::from_utf8_lossy(&panic_run.stderr) ); assert!( - combined.contains("no-gpu"), - "panic output should name reason: {combined}" + combined.contains("cannot skip") && combined.contains(reason::NO_GPU), + "panic output should report cannot skip and reason: {combined}" ); + // Intentionally run without `--nocapture` to prove the SKIPPED marker + // reaches stderr under default `libtest` output capture. let skip_run = Command::new(&exe) - .args(["--exact", "tests::child_skip_other_reason", "--nocapture"]) + .args(["--exact", "tests::child_skip_other_reason"]) .env(CHILD_ENV, "1") - .env(REQUIRE_ENV, "no-gpu") + .env(REQUIRE_ENV, reason::NO_GPU) .output() .expect("spawn child test for non-required reason"); assert!( @@ -144,7 +181,7 @@ mod tests { let stderr = String::from_utf8_lossy(&skip_run.stderr); assert!( stderr.contains("SKIPPED tests::child_skip_other_reason: no-dxc"), - "stderr should contain canonical skip marker: {stderr}" + "stderr should contain canonical skip marker under default capture: {stderr}" ); } } diff --git a/docs/architecture.md b/docs/architecture.md index 4b912906..82bd07c6 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -38,6 +38,7 @@ Converting them ahead of time from files is only a warm-up. | `xtp-formats` | the GPU's texture layouts (sizes, tiling, detiling), audio codecs; planned: BCPack | | `xtp-pipeline` | converter trait, content-addressed cache | | `xtp-game` | BTP 6-specific formats, build identities, hooks | +| `xtp-testkit` | dev-dependency only: visible test skips (`skip`, `XTP_REQUIRE`) | | `apps/xtp` | command-line front end | | `xtask` | asset-policy check, git hooks | | `tests/fixtures/hle-probe` | a stand-in "title" (our own code) for loader tests | diff --git a/docs/dev/START_HERE.md b/docs/dev/START_HERE.md index 9b65c6b2..40d9771e 100644 --- a/docs/dev/START_HERE.md +++ b/docs/dev/START_HERE.md @@ -29,6 +29,7 @@ in the repository ([asset policy](../asset-policy.md)). | `crates/xtp-formats` | the GPU's tiled texture layouts, BCPack, XMA and Opus audio; pure functions, bytes in and bytes out | `texture.rs`, `xma.rs` | | `crates/xtp-pipeline` | offline conversion of the user's data, content-addressed cache | `cache.rs` | | `crates/xtp-game` | BTP 6 only: its formats, build identities, hooks | `lib.rs` | +| `crates/xtp-testkit` | dev-dependency only: visible, countable test skips (`xtp_testkit::skip`, `XTP_REQUIRE`) | `lib.rs` | | `apps/xtp` | the `xtp` command: `inspect`, `census`, `shader-info`, `translate`, `run`, `config` | `main.rs`, `run.rs` | | `xtask` | repository tooling: `policy`, `conventions`, `pr-check`, `install-hooks` | `main.rs` | | `tests/fixtures/hle-probe` | a stand-in title we wrote, built by the loader's tests | `src/` |