5
5
import glob
6
6
import os
7
7
import shutil
8
- import tempfile
9
8
from pathlib import Path
10
9
from unittest import mock
11
10
@@ -111,10 +110,10 @@ def test_check_build_args(args, raises):
111
110
"2024-01-01" ,
112
111
],
113
112
)
114
- def test_build (version , test_data ):
113
+ def test_build (version , test_data , tmp_path ):
115
114
"""Test full catalog build process from config files"""
116
115
# Update the config_yaml paths
117
- build_base_path = tempfile . TemporaryDirectory (). name
116
+ build_base_path = str ( tmp_path )
118
117
119
118
configs = [
120
119
str (test_data / fname )
@@ -164,10 +163,10 @@ def test_build(version, test_data):
164
163
"v0.1.2" , # Old-style version numbers
165
164
],
166
165
)
167
- def test_build_bad_version (bad_vers , test_data ):
166
+ def test_build_bad_version (bad_vers , test_data , tmp_path ):
168
167
"""Test full catalog build process from config files"""
169
168
# Update the config_yaml paths
170
- build_base_path = tempfile . TemporaryDirectory (). name
169
+ build_base_path = str ( tmp_path )
171
170
172
171
configs = [
173
172
str (test_data / fname )
@@ -201,22 +200,23 @@ def test_build_bad_version(bad_vers, test_data):
201
200
"config/access-om2-bad.yaml" ,
202
201
"config/cmip5.yaml" ,
203
202
],
204
- build_base_path = tempfile . TemporaryDirectory (). name , # Use pytest fixture here?
203
+ build_base_path = None , # Use pytest fixture here?
205
204
catalog_base_path = None , # Not required, get_catalog_fp is mocked
206
205
data_base_path = "" ,
207
206
catalog_file = "cat.csv" ,
208
207
version = "v2024-01-01" ,
209
208
no_update = False ,
210
209
),
211
210
)
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 ):
213
212
"""
214
213
Test if bad metadata is detected
215
214
"""
216
215
# Update the config_yaml paths
217
216
for i , p in enumerate (mockargs .return_value .config_yaml ):
218
217
mockargs .return_value .config_yaml [i ] = os .path .join (test_data , p )
219
218
mockargs .return_value .data_base_path = test_data
219
+ mockargs .return_value .build_base_path = str (tmp_path )
220
220
221
221
# Write the catalog.yamls to where the catalogs go
222
222
get_catalog_fp .return_value = os .path .join (
@@ -235,15 +235,15 @@ def test_build_bad_metadata(mockargs, get_catalog_fp, test_data):
235
235
"config/access-om2.yaml" ,
236
236
"config/cmip5.yaml" ,
237
237
],
238
- build_base_path = tempfile . TemporaryDirectory (). name , # Use pytest fixture here?
238
+ build_base_path = "" , # Use pytest fixture here?
239
239
catalog_base_path = None , # Not required, get_catalog_fp is mocked
240
240
data_base_path = "" ,
241
241
catalog_file = "cat.csv" ,
242
242
version = "v2024-01-01" ,
243
243
no_update = False ,
244
244
),
245
245
)
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 ):
247
247
"""
248
248
Test if the intelligent versioning works correctly when there is
249
249
no significant change to the underlying catalogue
@@ -252,6 +252,7 @@ def test_build_repeat_nochange(mockargs, get_catalog_fp, test_data):
252
252
for i , p in enumerate (mockargs .return_value .config_yaml ):
253
253
mockargs .return_value .config_yaml [i ] = os .path .join (test_data , p )
254
254
mockargs .return_value .data_base_path = test_data
255
+ mockargs .return_value .build_base_path = str (tmp_path )
255
256
256
257
# Write the catalog.yamls to where the catalogs go
257
258
get_catalog_fp .return_value = os .path .join (
@@ -291,19 +292,20 @@ def test_build_repeat_nochange(mockargs, get_catalog_fp, test_data):
291
292
"config/access-om2.yaml" ,
292
293
# "config/cmip5.yaml", # Save this for addition
293
294
],
294
- build_base_path = tempfile . TemporaryDirectory (). name , # Use pytest fixture here?
295
+ build_base_path = None , # Use pytest fixture here?
295
296
catalog_base_path = None , # Not required, get_catalog_fp is mocked
296
297
data_base_path = "" ,
297
298
catalog_file = "cat.csv" ,
298
299
version = "v2024-01-01" ,
299
300
no_update = False ,
300
301
),
301
302
)
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 ):
303
304
# Update the config_yaml paths
304
305
for i , p in enumerate (mockargs .return_value .config_yaml ):
305
306
mockargs .return_value .config_yaml [i ] = os .path .join (test_data , p )
306
307
mockargs .return_value .data_base_path = test_data
308
+ mockargs .return_value .build_base_path = str (tmp_path )
307
309
308
310
# Write the catalog.yamls to where the catalogs go
309
311
get_catalog_fp .return_value = os .path .join (
@@ -364,15 +366,15 @@ def test_build_repeat_adddata(mockargs, get_catalog_fp, test_data):
364
366
("v2001-01-01" , None ),
365
367
],
366
368
)
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
+ ):
368
372
"""
369
373
Test if the build process can handle min and max catalog
370
374
versions when an original catalog.yaml does not exist
371
375
"""
372
376
# 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?
376
378
# Update the config_yaml paths
377
379
for i , p in enumerate (mockargs .return_value .config_yaml ):
378
380
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_
438
440
],
439
441
)
440
442
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
442
444
):
443
445
"""
444
446
Test if the build process can handle min and max catalog
445
447
versions when a old-style catalog.yaml exists
446
448
"""
447
449
# 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?
451
451
# Update the config_yaml paths
452
452
for i , p in enumerate (mockargs .return_value .config_yaml ):
453
453
mockargs .return_value .config_yaml [i ] = os .path .join (test_data , p )
@@ -525,22 +525,23 @@ def test_build_existing_data_existing_old_cat(
525
525
],
526
526
)
527
527
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
529
529
):
530
530
"""
531
531
Test if the intelligent versioning works correctly when there is
532
532
no significant change to the underlying catalogue
533
533
"""
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 )
538
539
for i , p in enumerate (mockargs .return_value .config_yaml ):
539
540
mockargs .return_value .config_yaml [i ] = os .path .join (test_data , p )
540
541
mockargs .return_value .data_base_path = test_data
541
542
542
543
# Write the catalog.yamls to its own directory
543
- catalog_dir = tempfile . TemporaryDirectory (). name
544
+ catalog_dir = str ( catdir )
544
545
mockargs .return_value .catalog_base_path = catalog_dir
545
546
get_catalog_fp .return_value = os .path .join (catalog_dir , "catalog.yaml" )
546
547
@@ -604,14 +605,14 @@ def test_build_separation_between_catalog_and_buildbase(
604
605
],
605
606
)
606
607
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
608
609
):
609
610
# Update the config_yaml paths
610
611
for i , p in enumerate (mockargs .return_value .config_yaml ):
611
612
mockargs .return_value .config_yaml [i ] = os .path .join (test_data , p )
612
613
mockargs .return_value .data_base_path = test_data
613
614
614
- mockargs .return_value .build_base_path = tempfile . TemporaryDirectory (). name
615
+ mockargs .return_value .build_base_path = str ( tmp_path )
615
616
mockargs .return_value .version = (
616
617
"v2024-01-01" # May have been overridden in previous parametrize pass
617
618
)
@@ -713,14 +714,14 @@ def test_build_repeat_renamecatalogyaml(
713
714
],
714
715
)
715
716
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
717
718
):
718
719
# Update the config_yaml paths
719
720
for i , p in enumerate (mockargs .return_value .config_yaml ):
720
721
mockargs .return_value .config_yaml [i ] = os .path .join (test_data , p )
721
722
mockargs .return_value .data_base_path = test_data
722
723
723
- mockargs .return_value .build_base_path = tempfile . TemporaryDirectory (). name
724
+ mockargs .return_value .build_base_path = str ( tmp_path )
724
725
mockargs .return_value .version = (
725
726
"v2024-01-01" # May have been overridden in previous parametrize pass
726
727
)
@@ -819,14 +820,14 @@ def test_build_repeat_altercatalogstruct(
819
820
],
820
821
)
821
822
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
823
824
):
824
825
# Update the config_yaml paths
825
826
for i , p in enumerate (mockargs .return_value .config_yaml ):
826
827
mockargs .return_value .config_yaml [i ] = os .path .join (test_data , p )
827
828
mockargs .return_value .data_base_path = test_data
828
829
829
- mockargs .return_value .build_base_path = tempfile . TemporaryDirectory (). name
830
+ mockargs .return_value .build_base_path = str ( tmp_path )
830
831
mockargs .return_value .version = (
831
832
"v2024-01-01" # May have been overridden in previous parametrize pass
832
833
)
@@ -945,10 +946,10 @@ def test_metadata_validate_no_file():
945
946
assert "No such file(s)" in str (excinfo .value )
946
947
947
948
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" )):
952
953
raise RuntimeError ("Didn't write template into temp dir" )
953
954
954
955
0 commit comments