Skip to content

Commit 8849592

Browse files
committed
feat(utils): add optional parameter opts to writefile()
1 parent c944f9c commit 8849592

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lua/orgmode/utils/init.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ end
5555

5656
---@param file string
5757
---@param data string|string[]
58+
---@param opts? {excl?: boolean}
5859
---@return OrgPromise<integer> bytes
59-
function utils.writefile(file, data)
60+
function utils.writefile(file, data, opts)
6061
return Promise.new(function(resolve, reject)
61-
uv.fs_open(file, 'w', 438, function(err1, fd)
62+
local flags = opts and opts.excl and 'wx' or 'w'
63+
uv.fs_open(file, flags, 438, function(err1, fd)
6264
if err1 then
6365
return reject(err1)
6466
end

tests/plenary/utils_spec.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,15 @@ describe('Util', function()
124124
local msg = err:sub(#err - #expected)
125125
assert.are.equal(expected, msg)
126126
end)
127+
128+
it('allows no-clobber writes', function()
129+
local promise = utils.writefile(filename, contents, { excl = true })
130+
---@type boolean, string?
131+
local ok, err = pcall(promise.wait, promise)
132+
assert.is.False(ok)
133+
assert(err)
134+
local expected = 'EEXIST: file already exists: ' .. filename
135+
assert.are.equal(expected, err)
136+
end)
127137
end)
128138
end)

0 commit comments

Comments
 (0)