--!strict local datetime = require("@lune/datetime") local fs = require("@lune/fs") local process = require("@lune/process") local serde = require("@lune/serde") local stdio = require("@lune/stdio") local progress_bar = require("./util/progress") local result = require("./util/result") local shared = require("./shared") local function test(origin: string): result.Identity local now = datetime.now() local begin = os.clock() local progress = progress_bar .new() :withStage("init", "Initializing") :withStage("fetch", "Fetching latest tests") :withStage("test", "Running tests") :withStage("cleanup", "Cleaning up") :withStage("metadata", "Writing test metadata") progress:start() -- init if not fs.metadata(origin).exists then progress:stop() stdio.ewrite(`๐Ÿ”ฅ {origin} is not a valid directory which exists.\n`) return result(false, `{origin} is not a valid directory which exists.`) end if not fs.metadata(`{origin}/jecs.luau`).exists then progress:stop() stdio.ewrite(`๐Ÿ”ฅ {origin}/jecs.luau is not a vald file which exists.\n`) return result(false, `{origin}/jecs.luau is not a vald file which exists.`) end if fs.metadata(`{origin}/test`).exists then fs.removeDir(`{origin}/test`) end fs.writeDir(`{origin}/test`) if fs.metadata(`{origin}/tools`).exists then fs.removeDir(`{origin}/tools`) end fs.writeDir(`{origin}/tools`) progress:nextStage() -- fetch do local contents = shared.fetch_raw("tools/testkit.luau") if not contents.ok then progress:stop() stdio.ewrite(`๐Ÿ”ฅ Couldn't get the latest source for tools/testkit.luau:\n{contents.err}\n`) return result(false, "Couldn't get the latest source for tools/testkit.luau.") end fs.writeFile(`{origin}/tools/testkit.luau`, contents.val) end do local contents = shared.fetch_raw("test/tests.luau") if not contents.ok then progress:stop() stdio.ewrite(`๐Ÿ”ฅ Couldn't get the latest source for test/tests.luau:\n{contents.err}\n`) return result(false, "Couldn't get the latest source for test/tests.luau.") end fs.writeFile(`{origin}/test/tests.luau`, contents.val) end progress:nextStage() -- test local test_result = process.spawn("luau", { "test/tests.luau" }, { cwd = origin }) if not test_result.ok then progress:stop() stdio.ewrite(`๐Ÿ”ฅ Tests failed to run:\n{test_result.stderr}\n`) return result(false, `Tests failed to run.`) end local passed = true if not string.find(test_result.stdout, "0 fails") then passed = false end progress:nextStage() -- cleanup fs.removeDir(`{origin}/test`) progress:nextStage() -- metadata local metadata = { timestamp = shared.iso_date_light(now), passed = passed, } local encoded_metadata = serde.encode("toml", metadata) fs.writeFile(`{origin}/test.txt`, encoded_metadata) fs.writeFile(`{origin}/test_fulllog.txt`, test_result.stdout) progress:stop() -- finish print(test_result.stdout) local passed_txt = if passed then `{stdio.style("bold")}{stdio.color("green")}passed{stdio.color("reset")}{stdio.style("reset")}` else `{stdio.style("bold")}{stdio.color("red")}failed{stdio.color("reset")}{stdio.style("reset")}` local took = shared.round_to((os.clock() - begin) * 1_000, 2) print(`๐Ÿงช Finished testing, {passed_txt} {stdio.style("dim")}(took {took}ms){stdio.style("reset")}`) return result(true, true) end return test