Clean up fs tests

This commit is contained in:
Filip Tibell 2023-07-20 20:06:42 +02:00
parent 02132a2eec
commit 49ae85af03
No known key found for this signature in database
6 changed files with 78 additions and 75 deletions

View file

@ -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 TEMP_ROOT_PATH_2 = TEMP_DIR_PATH .. "fs_copy_test_2"
local fs = require("@lune/fs") local fs = require("@lune/fs")
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
fs.writeDir(TEMP_DIR_PATH) fs.writeDir(TEMP_DIR_PATH)
if fs.isDir(TEMP_ROOT_PATH) then if fs.isDir(TEMP_ROOT_PATH) then
@ -34,9 +30,9 @@ end
fs.writeDir(TEMP_ROOT_PATH) fs.writeDir(TEMP_ROOT_PATH)
fs.writeDir(TEMP_ROOT_PATH .. "/foo") fs.writeDir(TEMP_ROOT_PATH .. "/foo")
fs.writeDir(TEMP_ROOT_PATH .. "/foo/bar") fs.writeDir(TEMP_ROOT_PATH .. "/foo/bar")
fs.writeFile(TEMP_ROOT_PATH .. "/foo/bar/baz", binary) fs.writeFile(TEMP_ROOT_PATH .. "/foo/bar/baz", utils.binaryBlob)
fs.writeFile(TEMP_ROOT_PATH .. "/foo/fizz", binary) fs.writeFile(TEMP_ROOT_PATH .. "/foo/fizz", utils.binaryBlob)
fs.writeFile(TEMP_ROOT_PATH .. "/foo/buzz", binary) fs.writeFile(TEMP_ROOT_PATH .. "/foo/buzz", utils.binaryBlob)
-- Copy the entire structure -- 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 -- Make sure the copied files are correct
assert( 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" "Invalid copied file - root/foo/bar/baz"
) )
assert( 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" "Invalid copied file - root/foo/fizz"
) )
assert( 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" "Invalid copied file - root/foo/buzz"
) )

View file

@ -1,24 +1,27 @@
local TEMP_DIR_PATH = "bin/"
local TEMP_ROOT_PATH = TEMP_DIR_PATH .. "fs_dirs_test"
local fs = require("@lune/fs") 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 -- Make sure dir checks succeed but file
-- checks fail for all levels of dirs -- checks fail for all levels of dirs
assert(fs.isDir("bin"), "Dir root isDir check failed") assert(fs.isDir(TEMP_DIR_PATH), "Dir root isDir check failed")
assert(fs.isDir("bin/test_dir"), "Dir outer isDir check failed") assert(fs.isDir(TEMP_ROOT_PATH), "Dir outer isDir check failed")
assert(fs.isDir("bin/test_dir/test_inner"), "Dir inner 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(TEMP_DIR_PATH), "Dir root isFile check failed")
assert(not fs.isFile("bin/test_dir"), "Dir outer isFile check failed") assert(not fs.isFile(TEMP_ROOT_PATH), "Dir outer isFile check failed")
assert(not fs.isFile("bin/test_dir/test_inner"), "Dir inner 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 -- Remove the created parent and child dirs and
-- make sure the APIs say they no longer exist -- 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.isDir(TEMP_ROOT_PATH), "After removal isDir check failed")
assert(not fs.isFile("bin/test_dir"), "After removal isFile check failed") assert(not fs.isFile(TEMP_ROOT_PATH), "After removal isFile check failed")

View file

@ -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 fs = require("@lune/fs")
local net = require("@lune/net") 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 = "" fs.writeDir(TEMP_DIR_PATH)
for _ = 1, 1024 do fs.writeDir(TEMP_ROOT_PATH)
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 -- Write both of our files
fs.writeFile("bin/test_binary", binary) fs.writeFile(TEMP_ROOT_PATH .. "/test_binary", utils.binaryBlob)
fs.writeFile("bin/test_json.json", json) fs.writeFile(TEMP_ROOT_PATH .. "/test_json.json", utils.jsonBlob)
-- Make sure reading the file we just -- Make sure reading the file we just
-- wrote gets us back the original strings -- wrote gets us back the original strings
assert( assert(
fs.readFile("bin/test_binary") == binary, fs.readFile(TEMP_ROOT_PATH .. "/test_binary") == utils.binaryBlob,
"Binary file round-trip resulted in different strings" "Binary file round-trip resulted in different strings"
) )
assert( 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" "JSON file round-trip resulted in different strings"
) )
-- Make sure file checks succeed but dir checks fail -- Make sure file checks succeed but dir checks fail
assert(fs.isFile("bin/test_binary"), "Binary file isFile check failed") assert(fs.isFile(TEMP_ROOT_PATH .. "/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_json.json"), "JSON file isFile check failed")
assert(not fs.isDir("bin/test_binary"), "Binary file isDir check failed") assert(not fs.isDir(TEMP_ROOT_PATH .. "/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_json.json"), "JSON file isDir check failed")
-- Remove the files and make sure -- Remove the files and make sure
-- the APIs say they no longer exist -- the APIs say they no longer exist
fs.removeFile("bin/test_binary") fs.removeFile(TEMP_ROOT_PATH .. "/test_binary")
fs.removeFile("bin/test_json.json") fs.removeFile(TEMP_ROOT_PATH .. "/test_json.json")
assert(not fs.isDir("bin/test_binary"), "Binary after removal isDir check failed") assert(not fs.isDir(TEMP_ROOT_PATH .. "/test_binary"), "Binary after removal isDir check failed")
assert(not fs.isFile("bin/test_binary"), "Binary after removal isFile 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.isDir(TEMP_ROOT_PATH .. "/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.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)

View file

@ -3,13 +3,9 @@ local TEMP_FILE_PATH = TEMP_DIR_PATH .. "metadata_test"
local fs = require("@lune/fs") local fs = require("@lune/fs")
local task = require("@lune/task") local task = require("@lune/task")
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
fs.writeDir(TEMP_DIR_PATH) fs.writeDir(TEMP_DIR_PATH)
if fs.isFile(TEMP_FILE_PATH) then 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") 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") 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) local metaBefore = fs.metadata(TEMP_FILE_PATH)
task.wait() task.wait()
fs.writeFile(TEMP_FILE_PATH, binary .. "\n") fs.writeFile(TEMP_FILE_PATH, utils.binaryBlob .. "\n")
local metaAfter = fs.metadata(TEMP_FILE_PATH) local metaAfter = fs.metadata(TEMP_FILE_PATH)
assert( assert(

View file

@ -1,25 +1,14 @@
local fs = require("@lune/fs") 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("bin")
-- Write both of our files -- Write both of our files
fs.writeFile("bin/move_test_binary", binary) fs.writeFile("bin/move_test_binary", utils.binaryBlob)
fs.writeFile("bin/move_test_json.json", json) fs.writeFile("bin/move_test_json.json", utils.jsonBlob)
-- Move / rename them to something else, to test we -- Move / rename them to something else, to test we
-- change the prefix in the names from "move" to "moved" -- 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 -- wrote gets us back the original strings
assert( assert(
fs.readFile("bin/moved_test_binary") == binary, fs.readFile("bin/moved_test_binary") == utils.binaryBlob,
"Binary file round-trip resulted in different strings" "Binary file round-trip resulted in different strings"
) )
assert( 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" "JSON file round-trip resulted in different strings"
) )

21
tests/fs/utils.luau Normal file
View file

@ -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,
}