@@ -93,3 +93,70 @@ func TestCreateTempDir(t *testing.T) {
9393 assert .NoError (t , os .RemoveAll (tempDir ))
9494 }()
9595}
96+
97+ func TestMoveFile_New (t * testing.T ) {
98+ // Init test
99+ sourcePath , destPath := initMoveTest (t )
100+
101+ // Move file
102+ assert .NoError (t , MoveFile (sourcePath , destPath ))
103+
104+ // Assert expected file paths
105+ assert .FileExists (t , destPath )
106+ assert .NoFileExists (t , sourcePath )
107+ }
108+
109+ func TestMoveFile_Override (t * testing.T ) {
110+ // Init test
111+ sourcePath , destPath := initMoveTest (t )
112+ err := os .WriteFile (destPath , []byte ("dst" ), 0600 )
113+ assert .NoError (t , err )
114+
115+ // Move file
116+ assert .NoError (t , MoveFile (sourcePath , destPath ))
117+
118+ // Assert file overidden
119+ assert .FileExists (t , destPath )
120+ destFileContent , err := os .ReadFile (destPath )
121+ assert .NoError (t , err )
122+ assert .Equal (t , "src" , string (destFileContent ))
123+
124+ // Assert source file removed
125+ assert .NoFileExists (t , sourcePath )
126+ }
127+
128+ func TestMoveFile_NoPerm (t * testing.T ) {
129+ // Init test
130+ sourcePath , destPath := initMoveTest (t )
131+ err := os .WriteFile (destPath , []byte ("dst" ), 0600 )
132+ assert .NoError (t , err )
133+
134+ // Remove all permissions from destination file
135+ assert .NoError (t , os .Chmod (destPath , 0000 ))
136+ _ , err = os .Create (destPath )
137+ assert .Error (t , err )
138+
139+ // Move file
140+ assert .NoError (t , MoveFile (sourcePath , destPath ))
141+
142+ // Assert file overidden
143+ assert .FileExists (t , destPath )
144+ destFileContent , err := os .ReadFile (destPath )
145+ assert .NoError (t , err )
146+ assert .Equal (t , "src" , string (destFileContent ))
147+
148+ // Assert source file removed
149+ assert .NoFileExists (t , sourcePath )
150+ }
151+
152+ func initMoveTest (t * testing.T ) (sourcePath , destPath string ) {
153+ // Create source and destination paths
154+ tmpDir := t .TempDir ()
155+ sourcePath = filepath .Join (tmpDir , "src" )
156+ destPath = filepath .Join (tmpDir , "dst" )
157+
158+ // Write content to source file
159+ err := os .WriteFile (sourcePath , []byte ("src" ), 0600 )
160+ assert .NoError (t , err )
161+ return
162+ }
0 commit comments