@@ -15,8 +15,18 @@ use std::path::Path;
15
15
16
16
use tempfile:: { Builder , TempDir } ;
17
17
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
+
18
26
#[ test]
19
27
fn test_tempdir ( ) {
28
+ configure_wasi_temp_dir ( ) ;
29
+
20
30
let path = {
21
31
let p = Builder :: new ( ) . prefix ( "foobar" ) . tempdir ( ) . unwrap ( ) ;
22
32
let p = p. path ( ) ;
@@ -28,20 +38,26 @@ fn test_tempdir() {
28
38
29
39
#[ test]
30
40
fn test_prefix ( ) {
41
+ configure_wasi_temp_dir ( ) ;
42
+
31
43
let tmpfile = TempDir :: with_prefix ( "prefix" ) . unwrap ( ) ;
32
44
let name = tmpfile. path ( ) . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
33
45
assert ! ( name. starts_with( "prefix" ) ) ;
34
46
}
35
47
36
48
#[ test]
37
49
fn test_suffix ( ) {
50
+ configure_wasi_temp_dir ( ) ;
51
+
38
52
let tmpfile = TempDir :: with_suffix ( "suffix" ) . unwrap ( ) ;
39
53
let name = tmpfile. path ( ) . file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
40
54
assert ! ( name. ends_with( "suffix" ) ) ;
41
55
}
42
56
43
57
#[ test]
44
58
fn test_customnamed ( ) {
59
+ configure_wasi_temp_dir ( ) ;
60
+
45
61
let tmpfile = Builder :: new ( )
46
62
. prefix ( "prefix" )
47
63
. suffix ( "suffix" )
@@ -57,6 +73,8 @@ fn test_customnamed() {
57
73
#[ test]
58
74
#[ cfg_attr( target_os = "wasi" , ignore = "thread::spawn is not supported" ) ]
59
75
fn test_rm_tempdir_threading ( ) {
76
+ configure_wasi_temp_dir ( ) ;
77
+
60
78
use std:: sync:: mpsc:: channel;
61
79
use std:: thread;
62
80
@@ -92,6 +110,8 @@ fn test_rm_tempdir_threading() {
92
110
93
111
#[ test]
94
112
fn test_tempdir_keep ( ) {
113
+ configure_wasi_temp_dir ( ) ;
114
+
95
115
let path = {
96
116
let tmp = TempDir :: new ( ) . unwrap ( ) ;
97
117
tmp. keep ( )
@@ -103,6 +123,8 @@ fn test_tempdir_keep() {
103
123
104
124
#[ test]
105
125
fn test_tmpdir_close ( ) {
126
+ configure_wasi_temp_dir ( ) ;
127
+
106
128
let tmp = TempDir :: new ( ) . unwrap ( ) ;
107
129
let path = tmp. path ( ) . to_path_buf ( ) ;
108
130
assert ! ( path. exists( ) ) ;
@@ -113,6 +135,8 @@ fn test_tmpdir_close() {
113
135
#[ test]
114
136
#[ cfg_attr( target_os = "wasi" , ignore = "unwinding is not supported" ) ]
115
137
fn dont_double_panic ( ) {
138
+ configure_wasi_temp_dir ( ) ;
139
+
116
140
use std:: thread;
117
141
let r: Result < ( ) , _ > = thread:: spawn ( move || {
118
142
let tmpdir = TempDir :: new ( ) . unwrap ( ) ;
@@ -129,6 +153,8 @@ fn dont_double_panic() {
129
153
130
154
#[ test]
131
155
fn pass_as_asref_path ( ) {
156
+ configure_wasi_temp_dir ( ) ;
157
+
132
158
let tempdir = TempDir :: new ( ) . unwrap ( ) ;
133
159
takes_asref_path ( & tempdir) ;
134
160
@@ -140,6 +166,8 @@ fn pass_as_asref_path() {
140
166
141
167
#[ test]
142
168
fn test_disable_cleanup ( ) {
169
+ configure_wasi_temp_dir ( ) ;
170
+
143
171
// Case 0: never mark as "disable cleanup"
144
172
// Case 1: enable "disable cleanup" in the builder, don't touch it after.
145
173
// Case 2: enable "disable cleanup" in the builder, turn it off after.
0 commit comments