diff --git a/tests/fs/copy.luau b/tests/fs/copy.luau index 5095c0e..c7ed6dd 100644 --- a/tests/fs/copy.luau +++ b/tests/fs/copy.luau @@ -3,13 +3,9 @@ local TEMP_ROOT_PATH = TEMP_DIR_PATH .. "fs_copy_test" local TEMP_ROOT_PATH_2 = TEMP_DIR_PATH .. "fs_copy_test_2" local fs = require("@lune/fs") +local utils = require("./utils") --- Generate test data & make sure our bin dir exists - -local binary = "" -for _ = 1, 1024 do - binary ..= string.char(math.random(1, 127)) -end +-- Make sure our bin dir exists fs.writeDir(TEMP_DIR_PATH) if fs.isDir(TEMP_ROOT_PATH) then @@ -34,9 +30,9 @@ end fs.writeDir(TEMP_ROOT_PATH) fs.writeDir(TEMP_ROOT_PATH .. "/foo") fs.writeDir(TEMP_ROOT_PATH .. "/foo/bar") -fs.writeFile(TEMP_ROOT_PATH .. "/foo/bar/baz", binary) -fs.writeFile(TEMP_ROOT_PATH .. "/foo/fizz", binary) -fs.writeFile(TEMP_ROOT_PATH .. "/foo/buzz", binary) +fs.writeFile(TEMP_ROOT_PATH .. "/foo/bar/baz", utils.binaryBlob) +fs.writeFile(TEMP_ROOT_PATH .. "/foo/fizz", utils.binaryBlob) +fs.writeFile(TEMP_ROOT_PATH .. "/foo/buzz", utils.binaryBlob) -- Copy the entire structure @@ -54,15 +50,15 @@ assert(fs.isFile(TEMP_ROOT_PATH_2 .. "/foo/buzz"), "Missing copied file - root/f -- Make sure the copied files are correct assert( - fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/bar/baz") == binary, + fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/bar/baz") == utils.binaryBlob, "Invalid copied file - root/foo/bar/baz" ) assert( - fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/fizz") == binary, + fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/fizz") == utils.binaryBlob, "Invalid copied file - root/foo/fizz" ) assert( - fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/buzz") == binary, + fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/buzz") == utils.binaryBlob, "Invalid copied file - root/foo/buzz" ) diff --git a/tests/fs/dirs.luau b/tests/fs/dirs.luau index 3aba43c..c2d60a3 100644 --- a/tests/fs/dirs.luau +++ b/tests/fs/dirs.luau @@ -1,24 +1,27 @@ +local TEMP_DIR_PATH = "bin/" +local TEMP_ROOT_PATH = TEMP_DIR_PATH .. "fs_dirs_test" + local fs = require("@lune/fs") --- Write two inner dirs in the bir dir, a parent and a child +-- Write two inner dirs in the bin dir, a parent and a child -fs.writeDir("bin/test_dir/test_inner") +fs.writeDir(TEMP_ROOT_PATH .. "/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(fs.isDir(TEMP_DIR_PATH), "Dir root isDir check failed") +assert(fs.isDir(TEMP_ROOT_PATH), "Dir outer isDir check failed") +assert(fs.isDir(TEMP_ROOT_PATH .. "/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") +assert(not fs.isFile(TEMP_DIR_PATH), "Dir root isFile check failed") +assert(not fs.isFile(TEMP_ROOT_PATH), "Dir outer isFile check failed") +assert(not fs.isFile(TEMP_ROOT_PATH .. "/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/") +fs.removeDir(TEMP_ROOT_PATH) -assert(not fs.isDir("bin/test_dir"), "After removal isDir check failed") -assert(not fs.isFile("bin/test_dir"), "After removal isFile check failed") +assert(not fs.isDir(TEMP_ROOT_PATH), "After removal isDir check failed") +assert(not fs.isFile(TEMP_ROOT_PATH), "After removal isFile check failed") diff --git a/tests/fs/files.luau b/tests/fs/files.luau index 95beebb..7278b50 100644 --- a/tests/fs/files.luau +++ b/tests/fs/files.luau @@ -1,55 +1,53 @@ +local TEMP_DIR_PATH = "bin/" +local TEMP_ROOT_PATH = TEMP_DIR_PATH .. "fs_files_test" + local fs = require("@lune/fs") local net = require("@lune/net") +local utils = require("./utils") --- Generate test data & make sure our bin dir exists +-- 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") +fs.writeDir(TEMP_DIR_PATH) +fs.writeDir(TEMP_ROOT_PATH) -- Write both of our files -fs.writeFile("bin/test_binary", binary) -fs.writeFile("bin/test_json.json", json) +fs.writeFile(TEMP_ROOT_PATH .. "/test_binary", utils.binaryBlob) +fs.writeFile(TEMP_ROOT_PATH .. "/test_json.json", utils.jsonBlob) -- Make sure reading the file we just -- wrote gets us back the original strings assert( - fs.readFile("bin/test_binary") == binary, + fs.readFile(TEMP_ROOT_PATH .. "/test_binary") == utils.binaryBlob, "Binary file round-trip resulted in different strings" ) assert( - fs.readFile("bin/test_json.json") == json, + fs.readFile(TEMP_ROOT_PATH .. "/test_json.json") == utils.jsonBlob, "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(fs.isFile(TEMP_ROOT_PATH .. "/test_binary"), "Binary file isFile check failed") +assert(fs.isFile(TEMP_ROOT_PATH .. "/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") +assert(not fs.isDir(TEMP_ROOT_PATH .. "/test_binary"), "Binary file isDir check failed") +assert(not fs.isDir(TEMP_ROOT_PATH .. "/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") +fs.removeFile(TEMP_ROOT_PATH .. "/test_binary") +fs.removeFile(TEMP_ROOT_PATH .. "/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(TEMP_ROOT_PATH .. "/test_binary"), "Binary after removal isDir check failed") +assert(not fs.isFile(TEMP_ROOT_PATH .. "/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") +assert(not fs.isDir(TEMP_ROOT_PATH .. "/test_json.json"), "JSON after removal isDir check failed") +assert(not fs.isFile(TEMP_ROOT_PATH .. "/test_json.json"), "JSON after removal isFile check failed") + +-- Remove the testing dir specific to this test + +fs.removeDir(TEMP_ROOT_PATH) diff --git a/tests/fs/metadata.luau b/tests/fs/metadata.luau index 3819b17..5d63a08 100644 --- a/tests/fs/metadata.luau +++ b/tests/fs/metadata.luau @@ -3,13 +3,9 @@ local TEMP_FILE_PATH = TEMP_DIR_PATH .. "metadata_test" local fs = require("@lune/fs") local task = require("@lune/task") +local utils = require("./utils") --- Generate test data & make sure our bin dir exists - -local binary = "" -for _ = 1, 1024 do - binary ..= string.char(math.random(1, 127)) -end +-- Make sure our bin dir exists fs.writeDir(TEMP_DIR_PATH) if fs.isFile(TEMP_FILE_PATH) then @@ -23,7 +19,7 @@ end ]] assert(not fs.metadata(TEMP_FILE_PATH).exists, "File metadata not exists failed") -fs.writeFile(TEMP_FILE_PATH, binary) +fs.writeFile(TEMP_FILE_PATH, utils.binaryBlob) assert(fs.metadata(TEMP_FILE_PATH).exists, "File metadata exists failed") --[[ @@ -46,7 +42,7 @@ assert(metaFile.kind == "file", "File metadata kind was invalid") local metaBefore = fs.metadata(TEMP_FILE_PATH) task.wait() -fs.writeFile(TEMP_FILE_PATH, binary .. "\n") +fs.writeFile(TEMP_FILE_PATH, utils.binaryBlob .. "\n") local metaAfter = fs.metadata(TEMP_FILE_PATH) assert( diff --git a/tests/fs/move.luau b/tests/fs/move.luau index 25bd90c..868a095 100644 --- a/tests/fs/move.luau +++ b/tests/fs/move.luau @@ -1,25 +1,14 @@ local fs = require("@lune/fs") -local net = require("@lune/net") +local utils = require("./utils") --- 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) +-- Make sure our bin dir exists fs.writeDir("bin") -- Write both of our files -fs.writeFile("bin/move_test_binary", binary) -fs.writeFile("bin/move_test_json.json", json) +fs.writeFile("bin/move_test_binary", utils.binaryBlob) +fs.writeFile("bin/move_test_json.json", utils.jsonBlob) -- Move / rename them to something else, to test we -- change the prefix in the names from "move" to "moved" @@ -31,12 +20,12 @@ fs.move("bin/move_test_json.json", "bin/moved_test_json.json") -- wrote gets us back the original strings assert( - fs.readFile("bin/moved_test_binary") == binary, + fs.readFile("bin/moved_test_binary") == utils.binaryBlob, "Binary file round-trip resulted in different strings" ) assert( - fs.readFile("bin/moved_test_json.json") == json, + fs.readFile("bin/moved_test_json.json") == utils.jsonBlob, "JSON file round-trip resulted in different strings" ) diff --git a/tests/fs/utils.luau b/tests/fs/utils.luau new file mode 100644 index 0000000..a5a947e --- /dev/null +++ b/tests/fs/utils.luau @@ -0,0 +1,21 @@ +local serde = require("@lune/serde") + +-- Generate testing data + +local binaryBlob = "" +for _ = 1, 1024 do + binaryBlob ..= string.char(math.random(1, 127)) +end + +local jsonBlob = serde.encode("json", { + Foo = "Bar", + Hello = "World", + Inner = { Array = { 1, 2, 3 } }, +}, true) + +-- Return testing data and utils + +return { + binaryBlob = binaryBlob, + jsonBlob = jsonBlob, +}