Skip to content

Commit a370b78

Browse files
committed
portable temp file remove for 3.10+
Remove temp files manually for OS portablity in python 3.10+ `delete_on_close` for temp files is added in [3.12](python/cpython#97015) Signed-off-by: Jeffrey Martin <[email protected]>
1 parent b1e1017 commit a370b78

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

tests/buffs/test_buff_config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
def test_include_original_prompt():
2626
# https://github.com/python/cpython/pull/97015 to ensure Windows compatibility
27-
with tempfile.NamedTemporaryFile(buffering=0, delete_on_close=False) as tmp:
27+
with tempfile.NamedTemporaryFile(buffering=0, delete=False) as tmp:
2828
tmp.write(
2929
"""---
3030
plugins:
@@ -37,6 +37,7 @@ def test_include_original_prompt():
3737
garak.cli.main(
3838
f"-m test -p test.Test -b lowercase.Lowercase --config {tmp.name} --report_prefix {prefix}".split()
3939
)
40+
os.remove(tmp.name)
4041

4142
prompts = []
4243
with open(f"{prefix}.report.jsonl", "r", encoding="utf-8") as reportfile:
@@ -57,7 +58,7 @@ def test_include_original_prompt():
5758

5859

5960
def test_exclude_original_prompt():
60-
with tempfile.NamedTemporaryFile(buffering=0, delete_on_close=False) as tmp:
61+
with tempfile.NamedTemporaryFile(buffering=0, delete=False) as tmp:
6162
tmp.write(
6263
"""---
6364
plugins:
@@ -70,6 +71,7 @@ def test_exclude_original_prompt():
7071
garak.cli.main(
7172
f"-m test -p test.Test -b lowercase.Lowercase --config {tmp.name} --report_prefix {prefix}".split()
7273
)
74+
os.remove(tmp.name)
7375

7476
prompts = []
7577
with open(f"{prefix}.report.jsonl", "r", encoding="utf-8") as reportfile:

tests/test_config.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ def test_yaml_param_settings(param):
110110
importlib.reload(_config)
111111

112112
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:
114114
tmp.write(f"---\n{param_locs[option]}:\n {option}: {value}\n".encode("utf-8"))
115115
tmp.close()
116116
garak.cli.main(
117117
["--config", tmp.name, "--list_config"]
118118
) # add list_config as the action so we don't actually run
119119
subconfig = getattr(_config, param_locs[option])
120+
os.remove(tmp.name)
120121
assert getattr(subconfig, option) == value
121122

122123

@@ -174,20 +175,21 @@ def test_cli_overrides_run_yaml():
174175

175176
orig_seed = 10101
176177
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:
178179
tmp.write(f"---\nrun:\n seed: {orig_seed}\n".encode("utf-8"))
179180
tmp.close()
180181
garak.cli.main(
181182
["--config", tmp.name, "-s", f"{override_seed}", "--list_config"]
182183
) # add list_config as the action so we don't actually run
184+
os.remove(tmp.name)
183185
assert _config.run.seed == override_seed
184186

185187

186188
# test probe_options YAML
187189
def test_probe_options_yaml(capsys):
188190
importlib.reload(_config)
189191

190-
with tempfile.NamedTemporaryFile(buffering=0, delete_on_close=False) as tmp:
192+
with tempfile.NamedTemporaryFile(buffering=0, delete=False) as tmp:
191193
tmp.write(
192194
"""
193195
---
@@ -204,14 +206,15 @@ def test_probe_options_yaml(capsys):
204206
garak.cli.main(
205207
["--config", tmp.name, "--list_config"]
206208
) # add list_config as the action so we don't actually run
209+
os.remove(tmp.name)
207210
assert _config.plugins.probes["test.Blank"]["gen_x"] == 37176
208211

209212

210213
# test generator_options YAML
211214
def test_generator_options_yaml(capsys):
212215
importlib.reload(_config)
213216

214-
with tempfile.NamedTemporaryFile(buffering=0, delete_on_close=False) as tmp:
217+
with tempfile.NamedTemporaryFile(buffering=0, delete=False) as tmp:
215218
tmp.write(
216219
"---\nplugins:\n model_type: test.Blank\n probe_spec: test.Blank\n generators:\n test.Blank:\n gen_x: 37176\n".encode(
217220
"utf-8"
@@ -221,21 +224,23 @@ def test_generator_options_yaml(capsys):
221224
garak.cli.main(
222225
["--config", tmp.name, "--list_config"]
223226
) # add list_config as the action so we don't actually run
227+
os.remove(tmp.name)
224228
assert _config.plugins.generators["test.Blank"]["gen_x"] == 37176
225229

226230

227231
# can a run be launched from a run YAML?
228232
def test_run_from_yaml(capsys):
229233
importlib.reload(_config)
230234

231-
with tempfile.NamedTemporaryFile(buffering=0, delete_on_close=False) as tmp:
235+
with tempfile.NamedTemporaryFile(buffering=0, delete=False) as tmp:
232236
tmp.write(
233237
"---\nrun:\n generations: 10\n\nplugins:\n model_type: test.Blank\n probe_spec: test.Blank\n".encode(
234238
"utf-8"
235239
)
236240
)
237241
tmp.close()
238242
garak.cli.main(["--config", tmp.name])
243+
os.remove(tmp.name)
239244
result = capsys.readouterr()
240245
output = result.out
241246
all_output = ""
@@ -256,13 +261,14 @@ def test_cli_generator_options_file():
256261
importlib.reload(_config)
257262

258263
# 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:
260265
json.dump({"test.Blank": {"this_is_a": "generator"}}, tmp)
261266
tmp.close()
262267
# invoke cli
263268
garak.cli.main(
264269
["--generator_option_file", tmp.name, "--list_config"]
265270
) # add list_config as the action so we don't actually run
271+
os.remove(tmp.name)
266272

267273
# check it was loaded
268274
assert _config.plugins.generators["test.Blank"] == {"this_is_a": "generator"}
@@ -273,13 +279,14 @@ def test_cli_probe_options_file():
273279
importlib.reload(_config)
274280

275281
# 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:
277283
json.dump({"test.Blank": {"probes_in_this_config": 1}}, tmp)
278284
tmp.close()
279285
# invoke cli
280286
garak.cli.main(
281287
["--probe_option_file", tmp.name, "--list_config"]
282288
) # add list_config as the action so we don't actually run
289+
os.remove(tmp.name)
283290

284291
# check it was loaded
285292
assert _config.plugins.probes["test.Blank"] == {"probes_in_this_config": 1}
@@ -290,14 +297,10 @@ def test_cli_probe_options_overrides_yaml_probe_options():
290297
importlib.reload(_config)
291298

292299
# 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:
296301
json.dump({"test.Blank": {"goal": "taken from CLI JSON"}}, probe_json_file)
297302
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:
301304
probe_yaml_file.write(
302305
"""
303306
---
@@ -320,6 +323,8 @@ def test_cli_probe_options_overrides_yaml_probe_options():
320323
"--list_config",
321324
]
322325
) # 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)
323328
# check it was loaded
324329
assert _config.plugins.probes["test.Blank"]["goal"] == "taken from CLI JSON"
325330

@@ -329,9 +334,7 @@ def test_cli_generator_options_overrides_yaml_probe_options():
329334
importlib.reload(_config)
330335

331336
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:
335338
generator_yaml_file.write(
336339
"""
337340
---
@@ -351,6 +354,7 @@ def test_cli_generator_options_overrides_yaml_probe_options():
351354
] # add list_config as the action so we don't actually run
352355
print(args)
353356
garak.cli.main(args)
357+
os.remove(generator_yaml_file.name)
354358
# check it was loaded
355359
assert _config.run.generations == cli_generations_count
356360

@@ -361,14 +365,15 @@ def test_blank_probe_instance_loads_yaml_config():
361365

362366
probe_name = "test.Blank"
363367
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:
365369
tmp.write(
366370
f"---\nplugins:\n probes:\n {probe_name}:\n goal: {revised_goal}\n".encode(
367371
"utf-8"
368372
)
369373
)
370374
tmp.close()
371375
garak.cli.main(["--config", tmp.name, "-p", probe_name])
376+
os.remove(tmp.name)
372377
probe = garak._plugins.load_plugin(f"probes.{probe_name}")
373378
assert probe.goal == revised_goal
374379

@@ -396,7 +401,7 @@ def test_blank_generator_instance_loads_yaml_config():
396401

397402
generator_name = "test.Blank"
398403
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:
400405
tmp.write(
401406
f"---\nplugins:\n generators:\n {generator_name}:\n temperature: {revised_temp}\n".encode(
402407
"utf-8"
@@ -406,6 +411,7 @@ def test_blank_generator_instance_loads_yaml_config():
406411
garak.cli.main(
407412
["--config", tmp.name, "--model_type", generator_name, "--probes", "none"]
408413
)
414+
os.remove(tmp.name)
409415
gen = garak._plugins.load_plugin(f"generators.{generator_name}")
410416
assert gen.temperature == revised_temp
411417

0 commit comments

Comments
 (0)