Skip to content

Commit 1ed631f

Browse files
committed
Move custom tempdir logic to test utils
1 parent 3467b4c commit 1ed631f

File tree

8 files changed

+57
-41
lines changed

8 files changed

+57
-41
lines changed

tests/data-files/get_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import os
66
import argparse
77
import json
8-
from tempfile import TemporaryDirectory
98
from subprocess import call
109
from typing import Any, Dict, List, Optional
1110
from urllib.error import HTTPError
1211

1312
import pystac
1413
from pystac.serialization import identify_stac_object
14+
from tests.utils import TemporaryDirectory
1515

1616

1717
def remove_bad_collection(js: Dict[str, Any]) -> Dict[str, Any]:
@@ -50,7 +50,7 @@ def remove_bad_collection(js: Dict[str, Any]) -> Dict[str, Any]:
5050

5151
examples_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "examples"))
5252

53-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
53+
with TemporaryDirectory() as tmp_dir:
5454
call(
5555
[
5656
"git",

tests/extensions/test_label.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import os
33
import unittest
4-
from tempfile import TemporaryDirectory
54

65
import pystac
76
from pystac import Catalog, Item, CatalogType
@@ -16,7 +15,7 @@
1615
)
1716
import pystac.validation
1817
from pystac.utils import get_opt
19-
from tests.utils import TestCases, assert_to_from_dict
18+
from tests.utils import TestCases, assert_to_from_dict, TemporaryDirectory
2019

2120

2221
class LabelTest(unittest.TestCase):
@@ -86,7 +85,7 @@ def test_validate_label(self) -> None:
8685
label_example_1_dict, pystac.STACObjectType.ITEM
8786
)
8887

89-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
88+
with TemporaryDirectory() as tmp_dir:
9089
cat_dir = os.path.join(tmp_dir, "catalog")
9190
catalog = TestCases.test_case_1()
9291
catalog.normalize_and_save(cat_dir, catalog_type=CatalogType.SELF_CONTAINED)

tests/test_catalog.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33
from typing import Any, Dict, List, Tuple, Union, cast
44
import unittest
5-
from tempfile import TemporaryDirectory
65
from datetime import datetime
76
from collections import defaultdict
87

@@ -18,13 +17,19 @@
1817
)
1918
from pystac.extensions.label import LabelClasses, LabelExtension, LabelType
2019
from pystac.utils import is_absolute_href, join_path_or_url, JoinType
21-
from tests.utils import TestCases, ARBITRARY_GEOM, ARBITRARY_BBOX, MockStacIO
20+
from tests.utils import (
21+
TestCases,
22+
ARBITRARY_GEOM,
23+
ARBITRARY_BBOX,
24+
MockStacIO,
25+
TemporaryDirectory,
26+
)
2227

2328

2429
class CatalogTypeTest(unittest.TestCase):
2530
def test_determine_type_for_absolute_published(self) -> None:
2631
cat = TestCases.test_case_1()
27-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
32+
with TemporaryDirectory() as tmp_dir:
2833
cat.normalize_and_save(tmp_dir, catalog_type=CatalogType.ABSOLUTE_PUBLISHED)
2934
cat_json = pystac.StacIO.default().read_json(
3035
os.path.join(tmp_dir, "catalog.json")
@@ -35,7 +40,7 @@ def test_determine_type_for_absolute_published(self) -> None:
3540

3641
def test_determine_type_for_relative_published(self) -> None:
3742
cat = TestCases.test_case_2()
38-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
43+
with TemporaryDirectory() as tmp_dir:
3944
cat.normalize_and_save(tmp_dir, catalog_type=CatalogType.RELATIVE_PUBLISHED)
4045
cat_json = pystac.StacIO.default().read_json(
4146
os.path.join(tmp_dir, "catalog.json")
@@ -63,7 +68,7 @@ def test_determine_type_for_unknown(self) -> None:
6368

6469
class CatalogTest(unittest.TestCase):
6570
def test_create_and_read(self) -> None:
66-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
71+
with TemporaryDirectory() as tmp_dir:
6772
cat_dir = os.path.join(tmp_dir, "catalog")
6873
catalog = TestCases.test_case_1()
6974

@@ -283,7 +288,7 @@ def test_clone_generates_correct_links(self) -> None:
283288
def test_save_uses_previous_catalog_type(self) -> None:
284289
catalog = TestCases.test_case_1()
285290
assert catalog.catalog_type == CatalogType.SELF_CONTAINED
286-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
291+
with TemporaryDirectory() as tmp_dir:
287292
catalog.normalize_hrefs(tmp_dir)
288293
href = catalog.self_href
289294
catalog.save()
@@ -360,7 +365,7 @@ def test_generate_subcatalogs_does_not_change_item_count(self) -> None:
360365

361366
catalog.generate_subcatalogs("${year}/${day}")
362367

363-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
368+
with TemporaryDirectory() as tmp_dir:
364369
catalog.normalize_hrefs(tmp_dir)
365370
catalog.save(pystac.CatalogType.SELF_CONTAINED)
366371

@@ -489,7 +494,7 @@ def item_mapper(item: pystac.Item) -> pystac.Item:
489494
item.properties["ITEM_MAPPER"] = "YEP"
490495
return item
491496

492-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
497+
with TemporaryDirectory() as tmp_dir:
493498
catalog = TestCases.test_case_1()
494499

495500
new_cat = catalog.map_items(item_mapper)
@@ -513,7 +518,7 @@ def item_mapper(item: pystac.Item) -> List[pystac.Item]:
513518
item2.properties["ITEM_MAPPER_2"] = "YEP"
514519
return [item, item2]
515520

516-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
521+
with TemporaryDirectory() as tmp_dir:
517522
catalog = TestCases.test_case_1()
518523
catalog_items = catalog.get_all_items()
519524

@@ -618,7 +623,7 @@ def asset_mapper(key: str, asset: pystac.Asset) -> pystac.Asset:
618623

619624
return asset
620625

621-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
626+
with TemporaryDirectory() as tmp_dir:
622627
catalog = TestCases.test_case_2()
623628

624629
new_cat = catalog.map_assets(asset_mapper)
@@ -651,7 +656,7 @@ def asset_mapper(
651656
else:
652657
return asset
653658

654-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
659+
with TemporaryDirectory() as tmp_dir:
655660
catalog = TestCases.test_case_2()
656661

657662
new_cat = catalog.map_assets(asset_mapper)
@@ -691,7 +696,7 @@ def asset_mapper(
691696
else:
692697
return asset
693698

694-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
699+
with TemporaryDirectory() as tmp_dir:
695700
catalog = TestCases.test_case_2()
696701

697702
new_cat = catalog.map_assets(asset_mapper)
@@ -766,7 +771,7 @@ def check_all_absolute(cat: Catalog) -> None:
766771
test_cases = TestCases.all_test_catalogs()
767772

768773
for catalog in test_cases:
769-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
774+
with TemporaryDirectory() as tmp_dir:
770775
c2 = catalog.full_copy()
771776
c2.normalize_hrefs(tmp_dir)
772777
c2.catalog_type = CatalogType.RELATIVE_PUBLISHED
@@ -792,7 +797,7 @@ def test_extra_fields(self) -> None:
792797

793798
catalog.extra_fields["type"] = "FeatureCollection"
794799

795-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
800+
with TemporaryDirectory() as tmp_dir:
796801
p = os.path.join(tmp_dir, "catalog.json")
797802
catalog.save_object(include_self_link=False, dest_href=p)
798803
with open(p) as f:
@@ -817,7 +822,7 @@ def test_validate_all(self) -> None:
817822
item = cat.get_item("area-1-1-labels", recursive=True)
818823
assert item is not None
819824
item.geometry = {"type": "INVALID", "coordinates": "NONE"}
820-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
825+
with TemporaryDirectory() as tmp_dir:
821826
cat.normalize_hrefs(tmp_dir)
822827
cat.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)
823828

@@ -838,7 +843,7 @@ def test_set_hrefs_manually(self) -> None:
838843
year += 1
839844
month += 1
840845

841-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
846+
with TemporaryDirectory() as tmp_dir:
842847
for root, _, items in catalog.walk():
843848

844849
# Set root's HREF based off the parent
@@ -928,7 +933,7 @@ def test_reading_iterating_and_writing_works_as_expected(self) -> None:
928933
for item in cat.get_all_items():
929934
pass
930935

931-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
936+
with TemporaryDirectory() as tmp_dir:
932937
new_stac_uri = os.path.join(tmp_dir, "test-case-6")
933938
cat.normalize_hrefs(new_stac_uri)
934939
cat.save(catalog_type=CatalogType.SELF_CONTAINED)
@@ -998,7 +1003,7 @@ def check_catalog(self, c: Catalog, tag: str) -> None:
9981003
self.check_item(item, tag)
9991004

10001005
def test_full_copy_1(self) -> None:
1001-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
1006+
with TemporaryDirectory() as tmp_dir:
10021007
cat = Catalog(id="test", description="test catalog")
10031008

10041009
item = Item(
@@ -1019,7 +1024,7 @@ def test_full_copy_1(self) -> None:
10191024
self.check_catalog(cat2, "dest")
10201025

10211026
def test_full_copy_2(self) -> None:
1022-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
1027+
with TemporaryDirectory() as tmp_dir:
10231028
cat = Catalog(id="test", description="test catalog")
10241029
image_item = Item(
10251030
id="Imagery",
@@ -1066,7 +1071,7 @@ def test_full_copy_2(self) -> None:
10661071
self.check_catalog(cat2, "dest")
10671072

10681073
def test_full_copy_3(self) -> None:
1069-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
1074+
with TemporaryDirectory() as tmp_dir:
10701075
root_cat = TestCases.test_case_1()
10711076
root_cat.normalize_hrefs(
10721077
os.path.join(tmp_dir, "catalog-full-copy-3-source")
@@ -1080,7 +1085,7 @@ def test_full_copy_3(self) -> None:
10801085
self.check_catalog(cat2, "dest")
10811086

10821087
def test_full_copy_4(self) -> None:
1083-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
1088+
with TemporaryDirectory() as tmp_dir:
10841089
root_cat = TestCases.test_case_2()
10851090
root_cat.normalize_hrefs(
10861091
os.path.join(tmp_dir, "catalog-full-copy-4-source")

tests/test_collection.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import unittest
22
import os
33
import json
4-
from tempfile import TemporaryDirectory
54
from datetime import datetime
65
from dateutil import tz
76

@@ -10,7 +9,7 @@
109
from pystac.validation import validate_dict
1110
from pystac import Collection, Item, Extent, SpatialExtent, TemporalExtent, CatalogType
1211
from pystac.utils import datetime_to_str
13-
from tests.utils import TestCases, ARBITRARY_GEOM, ARBITRARY_BBOX
12+
from tests.utils import TestCases, ARBITRARY_GEOM, ARBITRARY_BBOX, TemporaryDirectory
1413

1514
TEST_DATETIME = datetime(2020, 3, 14, 16, 32)
1615

@@ -35,7 +34,7 @@ def test_save_uses_previous_catalog_type(self) -> None:
3534
collection = TestCases.test_case_8()
3635
assert collection.STAC_OBJECT_TYPE == pystac.STACObjectType.COLLECTION
3736
self.assertEqual(collection.catalog_type, CatalogType.SELF_CONTAINED)
38-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
37+
with TemporaryDirectory() as tmp_dir:
3938
collection.normalize_hrefs(tmp_dir)
4039
href = collection.self_href
4140
collection.save()
@@ -84,7 +83,7 @@ def test_extra_fields(self) -> None:
8483

8584
collection.extra_fields["test"] = "extra"
8685

87-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
86+
with TemporaryDirectory() as tmp_dir:
8887
p = os.path.join(tmp_dir, "collection.json")
8988
collection.save_object(include_self_link=False, dest_href=p)
9089
with open(p) as f:

tests/test_item.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import json
44
from typing import Any, Dict, List
55
import unittest
6-
from tempfile import TemporaryDirectory
76

87
import pystac
98
from pystac import Asset, Item, Provider
109
from pystac.validation import validate_dict
1110
import pystac.serialization.common_properties
1211
from pystac.item import CommonMetadata
1312
from pystac.utils import datetime_to_str, get_opt, str_to_datetime, is_absolute_href
14-
from tests.utils import TestCases, assert_to_from_dict
13+
from tests.utils import TestCases, assert_to_from_dict, TemporaryDirectory
1514

1615

1716
class ItemTest(unittest.TestCase):
@@ -73,7 +72,7 @@ def test_extra_fields(self) -> None:
7372

7473
item.extra_fields["test"] = "extra"
7574

76-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
75+
with TemporaryDirectory() as tmp_dir:
7776
p = os.path.join(tmp_dir, "item.json")
7877
item.save_object(include_self_link=False, dest_href=p)
7978
with open(p) as f:

tests/test_writing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import os
21
import unittest
3-
from tempfile import TemporaryDirectory
42
from typing import Any, List
53

64
import pystac
75
from pystac import Collection, CatalogType, HIERARCHICAL_LINKS
86
from pystac.utils import is_absolute_href, make_absolute_href, make_relative_href
97
from pystac.validation import validate_dict
108

11-
from tests.utils import TestCases
9+
from tests.utils import TestCases, TemporaryDirectory
1210

1311

1412
class STACWritingTest(unittest.TestCase):
@@ -106,7 +104,7 @@ def validate_catalog_link_type(
106104
def do_test(
107105
self, catalog: pystac.Catalog, catalog_type: pystac.CatalogType
108106
) -> None:
109-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
107+
with TemporaryDirectory() as tmp_dir:
110108
catalog.normalize_hrefs(tmp_dir)
111109
self.validate_catalog(catalog)
112110

tests/utils/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# flake8: noqa
22

3-
from typing import Any, Dict, Type
3+
import os
4+
import tempfile
5+
from typing import Any, Dict, Type, Optional
46
import unittest
57
from tests.utils.test_cases import (
68
TestCases,
@@ -41,3 +43,18 @@ def _parse_times(a_dict: Dict[str, Any]) -> None:
4143
_parse_times(d1)
4244
_parse_times(d2)
4345
test_class.assertDictEqual(d1, d2)
46+
47+
48+
# Mypy raises an error for this class:
49+
# error: Missing type parameters for generic type "TemporaryDirectory" [type-arg]
50+
# Trying to add a concrete type (e.g. TemporaryDirectory[str]) satisfies mypy, but
51+
# raises a runtime exception:
52+
# TypeError: 'type' object is not subscriptable
53+
class TemporaryDirectory(tempfile.TemporaryDirectory): # type: ignore [type-arg]
54+
def __init__(self, suffix: Optional[str] = None, prefix: Optional[str] = None):
55+
"""In the GitHub Actions Windows runner the default TMPDIR directory is on a
56+
different drive (C:\\) than the code and test data files (D:\\). This was causing
57+
failures in os.path.relpath on Windows, so we put the temp directories in the
58+
current working directory instead. There os a "tmp*" line in the .gitignore file
59+
that ignores these directories."""
60+
super().__init__(suffix=suffix, prefix=prefix, dir=os.getcwd())

tests/validation/test_validate.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
from pystac.utils import get_opt
66
import shutil
77
import unittest
8-
from tempfile import TemporaryDirectory
98

109
import jsonschema
1110

1211
import pystac
1312
import pystac.validation
1413
from pystac.cache import CollectionCache
1514
from pystac.serialization.common_properties import merge_common_properties
16-
from tests.utils import TestCases
15+
from tests.utils import TestCases, TemporaryDirectory
1716

1817

1918
class ValidateTest(unittest.TestCase):
@@ -99,7 +98,7 @@ def test_validate_all(self) -> None:
9998
# Modify a 0.8.1 collection in a catalog to be invalid with a
10099
# since-renamed extension and make sure it catches the validation error.
101100

102-
with TemporaryDirectory(dir=os.getcwd()) as tmp_dir:
101+
with TemporaryDirectory() as tmp_dir:
103102
dst_dir = os.path.join(tmp_dir, "catalog")
104103
# Copy test case 7 to the temporary directory
105104
catalog_href = get_opt(TestCases.test_case_7().get_self_href())

0 commit comments

Comments
 (0)