Implement test suite for fs

This commit is contained in:
Filip Tibell 2023-01-21 02:01:46 -05:00
parent 10f1aaa479
commit 8e58a8ed10
No known key found for this signature in database
4 changed files with 77 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/bin
/target /target
.DS_Store .DS_Store

View file

@ -94,6 +94,8 @@ mod tests {
console_format: "console/format", console_format: "console/format",
console_set_color: "console/set_color", console_set_color: "console/set_color",
console_set_style: "console/set_style", console_set_style: "console/set_style",
fs_files: "fs/files",
fs_dirs: "fs/dirs",
process_args: "process/args", process_args: "process/args",
process_env: "process/env", process_env: "process/env",
process_spawn: "process/spawn", process_spawn: "process/spawn",

22
src/tests/fs/dirs.luau Normal file
View file

@ -0,0 +1,22 @@
-- Write two inner dirs in the bir dir, a parent and a child
fs.writeDir("bin/test_dir/test_inner")
-- Make sure dir checks succeed but file
-- checks fail for all levels of dirs
assert(fs.isDir("bin"), "Dir root isDir check failed")
assert(fs.isDir("bin/test_dir"), "Dir outer isDir check failed")
assert(fs.isDir("bin/test_dir/test_inner"), "Dir inner isDir check failed")
assert(not fs.isFile("bin"), "Dir root isFile check failed")
assert(not fs.isFile("bin/test_dir"), "Dir outer isFile check failed")
assert(not fs.isFile("bin/test_dir/test_inner"), "Dir inner isFile check failed")
-- Remove the created parent and child dirs and
-- make sure the APIs say they no longer exist
fs.removeDir("bin/test_dir/")
assert(not fs.isDir("bin/test_dir"), "After removal isDir check failed")
assert(not fs.isFile("bin/test_dir"), "After removal isFile check failed")

52
src/tests/fs/files.luau Normal file
View file

@ -0,0 +1,52 @@
-- Generate test data & make sure our bin dir exists
local binary = ""
for _ = 1, 1024 do
binary ..= string.char(math.random(1, 127))
end
local json = net.jsonEncode({
Foo = "Bar",
Hello = "World",
Inner = { Array = { 1, 2, 3 } },
}, true)
fs.writeDir("bin")
-- Write both of our files
fs.writeFile("bin/test_binary", binary)
fs.writeFile("bin/test_json.json", json)
-- Make sure reading the file we just
-- wrote gets us back the original strings
assert(
fs.readFile("bin/test_binary") == binary,
"Binary file round-trip resulted in different strings"
)
assert(
fs.readFile("bin/test_json.json") == json,
"JSON file round-trip resulted in different strings"
)
-- Make sure file checks succeed but dir checks fail
assert(fs.isFile("bin/test_binary"), "Binary file isFile check failed")
assert(fs.isFile("bin/test_json.json"), "JSON file isFile check failed")
assert(not fs.isDir("bin/test_binary"), "Binary file isDir check failed")
assert(not fs.isDir("bin/test_json.json"), "JSON file isDir check failed")
-- Remove the files and make sure
-- the APIs say they no longer exist
fs.removeFile("bin/test_binary")
fs.removeFile("bin/test_json.json")
assert(not fs.isDir("bin/test_binary"), "Binary after removal isDir check failed")
assert(not fs.isFile("bin/test_binary"), "Binary after removal isFile check failed")
assert(not fs.isDir("bin/test_json.json"), "JSON after removal isDir check failed")
assert(not fs.isFile("bin/test_json.json"), "JSON after removal isFile check failed")