Unit test before release, move src/init to cli form
This commit is contained in:
parent
7d7aa8de33
commit
b52882ef2d
15 changed files with 956 additions and 3008 deletions
105
src/test.luau
Normal file
105
src/test.luau
Normal file
|
@ -0,0 +1,105 @@
|
|||
--!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<boolean>
|
||||
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`)
|
||||
|
||||
progress:nextStage() -- fetch
|
||||
|
||||
do
|
||||
local contents = shared.fetch_raw("test/testkit.luau")
|
||||
if not contents.ok then
|
||||
progress:stop()
|
||||
stdio.ewrite(`🔥 Couldn't get the latest source for test/testkit.luau:\n{contents.err}\n`)
|
||||
return result(false, "Couldn't get the latest source for test/testkit.luau.")
|
||||
end
|
||||
fs.writeFile(`{origin}/test/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
|
Loading…
Add table
Add a link
Reference in a new issue