11package fs
22
33import (
4+ "fmt"
45 "os"
56 "path/filepath"
67 "testing"
78)
89
9- func TestCopyFile (t * testing.T ) {
10+ var renameFunc = os .Rename
11+
12+ func TestCopyFile_Success (t * testing.T ) {
1013 tempDir := t .TempDir ()
1114
1215 srcFile := filepath .Join (tempDir , "source.txt" )
1316 dstFile := filepath .Join (tempDir , "dest.txt" )
1417
1518 content := []byte ("test file content" )
1619
17- // Create source file
1820 if err := os .WriteFile (srcFile , content , 0644 ); err != nil {
1921 t .Fatalf ("failed to write source file: %v" , err )
2022 }
2123
22- // Perform copy operation
2324 if err := CopyFile (srcFile , dstFile ); err != nil {
2425 t .Fatalf ("CopyFile failed: %v" , err )
2526 }
2627
27- // Verify destination file content
2828 read , err := os .ReadFile (dstFile )
2929 if err != nil {
30- t .Fatalf ("failed to read destination file: %v" , err )
30+ t .Fatalf ("failed to read dest file: %v" , err )
3131 }
3232
3333 if string (read ) != string (content ) {
3434 t .Errorf ("content mismatch: expected %q, got %q" , content , read )
3535 }
3636}
3737
38- func TestMoveDir (t * testing.T ) {
38+ func TestCopyFile_FailureScenarios (t * testing.T ) {
39+ tempDir := t .TempDir ()
40+
41+ t .Run ("source does not exist" , func (t * testing.T ) {
42+ err := CopyFile (filepath .Join (tempDir , "no-source.txt" ), filepath .Join (tempDir , "dest.txt" ))
43+ if err == nil {
44+ t .Errorf ("expected error for missing source file, got nil" )
45+ }
46+ })
47+
48+ t .Run ("source is not regular file" , func (t * testing.T ) {
49+ dirPath := filepath .Join (tempDir , "some-dir" )
50+ if err := os .Mkdir (dirPath , 0755 ); err != nil {
51+ t .Fatalf ("failed to create dir: %v" , err )
52+ }
53+ err := CopyFile (dirPath , filepath .Join (tempDir , "dest.txt" ))
54+ if err == nil {
55+ t .Errorf ("expected error for non-regular source file, got nil" )
56+ }
57+ })
58+ }
59+
60+ func TestMoveDir_Success (t * testing.T ) {
3961 tempDir := t .TempDir ()
4062
4163 sourceDir := filepath .Join (tempDir , "source" )
4264 destDir := filepath .Join (tempDir , "dest" )
4365
44- // Create source directory
4566 if err := os .MkdirAll (sourceDir , 0755 ); err != nil {
46- t .Fatalf ("failed to create source directory : %v" , err )
67+ t .Fatalf ("failed to create source dir : %v" , err )
4768 }
4869
4970 testFile := filepath .Join (sourceDir , "test.txt" )
5071 if err := os .WriteFile (testFile , []byte ("move test" ), 0644 ); err != nil {
5172 t .Fatalf ("failed to write test file: %v" , err )
5273 }
5374
54- // Perform move operation
5575 if err := MoveDir (sourceDir , destDir ); err != nil {
5676 t .Fatalf ("MoveDir failed: %v" , err )
5777 }
5878
59- // Verify source directory was removed
6079 if _ , err := os .Stat (sourceDir ); ! os .IsNotExist (err ) {
61- t .Errorf ("source directory still exists after move" )
80+ t .Errorf ("source dir still exists after move" )
6281 }
6382
64- // Verify file was moved correctly
6583 read , err := os .ReadFile (filepath .Join (destDir , "test.txt" ))
6684 if err != nil {
6785 t .Fatalf ("failed to read moved file: %v" , err )
@@ -71,3 +89,36 @@ func TestMoveDir(t *testing.T) {
7189 t .Errorf ("content mismatch: expected 'move test', got %q" , string (read ))
7290 }
7391}
92+
93+ // simulate copyDir failure by mocking filepath.Walk (advanced scenario - optional in real pipelines)
94+
95+ func TestMoveDir_FallbackCrossDevice (t * testing.T ) {
96+ // here we simulate EXDEV manually to trigger the fallback
97+ tempDir := t .TempDir ()
98+
99+ sourceDir := filepath .Join (tempDir , "source" )
100+ destDir := filepath .Join (tempDir , "dest" )
101+
102+ if err := os .MkdirAll (sourceDir , 0755 ); err != nil {
103+ t .Fatalf ("failed to create source dir: %v" , err )
104+ }
105+
106+ testFile := filepath .Join (sourceDir , "test.txt" )
107+ if err := os .WriteFile (testFile , []byte ("move test" ), 0644 ); err != nil {
108+ t .Fatalf ("failed to write test file: %v" , err )
109+ }
110+
111+ // replace os.Rename temporarily to simulate EXDEV
112+ originalRename := renameFunc
113+ defer func () { renameFunc = originalRename }()
114+ renameFunc = func (_ , _ string ) error {
115+ return fmt .Errorf ("simulated rename error" )
116+ }
117+ if err := MoveDir (sourceDir , destDir ); err != nil {
118+ t .Fatalf ("MoveDir fallback failed: %v" , err )
119+ }
120+
121+ if _ , err := os .Stat (sourceDir ); ! os .IsNotExist (err ) {
122+ t .Errorf ("source dir still exists after fallback move" )
123+ }
124+ }
0 commit comments