Crafter.Build/tests/ShaderCompile/main.cpp
catbot 124c2285f9
All checks were successful
CI / build-test-release (pull_request) Successful in 7m26s
test: broaden self-test coverage beyond the compile smoke test
The suite had only HelloWorld, which built and exited an empty exe. Add
in-process tests covering each public surface area users actually touch:

- StaticLib / ModuleInterface / DependencyLink — Build() against
  fixtures for libraries, project-local module interfaces, and
  cross-config module deps with link verification (runs the built exe).
- ShaderCompile — drives Shader::Compile directly, validates SPIR-V
  magic + Check() idempotency.
- StandardArgs — covers --debug, --target=, --march=, --mtune=,
  --lib/--shared promotions, and ArgQuery::Has / Get.
- TestRunnerSpec — FromSpec parse rules, ForTarget routing for host,
  wasm32-wasip1, aarch64-linux-gnu (+ sysroot QEMU_LD_PREFIX),
  i686 → qemu-i386 rewrite, mingw → wine on Linux hosts, FromEnv.
- VariantId — confirms type / debug / sysroot / defines / compileFlags /
  target / march all perturb the cache key, plus PcmDir routing.
- WasiBrowserRuntime — calls EnableWasiBrowserRuntime, asserts the
  three cfg.files entries get registered and index.html had its
  template placeholder substituted.
- RunSingleTestExit — drives RunSingleTest against tiny sh scripts and
  pins the documented exit-code mapping (0/77/non-zero) and the
  Cmd-prefix runner path.

Closes #12.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-27 19:38:18 +00:00

53 lines
1.8 KiB
C++

import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
// Compile a tiny GLSL fragment shader directly via the Shader API. Verifies
// the SPIR-V output lands in the target dir and starts with the SPIR-V magic
// number, without dragging in the full Build() pipeline.
int main() {
fs::path src = fs::current_path() / "tests" / "ShaderCompile" / "fixture" / "triangle.frag";
if (!fs::exists(src)) {
std::println(std::cerr, "fixture missing: {}", src.string());
return 1;
}
fs::path outDir = fs::temp_directory_path() / "crafter-build-shader-test";
std::error_code ec;
fs::remove_all(outDir, ec);
fs::create_directories(outDir);
Shader shader(fs::path(src), "main", ShaderType::Fragment);
std::array<fs::path, 0> includeDirs = {};
std::string err = shader.Compile(outDir, includeDirs);
if (!err.empty()) {
std::println(std::cerr, "shader compile failed: {}", err);
return 1;
}
fs::path spv = outDir / "triangle.spv";
if (!fs::exists(spv)) {
std::println(std::cerr, "spv not produced at {}", spv.string());
return 1;
}
// SPIR-V binaries start with the magic number 0x07230203 (little-endian
// when written by glslang on x86). Read the first four bytes to confirm
// the produced file is real SPIR-V, not a fluke empty file.
std::ifstream in(spv, std::ios::binary);
std::uint32_t magic = 0;
in.read(reinterpret_cast<char*>(&magic), sizeof(magic));
if (!in || magic != 0x07230203u) {
std::println(std::cerr, "spv has wrong magic: 0x{:08x}", magic);
return 1;
}
// Compiling again should be a no-op since the source isn't newer.
if (!shader.Check(outDir)) {
std::println(std::cerr, "Shader::Check returned false on an up-to-date spv");
return 1;
}
return 0;
}