Skip to content

Commit 9f8ab34

Browse files
committed
add touch
1 parent c150316 commit 9f8ab34

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

lua/plenary/path2.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@ function Path:stat()
531531
return res
532532
end
533533

534+
---@deprecated
535+
function Path:_stat()
536+
return self:stat()
537+
end
538+
534539
--- Get file status. Like `Path:stat` but if the path points to a symbolic
535540
--- link, returns the symbolic link's information.
536541
--- Will throw error if path doesn't exist.
@@ -863,6 +868,22 @@ function Path:touch(opts)
863868
local mode = type(opts.parents) == "number" and opts.parents ---@cast mode number?
864869
_ = Path:new(self:parent()):mkdir { mode = mode, parents = true }
865870
end
871+
872+
local fd, _, err_msg = uv.fs_open(self:absolute(), "w", opts.mode)
873+
if fd == nil then
874+
error(err_msg)
875+
end
876+
877+
local ok
878+
ok, _, err_msg = uv.fs_close(fd)
879+
if not ok then
880+
error(err_msg)
881+
end
882+
883+
return true
884+
end
885+
886+
function Path:rm(opts)
866887
end
867888

868889
return Path

tests/plenary/path2_spec.lua

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,56 @@ describe("Path2", function()
466466
end)
467467
end)
468468

469+
describe("touch/rm", function()
470+
it("can create and delete new files", function()
471+
local p = Path:new "test_file.lua"
472+
assert(pcall(p.touch, p))
473+
assert(p:exists())
474+
475+
p:rm()
476+
assert(not p:exists())
477+
end)
478+
479+
it("does not effect already created files but updates last access", function()
480+
local p = Path:new "README.md"
481+
local last_atime = p:stat().atime.sec
482+
local last_mtime = p:stat().mtime.sec
483+
484+
local lines = p:readlines()
485+
486+
assert(pcall(p.touch, p))
487+
print(p:stat().atime.sec > last_atime)
488+
print(p:stat().mtime.sec > last_mtime)
489+
assert(p:exists())
490+
491+
assert.are.same(lines, p:readlines())
492+
end)
493+
494+
it("does not create dirs if nested in none existing dirs and parents not set", function()
495+
local p = Path:new { "nested", "nested2", "test_file.lua" }
496+
assert(not pcall(p.touch, p, { parents = false }))
497+
assert(not p:exists())
498+
end)
499+
500+
it("does create dirs if nested in none existing dirs", function()
501+
local p1 = Path:new { "nested", "nested2", "test_file.lua" }
502+
local p2 = Path:new { "nested", "asdf", ".hidden" }
503+
local d1 = Path:new { "nested", "dir", ".hidden" }
504+
assert(pcall(p1.touch, p1, { parents = true }))
505+
assert(pcall(p2.touch, p2, { parents = true }))
506+
assert(pcall(d1.mkdir, d1, { parents = true }))
507+
assert(p1:exists())
508+
assert(p2:exists())
509+
assert(d1:exists())
510+
511+
Path:new({ "nested" }):rm { recursive = true }
512+
assert(not p1:exists())
513+
assert(not p2:exists())
514+
assert(not d1:exists())
515+
assert(not Path:new({ "nested" }):exists())
516+
end)
517+
end)
518+
469519
describe("parents", function()
470520
it_cross_plat("should extract the ancestors of the path", function()
471521
local p = Path:new(vim.fn.getcwd())

0 commit comments

Comments
 (0)