1212
1313use std:: fs;
1414use std:: path:: Path ;
15- use std:: sync:: mpsc:: channel;
16- use std:: thread;
1715
1816use tempfile:: { Builder , TempDir } ;
1917
18+ #[ test]
2019fn test_tempdir ( ) {
2120 let path = {
22- let p = Builder :: new ( ) . prefix ( "foobar" ) . tempdir_in ( "." ) . unwrap ( ) ;
21+ let p = Builder :: new ( ) . prefix ( "foobar" ) . tempdir ( ) . unwrap ( ) ;
2322 let p = p. path ( ) ;
2423 assert ! ( p. to_str( ) . unwrap( ) . contains( "foobar" ) ) ;
2524 p. to_path_buf ( )
2625 } ;
2726 assert ! ( !path. exists( ) ) ;
2827}
2928
29+ #[ test]
3030fn test_prefix ( ) {
31- let tmpfile = TempDir :: with_prefix_in ( "prefix" , ". ") . unwrap ( ) ;
31+ let tmpfile = TempDir :: with_prefix ( "prefix" ) . unwrap ( ) ;
3232 let name = tmpfile. path ( ) . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
3333 assert ! ( name. starts_with( "prefix" ) ) ;
3434}
3535
36+ #[ test]
3637fn test_suffix ( ) {
37- let tmpfile = TempDir :: with_suffix_in ( "suffix" , ". ") . unwrap ( ) ;
38+ let tmpfile = TempDir :: with_suffix ( "suffix" ) . unwrap ( ) ;
3839 let name = tmpfile. path ( ) . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
3940 assert ! ( name. ends_with( "suffix" ) ) ;
4041}
4142
43+ #[ test]
4244fn test_customnamed ( ) {
4345 let tmpfile = Builder :: new ( )
4446 . prefix ( "prefix" )
@@ -52,7 +54,12 @@ fn test_customnamed() {
5254 assert_eq ! ( name. len( ) , 24 ) ;
5355}
5456
55- fn test_rm_tempdir ( ) {
57+ #[ test]
58+ #[ cfg_attr( target_os = "wasi" , ignore = "thread::spawn is not supported" ) ]
59+ fn test_rm_tempdir_threading ( ) {
60+ use std:: sync:: mpsc:: channel;
61+ use std:: thread;
62+
5663 let ( tx, rx) = channel ( ) ;
5764 let f = move || {
5865 let tmp = TempDir :: new ( ) . unwrap ( ) ;
@@ -81,61 +88,32 @@ fn test_rm_tempdir() {
8188 assert ! ( path. exists( ) ) ;
8289 }
8390 assert ! ( !path. exists( ) ) ;
91+ }
8492
85- let path;
86- {
93+ #[ test]
94+ fn test_tempdir_keep ( ) {
95+ let path = {
8796 let tmp = TempDir :: new ( ) . unwrap ( ) ;
88- path = tmp. keep ( ) ;
89- }
97+ tmp. keep ( )
98+ } ;
9099 assert ! ( path. exists( ) ) ;
91100 fs:: remove_dir_all ( & path) . unwrap ( ) ;
92101 assert ! ( !path. exists( ) ) ;
93102}
94103
95- fn test_rm_tempdir_close ( ) {
96- let ( tx, rx) = channel ( ) ;
97- let f = move || {
98- let tmp = TempDir :: new ( ) . unwrap ( ) ;
99- tx. send ( tmp. path ( ) . to_path_buf ( ) ) . unwrap ( ) ;
100- tmp. close ( ) . unwrap ( ) ;
101- panic ! ( "panic when unwinding past `tmp`" ) ;
102- } ;
103- let _ = thread:: spawn ( f) . join ( ) ;
104- let path = rx. recv ( ) . unwrap ( ) ;
105- assert ! ( !path. exists( ) ) ;
106-
104+ #[ test]
105+ fn test_tmpdir_close ( ) {
107106 let tmp = TempDir :: new ( ) . unwrap ( ) ;
108107 let path = tmp. path ( ) . to_path_buf ( ) ;
109- let f = move || {
110- let tmp = tmp;
111- tmp. close ( ) . unwrap ( ) ;
112- panic ! ( "panic when unwinding past `tmp`" ) ;
113- } ;
114- let _ = thread:: spawn ( f) . join ( ) ;
115- assert ! ( !path. exists( ) ) ;
116-
117- let path;
118- {
119- let f = move || TempDir :: new ( ) . unwrap ( ) ;
120-
121- let tmp = thread:: spawn ( f) . join ( ) . unwrap ( ) ;
122- path = tmp. path ( ) . to_path_buf ( ) ;
123- assert ! ( path. exists( ) ) ;
124- tmp. close ( ) . unwrap ( ) ;
125- }
126- assert ! ( !path. exists( ) ) ;
127-
128- let path;
129- {
130- let tmp = TempDir :: new ( ) . unwrap ( ) ;
131- path = tmp. keep ( ) ;
132- }
133108 assert ! ( path. exists( ) ) ;
134- fs :: remove_dir_all ( & path ) . unwrap ( ) ;
109+ tmp . close ( ) . unwrap ( ) ;
135110 assert ! ( !path. exists( ) ) ;
136111}
137112
113+ #[ test]
114+ #[ cfg_attr( target_os = "wasi" , ignore = "unwinding is not supported" ) ]
138115fn dont_double_panic ( ) {
116+ use std:: thread;
139117 let r: Result < ( ) , _ > = thread:: spawn ( move || {
140118 let tmpdir = TempDir :: new ( ) . unwrap ( ) ;
141119 // Remove the temporary directory so that TempDir sees
@@ -149,16 +127,7 @@ fn dont_double_panic() {
149127 assert ! ( r. is_err( ) ) ;
150128}
151129
152- fn in_tmpdir < F > ( f : F )
153- where
154- F : FnOnce ( ) ,
155- {
156- let tmpdir = TempDir :: new ( ) . unwrap ( ) ;
157- assert ! ( std:: env:: set_current_dir( tmpdir. path( ) ) . is_ok( ) ) ;
158-
159- f ( ) ;
160- }
161-
130+ #[ test]
162131fn pass_as_asref_path ( ) {
163132 let tempdir = TempDir :: new ( ) . unwrap ( ) ;
164133 takes_asref_path ( & tempdir) ;
@@ -169,6 +138,7 @@ fn pass_as_asref_path() {
169138 }
170139}
171140
141+ #[ test]
172142fn test_disable_cleanup ( ) {
173143 // Case 0: never mark as "disable cleanup"
174144 // Case 1: enable "disable cleanup" in the builder, don't touch it after.
@@ -196,17 +166,3 @@ fn test_disable_cleanup() {
196166 }
197167 }
198168}
199-
200- #[ test]
201- #[ cfg_attr( target_os = "wasi" , ignore = "thread::spawn is not supported" ) ]
202- fn main ( ) {
203- in_tmpdir ( test_tempdir) ;
204- in_tmpdir ( test_prefix) ;
205- in_tmpdir ( test_suffix) ;
206- in_tmpdir ( test_customnamed) ;
207- in_tmpdir ( test_rm_tempdir) ;
208- in_tmpdir ( test_rm_tempdir_close) ;
209- in_tmpdir ( dont_double_panic) ;
210- in_tmpdir ( pass_as_asref_path) ;
211- in_tmpdir ( test_disable_cleanup) ;
212- }
0 commit comments