lune/tests/fs/metadata.luau

71 lines
1.9 KiB
Lua
Raw Normal View History

2023-07-20 11:25:36 +01:00
local TEMP_DIR_PATH = "bin/"
local TEMP_FILE_PATH = TEMP_DIR_PATH .. "metadata_test"
local fs = require("@lune/fs")
local task = require("@lune/task")
-- Generate test data & 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)
if fs.isFile(TEMP_FILE_PATH) then
fs.removeFile(TEMP_FILE_PATH)
end
--[[
1. File should initially not exist
2. Write the file
3. File should now exist
]]
assert(not fs.metadata(TEMP_FILE_PATH).exists, "File metadata not exists failed")
fs.writeFile(TEMP_FILE_PATH, binary)
assert(fs.metadata(TEMP_FILE_PATH).exists, "File metadata exists failed")
--[[
1. Kind should be `dir` for our temp directory
2. Kind should be `file` for our temp file
]]
local metaDir = fs.metadata(TEMP_DIR_PATH)
local metaFile = fs.metadata(TEMP_FILE_PATH)
assert(metaDir.kind == "dir", "Dir metadata kind was invalid")
assert(metaFile.kind == "file", "File metadata kind was invalid")
--[[
1. Capture initial metadata
2. Wait for a bit so that timestamps can change
3. Write the file, with an extra newline
4. Metadata changed timestamp should be different
5. Metadata created timestamp should be the same different
]]
local metaBefore = fs.metadata(TEMP_FILE_PATH)
task.wait()
fs.writeFile(TEMP_FILE_PATH, binary .. "\n")
local metaAfter = fs.metadata(TEMP_FILE_PATH)
assert(
metaAfter.modifiedAt ~= metaBefore.modifiedAt,
"File metadata change timestamp did not change"
)
assert(
metaAfter.createdAt == metaBefore.createdAt,
"File metadata creation timestamp changed from modification"
)
--[[
1. Permissions should exist
2. Our newly created file should not be readonly
]]
assert(metaAfter.permissions ~= nil, "File metadata permissions are missing")
assert(not metaAfter.permissions.readOnly, "File metadata permissions are readonly")
-- Finally, clean up after us for any subsequent tests
fs.removeFile(TEMP_FILE_PATH)