Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/host/os_writefile_ifnotequal.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int os_writefile_ifnotequal(lua_State* L)

if (!error)
{
lua_pushinteger(L, error ? -1 : 0);
lua_pushinteger(L, 1);
Comment thread
mercury233 marked this conversation as resolved.
return 1;
}
}
Expand Down
58 changes: 58 additions & 0 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,18 @@
-- Helpers
--

-- Save the real functions before test runner installs its stubs.
local real_writefile_ifnotequal = os.writefile_ifnotequal
local real_io_open = io.open

local function real_readfile(filepath)
local f = real_io_open(filepath, "rb")
if not f then return nil end
local content = f:read("*a")
f:close()
return content
end

local tmpname = function()
local p = os.tmpname()
if p:startswith("\\") then
Expand All @@ -568,6 +580,52 @@
end


--
-- os.writefile_ifnotequal() tests.
--

function suite.writefile_ifnotequal_WritesNewFile()
local filepath = tmpname()
local content = "Hello, World!"
local ok, err = real_writefile_ifnotequal(content, filepath)
test.isequal(1, ok)
test.isnil(err)
test.istrue(os.isfile(filepath))
test.isequal(content, real_readfile(filepath))
os.remove(filepath)
end

function suite.writefile_ifnotequal_SkipsIdenticalContent()
local filepath = tmpname()
local content = "Identical content"
-- Write the file first
local ok1, err1 = real_writefile_ifnotequal(content, filepath)
test.isequal(1, ok1)
test.isnil(err1)
-- Try to write identical content
local ok2, err2 = real_writefile_ifnotequal(content, filepath)
test.isequal(0, ok2)
test.isnil(err2)
test.isequal(content, real_readfile(filepath))
os.remove(filepath)
end

function suite.writefile_ifnotequal_UpdatesWhenContentDiffers()
local filepath = tmpname()
local content1 = "First content"
local content2 = "Second content"
-- Write the file first
local ok1, err1 = real_writefile_ifnotequal(content1, filepath)
test.isequal(1, ok1)
test.isnil(err1)
-- Write different content
local ok2, err2 = real_writefile_ifnotequal(content2, filepath)
test.isequal(1, ok2)
test.isnil(err2)
test.isequal(content2, real_readfile(filepath))
os.remove(filepath)
end

--
-- os.remove() tests.
--
Expand Down
2 changes: 1 addition & 1 deletion website/docs/os/os.writefile_ifnotequal.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ok, err = os.writefile_ifnotequal("text", "filename")

### Return Value ###

True if successful, otherwise nil and an error message.
`1` if successful, `0` if the string is identical to the current file contents, otherwise `-1` and an error message.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest changing this to be a table

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`1` if successful, `0` if the string is identical to the current file contents, otherwise `-1` and an error message.
The first return value:
| Value | Explanation |
|-------|------------------------------------------------------|
| 1 | The string was written to the file |
| 0 | The string is identical to the current file contents |
| -1 | An error occurred |
The second return value is an error message if the first return value is -1, otherwise nil.

should it be like this?



### Availability ###
Expand Down
Loading