@@ -110,13 +110,14 @@ def test_yaml_param_settings(param):
110
110
importlib .reload (_config )
111
111
112
112
option , value = param
113
- with tempfile .NamedTemporaryFile (buffering = 0 , delete_on_close = False ) as tmp :
113
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as tmp :
114
114
tmp .write (f"---\n { param_locs [option ]} :\n { option } : { value } \n " .encode ("utf-8" ))
115
115
tmp .close ()
116
116
garak .cli .main (
117
117
["--config" , tmp .name , "--list_config" ]
118
118
) # add list_config as the action so we don't actually run
119
119
subconfig = getattr (_config , param_locs [option ])
120
+ os .remove (tmp .name )
120
121
assert getattr (subconfig , option ) == value
121
122
122
123
@@ -174,20 +175,21 @@ def test_cli_overrides_run_yaml():
174
175
175
176
orig_seed = 10101
176
177
override_seed = 37176
177
- with tempfile .NamedTemporaryFile (buffering = 0 , delete_on_close = False ) as tmp :
178
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as tmp :
178
179
tmp .write (f"---\n run:\n seed: { orig_seed } \n " .encode ("utf-8" ))
179
180
tmp .close ()
180
181
garak .cli .main (
181
182
["--config" , tmp .name , "-s" , f"{ override_seed } " , "--list_config" ]
182
183
) # add list_config as the action so we don't actually run
184
+ os .remove (tmp .name )
183
185
assert _config .run .seed == override_seed
184
186
185
187
186
188
# test probe_options YAML
187
189
def test_probe_options_yaml (capsys ):
188
190
importlib .reload (_config )
189
191
190
- with tempfile .NamedTemporaryFile (buffering = 0 , delete_on_close = False ) as tmp :
192
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as tmp :
191
193
tmp .write (
192
194
"""
193
195
---
@@ -204,14 +206,15 @@ def test_probe_options_yaml(capsys):
204
206
garak .cli .main (
205
207
["--config" , tmp .name , "--list_config" ]
206
208
) # add list_config as the action so we don't actually run
209
+ os .remove (tmp .name )
207
210
assert _config .plugins .probes ["test.Blank" ]["gen_x" ] == 37176
208
211
209
212
210
213
# test generator_options YAML
211
214
def test_generator_options_yaml (capsys ):
212
215
importlib .reload (_config )
213
216
214
- with tempfile .NamedTemporaryFile (buffering = 0 , delete_on_close = False ) as tmp :
217
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as tmp :
215
218
tmp .write (
216
219
"---\n plugins:\n model_type: test.Blank\n probe_spec: test.Blank\n generators:\n test.Blank:\n gen_x: 37176\n " .encode (
217
220
"utf-8"
@@ -221,21 +224,23 @@ def test_generator_options_yaml(capsys):
221
224
garak .cli .main (
222
225
["--config" , tmp .name , "--list_config" ]
223
226
) # add list_config as the action so we don't actually run
227
+ os .remove (tmp .name )
224
228
assert _config .plugins .generators ["test.Blank" ]["gen_x" ] == 37176
225
229
226
230
227
231
# can a run be launched from a run YAML?
228
232
def test_run_from_yaml (capsys ):
229
233
importlib .reload (_config )
230
234
231
- with tempfile .NamedTemporaryFile (buffering = 0 , delete_on_close = False ) as tmp :
235
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as tmp :
232
236
tmp .write (
233
237
"---\n run:\n generations: 10\n \n plugins:\n model_type: test.Blank\n probe_spec: test.Blank\n " .encode (
234
238
"utf-8"
235
239
)
236
240
)
237
241
tmp .close ()
238
242
garak .cli .main (["--config" , tmp .name ])
243
+ os .remove (tmp .name )
239
244
result = capsys .readouterr ()
240
245
output = result .out
241
246
all_output = ""
@@ -256,13 +261,14 @@ def test_cli_generator_options_file():
256
261
importlib .reload (_config )
257
262
258
263
# write an options file
259
- with tempfile .NamedTemporaryFile (mode = "w+" , delete_on_close = False ) as tmp :
264
+ with tempfile .NamedTemporaryFile (mode = "w+" , delete = False ) as tmp :
260
265
json .dump ({"test.Blank" : {"this_is_a" : "generator" }}, tmp )
261
266
tmp .close ()
262
267
# invoke cli
263
268
garak .cli .main (
264
269
["--generator_option_file" , tmp .name , "--list_config" ]
265
270
) # add list_config as the action so we don't actually run
271
+ os .remove (tmp .name )
266
272
267
273
# check it was loaded
268
274
assert _config .plugins .generators ["test.Blank" ] == {"this_is_a" : "generator" }
@@ -273,13 +279,14 @@ def test_cli_probe_options_file():
273
279
importlib .reload (_config )
274
280
275
281
# write an options file
276
- with tempfile .NamedTemporaryFile (mode = "w+" , delete_on_close = False ) as tmp :
282
+ with tempfile .NamedTemporaryFile (mode = "w+" , delete = False ) as tmp :
277
283
json .dump ({"test.Blank" : {"probes_in_this_config" : 1 }}, tmp )
278
284
tmp .close ()
279
285
# invoke cli
280
286
garak .cli .main (
281
287
["--probe_option_file" , tmp .name , "--list_config" ]
282
288
) # add list_config as the action so we don't actually run
289
+ os .remove (tmp .name )
283
290
284
291
# check it was loaded
285
292
assert _config .plugins .probes ["test.Blank" ] == {"probes_in_this_config" : 1 }
@@ -290,14 +297,10 @@ def test_cli_probe_options_overrides_yaml_probe_options():
290
297
importlib .reload (_config )
291
298
292
299
# write an options file
293
- with tempfile .NamedTemporaryFile (
294
- mode = "w+" , delete_on_close = False
295
- ) as probe_json_file :
300
+ with tempfile .NamedTemporaryFile (mode = "w+" , delete = False ) as probe_json_file :
296
301
json .dump ({"test.Blank" : {"goal" : "taken from CLI JSON" }}, probe_json_file )
297
302
probe_json_file .close ()
298
- with tempfile .NamedTemporaryFile (
299
- buffering = 0 , delete_on_close = False
300
- ) as probe_yaml_file :
303
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as probe_yaml_file :
301
304
probe_yaml_file .write (
302
305
"""
303
306
---
@@ -320,6 +323,8 @@ def test_cli_probe_options_overrides_yaml_probe_options():
320
323
"--list_config" ,
321
324
]
322
325
) # add list_config as the action so we don't actually run
326
+ os .remove (probe_json_file .name )
327
+ os .remove (probe_yaml_file .name )
323
328
# check it was loaded
324
329
assert _config .plugins .probes ["test.Blank" ]["goal" ] == "taken from CLI JSON"
325
330
@@ -329,9 +334,7 @@ def test_cli_generator_options_overrides_yaml_probe_options():
329
334
importlib .reload (_config )
330
335
331
336
cli_generations_count = 9001
332
- with tempfile .NamedTemporaryFile (
333
- buffering = 0 , delete_on_close = False
334
- ) as generator_yaml_file :
337
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as generator_yaml_file :
335
338
generator_yaml_file .write (
336
339
"""
337
340
---
@@ -351,6 +354,7 @@ def test_cli_generator_options_overrides_yaml_probe_options():
351
354
] # add list_config as the action so we don't actually run
352
355
print (args )
353
356
garak .cli .main (args )
357
+ os .remove (generator_yaml_file .name )
354
358
# check it was loaded
355
359
assert _config .run .generations == cli_generations_count
356
360
@@ -361,14 +365,15 @@ def test_blank_probe_instance_loads_yaml_config():
361
365
362
366
probe_name = "test.Blank"
363
367
revised_goal = "TEST GOAL make the model forget what to output"
364
- with tempfile .NamedTemporaryFile (buffering = 0 , delete_on_close = False ) as tmp :
368
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as tmp :
365
369
tmp .write (
366
370
f"---\n plugins:\n probes:\n { probe_name } :\n goal: { revised_goal } \n " .encode (
367
371
"utf-8"
368
372
)
369
373
)
370
374
tmp .close ()
371
375
garak .cli .main (["--config" , tmp .name , "-p" , probe_name ])
376
+ os .remove (tmp .name )
372
377
probe = garak ._plugins .load_plugin (f"probes.{ probe_name } " )
373
378
assert probe .goal == revised_goal
374
379
@@ -396,7 +401,7 @@ def test_blank_generator_instance_loads_yaml_config():
396
401
397
402
generator_name = "test.Blank"
398
403
revised_temp = 0.9001
399
- with tempfile .NamedTemporaryFile (buffering = 0 , delete_on_close = False ) as tmp :
404
+ with tempfile .NamedTemporaryFile (buffering = 0 , delete = False ) as tmp :
400
405
tmp .write (
401
406
f"---\n plugins:\n generators:\n { generator_name } :\n temperature: { revised_temp } \n " .encode (
402
407
"utf-8"
@@ -406,6 +411,7 @@ def test_blank_generator_instance_loads_yaml_config():
406
411
garak .cli .main (
407
412
["--config" , tmp .name , "--model_type" , generator_name , "--probes" , "none" ]
408
413
)
414
+ os .remove (tmp .name )
409
415
gen = garak ._plugins .load_plugin (f"generators.{ generator_name } " )
410
416
assert gen .temperature == revised_temp
411
417
0 commit comments