@@ -546,6 +546,95 @@ describe("Path2", function()
546
546
end )
547
547
end )
548
548
549
+ describe (" rename" , function ()
550
+ after_each (function ()
551
+ uv .fs_unlink " a_random_filename.lua"
552
+ uv .fs_unlink " not_a_random_filename.lua"
553
+ uv .fs_unlink " some_random_filename.lua"
554
+ uv .fs_unlink " ../some_random_filename.lua"
555
+ end )
556
+
557
+ it_cross_plat (" can rename a file" , function ()
558
+ local p = Path :new " a_random_filename.lua"
559
+ assert .no_error (function ()
560
+ p :touch ()
561
+ end )
562
+ assert .is_true (p :exists ())
563
+
564
+ local new_p
565
+ assert .no_error (function ()
566
+ new_p = p :rename { new_name = " not_a_random_filename.lua" }
567
+ end )
568
+ assert .not_nil (new_p )
569
+ assert .are .same (" not_a_random_filename.lua" , new_p .name )
570
+ end )
571
+
572
+ it_cross_plat (" can handle an invalid filename" , function ()
573
+ local p = Path :new " some_random_filename.lua"
574
+ assert .no_error (function ()
575
+ p :touch ()
576
+ end )
577
+ assert .is_true (p :exists ())
578
+
579
+ assert .has_error (function ()
580
+ p :rename { new_name = " " }
581
+ end )
582
+ assert .has_error (function ()
583
+ --- @diagnostic disable-next-line : missing-fields
584
+ p :rename {}
585
+ end )
586
+
587
+ assert .are .same (" some_random_filename.lua" , p .name )
588
+ end )
589
+
590
+ it_cross_plat (" can move to parent dir" , function ()
591
+ local p = Path :new " some_random_filename.lua"
592
+ assert .no_error (function ()
593
+ p :touch ()
594
+ end )
595
+ assert .is_true (p :exists ())
596
+
597
+ local new_p
598
+ assert .no_error (function ()
599
+ new_p = p :rename { new_name = " ../some_random_filename.lua" }
600
+ end )
601
+ assert .not_nil (new_p )
602
+ assert .are .same (Path :new (" ../some_random_filename.lua" ):absolute (), new_p :absolute ())
603
+ end )
604
+
605
+ it_cross_plat (" cannot rename to an existing filename" , function ()
606
+ local p1 = Path :new " a_random_filename.lua"
607
+ local p2 = Path :new " not_a_random_filename.lua"
608
+ assert .no_error (function ()
609
+ p1 :touch ()
610
+ p2 :touch ()
611
+ end )
612
+ assert .is_true (p1 :exists ())
613
+ assert .is_true (p2 :exists ())
614
+
615
+ assert .has_error (function ()
616
+ p1 :rename { new_name = " not_a_random_filename.lua" }
617
+ end )
618
+ assert .are .same (p1 .filename , " a_random_filename.lua" )
619
+ end )
620
+
621
+ it_cross_plat (" handles Path as new_name" , function ()
622
+ local p1 = Path :new " a_random_filename.lua"
623
+ local p2 = Path :new " not_a_random_filename.lua"
624
+ assert .no_error (function ()
625
+ p1 :touch ()
626
+ end )
627
+ assert .is_true (p1 :exists ())
628
+
629
+ local new_p
630
+ assert .no_error (function ()
631
+ new_p = p1 :rename { new_name = p2 }
632
+ end )
633
+ assert .not_nil (new_p )
634
+ assert .are .same (" not_a_random_filename.lua" , new_p .name )
635
+ end )
636
+ end )
637
+
549
638
describe (" parents" , function ()
550
639
it_cross_plat (" should extract the ancestors of the path" , function ()
551
640
local p = Path :new (vim .fn .getcwd ())
0 commit comments