lib/os: getenv rows skip loudly when the arranged env is absent

This commit is contained in:
2026-08-08 14:35:34 +09:00
parent 682c16cad0
commit 09ddce324b

View File

@@ -1,21 +1,19 @@
// ostest — exercises lib/os surface that doesn't have a dedicated
// test elsewhere. Covers [[os.getenv]] (against env state pre-
// arranged by test/wcc/974_getenv_run.c) and a direct
// test elsewhere. Covers [[os.getenv]] and a direct
// [[os.alloc]] / [[os.free]] roundtrip. memio's tests indirectly
// cover alloc/free; the direct row here pins the FFI shape under
// lib/os itself so future bindings refactors can't quietly drift.
//
// Hand-rolled @test fns dispatched from `main()` in source order; a
// failing case aborts via the assert/abort builtin (task #5 @test
// conversion).
//
// Pre-arranged env state (set by 974_getenv_run.c via setenv/unsetenv
// before exec'ing `ww run lib/os/ostest.ww`):
// getenv env contract (ww ships no setenv primitive, deliberately):
//
// WW_TEST_GETENV = "hello-world"
// WW_TEST_EMPTY = "" (set, but value is empty)
// WW_TEST_NOT_SET unset
//
// The rows that need a var PRESENT skip loudly when it is absent, so
// the suite passes bare; the arranged leg (env constructed per row,
// rows asserted `ok`, not SKIP) is test/libenv/libenv_test.ww.
//
// Don't `use io;` here — lib/os and lib/io both export read/write/
// close as C symbols that collide under the driver's flat-scope
// concat (task #17). os alone is sufficient: getenv + alloc/free are
@@ -25,6 +23,7 @@ package os_test;
import os;
import rt;
import test;
@@ -51,7 +50,7 @@ fn ostestwait(pid: i32, status: *i32) i32 = {
@test fn test_getenv_set() void = {
let r = os.getenv("WW_TEST_GETENV");
match (r) {
case void => abort();
case void => test.skip("WW_TEST_GETENV unset (arranged leg: test/libenv)");
case let s: str => {
assert(!(!streq(s, "hello-world")));
};
@@ -67,7 +66,7 @@ fn ostestwait(pid: i32, status: *i32) i32 = {
@test fn test_getenv_empty() void = {
let r = os.getenv("WW_TEST_EMPTY");
match (r) {
case void => abort();
case void => test.skip("WW_TEST_EMPTY unset (arranged leg: test/libenv)");
case let s: str => {
assert(!(s.len != 0));
};
@@ -89,9 +88,14 @@ fn ostestwait(pid: i32, status: *i32) i32 = {
// Probes that "WW_TEST_GETEN" (a prefix of WW_TEST_GETENV) doesn't
// match. Without the explicit `entry[name.len] == '='` check in
// getenv, a naive prefix matcher would return the value of any
// longer-named var that starts with the queried name.
// longer-named var that starts with the queried name. The row's
// premise is that the longer var exists, so it skips with it.
@test fn test_getenv_prefix_no_match() void = {
match (os.getenv("WW_TEST_GETENV")) {
case void => test.skip("WW_TEST_GETENV unset (arranged leg: test/libenv)");
case let s: str => {};
};
let r = os.getenv("WW_TEST_GETEN");
match (r) {
case void => {};
@@ -113,6 +117,14 @@ fn ostestwait(pid: i32, status: *i32) i32 = {
@test fn test_getenvs_entries() void = {
let env: []str = os.getenvs();
assert(!(env.len == 0));
match (os.getenv("WW_TEST_GETENV")) {
case void => test.skip("WW_TEST_GETENV unset (arranged leg: test/libenv)");
case let s: str => {};
};
match (os.getenv("WW_TEST_EMPTY")) {
case void => test.skip("WW_TEST_EMPTY unset (arranged leg: test/libenv)");
case let s: str => {};
};
let want: [2]str = ["WW_TEST_GETENV=hello-world", "WW_TEST_EMPTY="];
let w: i32 = 0;
for (w < 2) {