1
+ use std:: { fs:: Permissions , os:: unix:: fs:: PermissionsExt } ;
2
+
3
+ #[ monoio:: test_all]
4
+ async fn rename_file_in_the_same_directory ( ) {
5
+ let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
6
+ let file = tempfile:: NamedTempFile :: new_in ( temp_dir. path ( ) ) . unwrap ( ) ;
7
+
8
+ let old_file_path = file. path ( ) ;
9
+ let new_file_path = temp_dir. path ( ) . join ( "test-file" ) ;
10
+
11
+ let result = monoio:: fs:: rename ( old_file_path, & new_file_path) . await ;
12
+ assert ! ( result. is_ok( ) ) ;
13
+
14
+ assert ! ( new_file_path. exists( ) ) ;
15
+ assert ! ( !old_file_path. exists( ) ) ;
16
+ }
17
+
18
+ #[ monoio:: test_all]
19
+ async fn rename_file_in_different_directory ( ) {
20
+ let temp_dir1 = tempfile:: tempdir ( ) . unwrap ( ) ;
21
+ let temp_dir2 = tempfile:: tempdir ( ) . unwrap ( ) ;
22
+ let file = tempfile:: NamedTempFile :: new_in ( temp_dir1. path ( ) ) . unwrap ( ) ;
23
+
24
+ let old_file_path = file. path ( ) ;
25
+ let new_file_path = temp_dir2. path ( ) . join ( "test-file" ) ;
26
+
27
+ let result = monoio:: fs:: rename ( old_file_path, & new_file_path) . await ;
28
+ assert ! ( result. is_ok( ) ) ;
29
+
30
+ assert ! ( new_file_path. exists( ) ) ;
31
+ assert ! ( !old_file_path. exists( ) ) ;
32
+ }
33
+
34
+ #[ monoio:: test_all]
35
+ async fn mv_file_in_different_directory ( ) {
36
+ let temp_dir1 = tempfile:: tempdir ( ) . unwrap ( ) ;
37
+ let temp_dir2 = tempfile:: tempdir ( ) . unwrap ( ) ;
38
+ let file = tempfile:: NamedTempFile :: new_in ( temp_dir1. path ( ) ) . unwrap ( ) ;
39
+
40
+ let old_file_path = file. path ( ) ;
41
+ let old_file_name = old_file_path. file_name ( ) . unwrap ( ) ;
42
+ let new_file_path = temp_dir2. path ( ) . join ( old_file_name) ;
43
+
44
+ let result = monoio:: fs:: rename ( old_file_path, & new_file_path) . await ;
45
+ assert ! ( result. is_ok( ) ) ;
46
+
47
+ assert ! ( new_file_path. exists( ) ) ;
48
+ assert ! ( !old_file_path. exists( ) ) ;
49
+ }
50
+
51
+ #[ monoio:: test_all]
52
+ async fn rename_inexist_file ( ) {
53
+ let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
54
+
55
+ let old_file_path = temp_dir. path ( ) . join ( "inexist.txt" ) ;
56
+ let new_file_path = temp_dir. path ( ) . join ( "renamed.txt" ) ;
57
+
58
+ let result = monoio:: fs:: rename ( old_file_path, new_file_path) . await ;
59
+
60
+ assert ! ( result. is_err( ) ) ;
61
+ }
62
+
63
+ #[ monoio:: test_all]
64
+ async fn rename_file_without_permission ( ) {
65
+ let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
66
+ let temp_file = tempfile:: NamedTempFile :: new_in ( & temp_dir) . unwrap ( ) ;
67
+
68
+ std:: fs:: set_permissions ( temp_dir. path ( ) , Permissions :: from_mode ( 0o0 ) ) . unwrap ( ) ;
69
+
70
+ let old_file_path = temp_file. path ( ) ;
71
+ let new_file_path = temp_dir. path ( ) . join ( "test-file" ) ;
72
+
73
+ let result = monoio:: fs:: rename ( old_file_path, & new_file_path) . await ;
74
+
75
+ assert ! ( result. is_err( ) ) ;
76
+ }
0 commit comments