Make testing framework more generic

This commit is contained in:
2025-09-25 20:00:49 -04:00
parent 057b930711
commit eb617a454f
4 changed files with 44 additions and 63 deletions
+6 -6
View File
@@ -1,4 +1,5 @@
local serde = require("@lune/serde") local serde = require("@lune/serde")
local types = require("./types")
type CompressionModule = { type CompressionModule = {
zlibDecompress: (contents: string, recursive: boolean?) -> string | boolean zlibDecompress: (contents: string, recursive: boolean?) -> string | boolean
@@ -10,19 +11,18 @@ local compressionModule = {} :: CompressionModule
-- take in file contents and zlib decompress them -- take in file contents and zlib decompress them
-- if recursive param is true repeatedly zlib decompress the file until it fails -- if recursive param is true repeatedly zlib decompress the file until it fails
-- (modelscrape files are often double/triple zlib decompressed) -- (modelscrape files are often double/triple zlib decompressed)
function compressionModule.zlibDecompress(contents: string, recursive: boolean?): string | boolean function compressionModule.zlibDecompress(contents: string, opts: types.opts): string | boolean
assert(typeof(contents) == "string", "Expected contents to be of type 'string'") assert(typeof(contents) == "string", "Expected contents to be of type 'string'")
if recursive == nil then recursive = false end assert(typeof(opts) == "table", "Expected opts to be of type 'table'")
assert(typeof(recursive) == "boolean", "Expected recursive to be of type 'boolean'")
local success, decompressed: string = pcall(function() return serde.decompress("zlib", contents) end) local success, decompressed: string = pcall(function() return serde.decompress("zlib", contents) end)
if not success then if not success then
return false return false
end end
if recursive then if opts.zlibDecompressFilesRecursive then
local nextDecompressed = compressionModule.zlibDecompress(decompressed, true) local nextDecompressed = compressionModule.zlibDecompress(decompressed, opts)
while nextDecompressed do while nextDecompressed do
decompressed = nextDecompressed decompressed = nextDecompressed
nextDecompressed = compressionModule.zlibDecompress(decompressed, true) nextDecompressed = compressionModule.zlibDecompress(decompressed, opts)
end end
end end
return decompressed return decompressed
+2 -1
View File
@@ -40,7 +40,7 @@ function core.fileContainsWorkspace(fileContents: string, opts: types.opts): boo
assert(typeof(fileContents) == "string", "Expected fileContents to be of type 'string'") assert(typeof(fileContents) == "string", "Expected fileContents to be of type 'string'")
assert(typeof(opts) == "table", "Expected opts to be of type 'table'") assert(typeof(opts) == "table", "Expected opts to be of type 'table'")
if opts.zlibDecompressFiles or opts.zlibDecompressFilesRecursive then if opts.zlibDecompressFiles or opts.zlibDecompressFilesRecursive then
local success = pcall(function() fileContents = compression.zlibDecompress(fileContents, opts.zlibDecompressFilesRecursive) end) local success = pcall(function() fileContents = compression.zlibDecompress(fileContents, opts) end)
if not success then if not success then
stdio.write(stdio.color("yellow")) stdio.write(stdio.color("yellow"))
stdio.write("Warning: Failed to decompress file with zlib. Proceeding with original contents.\n") stdio.write("Warning: Failed to decompress file with zlib. Proceeding with original contents.\n")
@@ -54,6 +54,7 @@ function core.fileContainsWorkspace(fileContents: string, opts: types.opts): boo
return core.scanForWorkspace(instances, opts.printInstanceNames) return core.scanForWorkspace(instances, opts.printInstanceNames)
end end
-- formats the workspace detection result as a colored string -- formats the workspace detection result as a colored string
function core.formatResult(result: boolean, fileName: string): string function core.formatResult(result: boolean, fileName: string): string
assert(typeof(result) == "boolean", "Expected result to be of type 'boolean'") assert(typeof(result) == "boolean", "Expected result to be of type 'boolean'")
+35 -55
View File
@@ -2,67 +2,47 @@ local fs = require("@lune/fs")
local process = require("@lune/process") local process = require("@lune/process")
local core = require("../lib/core") local core = require("../lib/core")
local compression = require("../lib/compression") local compression = require("../lib/compression")
local types = require("../lib/types")
local containsWorkspaceDir = fs.readDir("./containsworkspace") function testFolder(callback: (file: string, opts: types.opts) -> boolean, opts: types.opts, folderPath: string): {boolean}
local containsWorkspaceTestsPassed = 0 local results = {}
local folder = fs.readDir(folderPath)
for _, filePath in containsWorkspaceDir do for _, file in pairs(folder) do
local fileContents = fs.readFile("./containsworkspace/" .. filePath) local filePath = `./{folderPath}/{file}`
local result = core.fileContainsWorkspace(fileContents, { local fileContents = fs.readFile(filePath)
forceBinaryRead = false, local result = callback(fileContents, opts)
directoryMode = false, results[file] = result
printInstanceNames = false,
zlibDecompressFiles = false,
zlibDecompressFilesRecursive = false
})
if result then
containsWorkspaceTestsPassed += 1
else
print("Test failed for file: " .. filePath)
end end
return results
end end
local doesntContainWorkspaceDir = fs.readDir("./doesntcontainworkspace") local containsWorkspaceTest = testFolder(core.fileContainsWorkspace, {outputFile = nil, directoryMode = false, directoryPath = nil, forceBinaryRead = false, printInstanceNames = false, zlibDecompressFiles = false, zlibDecompressFilesRecursive = false, filesToProcess = nil} , "containsworkspace")
local doesntContainWorkspaceTestsPassed = 0
for _, filePath in doesntContainWorkspaceDir do for fileName, result in pairs(containsWorkspaceTest) do
local fileContents = fs.readFile("./doesntcontainworkspace/" .. filePath)
local result = core.fileContainsWorkspace(fileContents, {
forceBinaryRead = false,
directoryMode = false,
printInstanceNames = false,
zlibDecompressFiles = false,
zlibDecompressFilesRecursive = false
})
if not result then if not result then
doesntContainWorkspaceTestsPassed += 1 print(`Test failed: {fileName} should contain a workspace instance.`)
else
print("Test failed for file: " .. filePath)
end
end
local zlibCompressedDir = fs.readDir("./zlibCompressed")
local zlibCompressedTestsPassed = 0
for _, filePath in zlibCompressedDir do
local fileContents = fs.readFile("./zlibCompressed/" .. filePath)
local result = compression.zlibDecompress(fileContents, true)
if result then
zlibCompressedTestsPassed += 1
else
print("Test failed for file: " .. filePath)
end
end
print(`Passed {containsWorkspaceTestsPassed}/{#containsWorkspaceDir} "contains workspace" tests.`)
print(`Passed {doesntContainWorkspaceTestsPassed}/{#doesntContainWorkspaceDir} "doesn't contain workspace" tests.`)
print(`Passed {zlibCompressedTestsPassed}/{#zlibCompressedDir} "zlib compressed" tests.`)
if containsWorkspaceTestsPassed == #containsWorkspaceDir and doesntContainWorkspaceTestsPassed == #doesntContainWorkspaceDir and zlibCompressedTestsPassed == #zlibCompressedDir then
print("All tests passed!")
process.exit(0)
else
print("Some tests failed.")
process.exit(1) process.exit(1)
end
end end
print("All tests passed for files in containsworkspace.")
local doesntContainWorkspaceTest = testFolder(core.fileContainsWorkspace, {outputFile = nil, directoryMode = false, directoryPath = nil, forceBinaryRead = false, printInstanceNames = false, zlibDecompressFiles = false, zlibDecompressFilesRecursive = false, filesToProcess = nil}, "doesntcontainworkspace")
for fileName, result in pairs(doesntContainWorkspaceTest) do
if result then
print(`Test failed: {fileName} shouldn't contain a workspace instance.`)
process.exit(1)
end
end
print("All tests passed for files in doesntcontainworkspace.")
local zlibDecompressedTest = testFolder(compression.zlibDecompress, {outputFile = nil, directoryMode = false, directoryPath = nil, forceBinaryRead = false, printInstanceNames = false, zlibDecompressFiles = false, zlibDecompressFilesRecursive = true, filesToProcess = nil}, "zlibCompressed")
for fileName, result in pairs(zlibDecompressedTest) do
if not result then
print(`Test failed: {fileName} should be zlib decompressed successfully.`)
process.exit(1)
end
end
print("All tests passed for zlib decompression.")
print("All tests passed.")