1
1
use std:: io;
2
2
use std:: path:: Path ;
3
3
4
- #[ cfg( windows) ]
5
- use crate :: fs:: _remove_dir_contents;
6
-
7
- #[ cfg( not( windows) ) ]
8
- use crate :: unix:: _remove_dir_contents;
4
+ cfg_if:: cfg_if! {
5
+ if #[ cfg( windows) ] {
6
+ use crate :: fs:: _remove_dir_contents;
7
+ } else {
8
+ use crate :: unix:: _remove_dir_contents;
9
+ }
10
+ }
9
11
10
12
/// Deletes the contents of `path`, but not the directory iteself.
11
13
///
@@ -28,26 +30,47 @@ pub fn remove_dir_contents<P: AsRef<Path>>(path: P) -> io::Result<()> {
28
30
/// a symlink to one).
29
31
pub fn ensure_empty_dir < P : AsRef < Path > > ( path : P ) -> io:: Result < ( ) > {
30
32
match std:: fs:: create_dir ( & path) {
31
- Err ( e) if e. kind ( ) == io:: ErrorKind :: AlreadyExists
32
- => remove_dir_contents ( path) ,
33
+ Err ( e) if e. kind ( ) == io:: ErrorKind :: AlreadyExists => remove_dir_contents ( path) ,
33
34
otherwise => otherwise,
34
35
}
35
36
}
36
37
37
38
#[ cfg( test) ]
38
39
mod test {
40
+ use std:: fs:: { self , File } ;
41
+ use std:: io;
42
+ use std:: path:: PathBuf ;
43
+
39
44
use tempfile:: TempDir ;
45
+
46
+ use crate :: ensure_empty_dir;
40
47
use crate :: remove_dir_all;
41
48
use crate :: remove_dir_contents;
42
- use crate :: ensure_empty_dir;
43
- use std:: fs:: { self , File } ;
44
- use std:: path:: PathBuf ;
45
- use std:: io;
46
49
47
- fn expect_failure < T > ( k : io:: ErrorKind , r : io:: Result < T > ) -> io:: Result < ( ) > {
50
+ cfg_if:: cfg_if! {
51
+ if #[ cfg( windows) ] {
52
+ const ENOTDIR : i32 = winapi:: shared:: winerror:: ERROR_DIRECTORY as i32 ;
53
+ const ENOENT : i32 = winapi:: shared:: winerror:: ERROR_FILE_NOT_FOUND as i32 ;
54
+ } else {
55
+ const ENOTDIR : i32 = libc:: ENOTDIR ;
56
+ const ENOENT : i32 = libc:: ENOENT ;
57
+ }
58
+ }
59
+
60
+ /// Expect a particular sort of failure
61
+ fn expect_failure < T > ( n : & [ i32 ] , r : io:: Result < T > ) -> io:: Result < ( ) > {
48
62
match r {
49
- Err ( e) if e. kind ( ) == k => Ok ( ( ) ) ,
50
- Err ( e) => Err ( e) ,
63
+ Err ( e)
64
+ if n. iter ( )
65
+ . map ( |n| Option :: Some ( * n) )
66
+ . any ( |n| n == e. raw_os_error ( ) ) =>
67
+ {
68
+ Ok ( ( ) )
69
+ }
70
+ Err ( e) => {
71
+ println ! ( "{e} {:?}, {:?}, {:?}" , e. raw_os_error( ) , e. kind( ) , n) ;
72
+ Err ( e)
73
+ }
51
74
Ok ( _) => Err ( io:: Error :: new (
52
75
io:: ErrorKind :: Other ,
53
76
"unexpected success" . to_string ( ) ,
@@ -61,39 +84,44 @@ mod test {
61
84
file : PathBuf ,
62
85
}
63
86
87
+ /// Create test setup: t.mkdir/file all in a tempdir.
64
88
fn prep ( ) -> Result < Prep , io:: Error > {
65
89
let tmp = TempDir :: new ( ) ?;
66
90
let ours = tmp. path ( ) . join ( "t.mkdir" ) ;
67
91
let file = ours. join ( "file" ) ;
68
92
fs:: create_dir ( & ours) ?;
69
93
File :: create ( & file) ?;
70
94
File :: open ( & file) ?;
71
- Ok ( Prep { _tmp : tmp, ours, file } )
95
+ Ok ( Prep {
96
+ _tmp : tmp,
97
+ ours,
98
+ file,
99
+ } )
72
100
}
73
101
74
102
#[ test]
75
103
fn mkdir_rm ( ) -> Result < ( ) , io:: Error > {
76
104
let p = prep ( ) ?;
77
105
78
- expect_failure ( io :: ErrorKind :: Other , remove_dir_contents ( & p. file ) ) ?;
106
+ expect_failure ( & [ ENOTDIR ] , remove_dir_contents ( & p. file ) ) ?;
79
107
80
108
remove_dir_contents ( & p. ours ) ?;
81
- expect_failure ( io :: ErrorKind :: NotFound , File :: open ( & p. file ) ) ?;
109
+ expect_failure ( & [ ENOENT ] , File :: open ( & p. file ) ) ?;
82
110
83
111
remove_dir_contents ( & p. ours ) ?;
84
112
remove_dir_all ( & p. ours ) ?;
85
- expect_failure ( io :: ErrorKind :: NotFound , remove_dir_contents ( & p. ours ) ) ?;
113
+ expect_failure ( & [ ENOENT ] , remove_dir_contents ( & p. ours ) ) ?;
86
114
Ok ( ( ) )
87
115
}
88
116
89
117
#[ test]
90
118
fn ensure_rm ( ) -> Result < ( ) , io:: Error > {
91
119
let p = prep ( ) ?;
92
120
93
- expect_failure ( io :: ErrorKind :: Other , ensure_empty_dir ( & p. file ) ) ?;
121
+ expect_failure ( & [ ENOTDIR ] , ensure_empty_dir ( & p. file ) ) ?;
94
122
95
123
ensure_empty_dir ( & p. ours ) ?;
96
- expect_failure ( io :: ErrorKind :: NotFound , File :: open ( & p. file ) ) ?;
124
+ expect_failure ( & [ ENOENT ] , File :: open ( & p. file ) ) ?;
97
125
ensure_empty_dir ( & p. ours ) ?;
98
126
99
127
remove_dir_all ( & p. ours ) ?;
0 commit comments