@@ -466,6 +466,56 @@ describe("Path2", function()
466
466
end )
467
467
end )
468
468
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
+
469
519
describe (" parents" , function ()
470
520
it_cross_plat (" should extract the ancestors of the path" , function ()
471
521
local p = Path :new (vim .fn .getcwd ())
0 commit comments