lune/tests/fs/files.luau

55 lines
1.9 KiB
Text
Raw Normal View History

2023-07-20 19:06:42 +01:00
local TEMP_DIR_PATH = "bin/"
local TEMP_ROOT_PATH = TEMP_DIR_PATH .. "fs_files_test"
local fs = require("@lune/fs")
2023-07-20 19:06:42 +01:00
local utils = require("./utils")
2023-07-20 19:06:42 +01:00
-- Make sure our bin dir exists
2023-01-21 07:01:46 +00:00
2023-07-20 19:06:42 +01:00
fs.writeDir(TEMP_DIR_PATH)
fs.writeDir(TEMP_ROOT_PATH)
2023-01-21 07:01:46 +00:00
-- Write both of our files
-- binaryBlob is of type buffer to make sure fs.writeFile
-- works with both strings and buffers
2023-07-20 19:06:42 +01:00
fs.writeFile(TEMP_ROOT_PATH .. "/test_binary", utils.binaryBlob)
fs.writeFile(TEMP_ROOT_PATH .. "/test_json.json", utils.jsonBlob)
2023-01-21 07:01:46 +00:00
-- Make sure reading the file we just
-- wrote gets us back the original strings
assert(
fs.readFile(TEMP_ROOT_PATH .. "/test_binary") == buffer.tostring(utils.binaryBlob),
2023-01-21 07:01:46 +00:00
"Binary file round-trip resulted in different strings"
)
assert(
2023-07-20 19:06:42 +01:00
fs.readFile(TEMP_ROOT_PATH .. "/test_json.json") == utils.jsonBlob,
2023-01-21 07:01:46 +00:00
"JSON file round-trip resulted in different strings"
)
-- Make sure file checks succeed but dir checks fail
2023-07-20 19:06:42 +01:00
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")
2023-01-21 07:01:46 +00:00
2023-07-20 19:06:42 +01:00
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")
2023-01-21 07:01:46 +00:00
-- Remove the files and make sure
-- the APIs say they no longer exist
2023-07-20 19:06:42 +01:00
fs.removeFile(TEMP_ROOT_PATH .. "/test_binary")
fs.removeFile(TEMP_ROOT_PATH .. "/test_json.json")
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(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")
2023-01-21 07:01:46 +00:00
2023-07-20 19:06:42 +01:00
-- Remove the testing dir specific to this test
2023-01-21 07:01:46 +00:00
2023-07-20 19:06:42 +01:00
fs.removeDir(TEMP_ROOT_PATH)