lune/tests/fs/copy.luau

69 lines
2 KiB
Lua
Raw Normal View History

2023-07-20 18:29:21 +01:00
local TEMP_DIR_PATH = "bin/"
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")
2023-07-20 19:06:42 +01:00
local utils = require("./utils")
2023-07-20 18:29:21 +01:00
2023-07-20 19:06:42 +01:00
-- Make sure our bin dir exists
2023-07-20 18:29:21 +01:00
fs.writeDir(TEMP_DIR_PATH)
if fs.isDir(TEMP_ROOT_PATH) then
fs.removeDir(TEMP_ROOT_PATH)
end
if fs.isDir(TEMP_ROOT_PATH_2) then
fs.removeDir(TEMP_ROOT_PATH_2)
end
--[[
Create a file structure like this:
-> fs_copy_test
-- -> foo (dir)
-- -- -> bar (dir)
-- -- -- -> baz (file)
-- -- -> fizz (file)
-- -- -> buzz (file)
]]
fs.writeDir(TEMP_ROOT_PATH)
fs.writeDir(TEMP_ROOT_PATH .. "/foo")
fs.writeDir(TEMP_ROOT_PATH .. "/foo/bar")
2023-07-20 19:06:42 +01:00
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)
2023-07-20 18:29:21 +01:00
-- Copy the entire structure
fs.copy(TEMP_ROOT_PATH, TEMP_ROOT_PATH_2)
-- Verify the copied structure
assert(fs.isDir(TEMP_ROOT_PATH_2), "Missing copied dir - root/")
assert(fs.isDir(TEMP_ROOT_PATH_2 .. "/foo"), "Missing copied dir - root/foo/")
assert(fs.isDir(TEMP_ROOT_PATH_2 .. "/foo/bar"), "Missing copied dir - root/foo/bar/")
assert(fs.isFile(TEMP_ROOT_PATH_2 .. "/foo/bar/baz"), "Missing copied file - root/foo/bar/baz")
assert(fs.isFile(TEMP_ROOT_PATH_2 .. "/foo/fizz"), "Missing copied file - root/foo/fizz")
assert(fs.isFile(TEMP_ROOT_PATH_2 .. "/foo/buzz"), "Missing copied file - root/foo/buzz")
-- Make sure the copied files are correct
assert(
2023-07-20 19:06:42 +01:00
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/bar/baz") == utils.binaryBlob,
2023-07-20 18:29:21 +01:00
"Invalid copied file - root/foo/bar/baz"
)
assert(
2023-07-20 19:06:42 +01:00
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/fizz") == utils.binaryBlob,
2023-07-20 18:29:21 +01:00
"Invalid copied file - root/foo/fizz"
)
assert(
2023-07-20 19:06:42 +01:00
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/buzz") == utils.binaryBlob,
2023-07-20 18:29:21 +01:00
"Invalid copied file - root/foo/buzz"
)
-- Finally, clean up after us for any subsequent tests
fs.removeDir(TEMP_ROOT_PATH)
fs.removeDir(TEMP_ROOT_PATH_2)