Skip to content

Commit

Permalink
Clarify behaviour of readAll at the end of a file
Browse files Browse the repository at this point in the history
This should return an empty string, to match PUC Lua.
  • Loading branch information
SquidDev committed Aug 18, 2024
1 parent 3299d0e commit 34a2fd0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ public Object[] read(Optional<Integer> countArg) throws LuaException {
/**
* Read the remainder of the file.
*
* @return The file, or {@code null} if at the end of it.
* @return The remaining contents of the file, or {@code null} in the event of an error.
* @throws LuaException If the file has been closed.
* @cc.treturn string|nil The remaining contents of the file, or {@code nil} if we are at the end.
* @cc.treturn string|nil The remaining contents of the file, or {@code nil} in the event of an error.
* @cc.since 1.80pr1
*/
@Nullable
Expand Down
18 changes: 18 additions & 0 deletions projects/core/src/test/resources/test-rom/spec/apis/fs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ describe("The fs library", function()
handle.close()
end)

it("reading an empty file returns nil", function()
local file = create_test_file ""

local handle = fs.open(file, mode)
expect(handle.read()):eq(nil)
handle.close()
end)

it("can read a line of text", function()
local file = create_test_file "some\nfile\r\ncontents\n\n"

Expand All @@ -223,6 +231,16 @@ describe("The fs library", function()
expect(handle.readLine(true)):eq(nil)
handle.close()
end)

it("readAll always returns a string", function()
local contents = "some\nfile\ncontents"
local file = create_test_file "some\nfile\ncontents"

local handle = fs.open(file, mode)
expect(handle.readAll()):eq(contents)
expect(handle.readAll()):eq("")
handle.close()
end)
end

describe("reading", function()
Expand Down

0 comments on commit 34a2fd0

Please sign in to comment.