|
| 1 | +defmodule XlaTargetValidatorTest do |
| 2 | + @moduledoc """ |
| 3 | + Tests for the XLA_TARGET preflight validator. |
| 4 | +
|
| 5 | + These tests manipulate `XLA_TARGET` via `System.put_env/2` and |
| 6 | + `System.delete_env/1`. Per AGENTS.md, tests may manipulate |
| 7 | + environment variables for config-boundary checks, which is exactly |
| 8 | + what this is. The suite is `async: false` because `System.put_env/2` |
| 9 | + is process-global and would otherwise race with other tests that |
| 10 | + read XLA_TARGET. |
| 11 | +
|
| 12 | + The validator module itself lives at |
| 13 | + `build_support/xla_target_validator.exs` and is loaded eagerly by |
| 14 | + `mix.exs`; by the time the test suite runs, it is already loaded |
| 15 | + into the Code server. |
| 16 | + """ |
| 17 | + |
| 18 | + use ExUnit.Case, async: false |
| 19 | + |
| 20 | + setup do |
| 21 | + original = System.get_env("XLA_TARGET") |
| 22 | + |
| 23 | + on_exit(fn -> |
| 24 | + case original do |
| 25 | + nil -> System.delete_env("XLA_TARGET") |
| 26 | + value -> System.put_env("XLA_TARGET", value) |
| 27 | + end |
| 28 | + end) |
| 29 | + |
| 30 | + :ok |
| 31 | + end |
| 32 | + |
| 33 | + describe "supported_xla_targets/0" do |
| 34 | + test "matches the bundled xla 0.9.x acceptance list exactly" do |
| 35 | + assert XlaTargetValidator.supported_xla_targets() == |
| 36 | + ["cpu", "cuda", "cuda12", "rocm", "tpu"] |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + describe "recommended_xla_target/0" do |
| 41 | + test "is cuda12 (the canonical CUDA lane for the current dep stack)" do |
| 42 | + assert XlaTargetValidator.recommended_xla_target() == "cuda12" |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + describe "validate!/0 — accepting cases" do |
| 47 | + test "accepts unset XLA_TARGET" do |
| 48 | + System.delete_env("XLA_TARGET") |
| 49 | + assert :ok = XlaTargetValidator.validate!() |
| 50 | + end |
| 51 | + |
| 52 | + test "accepts empty XLA_TARGET (treated as unset)" do |
| 53 | + System.put_env("XLA_TARGET", "") |
| 54 | + assert :ok = XlaTargetValidator.validate!() |
| 55 | + end |
| 56 | + |
| 57 | + test "accepts every value in supported_xla_targets/0" do |
| 58 | + for target <- XlaTargetValidator.supported_xla_targets() do |
| 59 | + System.put_env("XLA_TARGET", target) |
| 60 | + assert :ok = XlaTargetValidator.validate!(), "expected #{inspect(target)} to be accepted" |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + describe "validate!/0 — rejecting cases" do |
| 66 | + test "rejects cuda13 (the canonical reported failure) and names cuda12 as remediation" do |
| 67 | + System.put_env("XLA_TARGET", "cuda13") |
| 68 | + |
| 69 | + try do |
| 70 | + XlaTargetValidator.validate!() |
| 71 | + flunk("expected validate!/0 to raise on XLA_TARGET=cuda13") |
| 72 | + rescue |
| 73 | + e in Mix.Error -> |
| 74 | + msg = Exception.message(e) |
| 75 | + assert String.contains?(msg, "cuda13") |
| 76 | + assert String.contains?(msg, "cuda12") |
| 77 | + assert String.contains?(msg, "not accepted by the bundled xla 0.9.x") |
| 78 | + assert String.contains?(msg, "guides/troubleshooting.md") |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + test "rejects a garbage value and surfaces the actual value in the message" do |
| 83 | + System.put_env("XLA_TARGET", "garbage_target_xyz") |
| 84 | + |
| 85 | + try do |
| 86 | + XlaTargetValidator.validate!() |
| 87 | + flunk("expected validate!/0 to raise on a garbage target") |
| 88 | + rescue |
| 89 | + e in Mix.Error -> |
| 90 | + assert String.contains?(Exception.message(e), "garbage_target_xyz") |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + test "rejects case mismatch (xla is case-sensitive)" do |
| 95 | + System.put_env("XLA_TARGET", "CUDA12") |
| 96 | + |
| 97 | + try do |
| 98 | + XlaTargetValidator.validate!() |
| 99 | + flunk("expected validate!/0 to raise on CUDA12 (wrong case)") |
| 100 | + rescue |
| 101 | + e in Mix.Error -> |
| 102 | + assert String.contains?(Exception.message(e), "CUDA12") |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + test "rejects whitespace-padded value (xla does not trim)" do |
| 107 | + System.put_env("XLA_TARGET", "cuda12 ") |
| 108 | + |
| 109 | + try do |
| 110 | + XlaTargetValidator.validate!() |
| 111 | + flunk("expected validate!/0 to raise on a padded target") |
| 112 | + rescue |
| 113 | + e in Mix.Error -> |
| 114 | + assert String.contains?(Exception.message(e), "cuda12 ") |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + describe "raw_xla_target/0" do |
| 120 | + test "returns the literal env value (no trimming, no normalisation)" do |
| 121 | + System.put_env("XLA_TARGET", "cuda12 ") |
| 122 | + assert XlaTargetValidator.raw_xla_target() == "cuda12 " |
| 123 | + end |
| 124 | + |
| 125 | + test "returns nil when XLA_TARGET is unset" do |
| 126 | + System.delete_env("XLA_TARGET") |
| 127 | + assert XlaTargetValidator.raw_xla_target() == nil |
| 128 | + end |
| 129 | + end |
| 130 | +end |
0 commit comments