package localbuild_test; // End-to-end local package builds through both production drivers. Fixtures // are created at runtime so there is no manifest or hidden graph input: every // edge below comes from an ordinary WW import declaration. import os; import os.exec; import strings; import testenv; import time; fn fail(label: str, why: str) void = { let m: str = strings.concat("localbuild FAIL: ", label, " -- ", why, "\n"); os.write(2, m.ptr, m.len: u64); assert(false); }; fn tmo() time.duration = { return (240i64 * (time.second: i64)): time.duration; }; fn mkdir(path: str) void = { assert(os.mkdir(path, 493) == 0); }; fn command(dir: str, name: str, argv: []str, out: *testenv.commandout) void = { testenv.runcommand(dir, dir, name, argv, tmo(), out); }; fn code(dir: str, name: str, argv: []str) i32 = { let co: testenv.commandout; command(dir, name, argv, &co); if (co.termination != exec.termination.EXIT) { return -1; }; return co.code; }; fn build(dir: str, driver: str, tag: str, target: str, out: str) i32 = { let av: []str = [testenv.driver(driver), "build", "-I", dir, "-o", out, target]; return code(dir, strings.concat("build_", tag), av); }; fn roottraceenv(source: str, runtime: str, compiler: str, assembler: str, linker: str, compilertrace: str, assemblertrace: str, linkertrace: str, realcompiler: str, realassembler: str, reallinker: str) []str = { let base: []str = os.getenvs(); let env: []str = alloc([], (base.len + 11): u64)!; let i: i32 = 0; for (i < base.len) { if (!strings.hasprefix(base[i], "WW_SRCLIB=") && !strings.hasprefix(base[i], "WW_LIB=") && !strings.hasprefix(base[i], "WW_W6C=") && !strings.hasprefix(base[i], "WW_W6A=") && !strings.hasprefix(base[i], "WW_W6L=") && !strings.hasprefix(base[i], "WW_ROOT_COMPILER_TRACE=") && !strings.hasprefix(base[i], "WW_ROOT_ASSEMBLER_TRACE=") && !strings.hasprefix(base[i], "WW_ROOT_LINKER_TRACE=") && !strings.hasprefix(base[i], "WW_ROOT_REAL_COMPILER=") && !strings.hasprefix(base[i], "WW_ROOT_REAL_ASSEMBLER=") && !strings.hasprefix(base[i], "WW_ROOT_REAL_LINKER=")) { append(env, base[i]); }; i += 1; }; append(env, strings.concat("WW_SRCLIB=", source)); append(env, strings.concat("WW_LIB=", runtime)); append(env, strings.concat("WW_W6C=", compiler)); append(env, strings.concat("WW_W6A=", assembler)); append(env, strings.concat("WW_W6L=", linker)); append(env, strings.concat("WW_ROOT_COMPILER_TRACE=", compilertrace)); append(env, strings.concat("WW_ROOT_ASSEMBLER_TRACE=", assemblertrace)); append(env, strings.concat("WW_ROOT_LINKER_TRACE=", linkertrace)); append(env, strings.concat("WW_ROOT_REAL_COMPILER=", realcompiler)); append(env, strings.concat("WW_ROOT_REAL_ASSEMBLER=", realassembler)); append(env, strings.concat("WW_ROOT_REAL_LINKER=", reallinker)); return env; }; fn diagnosticbody(s: str) str = { let p: i32 = testenv.pos(s, ": error: "); // Driver-owned graph diagnostics have no source-location prefix. Compare // those in full; strip only compiler paths/locations from source errors. if (p < 0) { return s; }; let begin: i32 = p + 9; let end: i32 = begin; for (end < s.len && s[end] != '\n') { end += 1; }; return strings.sub(s, begin, end); }; fn rejectstable(dir: str, label: str, target: str, needle: str) void = { let drivers: []str = ["ww", "ww_ww", "ww", "ww_ww"]; let tags: []str = ["c1", "w1", "c2", "w2"]; let errors: []str = ["", "", "", ""]; let cwork: str = strings.concat(dir, "/work-", label, "-c"); let wwork: str = strings.concat(dir, "/work-", label, "-w"); mkdir(cwork); mkdir(wwork); let i: i32 = 0; for (i < drivers.len) { let out: str = strings.concat(dir, "/reject-", label, "-", tags[i]); let work: str = cwork; if (i == 1 || i == 3) { work = wwork; }; let av: []str = [testenv.driver(drivers[i]), "build", "-I", dir, "-w", work, "-o", out, target]; let co: testenv.commandout; command(dir, strings.concat("reject_", label, "_", tags[i]), av, &co); if (co.termination != exec.termination.EXIT || co.code == 0) { fail(label, strings.concat(drivers[i], " accepted invalid graph")); }; if (!testenv.has(co.stderr, needle)) { fail(label, strings.concat(drivers[i], " diagnostic missing '", needle, "': ", co.stderr)); }; errors[i] = co.stderr; i += 1; }; if (!testenv.same(errors[0], errors[2])) { fail(label, "C diagnostic changed across repeated builds"); }; if (!testenv.same(errors[1], errors[3])) { fail(label, "WW diagnostic changed across repeated builds"); }; let cb: str = diagnosticbody(errors[0]); let wb: str = diagnosticbody(errors[1]); if (!testenv.same(cb, wb)) { fail(label, strings.concat("C/WW diagnostic bodies differ: '", cb, "' vs '", wb, "'")); }; }; @test fn standalone_and_package_artifact() void = { let td: str = testenv.fresh(); let app: str = strings.concat(td, "/app"); mkdir(app); testenv.writefile(strings.concat(app, "/main.ww"), strings.concat( "package main;\n", "fn main() i32 = { return 23; };\n")); // Ordinary builds must not accidentally select test-only sources. testenv.writefile(strings.concat(app, "/broken_test.ww"), "package other;\nthis is deliberately invalid\n"); let c1: str = strings.concat(td, "/standalone-c1"); let c2: str = strings.concat(td, "/standalone-c2"); let ww: str = strings.concat(td, "/standalone-ww"); if (build(td, "ww", "standalone_c1", app, c1) != 0 || build(td, "ww", "standalone_c2", app, c2) != 0 || build(td, "ww_ww", "standalone_ww", app, ww) != 0) { fail("standalone", "manifest-free directory build failed"); }; let av1: []str = [c1]; let av2: []str = [ww]; if (code(td, "run_standalone_c", av1) != 23 || code(td, "run_standalone_ww", av2) != 23) { fail("standalone", "built executable returned the wrong value"); }; if (!testenv.same(testenv.readfile(c1), testenv.readfile(c2))) { fail("standalone", "two clean builds were not byte-identical"); }; let lib: str = strings.concat(td, "/libonly"); mkdir(lib); testenv.writefile(strings.concat(lib, "/lib.ww"), strings.concat( "package libonly;\n", "export fn shown() i32 = { return 7; };\n", "fn hidden() i32 = { return 99; };\n")); let ca: str = strings.concat(td, "/libonly-c.a"); let wa: str = strings.concat(td, "/libonly-w.a"); let cav: []str = [testenv.driver("ww"), "build", "-p", "-I", td, "-o", ca, lib]; let wav: []str = [testenv.driver("ww_ww"), "build", "-p", "-I", td, "-o", wa, lib]; if (code(td, "build_package_c", cav) != 0 || code(td, "build_package_w", wav) != 0) { fail("package-artifact", "non-main package build failed"); }; if (!testenv.exists(strings.concat(ca, ".wwi")) || !testenv.exists(strings.concat(wa, ".wwi"))) { fail("package-artifact", "missing compiler export sidecar"); }; if (!testenv.same(testenv.readfile(ca), testenv.readfile(wa)) || !testenv.same(testenv.readfile(strings.concat(ca, ".wwi")), testenv.readfile(strings.concat(wa, ".wwi")))) { fail("package-artifact", "C/WW package products differ"); }; let iface: str = testenv.readfile(strings.concat(ca, ".wwi")); if (!testenv.has(iface, "shown") || testenv.has(iface, "hidden")) { fail("package-artifact", "export interface crossed the private boundary"); }; testenv.clean(td); }; @test fn self_contained_direct_exports() void = { let td: str = testenv.fresh(); let dimensions: str = strings.concat(td, "/dimensions"); let implementation: str = strings.concat(td, "/implementation"); let api: str = strings.concat(td, "/api"); let main: str = strings.concat(td, "/main"); mkdir(dimensions); mkdir(implementation); mkdir(api); mkdir(main); let dimensionssrc: str = strings.concat( "package dimensions;\n", "export def WIDTH: i32 = 4;\n", "export def UNUSED_WIDTH: i32 = 8;\n"); let implsrc: str = strings.concat( "package implementation;\n", "import dimensions;\n", "def PRIVATE_WIDTH: i32 = 3;\n", "export type Buffer = [PRIVATE_WIDTH]u8;\n", "export type PublicBuffer = [WIDTH]u8;\n", "export type CastBuffer = [(WIDTH: u64)]u8;\n", "export type Detail = struct { code: i64, };\n", "export type Member = struct { detail: Detail, payload: i64, };\n", "export type Unused = struct { noise: i32, };\n", "export def UNUSED: i32 = 99;\n", "type private_type = struct { secret: i32, };\n", "def private_value: i32 = 77;\n", "export fn construct(v: i32) Member = { return Member{detail=Detail{code=(v: i64) + 1i64}, payload=(v: i64) + 2i64}; };\n", "export fn unrelated() i32 = { return UNUSED; };\n", "fn hidden() i32 = { return private_value; };\n"); let apisrc: str = strings.concat( "package api;\n", "import implementation;\n", "export type Member = struct { local: i32, };\n", "export fn produce(v: i32) implementation.Member = { return implementation.construct(v); };\n", "export fn score(m: implementation.Member) i32 = { return (m.detail.code + m.payload): i32; };\n", "export fn width_of(b: Buffer) i32 = { return len(b): i32; };\n", "export fn public_width_of(b: implementation.PublicBuffer) i32 = { return len(b): i32; };\n", "export fn cast_width_of(b: implementation.CastBuffer) i32 = { return len(b): i32; };\n"); let mainsrc: str = strings.concat( "package main;\n", "import api;\n", "fn main() i32 = { let m = api.produce(19); let p: *i64 = alloc(api.score(m): i64)!; return (*p): i32; };\n"); testenv.writefile(strings.concat(dimensions, "/dimensions.ww"), dimensionssrc); testenv.writefile(strings.concat(implementation, "/implementation.ww"), implsrc); testenv.writefile(strings.concat(api, "/api.ww"), apisrc); testenv.writefile(strings.concat(main, "/main.ww"), mainsrc); let outputs: []str = [strings.concat(td, "/direct-c1"), strings.concat(td, "/direct-c2"), strings.concat(td, "/direct-w")]; let drivers: []str = ["ww", "ww", "ww_ww"]; let tags: []str = ["direct_c1", "direct_c2", "direct_w"]; let i: i32 = 0; for (i < outputs.len) { if (build(td, drivers[i], tags[i], main, outputs[i]) != 0) { fail("self-contained", strings.concat(drivers[i], " build failed")); }; let runav: []str = [outputs[i]]; if (code(td, strings.concat("run_", tags[i]), runav) != 41) { fail("self-contained", "implementation archive was not linked"); }; i += 1; }; if (!testenv.same(testenv.readfile(outputs[0]), testenv.readfile(outputs[1])) || !testenv.same(testenv.readfile(outputs[0]), testenv.readfile(outputs[2]))) { fail("self-contained", "clean C/C/WW executable outputs differ"); }; let cwork: str = strings.concat(outputs[0], ".sepwork/"); let cwork2: str = strings.concat(outputs[1], ".sepwork/"); let wwork: str = strings.concat(outputs[2], ".sepwork/"); let apiiface: str = testenv.readfile(strings.concat(cwork, "api.wwi")); if (!testenv.same(apiiface, testenv.readfile(strings.concat(cwork2, "api.wwi"))) || !testenv.same(apiiface, testenv.readfile(strings.concat( wwork, "api.wwi")))) { fail("self-contained", "C/C/WW api exports differ"); }; if (!testenv.has(apiiface, "//ww:module implementation\n") || !testenv.has(apiiface, "export type Buffer = [3]u8;") || !testenv.has(apiiface, "export type PublicBuffer = [4]u8;") || !testenv.has(apiiface, "export type CastBuffer = [4]u8;") || !testenv.has(apiiface, "export type Detail = struct") || testenv.occurrences(apiiface, "export type Member = struct") != 2) { fail("self-contained", "api export lacks the recursive public type facts"); }; if (testenv.has(apiiface, "//ww:module dimensions\n") || testenv.has(apiiface, "WIDTH") || testenv.has(apiiface, "export def UNUSED_WIDTH") || testenv.has(apiiface, "PRIVATE_WIDTH") || testenv.has(apiiface, "export type Unused") || testenv.has(apiiface, "export def UNUSED") || testenv.has(apiiface, "export fn construct") || testenv.has(apiiface, "unrelated") || testenv.has(apiiface, "private_type") || testenv.has(apiiface, "private_value") || testenv.has(apiiface, "hidden")) { fail("self-contained", "api export leaked unrelated or private declarations"); }; let expectedunit: str = strings.concat("//ww:module-reset main\n", mainsrc, "\n"); if (!testenv.same(expectedunit, testenv.readfile(strings.concat(cwork, "__root.unit.ww"))) || !testenv.same(expectedunit, testenv.readfile(strings.concat(wwork, "__root.unit.ww")))) { fail("self-contained", "consumer unit was not exactly its owned source"); }; if (!testenv.exists(strings.concat(cwork, "implementation.a")) || !testenv.exists(strings.concat(cwork, "dimensions.a"))) { fail("self-contained", "reachable archives missing from link closure"); }; let packages: []str = [strings.concat(td, "/api-c1.a"), strings.concat(td, "/api-c2.a"), strings.concat(td, "/api-w.a")]; i = 0; for (i < packages.len) { let pav: []str = [testenv.driver(drivers[i]), "build", "-p", "-I", td, "-o", packages[i], api]; if (code(td, strings.concat("package_", tags[i]), pav) != 0) { fail("self-contained", "ww build -p api failed"); }; i += 1; }; if (!testenv.same(testenv.readfile(packages[0]), testenv.readfile(packages[1])) || !testenv.same(testenv.readfile(packages[0]), testenv.readfile(packages[2])) || !testenv.same(apiiface, testenv.readfile(strings.concat(packages[0], ".wwi"))) || !testenv.same(apiiface, testenv.readfile(strings.concat( packages[1], ".wwi"))) || !testenv.same(apiiface, testenv.readfile(strings.concat(packages[2], ".wwi")))) { fail("self-contained", "deterministic package artifact/export mismatch"); }; testenv.writefile(strings.concat(td, "/deep-qualifier.ww"), strings.concat( "package main;\nimport api;\n", "fn main() i32 = { let m: implementation.Member; return 0; };\n")); rejectstable(td, "deep-qualifier", strings.concat(td, "/deep-qualifier.ww"), "unknown type 'implementation.Member'"); testenv.writefile(strings.concat(td, "/deep-value.ww"), strings.concat( "package main;\nimport api;\n", "fn main() i32 = { return construct(1); };\n")); rejectstable(td, "deep-value", strings.concat(td, "/deep-value.ww"), "undefined: construct"); testenv.writefile(strings.concat(td, "/deep-const.ww"), strings.concat( "package main;\nimport api;\n", "fn main() i32 = { return WIDTH; };\n")); rejectstable(td, "deep-const", strings.concat(td, "/deep-const.ww"), "undefined: WIDTH"); testenv.writefile(strings.concat(td, "/deep-type.ww"), strings.concat( "package main;\nimport api;\n", "fn main() i32 = { let d: Detail; return 0; };\n")); rejectstable(td, "deep-type", strings.concat(td, "/deep-type.ww"), "unknown type 'Detail'"); testenv.writefile(strings.concat(td, "/private-direct.ww"), strings.concat( "package main;\nimport implementation;\n", "fn main() i32 = { return implementation.hidden(); };\n")); rejectstable(td, "private-direct", strings.concat(td, "/private-direct.ww"), "has no exported declaration 'hidden'"); testenv.clean(td); }; fn writediamond(td: str, reverse: bool) str = { let shared: str = strings.concat(td, "/shared"); let left: str = strings.concat(td, "/left"); let right: str = strings.concat(td, "/right"); let main: str = strings.concat(td, "/main"); if (reverse) { mkdir(left); mkdir(main); mkdir(shared); mkdir(right); } else { mkdir(right); mkdir(shared); mkdir(main); mkdir(left); }; let zsrc: str = strings.concat( "package shared;\n", "fn hidden() i32 = { return 91; };\n"); let asrc: str = strings.concat( "package shared;\n", "export type token = struct { value: i32, };\n", "export fn base() i32 = { return 3; };\n"); if (reverse) { testenv.writefile(strings.concat(shared, "/a.ww"), asrc); testenv.writefile(strings.concat(shared, "/z.ww"), zsrc); } else { testenv.writefile(strings.concat(shared, "/z.ww"), zsrc); testenv.writefile(strings.concat(shared, "/a.ww"), asrc); }; testenv.writefile(strings.concat(left, "/left.ww"), strings.concat( "package left;\nimport shared;\nimport shared;\n", "export fn make() shared.token = { return shared.token{value=shared.base() + 1}; };\n")); testenv.writefile(strings.concat(right, "/right.ww"), strings.concat( "package right;\nimport shared;\n", "export fn value(t: shared.token) i32 = { return t.value + shared.base() + 2; };\n")); testenv.writefile(strings.concat(main, "/main.ww"), strings.concat( "package main;\nimport right;\nimport left;\n", "fn main() i32 = { return right.value(left.make()); };\n")); return main; }; @test fn direct_and_diamond() void = { let td: str = testenv.fresh(); let treea: str = strings.concat(td, "/tree-a"); mkdir(treea); let treeb: str = strings.concat(td, "/tree-b"); mkdir(treeb); let main: str = writediamond(treea, false); let shuffled: str = writediamond(treeb, true); let drivers: []str = ["ww", "ww_ww"]; let tags: []str = ["c", "w"]; let i: i32 = 0; for (i < drivers.len) { let out: str = strings.concat(td, "/diamond-", tags[i]); if (build(treea, drivers[i], strings.concat("diamond_", tags[i]), main, out) != 0) { fail("diamond", strings.concat(drivers[i], " build failed")); }; let av: []str = [out]; if (code(td, strings.concat("run_diamond_", tags[i]), av) != 9) { fail("diamond", "linked program returned the wrong value"); }; let scratch: str = strings.concat(out, ".sepwork/"); if (!testenv.exists(strings.concat(scratch, "shared.a")) || testenv.exists(strings.concat(scratch, "shared.shared.a"))) { fail("diamond", "shared package was not interned exactly once"); }; let unit: str = testenv.readfile(strings.concat(scratch, "__root.unit.ww")); let rootbody: str = testenv.readfile(strings.concat(main, "/main.ww")); let wantroot: str = strings.concat("//ww:module-reset main\n", rootbody, "\n"); if (!testenv.same(unit, wantroot) || testenv.has(unit, "//ww:module ")) { fail("diamond", "root unit contains non-owned export text"); }; let sharedunit: str = testenv.readfile(strings.concat(scratch, "shared.unit.ww")); let ap: i32 = testenv.pos(sharedunit, "export type token"); let zp: i32 = testenv.pos(sharedunit, "fn hidden"); if (ap < 0 || zp < 0 || ap >= zp) { fail("diamond", "package sources were not byte-sorted"); }; let leftiface: str = testenv.readfile(strings.concat(scratch, "left.wwi")); let rightiface: str = testenv.readfile(strings.concat(scratch, "right.wwi")); if (testenv.occurrences(leftiface, "import shared;") != 1) { fail("diamond", "duplicate import escaped into export data"); }; if (testenv.occurrences(leftiface, "export type token") != 1 || testenv.occurrences(rightiface, "export type token") != 1 || testenv.has(unit, "export type token")) { fail("diamond", "origin fact closure did not merge deterministically"); }; i += 1; }; let shuffledout: str = strings.concat(td, "/diamond-c-shuffled"); if (build(treeb, "ww", "diamond_c_shuffled", shuffled, shuffledout) != 0) { fail("diamond", "shuffled directory build failed"); }; let firstout: str = strings.concat(td, "/diamond-c"); if (!testenv.same(testenv.readfile(firstout), testenv.readfile(shuffledout))) { fail("diamond", "shuffled directory enumeration changed output"); }; let firstscratch: str = strings.concat(firstout, ".sepwork/"); let shuffledscratch: str = strings.concat(shuffledout, ".sepwork/"); if (!testenv.same(testenv.readfile(strings.concat(firstscratch, "__root.unit.ww")), testenv.readfile(strings.concat(shuffledscratch, "__root.unit.ww"))) || !testenv.same(testenv.readfile(strings.concat( firstscratch, "shared.unit.ww")), testenv.readfile(strings.concat( shuffledscratch, "shared.unit.ww")))) { fail("diamond", "shuffled enumeration changed package units"); }; testenv.clean(td); }; @test fn toolchain_library_root_stage_parity() void = { let td: str = testenv.fresh(); let source: str = strings.concat(td, "/source library"); let app: str = strings.concat(source, "/app"); let dep: str = strings.concat(source, "/dep"); let leaf: str = strings.concat(source, "/leaf"); let runtime: str = strings.concat(td, "/runtime library"); let decoy: str = strings.concat(runtime, "/app"); let tools: str = strings.concat(td, "/tool wrappers"); let cwd: str = strings.concat(td, "/isolated cwd"); let defaultapp: str = strings.concat(td, "/default app"); let fallbackbuf: []u8 = alloc([], os.PATH_MAX: u64)!; fallbackbuf.len = os.PATH_MAX; let fi: i32 = 0; for (fi < fallbackbuf.len) { fallbackbuf[fi] = 120u8; fi += 1; }; let fallback: str = strings.frombytes(fallbackbuf); mkdir(source); mkdir(app); mkdir(dep); mkdir(leaf); mkdir(runtime); mkdir(decoy); mkdir(tools); mkdir(cwd); mkdir(defaultapp); testenv.writefile(strings.concat(leaf, "/leaf.ww"), strings.concat( "package leaf;\n", "export fn value() i32 = { return 40; };\n")); testenv.writefile(strings.concat(dep, "/dep.ww"), strings.concat( "package dep;\nimport leaf;\n", "export fn value() i32 = { return leaf.value() + 2; };\n")); testenv.writefile(strings.concat(app, "/app.ww"), strings.concat( "package main;\nimport dep;\n", "fn main() i32 = { return dep.value(); };\n")); testenv.writefile(strings.concat(decoy, "/app.ww"), "package main;\nfn main() i32 = { return 7; };\n"); testenv.writefile(strings.concat(defaultapp, "/main.ww"), strings.concat( "package main;\nimport types;\n", "fn main() i32 = { return (types.I8_MAX: i32) - 85; };\n")); testenv.writefile(strings.concat(runtime, "/libwwrt.a"), testenv.readfile(strings.concat(testenv.repo(), "/out/lib/libwwrt.a"))); let compiler: str = strings.concat(tools, "/w6c wrapper.sh"); let assembler: str = strings.concat(tools, "/w6a wrapper.sh"); let linker: str = strings.concat(tools, "/w6l wrapper.sh"); testenv.writeexecutable(compiler, strings.concat( "#!/bin/sh\n", "printf 'BEGIN' >> \"$WW_ROOT_COMPILER_TRACE\"\n", "for arg in \"$@\"; do printf '<%s>' \"$arg\" >> ", "\"$WW_ROOT_COMPILER_TRACE\"; done\n", "printf '\\n' >> \"$WW_ROOT_COMPILER_TRACE\"\n", "exec \"$WW_ROOT_REAL_COMPILER\" \"$@\"\n")); testenv.writeexecutable(assembler, strings.concat( "#!/bin/sh\n", "printf 'BEGIN' >> \"$WW_ROOT_ASSEMBLER_TRACE\"\n", "for arg in \"$@\"; do printf '<%s>' \"$arg\" >> ", "\"$WW_ROOT_ASSEMBLER_TRACE\"; done\n", "printf '\\n' >> \"$WW_ROOT_ASSEMBLER_TRACE\"\n", "exec \"$WW_ROOT_REAL_ASSEMBLER\" \"$@\"\n")); testenv.writeexecutable(linker, strings.concat( "#!/bin/sh\n", "printf 'BEGIN' >> \"$WW_ROOT_LINKER_TRACE\"\n", "for arg in \"$@\"; do printf '<%s>' \"$arg\" >> ", "\"$WW_ROOT_LINKER_TRACE\"; done\n", "printf '\\n' >> \"$WW_ROOT_LINKER_TRACE\"\n", "exec \"$WW_ROOT_REAL_LINKER\" \"$@\"\n")); let stages: []str = ["ww", "ww_ww"]; let compilers: []str = ["w6c", "w6c_ww"]; let assemblers: []str = ["w6a", "w6a_ww"]; let linkers: []str = ["w6l", "w6l_ww"]; let tags: []str = ["c", "ww"]; let modes: []str = ["archive", "overlong-fallback"]; let runtimes: []str = [runtime, fallback]; let bin: str = strings.concat(td, "/published binary"); let workroot: str = strings.concat(bin, ".sepwork"); let work: str = strings.concat(workroot, "/"); let mi: i32 = 0; for (mi < modes.len) { let referenceunit: str = ""; let referencewwi: str = ""; let referencearchive: str = ""; let referencerootwwi: str = ""; let referencerootarchive: str = ""; let referencebin: str = ""; let referencecompiler: str = ""; let referenceassembler: str = ""; let referencelinker: str = ""; let si: i32 = 0; for (si < stages.len) { let prefix: str = strings.concat(modes[mi], "-", tags[si]); let compilertrace: str = strings.concat(td, "/", prefix, " compiler trace"); let assemblertrace: str = strings.concat(td, "/", prefix, " assembler trace"); let linkertrace: str = strings.concat(td, "/", prefix, " linker trace"); testenv.writefile(compilertrace, ""); testenv.writefile(assemblertrace, ""); testenv.writefile(linkertrace, ""); let env: []str = roottraceenv(source, runtimes[mi], compiler, assembler, linker, compilertrace, assemblertrace, linkertrace, testenv.driver(compilers[si]), testenv.driver(assemblers[si]), testenv.driver(linkers[si])); let av: []str = [testenv.driver(stages[si]), "build", "-o", bin, "app"]; let out: testenv.commandout; testenv.runcommandenv(cwd, td, strings.concat("library-root-", prefix), av, env, tmo(), &out); if (out.termination != exec.termination.EXIT || out.code != 0 || out.stderr.len != 0) { fail("library-roots", strings.concat(prefix, " build failed: ", out.stderr)); }; let rootunit: str = testenv.readfile(strings.concat(work, "__root.unit.ww")); let depunit: str = testenv.readfile(strings.concat(work, "dep.unit.ww")); if (!testenv.has(rootunit, "//ww:module-reset main\npackage main;") || testenv.has(rootunit, "//ww:module ") || !testenv.has(depunit, "//ww:module-reset dep\npackage dep;") || testenv.has(depunit, "//ww:module ")) { fail("library-roots", "package units contain non-owned export text"); }; if (!testenv.exists(strings.concat(work, "leaf.wwi")) || !testenv.exists(strings.concat(work, "leaf.a")) || !testenv.exists(strings.concat(work, "dep.wwi")) || !testenv.exists(strings.concat(work, "dep.a")) || !testenv.exists(strings.concat(work, "__root.o")) || !testenv.exists(strings.concat(work, "__root.wwi")) || !testenv.exists(strings.concat(work, "__root.a"))) { fail("library-roots", "package artifacts do not match root ownership"); }; let ctrace: str = testenv.readfile(compilertrace); let atrace: str = testenv.readfile(assemblertrace); let ltrace: str = testenv.readfile(linkertrace); if (testenv.occurrences(ctrace, strings.concat("<", work, "leaf.unit.ww>")) != 1 || testenv.occurrences(ctrace, strings.concat("<", work, "dep.unit.ww>")) != 1 || testenv.occurrences(ctrace, strings.concat("<", work, "__root.unit.ww>")) != 1 || testenv.pos(ctrace, strings.concat("<", work, "leaf.unit.ww>")) >= testenv.pos(ctrace, strings.concat("<", work, "dep.unit.ww>")) || testenv.pos(ctrace, strings.concat("<", work, "dep.unit.ww>")) >= testenv.pos(ctrace, strings.concat("<", work, "__root.unit.ww>"))) { fail("library-roots", "compiler actions were not deterministic postorder"); }; if (!testenv.has(ctrace, strings.concat( "BEGIN<-c><--import><", work, "leaf.wwi><-I><", work, "dep.wwi><-o><", work, "dep.s><", work, "dep.unit.ww>")) || !testenv.has(ctrace, strings.concat( "BEGIN<--entry><-c><--import><", work, "dep.wwi><-I><", work, "__root.wwi><-o><", work, "__root.s><", work, "__root.unit.ww>")) || testenv.occurrences(atrace, "\n") != 3 || !testenv.has(atrace, strings.concat("BEGIN<-o><", work, "dep.o><", work, "dep.s>"))) { fail("library-roots", "compiler or assembler argv changed"); }; if (testenv.occurrences(ltrace, "\n") != 1 || !testenv.has(ltrace, strings.concat("BEGIN<-o><", bin, "><", work, "__root.a>")) || testenv.pos(ltrace, strings.concat("<", work, "dep.a>")) < 0 || testenv.pos(ltrace, strings.concat("<", work, "leaf.a>")) < testenv.pos(ltrace, strings.concat("<", work, "dep.a>")) || testenv.has(ltrace, ".wwi>")) { fail("library-roots", "linker did not receive the reachable archive closure"); }; if (mi == 0) { if (!testenv.has(ltrace, strings.concat("<", runtime, "/libwwrt.a>")) || testenv.has(ltrace, strings.concat("<", testenv.repo(), "/out/lib/libwwrt.a>"))) { fail("library-roots", "WW_LIB did not select the runtime archive"); }; } else { if (!testenv.has(ltrace, strings.concat("<", testenv.repo(), "/out/bin/../obj/rt/start.o>")) || !testenv.has(ltrace, strings.concat("<", testenv.repo(), "/out/bin/../obj/rt/syscall.o>"))) { fail("library-roots", "missing runtime archive did not use object fallback"); }; }; let runav: []str = [bin]; if (code(cwd, strings.concat("run-library-root-", prefix), runav) != 42) { fail("library-roots", "WW_SRCLIB did not win over the WW_LIB decoy"); }; if (si == 0) { referenceunit = strings.dup(rootunit); referencewwi = strings.dup(testenv.readfile(strings.concat(work, "dep.wwi"))); referencearchive = strings.dup(testenv.readfile(strings.concat(work, "dep.a"))); referencerootwwi = strings.dup(testenv.readfile(strings.concat(work, "__root.wwi"))); referencerootarchive = strings.dup(testenv.readfile(strings.concat(work, "__root.a"))); referencebin = strings.dup(testenv.readfile(bin)); referencecompiler = strings.dup(ctrace); referenceassembler = strings.dup(atrace); referencelinker = strings.dup(ltrace); } else { if (!testenv.same(referenceunit, rootunit) || !testenv.same(referencewwi, testenv.readfile(strings.concat( work, "dep.wwi"))) || !testenv.same(referencearchive, testenv.readfile(strings.concat( work, "dep.a"))) || !testenv.same(referencerootwwi, testenv.readfile(strings.concat(work, "__root.wwi"))) || !testenv.same(referencerootarchive, testenv.readfile(strings.concat(work, "__root.a"))) || !testenv.same(referencebin, testenv.readfile(bin)) || !testenv.same(referencecompiler, ctrace) || !testenv.same(referenceassembler, atrace) || !testenv.same(referencelinker, ltrace)) { fail("library-roots", "Cstage and WWstage products or argv differ"); }; }; if (mi == 0) { let missing: []str = [testenv.driver(stages[si]), "build", "-o", strings.concat(td, "/missing binary"), "absent"]; testenv.runcommandenv(cwd, td, strings.concat("library-missing-", tags[si]), missing, env, tmo(), &out); if (out.termination != exec.termination.EXIT || out.code != 1 || !testenv.same(out.stderr, "ww build: cannot find module absent\n")) { fail("library-roots", "bare-target diagnostic differs by stage"); }; }; testenv.clean(workroot); assert(os.remove(bin) == 0); si += 1; }; mi += 1; }; let emptyunitreference: str = ""; let emptywwireference: str = ""; let emptyarchivereference: str = ""; let emptybinreference: str = ""; let emptycompilerreference: str = ""; let emptyassemblerreference: str = ""; let emptylinkerreference: str = ""; let emptyout: str = strings.concat(td, "/empty published binary"); let si: i32 = 0; for (si < stages.len) { let outpath: str = emptyout; let emptyworkroot: str = strings.concat(outpath, ".sepwork"); let emptywork: str = strings.concat(emptyworkroot, "/"); let compilertrace: str = strings.concat(td, "/empty-", tags[si], " compiler trace"); let assemblertrace: str = strings.concat(td, "/empty-", tags[si], " assembler trace"); let linkertrace: str = strings.concat(td, "/empty-", tags[si], " linker trace"); testenv.writefile(compilertrace, ""); testenv.writefile(assemblertrace, ""); testenv.writefile(linkertrace, ""); let emptyenv: []str = roottraceenv("", "", compiler, assembler, linker, compilertrace, assemblertrace, linkertrace, testenv.driver(compilers[si]), testenv.driver(assemblers[si]), testenv.driver(linkers[si])); let av: []str = [testenv.driver(stages[si]), "build", "-o", outpath, defaultapp]; let out: testenv.commandout; testenv.runcommandenv(cwd, td, strings.concat("empty-library-", tags[si]), av, emptyenv, tmo(), &out); let rootunit: str = testenv.readfile(strings.concat(emptywork, "__root.unit.ww")); let ctrace: str = testenv.readfile(compilertrace); let atrace: str = testenv.readfile(assemblertrace); let ltrace: str = testenv.readfile(linkertrace); let runav: []str = [outpath]; if (out.termination != exec.termination.EXIT || out.code != 0 || out.stderr.len != 0 || code(cwd, strings.concat("run-empty-library-", tags[si]), runav) != 42) { fail("library-roots", "empty override did not use default roots"); }; if (!testenv.has(rootunit, "//ww:module-reset main\npackage main;") || testenv.has(rootunit, "//ww:module ") || !testenv.exists(strings.concat(emptywork, "types.wwi")) || !testenv.exists(strings.concat(emptywork, "types.a")) || testenv.occurrences(ctrace, strings.concat("<", emptywork, "types.unit.ww>")) != 1 || testenv.occurrences(atrace, "\n") != 2) { fail("library-roots", "empty WW_SRCLIB did not use the default source root"); }; if (testenv.occurrences(ltrace, "\n") != 1 || !testenv.has(ltrace, strings.concat("<", emptywork, "types.a>")) || !testenv.has(ltrace, strings.concat("<", testenv.repo(), "/out/bin/../lib/libwwrt.a>")) || testenv.has(ltrace, "/../obj/rt/start.o>") || testenv.has(ltrace, "/../obj/rt/syscall.o>")) { fail("library-roots", "empty WW_LIB did not use the default runtime archive"); }; if (si == 0) { emptyunitreference = strings.dup(rootunit); emptywwireference = strings.dup(testenv.readfile(strings.concat( emptywork, "types.wwi"))); emptyarchivereference = strings.dup(testenv.readfile(strings.concat( emptywork, "types.a"))); emptybinreference = strings.dup(testenv.readfile(outpath)); emptycompilerreference = strings.dup(ctrace); emptyassemblerreference = strings.dup(atrace); emptylinkerreference = strings.dup(ltrace); } else { if (!testenv.same(emptyunitreference, rootunit) || !testenv.same(emptywwireference, testenv.readfile(strings.concat( emptywork, "types.wwi"))) || !testenv.same(emptyarchivereference, testenv.readfile( strings.concat(emptywork, "types.a"))) || !testenv.same(emptybinreference, testenv.readfile(outpath)) || !testenv.same(emptycompilerreference, ctrace) || !testenv.same(emptyassemblerreference, atrace) || !testenv.same(emptylinkerreference, ltrace)) { fail("library-roots", "empty-root fallback changed stage output or argv"); }; }; testenv.clean(emptyworkroot); assert(os.remove(outpath) == 0); si += 1; }; testenv.clean(td); }; // Unit bytes cannot identify driver-owned graph, archive, and commit semantics. // Keep their executable content beside the compiler/assembler identities so a // warm voucher never crosses a builder change. @test fn persistent_build_reuse_tracks_driver_identity() void = { let td: str = testenv.fresh(); let source: str = strings.concat(td, "/source tree"); let app: str = strings.concat(source, "/app"); let dep: str = strings.concat(source, "/dep"); let leaf: str = strings.concat(source, "/leaf"); let tools: str = strings.concat(td, "/tool wrappers"); let cdrivers: str = strings.concat(td, "/c driver copy"); let wdrivers: str = strings.concat(td, "/ww driver copy"); let cwd: str = strings.concat(td, "/isolated cwd"); let work: str = strings.concat(td, "/persistent work"); let bin: str = strings.concat(td, "/published binary"); mkdir(source); mkdir(app); mkdir(dep); mkdir(leaf); mkdir(tools); mkdir(cdrivers); mkdir(wdrivers); mkdir(cwd); mkdir(work); testenv.writefile(strings.concat(leaf, "/leaf.ww"), strings.concat( "package leaf;\n", "export fn value() i32 = { return 40; };\n")); testenv.writefile(strings.concat(dep, "/dep.ww"), strings.concat( "package dep;\nimport leaf;\n", "export fn value() i32 = { return leaf.value() + 2; };\n")); testenv.writefile(strings.concat(app, "/main.ww"), strings.concat( "package main;\nimport dep;\n", "fn main() i32 = { return dep.value(); };\n")); let compiler: str = strings.concat(tools, "/w6c wrapper.sh"); let assembler: str = strings.concat(tools, "/w6a wrapper.sh"); let linker: str = strings.concat(tools, "/w6l wrapper.sh"); testenv.writeexecutable(compiler, strings.concat( "#!/bin/sh\n", "printf 'BEGIN' >> \"$WW_ROOT_COMPILER_TRACE\"\n", "for arg in \"$@\"; do printf '<%s>' \"$arg\" >> ", "\"$WW_ROOT_COMPILER_TRACE\"; done\n", "printf '\\n' >> \"$WW_ROOT_COMPILER_TRACE\"\n", "exec \"$WW_ROOT_REAL_COMPILER\" \"$@\"\n")); testenv.writeexecutable(assembler, strings.concat( "#!/bin/sh\n", "printf 'BEGIN' >> \"$WW_ROOT_ASSEMBLER_TRACE\"\n", "for arg in \"$@\"; do printf '<%s>' \"$arg\" >> ", "\"$WW_ROOT_ASSEMBLER_TRACE\"; done\n", "printf '\\n' >> \"$WW_ROOT_ASSEMBLER_TRACE\"\n", "exec \"$WW_ROOT_REAL_ASSEMBLER\" \"$@\"\n")); testenv.writeexecutable(linker, strings.concat( "#!/bin/sh\n", "printf 'BEGIN' >> \"$WW_ROOT_LINKER_TRACE\"\n", "for arg in \"$@\"; do printf '<%s>' \"$arg\" >> ", "\"$WW_ROOT_LINKER_TRACE\"; done\n", "printf '\\n' >> \"$WW_ROOT_LINKER_TRACE\"\n", "exec \"$WW_ROOT_REAL_LINKER\" \"$@\"\n")); let stages: []str = ["ww", "ww_ww"]; let compilers: []str = ["w6c", "w6c_ww"]; let assemblers: []str = ["w6a", "w6a_ww"]; let linkers: []str = ["w6l", "w6l_ww"]; let tags: []str = ["c", "ww"]; let copied: []str = [strings.concat(cdrivers, "/ww"), strings.concat(wdrivers, "/ww_ww")]; let runtime: str = strings.concat(testenv.repo(), "/out/lib"); let referenceunit: str = ""; let referencewwi: str = ""; let referencearchive: str = ""; let referencebin: str = ""; let referencecompiler: str = ""; let referenceassembler: str = ""; let referencelinker: str = ""; let referenceerror: str = ""; let si: i32 = 0; for (si < stages.len) { let compilertrace: str = strings.concat(td, "/", tags[si], " compiler trace"); let assemblertrace: str = strings.concat(td, "/", tags[si], " assembler trace"); let linkertrace: str = strings.concat(td, "/", tags[si], " linker trace"); testenv.writefile(compilertrace, ""); testenv.writefile(assemblertrace, ""); testenv.writefile(linkertrace, ""); let driverbytes: str = testenv.readfile(testenv.driver(stages[si])); testenv.writeexecutable(copied[si], driverbytes); let env: []str = roottraceenv(source, runtime, compiler, assembler, linker, compilertrace, assemblertrace, linkertrace, testenv.driver(compilers[si]), testenv.driver(assemblers[si]), testenv.driver(linkers[si])); let av: []str = [copied[si], "build", "-w", work, "-o", bin, "app"]; let out: testenv.commandout; testenv.runcommandenv(cwd, td, strings.concat("driver-cold-", tags[si]), av, env, tmo(), &out); if (out.termination != exec.termination.EXIT || out.code != 0 || out.stderr.len != 0) { fail("driver-identity", strings.concat(tags[si], " cold build failed: ", out.stderr)); }; let rootunitpath: str = strings.concat(work, "/__root.unit.ww"); let depunitpath: str = strings.concat(work, "/dep.unit.ww"); let rootunit: str = testenv.readfile(rootunitpath); let depunit: str = testenv.readfile(depunitpath); if (!testenv.has(rootunit, "//ww:module-reset main\npackage main;") || testenv.has(rootunit, "//ww:module ") || !testenv.has(depunit, "//ww:module-reset dep\npackage dep;") || testenv.has(depunit, "//ww:module ")) { fail("driver-identity", "warm units contain non-owned export text"); }; if (!testenv.exists(strings.concat(work, "/leaf.wwi")) || !testenv.exists(strings.concat(work, "/leaf.a")) || !testenv.exists(strings.concat(work, "/dep.wwi")) || !testenv.exists(strings.concat(work, "/dep.a")) || !testenv.exists(strings.concat(work, "/__root.wwi")) || !testenv.exists(strings.concat(work, "/__root.a")) || !testenv.same(testenv.readfile(strings.concat(work, "/.wwtool.ww")), testenv.readfile(copied[si])) || !testenv.same(testenv.readfile(strings.concat(work, "/.wwtool.w6c")), testenv.readfile(compiler)) || !testenv.same(testenv.readfile(strings.concat(work, "/.wwtool.w6a")), testenv.readfile(assembler)) || !testenv.same(testenv.readfile(strings.concat(work, "/.wwtool.stamp")), "ww workdir fmt 7 mode build asm 0\n")) { fail("driver-identity", "persistent artifacts or identities are incomplete"); }; let coldwwi: str = testenv.readfile(strings.concat(work, "/dep.wwi")); let coldarchive: str = testenv.readfile(strings.concat(work, "/dep.a")); let coldbin: str = testenv.readfile(bin); let coldcompiler: str = testenv.readfile(compilertrace); let coldassembler: str = testenv.readfile(assemblertrace); let coldlinker: str = testenv.readfile(linkertrace); if (testenv.occurrences(coldcompiler, "\n") != 3 || !testenv.has(coldcompiler, strings.concat( "BEGIN<-c><--import><", work, "/leaf.wwi><-I><", work, "/dep.wwi.new><-o><", work, "/dep.s.new><", work, "/dep.unit.new>")) || !testenv.has(coldcompiler, strings.concat( "BEGIN<--entry><-c><--import><", work, "/dep.wwi><-I><", work, "/__root.wwi.new><-o><", work, "/__root.s.new><", work, "/__root.unit.new>")) || testenv.occurrences(coldassembler, "\n") != 3 || !testenv.has(coldassembler, strings.concat( "BEGIN<-o><", work, "/dep.o.new><", work, "/dep.s.new>"))) { fail("driver-identity", "cold compiler or assembler argv changed"); }; if (testenv.occurrences(coldlinker, "\n") != 1 || !testenv.has(coldlinker, strings.concat("BEGIN<-o><", bin, "><", work, "/__root.a>")) || testenv.pos(coldlinker, strings.concat("<", work, "/dep.a>")) < 0 || testenv.pos(coldlinker, strings.concat("<", work, "/leaf.a>")) < testenv.pos(coldlinker, strings.concat("<", work, "/dep.a>")) || !testenv.has(coldlinker, strings.concat("<", runtime, "/libwwrt.a>")) || testenv.has(coldlinker, ".wwi>")) { fail("driver-identity", "cold link omitted the reachable archive closure"); }; let runav: []str = [bin]; if (code(cwd, strings.concat("driver-cold-run-", tags[si]), runav) != 42) { fail("driver-identity", "cold binary returned the wrong value"); }; testenv.writefile(strings.concat(leaf, "/extra.ww"), strings.concat( "package leaf;\n", "export fn unchanged_api() i32 = { return 1; };\n")); testenv.runcommandenv(cwd, td, strings.concat("driver-export-change-", tags[si]), av, env, tmo(), &out); if (out.termination != exec.termination.EXIT || out.code != 0 || out.stderr.len != 0) { fail("driver-identity", "dependency export-change build failed"); }; let changedexportcompiler: str = testenv.readfile(compilertrace); let changedexportassembler: str = testenv.readfile(assemblertrace); if (testenv.occurrences(changedexportcompiler, strings.concat("<", work, "/leaf.unit.new>")) != 2 || testenv.occurrences(changedexportcompiler, strings.concat("<", work, "/dep.unit.new>")) != 2 || testenv.occurrences(changedexportcompiler, strings.concat("<", work, "/__root.unit.new>")) != 1 || testenv.occurrences(changedexportcompiler, "\n") != 5 || testenv.occurrences(changedexportassembler, "\n") != 5) { fail("driver-identity", "changed export did not stop at an unchanged importer export"); }; if (code(cwd, strings.concat("driver-export-change-run-", tags[si]), runav) != 42) { fail("driver-identity", "export-change binary returned the wrong value"); }; let changedexportbin: str = testenv.readfile(bin); let beforewarmcompiler: str = strings.dup(changedexportcompiler); let beforewarmassembler: str = strings.dup(changedexportassembler); testenv.runcommandenv(cwd, td, strings.concat("driver-warm-", tags[si]), av, env, tmo(), &out); if (out.termination != exec.termination.EXIT || out.code != 0 || out.stderr.len != 0 || !testenv.same(beforewarmcompiler, testenv.readfile(compilertrace)) || !testenv.same(beforewarmassembler, testenv.readfile(assemblertrace)) || testenv.occurrences(testenv.readfile(linkertrace), "\n") != 3 || !testenv.same(rootunit, testenv.readfile(rootunitpath)) || !testenv.same(changedexportbin, testenv.readfile(bin))) { fail("driver-identity", "unchanged warm build did not reuse packages"); }; assert(os.remove(strings.concat(leaf, "/extra.ww")) == 0); testenv.runcommandenv(cwd, td, strings.concat("driver-export-restore-", tags[si]), av, env, tmo(), &out); if (out.termination != exec.termination.EXIT || out.code != 0 || out.stderr.len != 0 || testenv.occurrences(testenv.readfile(compilertrace), strings.concat( "<", work, "/leaf.unit.new>")) != 3 || testenv.occurrences(testenv.readfile(compilertrace), strings.concat( "<", work, "/dep.unit.new>")) != 3 || testenv.occurrences(testenv.readfile(compilertrace), strings.concat( "<", work, "/__root.unit.new>")) != 1 || testenv.occurrences(testenv.readfile(compilertrace), "\n") != 7 || testenv.occurrences(testenv.readfile(assemblertrace), "\n") != 7 || !testenv.same(coldbin, testenv.readfile(bin))) { fail("driver-identity", "restored export did not rebuild only its direct importer"); }; assert(os.remove(copied[si]) == 0); testenv.writeexecutable(copied[si], strings.concat(driverbytes, "\nww driver identity mutation\n")); testenv.runcommandenv(cwd, td, strings.concat("driver-changed-", tags[si]), av, env, tmo(), &out); if (out.termination != exec.termination.EXIT || out.code != 0 || out.stderr.len != 0) { fail("driver-identity", strings.concat(tags[si], " changed-driver build failed: ", out.stderr)); }; let changedcompiler: str = testenv.readfile(compilertrace); let changedassembler: str = testenv.readfile(assemblertrace); let changedlinker: str = testenv.readfile(linkertrace); if (testenv.occurrences(changedcompiler, strings.concat("<", work, "/leaf.unit.new>")) != 4 || testenv.occurrences(changedcompiler, strings.concat("<", work, "/dep.unit.new>")) != 4 || testenv.occurrences(changedcompiler, strings.concat("<", work, "/__root.unit.new>")) != 2 || testenv.occurrences(changedcompiler, "\n") != 10 || testenv.occurrences(changedassembler, "\n") != 10 || testenv.occurrences(changedlinker, "\n") != 5) { fail("driver-identity", "driver change reused stale package actions"); }; if (!testenv.same(testenv.readfile(strings.concat(work, "/.wwtool.ww")), testenv.readfile(copied[si])) || !testenv.same(rootunit, testenv.readfile(rootunitpath)) || !testenv.same(coldwwi, testenv.readfile(strings.concat(work, "/dep.wwi"))) || !testenv.same(coldarchive, testenv.readfile(strings.concat(work, "/dep.a"))) || !testenv.same(coldbin, testenv.readfile(bin)) || code(cwd, strings.concat("driver-changed-run-", tags[si]), runav) != 42) { fail("driver-identity", "driver invalidation changed package products"); }; let beforecompiler: str = strings.dup(changedcompiler); let beforeassembler: str = strings.dup(changedassembler); let beforelinker: str = strings.dup(changedlinker); let missingwork: str = strings.concat(td, "/missing workdir"); let missingbin: str = strings.concat(td, "/missing binary"); let badav: []str = [copied[si], "build", "-w", missingwork, "-o", missingbin, "app"]; testenv.runcommandenv(cwd, td, strings.concat("driver-missing-", tags[si]), badav, env, tmo(), &out); let wantdiag: str = strings.concat("ww: workdir ", missingwork, " is not a directory\n"); if (out.termination != exec.termination.EXIT || out.code != 1 || !testenv.same(out.stderr, wantdiag) || !testenv.same(beforecompiler, testenv.readfile(compilertrace)) || !testenv.same(beforeassembler, testenv.readfile(assemblertrace)) || !testenv.same(beforelinker, testenv.readfile(linkertrace)) || testenv.exists(missingbin)) { fail("driver-identity", "invalid workdir crossed the tool boundary"); }; if (si == 0) { referenceunit = strings.dup(rootunit); referencewwi = strings.dup(coldwwi); referencearchive = strings.dup(coldarchive); referencebin = strings.dup(coldbin); referencecompiler = strings.dup(changedcompiler); referenceassembler = strings.dup(changedassembler); referencelinker = strings.dup(changedlinker); referenceerror = strings.dup(out.stderr); } else { if (!testenv.same(referenceunit, rootunit) || !testenv.same(referencewwi, coldwwi) || !testenv.same(referencearchive, coldarchive) || !testenv.same(referencebin, coldbin) || !testenv.same(referencecompiler, changedcompiler) || !testenv.same(referenceassembler, changedassembler) || !testenv.same(referencelinker, changedlinker) || !testenv.same(referenceerror, out.stderr)) { fail("driver-identity", "Cstage and WWstage warm behavior differs"); }; }; testenv.clean(work); assert(os.remove(bin) == 0); if (si + 1 < stages.len) { mkdir(work); }; si += 1; }; testenv.clean(td); }; @test fn builtin_type_precedence() void = { let td: str = testenv.fresh(); let dep: str = strings.concat(td, "/builtinshadow"); mkdir(dep); let main: str = strings.concat(td, "/main"); mkdir(main); testenv.writefile(strings.concat(dep, "/dep.ww"), strings.concat( "package builtinshadow;\n", "export type i32 = i8;\n", "export fn width() i32 = { return size(i32): i32; };\n")); testenv.writefile(strings.concat(main, "/main.ww"), strings.concat( "package main;\nimport builtinshadow;\n", "fn main() i32 = { return builtinshadow.width(); };\n")); let drivers: []str = ["ww", "ww_ww"]; let tags: []str = ["c", "w"]; let outputs: []str = ["", ""]; let i: i32 = 0; for (i < drivers.len) { outputs[i] = strings.concat(td, "/builtin-", tags[i]); if (build(td, drivers[i], strings.concat("builtin_", tags[i]), main, outputs[i]) != 0) { fail("builtin-type", strings.concat(drivers[i], " build failed")); }; let av: []str = [outputs[i]]; if (code(td, strings.concat("run_builtin_", tags[i]), av) != 4) { fail("builtin-type", strings.concat(drivers[i], " let an imported type shadow intrinsic i32")); }; i += 1; }; if (!testenv.same(testenv.readfile(outputs[0]), testenv.readfile(outputs[1]))) { fail("builtin-type", "C/WW products differ"); }; testenv.clean(td); }; @test fn export_visibility() void = { let td: str = testenv.fresh(); let main: str = writediamond(td, false); // The successful diamond already proves a direct exported declaration. testenv.writefile(strings.concat(td, "/private.ww"), strings.concat( "package main;\nimport shared;\n", "fn main() i32 = { return shared.hidden(); };\n")); rejectstable(td, "private", strings.concat(td, "/private.ww"), "has no exported declaration 'hidden'"); testenv.writefile(strings.concat(td, "/transitive.ww"), strings.concat( "package main;\nimport left;\n", "fn main() i32 = { return shared.base(); };\n")); rejectstable(td, "transitive", strings.concat(td, "/transitive.ww"), "package 'shared' is not directly imported"); testenv.writefile(strings.concat(td, "/transitive-bare-value.ww"), strings.concat("package main;\nimport left;\n", "fn main() i32 = { return base(); };\n")); rejectstable(td, "transitive-bare-value", strings.concat(td, "/transitive-bare-value.ww"), "undefined: base"); testenv.writefile(strings.concat(td, "/transitive-bare-type.ww"), strings.concat("package main;\nimport left;\n", "fn main() i32 = { let x: token; return 0; };\n")); rejectstable(td, "transitive-bare-type", strings.concat(td, "/transitive-bare-type.ww"), "unknown type 'token'"); // Keep the helper's main directory live so the graph also has a normal // successful consumer in this fixture tree. assert(testenv.exists(main)); testenv.clean(td); }; @test fn import_diagnostics() void = { let td: str = testenv.fresh(); let missing: str = strings.concat(td, "/missing"); mkdir(missing); testenv.writefile(strings.concat(missing, "/main.ww"), strings.concat( "package main;\nimport nowhere;\n", "fn main() i32 = { return 0; };\n")); rejectstable(td, "missing", missing, "cannot find package nowhere"); let self: str = strings.concat(td, "/self"); mkdir(self); testenv.writefile(strings.concat(self, "/main.ww"), strings.concat( "package main;\nimport self;\n", "fn main() i32 = { return 0; };\n")); rejectstable(td, "self", self, "self-import"); let malformed: str = strings.concat(td, "/malformed"); mkdir(malformed); testenv.writefile(strings.concat(malformed, "/main.ww"), strings.concat( "package main;\nimport ;\n", "fn main() i32 = { return 0; };\n")); rejectstable(td, "malformed", malformed, "main.ww:2:8: error: expected identifier"); let attributed: str = strings.concat(td, "/attributed"); mkdir(attributed); testenv.writefile(strings.concat(attributed, "/main.ww"), strings.concat( "package main;\n@trace import nowhere;\n", "fn main() i32 = { return 0; };\n")); rejectstable(td, "attributed", attributed, "import cannot be exported or attributed"); let exported: str = strings.concat(td, "/exported"); mkdir(exported); testenv.writefile(strings.concat(exported, "/main.ww"), strings.concat( "package main;\nexport import nowhere;\n", "fn main() i32 = { return 0; };\n")); rejectstable(td, "exported", exported, "main.ww:2:8: error: import cannot be exported or attributed"); let conflict: str = strings.concat(td, "/conflict"); mkdir(conflict); testenv.writefile(strings.concat(conflict, "/a.ww"), "package main;\nfn main() i32 = { return 0; };\n"); testenv.writefile(strings.concat(conflict, "/z.ww"), "package other;\nfn spare() void = { };\n"); rejectstable(td, "conflict", conflict, "conflicting package names"); let real: str = strings.concat(td, "/real"); mkdir(real); testenv.writefile(strings.concat(real, "/real.ww"), strings.concat( "package real;\n", "export fn value() i32 = { return 1; };\n")); let alias: str = strings.concat(td, "/alias"); assert(os.symlink(real, alias) == 0); let aliasmain: str = strings.concat(td, "/alias-main"); mkdir(aliasmain); testenv.writefile(strings.concat(aliasmain, "/main.ww"), strings.concat( "package main;\nimport real;\nimport alias;\n", "fn main() i32 = { return 0; };\n")); rejectstable(td, "identity-alias", aliasmain, "identities"); testenv.clean(td); }; @test fn package_artifact_identity_and_flag_contract() void = { let td: str = testenv.fresh(); let root: str = strings.concat(td, "/root"); mkdir(root); let foo: str = strings.concat(root, "/foo"); mkdir(foo); let bar: str = strings.concat(foo, "/bar"); mkdir(bar); testenv.writefile(strings.concat(bar, "/bar.ww"), strings.concat( "package bar;\n", "export fn nestedvalue() i32 = { return 41; };\n")); let ca: str = strings.concat(td, "/nested-c.a"); let wa: str = strings.concat(td, "/nested-w.a"); let cav: []str = [testenv.driver("ww"), "build", "-p", "-I", root, "-o", ca, "foo.bar"]; let wav: []str = [testenv.driver("ww_ww"), "build", "-p", "-I", root, "-o", wa, "foo.bar"]; if (code(td, "nested_package_c", cav) != 0 || code(td, "nested_package_w", wav) != 0) { fail("package-identity", "nested logical package build failed"); }; if (!testenv.same(testenv.readfile(ca), testenv.readfile(wa)) || !testenv.same(testenv.readfile(strings.concat(ca, ".wwi")), testenv.readfile(strings.concat(wa, ".wwi")))) { fail("package-identity", "nested C/WW package products differ"); }; let nestedasm: str = testenv.readfile(strings.concat(ca, ".sepwork/foo.bar.s")); if (!testenv.has(nestedasm, "foo.bar.nestedvalue")) { fail("package-identity", "archive lost the full logical import path"); }; let drivers: []str = ["ww", "ww_ww"]; let i: i32 = 0; for (i < drivers.len) { let badout: str = strings.concat(td, "/bad-flags-", drivers[i]); let av: []str = [testenv.driver(drivers[i]), "build", "-p", "-S", "-I", root, "-o", badout, "foo.bar"]; let co: testenv.commandout; command(td, strings.concat("package_flag_contract_", drivers[i]), av, &co); if (co.termination != exec.termination.EXIT || co.code == 0 || !testenv.has(co.stderr, "ww build: -p and -S cannot be combined")) { fail("package-flags", strings.concat(drivers[i], " accepted -p -S")); }; i += 1; }; testenv.clean(td); }; @test fn generated_test_hook_is_not_a_user_exemption() void = { let td: str = testenv.fresh(); let src: str = strings.concat(td, "/case.ww"); testenv.writefile(src, strings.concat( "package probe;\nimport test;\n", "fn bad() void = { test.run(); };\n", "@test fn one() void = {};\n")); let compilers: []str = ["w6c", "w6c_ww"]; let i: i32 = 0; for (i < compilers.len) { let asm: str = strings.concat(td, "/case-", compilers[i], ".s"); let av: []str = [testenv.driver(compilers[i]), "-T", "-c", "-o", asm, src]; let co: testenv.commandout; command(td, strings.concat("test_hook_", compilers[i]), av, &co); if (co.termination != exec.termination.EXIT || co.code == 0 || !testenv.has(co.stderr, "package 'test' has no exported declaration 'run'")) { fail("test-hook", strings.concat(compilers[i], " exempted a user-authored test.run")); }; i += 1; }; testenv.clean(td); }; fn writecyclepkg(td: str, name: str, next: str) void = { let dir: str = strings.concat(td, "/", name); mkdir(dir); testenv.writefile(strings.concat(dir, "/p.ww"), strings.concat( "package ", name, ";\nimport ", next, ";\n", "export fn value() i32 = { return 1; };\n")); }; @test fn cycle_diagnostics() void = { let td: str = testenv.fresh(); let two: str = strings.concat(td, "/two-main"); mkdir(two); testenv.writefile(strings.concat(two, "/main.ww"), "package main;\nimport aa;\nfn main() i32 = { return 0; };\n"); writecyclepkg(td, "aa", "bb"); writecyclepkg(td, "bb", "aa"); rejectstable(td, "cycle-two", two, "dependency cycle: aa -> bb -> aa"); rejectstable(td, "cycle-root", strings.concat(td, "/aa"), "dependency cycle: aa -> bb -> aa"); // A second isolated root set avoids reusing the two-package graph. let longroot: str = strings.concat(td, "/long-main"); mkdir(longroot); testenv.writefile(strings.concat(longroot, "/main.ww"), "package main;\nimport ca;\nfn main() i32 = { return 0; };\n"); writecyclepkg(td, "ca", "cb"); writecyclepkg(td, "cb", "cc"); writecyclepkg(td, "cc", "ca"); rejectstable(td, "cycle-long", longroot, "dependency cycle: ca -> cb -> cc -> ca"); testenv.clean(td); };