@@ -4,6 +4,7 @@ use std::ffi::{OsStr, OsString};
44use std:: fs:: File ;
55use std:: io:: { self , Read , Seek , SeekFrom , Write } ;
66use std:: path:: { Path , PathBuf } ;
7+ use std:: time:: Duration ;
78use tempfile:: { env, tempdir, Builder , NamedTempFile , TempPath } ;
89
910fn exists < P : AsRef < Path > > ( path : P ) -> bool {
@@ -280,7 +281,7 @@ fn temp_path_from_existing() {
280281 File :: create ( & tmp_file_path_2) . unwrap ( ) ;
281282 assert ! ( tmp_file_path_2. exists( ) , "Test file 2 hasn't been created" ) ;
282283
283- let tmp_path = TempPath :: from_path ( & tmp_file_path_1) ;
284+ let tmp_path = TempPath :: try_from_path ( & tmp_file_path_1) . unwrap ( ) ;
284285 assert ! (
285286 tmp_file_path_1. exists( ) ,
286287 "Test file has been deleted before dropping TempPath"
@@ -303,13 +304,49 @@ fn temp_path_from_argument_types() {
303304 // This just has to compile
304305 return ;
305306
306- TempPath :: from_path ( "" ) ;
307- TempPath :: from_path ( String :: new ( ) ) ;
308- TempPath :: from_path ( OsStr :: new ( "" ) ) ;
309- TempPath :: from_path ( OsString :: new ( ) ) ;
310- TempPath :: from_path ( Path :: new ( "" ) ) ;
311- TempPath :: from_path ( PathBuf :: new ( ) ) ;
312- TempPath :: from_path ( PathBuf :: new ( ) . into_boxed_path ( ) ) ;
307+ TempPath :: try_from_path ( "" ) . unwrap ( ) ;
308+ TempPath :: try_from_path ( String :: new ( ) ) . unwrap ( ) ;
309+ TempPath :: try_from_path ( OsStr :: new ( "" ) ) . unwrap ( ) ;
310+ TempPath :: try_from_path ( OsString :: new ( ) ) . unwrap ( ) ;
311+ TempPath :: try_from_path ( Path :: new ( "" ) ) . unwrap ( ) ;
312+ TempPath :: try_from_path ( PathBuf :: new ( ) ) . unwrap ( ) ;
313+ TempPath :: try_from_path ( PathBuf :: new ( ) . into_boxed_path ( ) ) . unwrap ( ) ;
314+ }
315+
316+ #[ test]
317+ fn test_temp_path_resolve_missing_cwd ( ) {
318+ let tmpdir = tempdir ( ) . unwrap ( ) ;
319+ std:: env:: set_current_dir ( & tmpdir) . expect ( "failed to change to the temporary directory" ) ;
320+ tmpdir. close ( ) . unwrap ( ) ;
321+ // It can sometimes take a bit for the OS to realize the CWD is removed. I'm not sure why, but
322+ // this test fails occasionally without this.
323+ while std:: env:: current_dir ( ) . is_ok ( ) {
324+ std:: thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
325+ }
326+
327+ #[ allow( deprecated) ]
328+ let path = TempPath :: from_path ( "foo" ) ;
329+ assert_eq ! ( & * path, "foo" ) ;
330+
331+ TempPath :: try_from_path ( "foo" ) . expect_err ( "should have failed to make path absolute file" ) ;
332+ }
333+
334+ #[ test]
335+ fn test_temp_path_resolve_existing_cwd ( ) {
336+ let tmpdir = tempdir ( ) . unwrap ( ) ;
337+ std:: env:: set_current_dir ( & tmpdir) . expect ( "failed to change to directory" ) ;
338+
339+ #[ allow( deprecated) ]
340+ let path = TempPath :: from_path ( "foo" ) ;
341+ assert_eq ! ( & * path, tmpdir. path( ) . join( "foo" ) ) ;
342+
343+ #[ allow( deprecated) ]
344+ let path = TempPath :: from_path ( "" ) ;
345+ assert_eq ! ( & * path, "" ) ;
346+
347+ TempPath :: try_from_path ( "" ) . expect_err ( "empty paths should fail" ) ;
348+
349+ tmpdir. close ( ) . unwrap ( ) ;
313350}
314351
315352#[ test]
@@ -327,7 +364,7 @@ fn test_change_dir() {
327364 let dir_a = tempdir ( ) . unwrap ( ) ;
328365 let dir_b = tempdir ( ) . unwrap ( ) ;
329366
330- std:: env:: set_current_dir ( & dir_a) . expect ( "failed to change to directory ~ " ) ;
367+ std:: env:: set_current_dir ( & dir_a) . expect ( "failed to change to directory A " ) ;
331368 let tmpfile = NamedTempFile :: new_in ( "." ) . unwrap ( ) ;
332369 let path = std:: env:: current_dir ( ) . unwrap ( ) . join ( tmpfile. path ( ) ) ;
333370 std:: env:: set_current_dir ( & dir_b) . expect ( "failed to change to directory B" ) ;
@@ -574,7 +611,10 @@ fn test_reseed() {
574611 . open ( path)
575612 . unwrap ( ) ;
576613
577- files. push ( NamedTempFile :: from_parts ( f, TempPath :: from_path ( path) ) ) ;
614+ files. push ( NamedTempFile :: from_parts (
615+ f,
616+ TempPath :: try_from_path ( path) . unwrap ( ) ,
617+ ) ) ;
578618 Err ( io:: Error :: new ( io:: ErrorKind :: AlreadyExists , "fake!" ) )
579619 } ) ;
580620
0 commit comments