@@ -1326,3 +1326,194 @@ func TestHasQuery(t *testing.T) {
13261326 }
13271327 }
13281328}
1329+
1330+ func TestMergeMove_SourceDoesNotExist (t * testing.T ) {
1331+ src := New ("nonexistent.txt" )
1332+ dst := New ("dst.txt" )
1333+ err := src .MergeMove (dst )
1334+ if err == nil {
1335+ t .Fatal ("expected error for non-existent source, got nil" )
1336+ }
1337+ }
1338+
1339+ func TestMergeMove_MoveFileToNonExistentDst (t * testing.T ) {
1340+ // Create a temporary file as source
1341+ tempDir := t .TempDir ()
1342+ srcPath := New (filepath .Join (tempDir , "src.txt" ))
1343+ dstPath := New (filepath .Join (tempDir , "dst.txt" ))
1344+
1345+ // Write some content to the source file
1346+ if err := srcPath .WriteFile ([]byte ("merge move test" )); err != nil {
1347+ t .Fatalf ("WriteFile: %v" , err )
1348+ }
1349+
1350+ // Ensure destination does not exist
1351+ os .Remove (dstPath .String ())
1352+
1353+ // Call MergeMove, should perform a rename
1354+ if err := srcPath .MergeMove (dstPath ); err != nil {
1355+ t .Fatalf ("MergeMove: %v" , err )
1356+ }
1357+
1358+ // Source should no longer exist and destination file should now exist
1359+ if srcPath .Exists () {
1360+ t .Errorf ("expected source file to be moved (non-existent)" )
1361+ }
1362+ if ! dstPath .Exists () {
1363+ t .Errorf ("expected destination file to exist" )
1364+ }
1365+ }
1366+
1367+ func TestMergeMove_MoveFileToExistingDirectory (t * testing.T ) {
1368+ // Create a temporary file as source and a directory as destination
1369+ tempDir := t .TempDir ()
1370+ srcPath := New (filepath .Join (tempDir , "src.txt" ))
1371+ dstDir := New (filepath .Join (tempDir , "destDir" ))
1372+
1373+ // Write content to the source file
1374+ if err := srcPath .WriteFile ([]byte ("file to dir" )); err != nil {
1375+ t .Fatalf ("WriteFile: %v" , err )
1376+ }
1377+ // Create the destination directory
1378+ if err := dstDir .MkdirIfNotExist (); err != nil {
1379+ t .Fatalf ("MkdirIfNotExist: %v" , err )
1380+ }
1381+
1382+ // Call MergeMove; it should move src file inside dst directory
1383+ if err := srcPath .MergeMove (dstDir ); err != nil {
1384+ t .Fatalf ("MergeMove: %v" , err )
1385+ }
1386+
1387+ // The moved file should now be at dstDir joined with base name of srcPath.
1388+ movedFile := dstDir .JoinPath (srcPath .Base ())
1389+ if srcPath .Exists () {
1390+ t .Errorf ("expected source file to be moved" )
1391+ }
1392+ if ! movedFile .Exists () {
1393+ t .Errorf ("expected moved file (%s) to exist" , movedFile .String ())
1394+ }
1395+ }
1396+
1397+ func TestMergeMove_MoveFileToExistingFile (t * testing.T ) {
1398+ tempDir := t .TempDir ()
1399+ // Create a source and destination file paths
1400+ srcPath := New (filepath .Join (tempDir , "src.txt" ))
1401+ dstPath := New (filepath .Join (tempDir , "dst.txt" ))
1402+
1403+ // Write different contents to source and destination
1404+ srcContent := []byte ("source content" )
1405+ dstContent := []byte ("old destination" )
1406+
1407+ if err := srcPath .WriteFile (srcContent ); err != nil {
1408+ t .Fatalf ("WriteFile src: %v" , err )
1409+ }
1410+ if err := dstPath .WriteFile (dstContent ); err != nil {
1411+ t .Fatalf ("WriteFile dst: %v" , err )
1412+ }
1413+
1414+ // Call MergeMove: it should delete the destination and rename the source to dstPath.
1415+ if err := srcPath .MergeMove (dstPath ); err != nil {
1416+ t .Fatalf ("MergeMove: %v" , err )
1417+ }
1418+
1419+ // Source should not exist, and destination should have source content.
1420+ if srcPath .Exists () {
1421+ t .Errorf ("expected source file to be moved" )
1422+ }
1423+ if ! dstPath .Exists () {
1424+ t .Errorf ("expected destination file to exist" )
1425+ }
1426+ result , err := dstPath .ReadFile ()
1427+ if err != nil {
1428+ t .Fatalf ("ReadFile: %v" , err )
1429+ }
1430+ if string (result ) != string (srcContent ) {
1431+ t .Errorf ("expected destination file content %q, got %q" , srcContent , result )
1432+ }
1433+ }
1434+
1435+ func TestMergeMove_MergeMoveDirectory (t * testing.T ) {
1436+ tempDir := t .TempDir ()
1437+ // Create source directory with multiple files
1438+ srcDir := New (filepath .Join (tempDir , "srcDir" ))
1439+ if err := srcDir .MkdirIfNotExist (); err != nil {
1440+ t .Fatalf ("MkdirIfNotExist srcDir: %v" , err )
1441+ }
1442+
1443+ // Create a couple of files inside source directory
1444+ file1 := srcDir .Join ("file1.txt" )
1445+ file2 := srcDir .Join ("file2.txt" )
1446+ if err := file1 .WriteFile ([]byte ("file1 content" )); err != nil {
1447+ t .Fatalf ("WriteFile file1: %v" , err )
1448+ }
1449+ if err := file2 .WriteFile ([]byte ("file2 content" )); err != nil {
1450+ t .Fatalf ("WriteFile file2: %v" , err )
1451+ }
1452+
1453+ // Create destination directory where the merge will occur; it already exists
1454+ dstDir := New (filepath .Join (tempDir , "dstDir" ))
1455+ if err := dstDir .MkdirIfNotExist (); err != nil {
1456+ t .Fatalf ("MkdirIfNotExist dstDir: %v" , err )
1457+ }
1458+
1459+ // MergeMove srcDir to dstDir; expect the files to be moved into dstDir
1460+ if err := srcDir .MergeMove (dstDir ); err != nil {
1461+ t .Fatalf ("MergeMove directory: %v" , err )
1462+ }
1463+
1464+ // Source directory should be deleted.
1465+ if srcDir .Exists () {
1466+ t .Errorf ("expected source directory to be deleted" )
1467+ }
1468+
1469+ // Files should now exist in dstDir.
1470+ movedFile1 := dstDir .Join ("file1.txt" )
1471+ movedFile2 := dstDir .Join ("file2.txt" )
1472+ if ! movedFile1 .Exists () {
1473+ t .Errorf ("expected moved file1 (%s) to exist" , movedFile1 .String ())
1474+ }
1475+ if ! movedFile2 .Exists () {
1476+ t .Errorf ("expected moved file2 (%s) to exist" , movedFile2 .String ())
1477+ }
1478+
1479+ // Verify contents
1480+ f1Content , err := movedFile1 .ReadFile ()
1481+ if err != nil {
1482+ t .Fatalf ("ReadFile file1: %v" , err )
1483+ }
1484+ if string (f1Content ) != "file1 content" {
1485+ t .Errorf ("expected file1 content %q, got %q" , "file1 content" , f1Content )
1486+ }
1487+ f2Content , err := movedFile2 .ReadFile ()
1488+ if err != nil {
1489+ t .Fatalf ("ReadFile file2: %v" , err )
1490+ }
1491+ if string (f2Content ) != "file2 content" {
1492+ t .Errorf ("expected file2 content %q, got %q" , "file2 content" , f2Content )
1493+ }
1494+ }
1495+
1496+ func TestMergeMove_DirectoryToNonDirectory (t * testing.T ) {
1497+ tempDir := t .TempDir ()
1498+ // Create source directory with one file
1499+ srcDir := New (filepath .Join (tempDir , "srcDir" ))
1500+ if err := srcDir .MkdirIfNotExist (); err != nil {
1501+ t .Fatalf ("MkdirIfNotExist srcDir: %v" , err )
1502+ }
1503+ srcFile := srcDir .Join ("file.txt" )
1504+ if err := srcFile .WriteFile ([]byte ("content" )); err != nil {
1505+ t .Fatalf ("WriteFile: %v" , err )
1506+ }
1507+
1508+ // Create a destination regular file
1509+ dstPath := New (filepath .Join (tempDir , "dst.txt" ))
1510+ if err := dstPath .WriteFile ([]byte ("destination" )); err != nil {
1511+ t .Fatalf ("WriteFile dst: %v" , err )
1512+ }
1513+
1514+ // Attempting to merge move a directory into a non-directory should error.
1515+ err := srcDir .MergeMove (dstPath )
1516+ if err == nil {
1517+ t .Fatal ("expected error when moving directory to non-directory, got nil" )
1518+ }
1519+ }
0 commit comments