Make testing framework more generic
This commit is contained in:
@@ -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
@@ -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'")
|
||||||
|
|||||||
+7
-7
@@ -3,14 +3,14 @@
|
|||||||
local types = {}
|
local types = {}
|
||||||
|
|
||||||
export type opts = {
|
export type opts = {
|
||||||
outputFile: string?,
|
outputFile: string?,
|
||||||
directoryMode: boolean,
|
directoryMode: boolean,
|
||||||
directoryPath: string?,
|
directoryPath: string?,
|
||||||
forceBinaryRead: boolean,
|
forceBinaryRead: boolean,
|
||||||
printInstanceNames: boolean,
|
printInstanceNames: boolean,
|
||||||
zlibDecompressFiles: boolean,
|
zlibDecompressFiles: boolean,
|
||||||
zlibDecompressFilesRecursive: boolean,
|
zlibDecompressFilesRecursive: boolean,
|
||||||
filesToProcess: {string?}
|
filesToProcess: {string?}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type model = {Instance}
|
export type model = {Instance}
|
||||||
|
|||||||
+28
-48
@@ -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
|
process.exit(1)
|
||||||
print("Test failed for file: " .. filePath)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
print("All tests passed for files in containsworkspace.")
|
||||||
|
|
||||||
local zlibCompressedDir = fs.readDir("./zlibCompressed")
|
local doesntContainWorkspaceTest = testFolder(core.fileContainsWorkspace, {outputFile = nil, directoryMode = false, directoryPath = nil, forceBinaryRead = false, printInstanceNames = false, zlibDecompressFiles = false, zlibDecompressFilesRecursive = false, filesToProcess = nil}, "doesntcontainworkspace")
|
||||||
local zlibCompressedTestsPassed = 0
|
for fileName, result in pairs(doesntContainWorkspaceTest) do
|
||||||
|
|
||||||
for _, filePath in zlibCompressedDir do
|
|
||||||
local fileContents = fs.readFile("./zlibCompressed/" .. filePath)
|
|
||||||
local result = compression.zlibDecompress(fileContents, true)
|
|
||||||
if result then
|
if result then
|
||||||
zlibCompressedTestsPassed += 1
|
print(`Test failed: {fileName} shouldn't contain a workspace instance.`)
|
||||||
else
|
process.exit(1)
|
||||||
print("Test failed for file: " .. filePath)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
print("All tests passed for files in doesntcontainworkspace.")
|
||||||
|
|
||||||
print(`Passed {containsWorkspaceTestsPassed}/{#containsWorkspaceDir} "contains workspace" tests.`)
|
local zlibDecompressedTest = testFolder(compression.zlibDecompress, {outputFile = nil, directoryMode = false, directoryPath = nil, forceBinaryRead = false, printInstanceNames = false, zlibDecompressFiles = false, zlibDecompressFilesRecursive = true, filesToProcess = nil}, "zlibCompressed")
|
||||||
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
|
for fileName, result in pairs(zlibDecompressedTest) do
|
||||||
print("All tests passed!")
|
if not result then
|
||||||
process.exit(0)
|
print(`Test failed: {fileName} should be zlib decompressed successfully.`)
|
||||||
else
|
process.exit(1)
|
||||||
print("Some tests failed.")
|
end
|
||||||
process.exit(1)
|
|
||||||
end
|
end
|
||||||
|
print("All tests passed for zlib decompression.")
|
||||||
|
print("All tests passed.")
|
||||||
Reference in New Issue
Block a user