@@ -141,14 +141,13 @@ fn test_protocol_v2_and_above_have_proto() {
141141fn test_cli_single_file_generation ( ) {
142142 use std:: fs;
143143 use std:: process:: Command ;
144+ use tempfile:: NamedTempFile ;
144145
145- let temp_file = "/tmp/test_pickle_cli.pkl" ;
146-
147- // clean up if exists
148- let _ = fs:: remove_file ( temp_file) ;
146+ let temp_file = NamedTempFile :: new ( ) . expect ( "failed to create temp file" ) ;
147+ let temp_path = temp_file. path ( ) . to_str ( ) . unwrap ( ) ;
149148
150149 let output = Command :: new ( "cargo" )
151- . args ( [ "run" , "--quiet" , "--" , temp_file ] )
150+ . args ( [ "run" , "--quiet" , "--" , temp_path ] )
152151 . output ( )
153152 . expect ( "failed to execute command" ) ;
154153
@@ -157,83 +156,76 @@ fn test_cli_single_file_generation() {
157156 "CLI command failed: {:?}" ,
158157 String :: from_utf8_lossy( & output. stderr)
159158 ) ;
160- assert ! ( fs:: metadata( temp_file ) . is_ok( ) , "output file not created" ) ;
159+ assert ! ( fs:: metadata( temp_path ) . is_ok( ) , "output file not created" ) ;
161160
162- let contents = fs:: read ( temp_file ) . expect ( "failed to read output file" ) ;
161+ let contents = fs:: read ( temp_path ) . expect ( "failed to read output file" ) ;
163162 assert ! ( !contents. is_empty( ) , "output file is empty" ) ;
164163 assert_eq ! ( contents[ contents. len( ) - 1 ] , b'.' , "missing STOP opcode" ) ;
165-
166- // cleanup
167- let _ = fs:: remove_file ( temp_file) ;
168164}
169165
170166#[ test]
171167fn test_cli_with_protocol_flag ( ) {
172168 use std:: fs;
173169 use std:: process:: Command ;
170+ use tempfile:: NamedTempFile ;
174171
175- let temp_file = "/tmp/test_pickle_protocol.pkl" ;
176- let _ = fs :: remove_file ( temp_file) ;
172+ let temp_file = NamedTempFile :: new ( ) . expect ( "failed to create temp file" ) ;
173+ let temp_path = temp_file. path ( ) . to_str ( ) . unwrap ( ) ;
177174
178175 let output = Command :: new ( "cargo" )
179- . args ( [ "run" , "--quiet" , "--" , "--protocol" , "4" , temp_file ] )
176+ . args ( [ "run" , "--quiet" , "--" , "--protocol" , "4" , temp_path ] )
180177 . output ( )
181178 . expect ( "failed to execute command" ) ;
182179
183180 assert ! ( output. status. success( ) , "CLI command failed" ) ;
184181
185- let contents = fs:: read ( temp_file ) . expect ( "failed to read output file" ) ;
182+ let contents = fs:: read ( temp_path ) . expect ( "failed to read output file" ) ;
186183 assert_eq ! ( contents[ 0 ] , 0x80 , "should start with PROTO opcode" ) ;
187184 assert_eq ! ( contents[ 1 ] , 4 , "should be protocol 4" ) ;
188-
189- let _ = fs:: remove_file ( temp_file) ;
190185}
191186
192187#[ test]
193188fn test_cli_with_seed_produces_deterministic_output ( ) {
194189 use std:: fs;
195190 use std:: process:: Command ;
191+ use tempfile:: NamedTempFile ;
196192
197- let temp_file1 = "/tmp/test_pickle_seed1.pkl" ;
198- let temp_file2 = "/tmp/test_pickle_seed2.pkl" ;
199-
200- let _ = fs:: remove_file ( temp_file1) ;
201- let _ = fs:: remove_file ( temp_file2) ;
193+ let temp_file1 = NamedTempFile :: new ( ) . expect ( "failed to create temp file 1" ) ;
194+ let temp_file2 = NamedTempFile :: new ( ) . expect ( "failed to create temp file 2" ) ;
195+ let temp_path1 = temp_file1. path ( ) . to_str ( ) . unwrap ( ) ;
196+ let temp_path2 = temp_file2. path ( ) . to_str ( ) . unwrap ( ) ;
202197
203198 // generate with same seed twice
204199 Command :: new ( "cargo" )
205- . args ( [ "run" , "--quiet" , "--" , "--seed" , "42" , temp_file1 ] )
200+ . args ( [ "run" , "--quiet" , "--" , "--seed" , "42" , temp_path1 ] )
206201 . output ( )
207202 . expect ( "failed to execute command" ) ;
208203
209204 Command :: new ( "cargo" )
210- . args ( [ "run" , "--quiet" , "--" , "--seed" , "42" , temp_file2 ] )
205+ . args ( [ "run" , "--quiet" , "--" , "--seed" , "42" , temp_path2 ] )
211206 . output ( )
212207 . expect ( "failed to execute command" ) ;
213208
214- let contents1 = fs:: read ( temp_file1 ) . expect ( "failed to read file 1" ) ;
215- let contents2 = fs:: read ( temp_file2 ) . expect ( "failed to read file 2" ) ;
209+ let contents1 = fs:: read ( temp_path1 ) . expect ( "failed to read file 1" ) ;
210+ let contents2 = fs:: read ( temp_path2 ) . expect ( "failed to read file 2" ) ;
216211
217212 assert_eq ! (
218213 contents1, contents2,
219214 "same seed should produce identical output"
220215 ) ;
221-
222- let _ = fs:: remove_file ( temp_file1) ;
223- let _ = fs:: remove_file ( temp_file2) ;
224216}
225217
226218#[ test]
227219fn test_cli_batch_mode ( ) {
228220 use std:: fs;
229221 use std:: process:: Command ;
222+ use tempfile:: TempDir ;
230223
231- let temp_dir = "tests/test_pickle_batch" ;
232- let _ = fs:: remove_dir_all ( temp_dir) ;
233- fs:: create_dir_all ( temp_dir) . expect ( "failed to create temp dir" ) ;
224+ let temp_dir = TempDir :: new ( ) . expect ( "failed to create temp dir" ) ;
225+ let temp_path = temp_dir. path ( ) . to_str ( ) . unwrap ( ) ;
234226
235227 let output = Command :: new ( "cargo" )
236- . args ( [ "run" , "--quiet" , "--" , "--dir" , temp_dir , "--samples" , "5" ] )
228+ . args ( [ "run" , "--quiet" , "--" , "--dir" , temp_path , "--samples" , "5" ] )
237229 . output ( )
238230 . expect ( "failed to execute command" ) ;
239231
@@ -244,26 +236,25 @@ fn test_cli_batch_mode() {
244236 ) ;
245237
246238 // check that files were created
247- let entries = fs:: read_dir ( temp_dir ) . expect ( "failed to read dir" ) ;
239+ let entries = fs:: read_dir ( temp_path ) . expect ( "failed to read dir" ) ;
248240 let count = entries. count ( ) ;
249241 assert_eq ! ( count, 5 , "should create 5 pickle files" ) ;
250242
251243 // verify one of the files
252- let test_file = format ! ( "{}/0.pkl" , temp_dir ) ;
244+ let test_file = format ! ( "{}/0.pkl" , temp_path ) ;
253245 let contents = fs:: read ( & test_file) . expect ( "failed to read generated file" ) ;
254246 assert ! ( !contents. is_empty( ) ) ;
255247 assert_eq ! ( contents[ contents. len( ) - 1 ] , b'.' ) ;
256-
257- let _ = fs:: remove_dir_all ( temp_dir) ;
258248}
259249
260250#[ test]
261251fn test_cli_with_opcode_range ( ) {
262252 use std:: fs;
263253 use std:: process:: Command ;
254+ use tempfile:: NamedTempFile ;
264255
265- let temp_file = "/tmp/test_pickle_opcodes.pkl" ;
266- let _ = fs :: remove_file ( temp_file) ;
256+ let temp_file = NamedTempFile :: new ( ) . expect ( "failed to create temp file" ) ;
257+ let temp_path = temp_file. path ( ) . to_str ( ) . unwrap ( ) ;
267258
268259 let output = Command :: new ( "cargo" )
269260 . args ( [
@@ -274,24 +265,23 @@ fn test_cli_with_opcode_range() {
274265 "10" ,
275266 "--max-opcodes" ,
276267 "20" ,
277- temp_file ,
268+ temp_path ,
278269 ] )
279270 . output ( )
280271 . expect ( "failed to execute command" ) ;
281272
282273 assert ! ( output. status. success( ) , "CLI command failed" ) ;
283- assert ! ( fs:: metadata( temp_file) . is_ok( ) , "output file not created" ) ;
284-
285- let _ = fs:: remove_file ( temp_file) ;
274+ assert ! ( fs:: metadata( temp_path) . is_ok( ) , "output file not created" ) ;
286275}
287276
288277#[ test]
289278fn test_cli_with_mutators ( ) {
290279 use std:: fs;
291280 use std:: process:: Command ;
281+ use tempfile:: NamedTempFile ;
292282
293- let temp_file = "/tmp/test_pickle_mutators.pkl" ;
294- let _ = fs :: remove_file ( temp_file) ;
283+ let temp_file = NamedTempFile :: new ( ) . expect ( "failed to create temp file" ) ;
284+ let temp_path = temp_file. path ( ) . to_str ( ) . unwrap ( ) ;
295285
296286 let output = Command :: new ( "cargo" )
297287 . args ( [
@@ -303,13 +293,11 @@ fn test_cli_with_mutators() {
303293 "boundary" ,
304294 "--mutation-rate" ,
305295 "0.5" ,
306- temp_file ,
296+ temp_path ,
307297 ] )
308298 . output ( )
309299 . expect ( "failed to execute command" ) ;
310300
311301 assert ! ( output. status. success( ) , "CLI command with mutators failed" ) ;
312- assert ! ( fs:: metadata( temp_file) . is_ok( ) , "output file not created" ) ;
313-
314- let _ = fs:: remove_file ( temp_file) ;
302+ assert ! ( fs:: metadata( temp_path) . is_ok( ) , "output file not created" ) ;
315303}
0 commit comments