Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/**'
Expand Down
15 changes: 9 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <crate> -- --ignored`. Document a
new variable in the test's doc comment and in the pull request.
`xtp_testkit::skip` writes `SKIPPED <test>: <reason>` directly to stderr so
skips stay visible under default `cargo test` capture; setting
`XTP_REQUIRE=<reason,...>` turns matching skips into a panic. Run the ignored
ones with `cargo test -p <crate> -- --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`).
Expand All @@ -162,9 +165,9 @@ Every commit message and every pull request title starts with a header:
```

- `<area>` is one of: `core`, `pe`, `loader`, `hle`, `gpu`, `shader`,
`formats`, `pipeline`, `game` (the crates `crates/xtp-<area>`), `app`
(`apps/xtp`), `xtask`, `tests`, `ci` (`.github/`, `.githooks/`), `docs`,
`deps` (dependency updates), `build` (workspace manifest, toolchain,
`formats`, `pipeline`, `game`, `testkit` (the crates `crates/xtp-<area>`),
`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: ...`).
- `<summary>` says what the change does, in the imperative ("clamp indirect
Expand Down
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions crates/xtp-testkit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
187 changes: 187 additions & 0 deletions crates/xtp-testkit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
//! 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 writes a uniform line
//! (`SKIPPED <test>: <reason>`) 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=<reason,...>` 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`.
///
/// Writes `SKIPPED <test>: <reason>` 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();
if is_required(reason, require.as_deref()) {
panic!(
"test `{test}` cannot skip for `{reason}` ({REQUIRE_ENV}={})",
require.as_deref().unwrap_or_default()
);
}
let _ = writeln!(std::io::stderr().lock(), "{}", 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
/// `<unnamed>` outside a named thread.
pub fn current_test_name() -> String {
std::thread::current()
.name()
.filter(|n| !n.is_empty())
.unwrap_or("<unnamed>")
.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, reason::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(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]
fn child_skip_no_gpu() {
if std::env::var_os(CHILD_ENV).is_none() {
return;
}
skip(reason::NO_GPU);
}

#[test]
fn child_skip_other_reason() {
if std::env::var_os(CHILD_ENV).is_none() {
return;
}
skip(reason::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"])
.env(CHILD_ENV, "1")
.env(REQUIRE_ENV, reason::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("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"])
.env(CHILD_ENV, "1")
.env(REQUIRE_ENV, reason::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 under default capture: {stderr}"
);
}
}
1 change: 1 addition & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
1 change: 1 addition & 0 deletions docs/dev/START_HERE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/` |
Expand Down
1 change: 1 addition & 0 deletions xtask/src/conventions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/"),
Expand Down
Loading