From 8e58a8ed108d3ff0eb514b7163f3d994c6ab6c80 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 21 Jan 2023 02:01:46 -0500 Subject: [PATCH] Implement test suite for fs --- .gitignore | 1 + src/lib/lib.rs | 2 ++ src/tests/fs/dirs.luau | 22 +++++++++++++++++ src/tests/fs/files.luau | 52 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 src/tests/fs/dirs.luau create mode 100644 src/tests/fs/files.luau diff --git a/.gitignore b/.gitignore index 2449f0c..7ce1326 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/bin /target .DS_Store diff --git a/src/lib/lib.rs b/src/lib/lib.rs index cc69f2a..cd88d0c 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -94,6 +94,8 @@ mod tests { console_format: "console/format", console_set_color: "console/set_color", console_set_style: "console/set_style", + fs_files: "fs/files", + fs_dirs: "fs/dirs", process_args: "process/args", process_env: "process/env", process_spawn: "process/spawn", diff --git a/src/tests/fs/dirs.luau b/src/tests/fs/dirs.luau new file mode 100644 index 0000000..7ad4594 --- /dev/null +++ b/src/tests/fs/dirs.luau @@ -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") diff --git a/src/tests/fs/files.luau b/src/tests/fs/files.luau new file mode 100644 index 0000000..47c4653 --- /dev/null +++ b/src/tests/fs/files.luau @@ -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")