@@ -15,8 +15,18 @@ use std::path::Path;
1515
1616use tempfile:: { Builder , TempDir } ;
1717
18+ /// For the wasi platforms, `std::env::temp_dir` will panic. For those targets, configure the /tmp
19+ /// directory instead as the base directory for temp files.
20+ fn configure_wasi_temp_dir ( ) {
21+ if cfg ! ( target_os = "wasi" ) {
22+ let _ = tempfile:: env:: override_temp_dir ( std:: path:: Path :: new ( "/tmp" ) ) ;
23+ }
24+ }
25+
1826#[ test]
1927fn test_tempdir ( ) {
28+ configure_wasi_temp_dir ( ) ;
29+
2030 let path = {
2131 let p = Builder :: new ( ) . prefix ( "foobar" ) . tempdir ( ) . unwrap ( ) ;
2232 let p = p. path ( ) ;
@@ -28,20 +38,26 @@ fn test_tempdir() {
2838
2939#[ test]
3040fn test_prefix ( ) {
41+ configure_wasi_temp_dir ( ) ;
42+
3143 let tmpfile = TempDir :: with_prefix ( "prefix" ) . unwrap ( ) ;
3244 let name = tmpfile. path ( ) . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
3345 assert ! ( name. starts_with( "prefix" ) ) ;
3446}
3547
3648#[ test]
3749fn test_suffix ( ) {
50+ configure_wasi_temp_dir ( ) ;
51+
3852 let tmpfile = TempDir :: with_suffix ( "suffix" ) . unwrap ( ) ;
3953 let name = tmpfile. path ( ) . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
4054 assert ! ( name. ends_with( "suffix" ) ) ;
4155}
4256
4357#[ test]
4458fn test_customnamed ( ) {
59+ configure_wasi_temp_dir ( ) ;
60+
4561 let tmpfile = Builder :: new ( )
4662 . prefix ( "prefix" )
4763 . suffix ( "suffix" )
@@ -57,6 +73,8 @@ fn test_customnamed() {
5773#[ test]
5874#[ cfg_attr( target_os = "wasi" , ignore = "thread::spawn is not supported" ) ]
5975fn test_rm_tempdir_threading ( ) {
76+ configure_wasi_temp_dir ( ) ;
77+
6078 use std:: sync:: mpsc:: channel;
6179 use std:: thread;
6280
@@ -92,6 +110,8 @@ fn test_rm_tempdir_threading() {
92110
93111#[ test]
94112fn test_tempdir_keep ( ) {
113+ configure_wasi_temp_dir ( ) ;
114+
95115 let path = {
96116 let tmp = TempDir :: new ( ) . unwrap ( ) ;
97117 tmp. keep ( )
@@ -103,6 +123,8 @@ fn test_tempdir_keep() {
103123
104124#[ test]
105125fn test_tmpdir_close ( ) {
126+ configure_wasi_temp_dir ( ) ;
127+
106128 let tmp = TempDir :: new ( ) . unwrap ( ) ;
107129 let path = tmp. path ( ) . to_path_buf ( ) ;
108130 assert ! ( path. exists( ) ) ;
@@ -113,6 +135,8 @@ fn test_tmpdir_close() {
113135#[ test]
114136#[ cfg_attr( target_os = "wasi" , ignore = "unwinding is not supported" ) ]
115137fn dont_double_panic ( ) {
138+ configure_wasi_temp_dir ( ) ;
139+
116140 use std:: thread;
117141 let r: Result < ( ) , _ > = thread:: spawn ( move || {
118142 let tmpdir = TempDir :: new ( ) . unwrap ( ) ;
@@ -129,6 +153,8 @@ fn dont_double_panic() {
129153
130154#[ test]
131155fn pass_as_asref_path ( ) {
156+ configure_wasi_temp_dir ( ) ;
157+
132158 let tempdir = TempDir :: new ( ) . unwrap ( ) ;
133159 takes_asref_path ( & tempdir) ;
134160
@@ -140,6 +166,8 @@ fn pass_as_asref_path() {
140166
141167#[ test]
142168fn test_disable_cleanup ( ) {
169+ configure_wasi_temp_dir ( ) ;
170+
143171 // Case 0: never mark as "disable cleanup"
144172 // Case 1: enable "disable cleanup" in the builder, don't touch it after.
145173 // Case 2: enable "disable cleanup" in the builder, turn it off after.
0 commit comments