44#![ feature( io_error_more) ]
55#![ feature( rustc_private) ]
66
7+ use std:: path:: PathBuf ;
8+
79extern crate libc;
810
9- fn tmp ( ) -> std :: path :: PathBuf {
11+ fn tmp ( ) -> PathBuf {
1012 std:: env:: var ( "MIRI_TEMP" )
11- . map ( std:: path:: PathBuf :: from)
13+ . map ( |tmp| {
14+ // MIRI_TEMP is set outside of our emulated
15+ // program, so it may have path separators that don't
16+ // correspond to our target platform. We normalize them here
17+ // before constructing a `PathBuf`
18+
19+ #[ cfg( windows) ]
20+ return PathBuf :: from ( tmp. replace ( "/" , "\\ " ) ) ;
21+
22+ #[ cfg( not( windows) ) ]
23+ return PathBuf :: from ( tmp. replace ( "\\ " , "/" ) ) ;
24+ } )
1225 . unwrap_or_else ( |_| std:: env:: temp_dir ( ) )
1326}
1427
@@ -19,7 +32,6 @@ fn test_posix_realpath_alloc() {
1932 use std:: fs:: { remove_file, File } ;
2033 use std:: os:: unix:: ffi:: OsStrExt ;
2134 use std:: os:: unix:: ffi:: OsStringExt ;
22- use std:: path:: PathBuf ;
2335
2436 let buf;
2537 let path = tmp ( ) . join ( "miri_test_libc_posix_realpath_alloc" ) ;
@@ -36,18 +48,17 @@ fn test_posix_realpath_alloc() {
3648 libc:: free ( r as * mut _ ) ;
3749 }
3850 let canonical = PathBuf :: from ( OsString :: from_vec ( buf) ) ;
51+ assert_eq ! ( path. file_name( ) , canonical. file_name( ) ) ;
52+
3953 // Cleanup after test.
4054 remove_file ( & path) . unwrap ( ) ;
41-
42- assert_eq ! ( path. file_name( ) , canonical. file_name( ) ) ;
4355}
4456
4557/// Test non-allocating variant of `realpath`.
4658fn test_posix_realpath_noalloc ( ) {
4759 use std:: ffi:: { CStr , CString } ;
4860 use std:: fs:: { remove_file, File } ;
4961 use std:: os:: unix:: ffi:: OsStrExt ;
50- use std:: path:: PathBuf ;
5162
5263 let path = tmp ( ) . join ( "miri_test_libc_posix_realpath_noalloc" ) ;
5364 let c_path = CString :: new ( path. as_os_str ( ) . as_bytes ( ) ) . expect ( "CString::new failed" ) ;
@@ -64,17 +75,18 @@ fn test_posix_realpath_noalloc() {
6475 }
6576 let c = unsafe { CStr :: from_ptr ( v. as_ptr ( ) ) } ;
6677 let canonical = PathBuf :: from ( c. to_str ( ) . expect ( "CStr to str" ) ) ;
67- // Cleanup after test.
68- remove_file ( & path) . unwrap ( ) ;
6978
7079 assert_eq ! ( path. file_name( ) , canonical. file_name( ) ) ;
80+
81+ // Cleanup after test.
82+ remove_file ( & path) . unwrap ( ) ;
7183}
7284
7385/// Test failure cases for `realpath`.
7486fn test_posix_realpath_errors ( ) {
7587 use std:: convert:: TryInto ;
7688 use std:: ffi:: CString ;
77- use std:: fs:: { create_dir_all, remove_dir , remove_file } ;
89+ use std:: fs:: { create_dir_all, remove_dir_all } ;
7890 use std:: io:: ErrorKind ;
7991 use std:: os:: unix:: ffi:: OsStrExt ;
8092 use std:: os:: unix:: fs:: symlink;
@@ -89,13 +101,15 @@ fn test_posix_realpath_errors() {
89101
90102 // Test that a long path returns an error.
91103 //
92- // Linux first checks if the path to exists and macos does not.
104+ // Linux first checks if the path exists and macos does not.
93105 // Using an existing path ensures all platforms return `ENAMETOOLONG` given a long path.
94106 //
95107 // Rather than creating a bunch of directories, we create two directories containing symlinks.
96108 // Sadly we can't avoid creating directories and instead use a path like "./././././" or "./../../" as linux
97109 // appears to collapse "." and ".." before checking path length.
98- let path = tmp ( ) ;
110+ let path = tmp ( ) . join ( "posix_realpath_errors" ) ;
111+ // Cleanup before test.
112+ remove_dir_all ( & path) . ok ( ) ;
99113
100114 // The directories we will put symlinks in.
101115 let x = path. join ( "x/" ) ;
@@ -105,12 +119,6 @@ fn test_posix_realpath_errors() {
105119 let yx_sym = y. join ( "x" ) ;
106120 let xy_sym = x. join ( "y" ) ;
107121
108- // Cleanup before test.
109- remove_file ( & yx_sym) . ok ( ) ;
110- remove_file ( & xy_sym) . ok ( ) ;
111- remove_dir ( & x) . ok ( ) ;
112- remove_dir ( & y) . ok ( ) ;
113-
114122 // Create directories.
115123 create_dir_all ( & x) . expect ( "dir x" ) ;
116124 create_dir_all ( & y) . expect ( "dir y" ) ;
@@ -126,15 +134,12 @@ fn test_posix_realpath_errors() {
126134 let r = unsafe { libc:: realpath ( c_path. as_ptr ( ) , std:: ptr:: null_mut ( ) ) } ;
127135 let e = std:: io:: Error :: last_os_error ( ) ;
128136
129- // Cleanup after test.
130- remove_file ( & yx_sym) . ok ( ) ;
131- remove_file ( & xy_sym) . ok ( ) ;
132- remove_dir ( & x) . ok ( ) ;
133- remove_dir ( & y) . ok ( ) ;
134-
135137 assert ! ( r. is_null( ) ) ;
136138 assert_eq ! ( e. raw_os_error( ) , Some ( libc:: ENAMETOOLONG ) ) ;
137139 assert_eq ! ( e. kind( ) , ErrorKind :: InvalidFilename ) ;
140+
141+ // Cleanup after test.
142+ remove_dir_all ( & path) . ok ( ) ;
138143}
139144
140145#[ cfg( any( target_os = "linux" , target_os = "freebsd" ) ) ]
0 commit comments