From a1b11345a12290c8000b099f1b797edf85949539 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sun, 21 Jan 2024 11:06:36 +0530 Subject: [PATCH] feat(types + tests): updated types and tests for @lune/fs --- tests/fs/files.luau | 23 ++++++++++++++++++----- types/fs.luau | 4 ++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/tests/fs/files.luau b/tests/fs/files.luau index a49412e..f9b2611 100644 --- a/tests/fs/files.luau +++ b/tests/fs/files.luau @@ -9,22 +9,35 @@ local utils = require("./utils") fs.writeDir(TEMP_DIR_PATH) fs.writeDir(TEMP_ROOT_PATH) --- Write both of our files +-- Write both of our files with string and buffer contents fs.writeFile(TEMP_ROOT_PATH .. "/test_binary", utils.binaryBlob) fs.writeFile(TEMP_ROOT_PATH .. "/test_json.json", utils.jsonBlob) +fs.writeFile(TEMP_ROOT_PATH .. "/test_binary_buf", buffer.fromstring(utils.binaryBlob)) +fs.writeFile(TEMP_ROOT_PATH .. "/test_json_buf.json", buffer.fromstring(utils.jsonBlob)) + -- Make sure reading the file we just -- wrote gets us back the original strings assert( - fs.readFile(TEMP_ROOT_PATH .. "/test_binary") == utils.binaryBlob, - "Binary file round-trip resulted in different strings" + buffer.tostring(fs.readFile(TEMP_ROOT_PATH .. "/test_binary")) == utils.binaryBlob, + "(String) Binary file round-trip resulted in different strings" ) assert( - fs.readFile(TEMP_ROOT_PATH .. "/test_json.json") == utils.jsonBlob, - "JSON file round-trip resulted in different strings" + buffer.tostring(fs.readFile(TEMP_ROOT_PATH .. "/test_json.json")) == utils.jsonBlob, + "(String) JSON file round-trip resulted in different strings" +) + +assert( + buffer.tostring(fs.readFile(TEMP_ROOT_PATH .. "/test_binary_buf")) == utils.binaryBlob, + "(Buffer) Binary file round-trip resulted in different strings" +) + +assert( + buffer.tostring(fs.readFile(TEMP_ROOT_PATH .. "/test_json_buf.json")) == utils.jsonBlob, + "(Buffer) JSON file round-trip resulted in different strings" ) -- Make sure file checks succeed but dir checks fail diff --git a/types/fs.luau b/types/fs.luau index d81ce6d..1c33ac5 100644 --- a/types/fs.luau +++ b/types/fs.luau @@ -107,7 +107,7 @@ local fs = {} @param path The path to the file to read @return The contents of the file ]=] -function fs.readFile(path: string): string +function fs.readFile(path: string): buffer return nil :: any end @@ -144,7 +144,7 @@ end @param path The path of the file @param contents The contents of the file ]=] -function fs.writeFile(path: string, contents: string) end +function fs.writeFile(path: string, contents: buffer | string) end --[=[ @within FS