Skip to content

Commit d454aad

Browse files
authored
Remove tempfile related tempdirs (switch to pytest tmp_path) (#276)
1 parent 18b48cd commit d454aad

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

tests/test_cli.py

+36-35
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import glob
66
import os
77
import shutil
8-
import tempfile
98
from pathlib import Path
109
from unittest import mock
1110

@@ -111,10 +110,10 @@ def test_check_build_args(args, raises):
111110
"2024-01-01",
112111
],
113112
)
114-
def test_build(version, test_data):
113+
def test_build(version, test_data, tmp_path):
115114
"""Test full catalog build process from config files"""
116115
# Update the config_yaml paths
117-
build_base_path = tempfile.TemporaryDirectory().name
116+
build_base_path = str(tmp_path)
118117

119118
configs = [
120119
str(test_data / fname)
@@ -164,10 +163,10 @@ def test_build(version, test_data):
164163
"v0.1.2", # Old-style version numbers
165164
],
166165
)
167-
def test_build_bad_version(bad_vers, test_data):
166+
def test_build_bad_version(bad_vers, test_data, tmp_path):
168167
"""Test full catalog build process from config files"""
169168
# Update the config_yaml paths
170-
build_base_path = tempfile.TemporaryDirectory().name
169+
build_base_path = str(tmp_path)
171170

172171
configs = [
173172
str(test_data / fname)
@@ -201,22 +200,23 @@ def test_build_bad_version(bad_vers, test_data):
201200
"config/access-om2-bad.yaml",
202201
"config/cmip5.yaml",
203202
],
204-
build_base_path=tempfile.TemporaryDirectory().name, # Use pytest fixture here?
203+
build_base_path=None, # Use pytest fixture here?
205204
catalog_base_path=None, # Not required, get_catalog_fp is mocked
206205
data_base_path="",
207206
catalog_file="cat.csv",
208207
version="v2024-01-01",
209208
no_update=False,
210209
),
211210
)
212-
def test_build_bad_metadata(mockargs, get_catalog_fp, test_data):
211+
def test_build_bad_metadata(mockargs, get_catalog_fp, test_data, tmp_path):
213212
"""
214213
Test if bad metadata is detected
215214
"""
216215
# Update the config_yaml paths
217216
for i, p in enumerate(mockargs.return_value.config_yaml):
218217
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
219218
mockargs.return_value.data_base_path = test_data
219+
mockargs.return_value.build_base_path = str(tmp_path)
220220

221221
# Write the catalog.yamls to where the catalogs go
222222
get_catalog_fp.return_value = os.path.join(
@@ -235,15 +235,15 @@ def test_build_bad_metadata(mockargs, get_catalog_fp, test_data):
235235
"config/access-om2.yaml",
236236
"config/cmip5.yaml",
237237
],
238-
build_base_path=tempfile.TemporaryDirectory().name, # Use pytest fixture here?
238+
build_base_path="", # Use pytest fixture here?
239239
catalog_base_path=None, # Not required, get_catalog_fp is mocked
240240
data_base_path="",
241241
catalog_file="cat.csv",
242242
version="v2024-01-01",
243243
no_update=False,
244244
),
245245
)
246-
def test_build_repeat_nochange(mockargs, get_catalog_fp, test_data):
246+
def test_build_repeat_nochange(mockargs, get_catalog_fp, test_data, tmp_path):
247247
"""
248248
Test if the intelligent versioning works correctly when there is
249249
no significant change to the underlying catalogue
@@ -252,6 +252,7 @@ def test_build_repeat_nochange(mockargs, get_catalog_fp, test_data):
252252
for i, p in enumerate(mockargs.return_value.config_yaml):
253253
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
254254
mockargs.return_value.data_base_path = test_data
255+
mockargs.return_value.build_base_path = str(tmp_path)
255256

256257
# Write the catalog.yamls to where the catalogs go
257258
get_catalog_fp.return_value = os.path.join(
@@ -291,19 +292,20 @@ def test_build_repeat_nochange(mockargs, get_catalog_fp, test_data):
291292
"config/access-om2.yaml",
292293
# "config/cmip5.yaml", # Save this for addition
293294
],
294-
build_base_path=tempfile.TemporaryDirectory().name, # Use pytest fixture here?
295+
build_base_path=None, # Use pytest fixture here?
295296
catalog_base_path=None, # Not required, get_catalog_fp is mocked
296297
data_base_path="",
297298
catalog_file="cat.csv",
298299
version="v2024-01-01",
299300
no_update=False,
300301
),
301302
)
302-
def test_build_repeat_adddata(mockargs, get_catalog_fp, test_data):
303+
def test_build_repeat_adddata(mockargs, get_catalog_fp, test_data, tmp_path):
303304
# Update the config_yaml paths
304305
for i, p in enumerate(mockargs.return_value.config_yaml):
305306
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
306307
mockargs.return_value.data_base_path = test_data
308+
mockargs.return_value.build_base_path = str(tmp_path)
307309

308310
# Write the catalog.yamls to where the catalogs go
309311
get_catalog_fp.return_value = os.path.join(
@@ -364,15 +366,15 @@ def test_build_repeat_adddata(mockargs, get_catalog_fp, test_data):
364366
("v2001-01-01", None),
365367
],
366368
)
367-
def test_build_existing_data(mockargs, get_catalog_fp, test_data, min_vers, max_vers):
369+
def test_build_existing_data(
370+
mockargs, get_catalog_fp, test_data, min_vers, max_vers, tmp_path
371+
):
368372
"""
369373
Test if the build process can handle min and max catalog
370374
versions when an original catalog.yaml does not exist
371375
"""
372376
# New temp directory for each test
373-
mockargs.return_value.build_base_path = (
374-
tempfile.TemporaryDirectory().name
375-
) # Use pytest fixture here?
377+
mockargs.return_value.build_base_path = str(tmp_path) # Use pytest fixture here?
376378
# Update the config_yaml paths
377379
for i, p in enumerate(mockargs.return_value.config_yaml):
378380
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
@@ -438,16 +440,14 @@ def test_build_existing_data(mockargs, get_catalog_fp, test_data, min_vers, max_
438440
],
439441
)
440442
def test_build_existing_data_existing_old_cat(
441-
mockargs, get_catalog_fp, test_data, min_vers, max_vers
443+
mockargs, get_catalog_fp, test_data, min_vers, max_vers, tmp_path
442444
):
443445
"""
444446
Test if the build process can handle min and max catalog
445447
versions when a old-style catalog.yaml exists
446448
"""
447449
# New temp directory for each test
448-
mockargs.return_value.build_base_path = (
449-
tempfile.TemporaryDirectory().name
450-
) # Use pytest fixture here?
450+
mockargs.return_value.build_base_path = str(tmp_path) # Use pytest fixture here?
451451
# Update the config_yaml paths
452452
for i, p in enumerate(mockargs.return_value.config_yaml):
453453
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
@@ -525,22 +525,23 @@ def test_build_existing_data_existing_old_cat(
525525
],
526526
)
527527
def test_build_separation_between_catalog_and_buildbase(
528-
mockargs, get_catalog_fp, test_data, min_vers, max_vers
528+
mockargs, get_catalog_fp, test_data, min_vers, max_vers, tmp_path
529529
):
530530
"""
531531
Test if the intelligent versioning works correctly when there is
532532
no significant change to the underlying catalogue
533533
"""
534-
mockargs.return_value.build_base_path = (
535-
tempfile.TemporaryDirectory().name
536-
) # Use pytest fixture here?
537-
# Update the config_yaml paths
534+
bbp = os.path.join(tmp_path, "bbp")
535+
os.mkdir(bbp)
536+
catdir = os.path.join(tmp_path, "catdir")
537+
os.mkdir(catdir)
538+
mockargs.return_value.build_base_path = str(bbp)
538539
for i, p in enumerate(mockargs.return_value.config_yaml):
539540
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
540541
mockargs.return_value.data_base_path = test_data
541542

542543
# Write the catalog.yamls to its own directory
543-
catalog_dir = tempfile.TemporaryDirectory().name
544+
catalog_dir = str(catdir)
544545
mockargs.return_value.catalog_base_path = catalog_dir
545546
get_catalog_fp.return_value = os.path.join(catalog_dir, "catalog.yaml")
546547

@@ -604,14 +605,14 @@ def test_build_separation_between_catalog_and_buildbase(
604605
],
605606
)
606607
def test_build_repeat_renamecatalogyaml(
607-
mockargs, get_catalog_fp, test_data, min_vers, max_vers
608+
mockargs, get_catalog_fp, test_data, min_vers, max_vers, tmp_path
608609
):
609610
# Update the config_yaml paths
610611
for i, p in enumerate(mockargs.return_value.config_yaml):
611612
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
612613
mockargs.return_value.data_base_path = test_data
613614

614-
mockargs.return_value.build_base_path = tempfile.TemporaryDirectory().name
615+
mockargs.return_value.build_base_path = str(tmp_path)
615616
mockargs.return_value.version = (
616617
"v2024-01-01" # May have been overridden in previous parametrize pass
617618
)
@@ -713,14 +714,14 @@ def test_build_repeat_renamecatalogyaml(
713714
],
714715
)
715716
def test_build_repeat_altercatalogstruct(
716-
mockargs, get_catalog_fp, test_data, min_vers, max_vers
717+
mockargs, get_catalog_fp, test_data, min_vers, max_vers, tmp_path
717718
):
718719
# Update the config_yaml paths
719720
for i, p in enumerate(mockargs.return_value.config_yaml):
720721
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
721722
mockargs.return_value.data_base_path = test_data
722723

723-
mockargs.return_value.build_base_path = tempfile.TemporaryDirectory().name
724+
mockargs.return_value.build_base_path = str(tmp_path)
724725
mockargs.return_value.version = (
725726
"v2024-01-01" # May have been overridden in previous parametrize pass
726727
)
@@ -819,14 +820,14 @@ def test_build_repeat_altercatalogstruct(
819820
],
820821
)
821822
def test_build_repeat_altercatalogstruct_multivers(
822-
mockargs, get_catalog_fp, test_data, min_vers, max_vers
823+
mockargs, get_catalog_fp, test_data, min_vers, max_vers, tmp_path
823824
):
824825
# Update the config_yaml paths
825826
for i, p in enumerate(mockargs.return_value.config_yaml):
826827
mockargs.return_value.config_yaml[i] = os.path.join(test_data, p)
827828
mockargs.return_value.data_base_path = test_data
828829

829-
mockargs.return_value.build_base_path = tempfile.TemporaryDirectory().name
830+
mockargs.return_value.build_base_path = str(tmp_path)
830831
mockargs.return_value.version = (
831832
"v2024-01-01" # May have been overridden in previous parametrize pass
832833
)
@@ -945,10 +946,10 @@ def test_metadata_validate_no_file():
945946
assert "No such file(s)" in str(excinfo.value)
946947

947948

948-
def test_metadata_template():
949-
loc = tempfile.TemporaryDirectory()
950-
metadata_template(loc=loc.name)
951-
if not os.path.isfile(os.path.join(loc.name, "metadata.yaml")):
949+
def test_metadata_template(tmp_path):
950+
loc = str(tmp_path)
951+
metadata_template(loc=loc)
952+
if not os.path.isfile(os.path.join(loc, "metadata.yaml")):
952953
raise RuntimeError("Didn't write template into temp dir")
953954

954955

0 commit comments

Comments
 (0)