robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
af23d17388
master
1" Test rename() 2 3source shared.vim 4 5func Test_rename_file_to_file () 6call writefile ([ 'foo' ], 'Xrename1' ) 7 8call assert_equal ( 0 , rename ( 'Xrename1' , 'Xrename2' )) 9 10call assert_equal ( '' , glob ( 'Xrename1' )) 11call assert_equal ([ 'foo' ], readfile ( 'Xrename2' )) 12 13" When the destination file already exists, it should be overwritten. 14call writefile ([ 'foo' ], 'Xrename1' ) 15call writefile ([ 'bar' ], 'Xrename2' ) 16 17call assert_equal ( 0 , rename ( 'Xrename1' , 'Xrename2' )) 18call assert_equal ( '' , glob ( 'Xrename1' )) 19call assert_equal ([ 'foo' ], readfile ( 'Xrename2' )) 20 21call delete ( 'Xrename2' ) 22endfunc 23 24func Test_rename_file_ignore_case () 25" With 'fileignorecase', renaming file will go through a temp file 26" when the source and destination file only differ by case. 27set fileignorecase 28call writefile ([ 'foo' ], 'Xrename' ) 29 30call assert_equal ( 0 , 'Xrename' ->rename ( 'XRENAME' )) 31 32call assert_equal ([ 'foo' ], readfile ( 'XRENAME' )) 33 34set fileignorecase & 35call delete ( 'XRENAME' ) 36endfunc 37 38func Test_rename_same_file () 39call writefile ([ 'foo' ], 'Xrename' ) 40 41" When the source and destination are the same file, nothing 42" should be done. The source file should not be deleted. 43call assert_equal ( 0 , rename ( 'Xrename' , 'Xrename' )) 44call assert_equal ([ 'foo' ], readfile ( 'Xrename' )) 45 46call assert_equal ( 0 , rename ( './Xrename' , 'Xrename' )) 47call assert_equal ([ 'foo' ], readfile ( 'Xrename' )) 48 49call delete ( 'Xrename' ) 50endfunc 51 52func Test_rename_dir_to_dir () 53call mkdir ( 'Xrenamedir1' ) 54call writefile ([ 'foo' ], 'Xrenamedir1/Xrenamefile' ) 55 56call assert_equal ( 0 , rename ( 'Xrenamedir1' , 'Xrenamedir2' )) 57 58call assert_equal ( '' , glob ( 'Xrenamedir1' )) 59call assert_equal ([ 'foo' ], readfile ( 'Xrenamedir2/Xrenamefile' )) 60 61call delete ( 'Xrenamedir2/Xrenamefile' ) 62call delete ( 'Xrenamedir2' , 'd' ) 63endfunc 64 65func Test_rename_same_dir () 66call mkdir ( 'Xrenamedir' ) 67call writefile ([ 'foo' ], 'Xrenamedir/Xrenamefile' ) 68 69call assert_equal ( 0 , rename ( 'Xrenamedir' , 'Xrenamedir' )) 70 71call assert_equal ([ 'foo' ], readfile ( 'Xrenamedir/Xrenamefile' )) 72 73call delete ( 'Xrenamedir/Xrenamefile' ) 74call delete ( 'Xrenamedir' , 'd' ) 75endfunc 76 77func Test_rename_copy () 78" Check that when original file can't be deleted, rename() 79" still succeeds but copies the file. 80call mkdir ( 'Xrenamedir' ) 81call writefile ([ 'foo' ], 'Xrenamedir/Xrenamefile' ) 82call setfperm ( 'Xrenamedir' , 'r-xr-xr-x' ) 83 84call assert_equal ( 0 , rename ( 'Xrenamedir/Xrenamefile' , 'Xrenamefile' )) 85 86if ! has ( 'win32' ) && ! IsRoot () 87" On Windows, the source file is removed despite 88" its directory being made not writable. 89call assert_equal ([ 'foo' ], readfile ( 'Xrenamedir/Xrenamefile' )) 90endif 91call assert_equal ([ 'foo' ], readfile ( 'Xrenamefile' )) 92 93call setfperm ( 'Xrenamedir' , 'rwxrwxrwx' ) 94call delete ( 'Xrenamedir/Xrenamefile' ) 95call delete ( 'Xrenamedir' , 'd' ) 96call delete ( 'Xrenamefile' ) 97endfunc 98 99func Test_rename_fails () 100call writefile ([ 'foo' ], 'Xrenamefile' ) 101 102" Can't rename into a non-existing directory. 103call assert_notequal ( 0 , rename ( 'Xrenamefile' , 'Xdoesnotexist/Xrenamefile' )) 104 105" Can't rename a non-existing file. 106call assert_notequal ( 0 , rename ( 'Xdoesnotexist' , 'Xrenamefile2' )) 107call assert_equal ( '' , glob ( 'Xrenamefile2' )) 108 109" When rename() fails, the destination file should not be deleted. 110call assert_notequal ( 0 , rename ( 'Xdoesnotexist' , 'Xrenamefile' )) 111call assert_equal ([ 'foo' ], readfile ( 'Xrenamefile' )) 112 113" Can't rename to en empty file name. 114call assert_notequal ( 0 , rename ( 'Xrenamefile' , '' )) 115 116call assert_fails ( 'call rename("Xrenamefile", [])' , 'E730' ) 117call assert_fails ( 'call rename(0z, "Xrenamefile")' , 'E976' ) 118 119call delete ( 'Xrenamefile' ) 120endfunc