Skip to content

Commit

Permalink
Merge pull request #445 from duckontheweb/fix/gh-418-temp-dir
Browse files Browse the repository at this point in the history
Use environment variable for tmp dir location
  • Loading branch information
Jon Duckworth authored Jun 15, 2021
2 parents cfff3c5 + 2bd05b6 commit f90d52b
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 55 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ jobs:
path: ~\AppData\Local\pip\Cache
# Cache based on OS, Python version, and dependency hash
key: pip-${{ runner.os }}-python${{ matrix.python-version }}-${{ hashFiles('requirements-test.txt') }}

- name: Install dependencies
run: |
pip install -r requirements-test.txt
pip install -e ".[validation]"
- name: Execute test suite
run: ./scripts/test
shell: bash
env:
TMPDIR: "${{ matrix.os == 'windows-latest' && 'D:\\a\\_temp' || '' }}"

- name: Upload All coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
tmp*
*.pyc
*.egg-info
*.eggs
Expand Down
4 changes: 2 additions & 2 deletions tests/data-files/get_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import argparse
import json
from subprocess import call
import tempfile
from typing import Any, Dict, List, Optional
from urllib.error import HTTPError

import pystac
from pystac.serialization import identify_stac_object
from tests.utils import get_temp_dir


def remove_bad_collection(js: Dict[str, Any]) -> Dict[str, Any]:
Expand Down Expand Up @@ -50,7 +50,7 @@ def remove_bad_collection(js: Dict[str, Any]) -> Dict[str, Any]:

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

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
call(
[
"git",
Expand Down
5 changes: 3 additions & 2 deletions tests/extensions/test_label.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import unittest
import tempfile

import pystac
from pystac import Catalog, Item, CatalogType
Expand All @@ -15,7 +16,7 @@
)
import pystac.validation
from pystac.utils import get_opt
from tests.utils import TestCases, assert_to_from_dict, get_temp_dir
from tests.utils import TestCases, assert_to_from_dict


class LabelTest(unittest.TestCase):
Expand Down Expand Up @@ -85,7 +86,7 @@ def test_validate_label(self) -> None:
label_example_1_dict, pystac.STACObjectType.ITEM
)

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
cat_dir = os.path.join(tmp_dir, "catalog")
catalog = TestCases.test_case_1()
catalog.normalize_and_save(cat_dir, catalog_type=CatalogType.SELF_CONTAINED)
Expand Down
40 changes: 20 additions & 20 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import json
import tempfile
from typing import Any, Dict, List, Tuple, Union, cast
import unittest
from datetime import datetime
Expand All @@ -22,14 +23,13 @@
ARBITRARY_GEOM,
ARBITRARY_BBOX,
MockStacIO,
get_temp_dir,
)


class CatalogTypeTest(unittest.TestCase):
def test_determine_type_for_absolute_published(self) -> None:
cat = TestCases.test_case_1()
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
cat.normalize_and_save(tmp_dir, catalog_type=CatalogType.ABSOLUTE_PUBLISHED)
cat_json = pystac.StacIO.default().read_json(
os.path.join(tmp_dir, "catalog.json")
Expand All @@ -40,7 +40,7 @@ def test_determine_type_for_absolute_published(self) -> None:

def test_determine_type_for_relative_published(self) -> None:
cat = TestCases.test_case_2()
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
cat.normalize_and_save(tmp_dir, catalog_type=CatalogType.RELATIVE_PUBLISHED)
cat_json = pystac.StacIO.default().read_json(
os.path.join(tmp_dir, "catalog.json")
Expand Down Expand Up @@ -68,7 +68,7 @@ def test_determine_type_for_unknown(self) -> None:

class CatalogTest(unittest.TestCase):
def test_create_and_read(self) -> None:
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
cat_dir = os.path.join(tmp_dir, "catalog")
catalog = TestCases.test_case_1()

Expand Down Expand Up @@ -288,7 +288,7 @@ def test_clone_generates_correct_links(self) -> None:
def test_save_uses_previous_catalog_type(self) -> None:
catalog = TestCases.test_case_1()
assert catalog.catalog_type == CatalogType.SELF_CONTAINED
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog.normalize_hrefs(tmp_dir)
href = catalog.self_href
catalog.save()
Expand Down Expand Up @@ -365,7 +365,7 @@ def test_generate_subcatalogs_does_not_change_item_count(self) -> None:

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

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog.normalize_hrefs(tmp_dir)
catalog.save(pystac.CatalogType.SELF_CONTAINED)

Expand Down Expand Up @@ -494,7 +494,7 @@ def item_mapper(item: pystac.Item) -> pystac.Item:
item.properties["ITEM_MAPPER"] = "YEP"
return item

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog = TestCases.test_case_1()

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

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog = TestCases.test_case_1()
catalog_items = catalog.get_all_items()

Expand Down Expand Up @@ -623,7 +623,7 @@ def asset_mapper(key: str, asset: pystac.Asset) -> pystac.Asset:

return asset

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog = TestCases.test_case_2()

new_cat = catalog.map_assets(asset_mapper)
Expand Down Expand Up @@ -656,7 +656,7 @@ def asset_mapper(
else:
return asset

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog = TestCases.test_case_2()

new_cat = catalog.map_assets(asset_mapper)
Expand Down Expand Up @@ -696,7 +696,7 @@ def asset_mapper(
else:
return asset

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog = TestCases.test_case_2()

new_cat = catalog.map_assets(asset_mapper)
Expand Down Expand Up @@ -771,7 +771,7 @@ def check_all_absolute(cat: Catalog) -> None:
test_cases = TestCases.all_test_catalogs()

for catalog in test_cases:
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
c2 = catalog.full_copy()
c2.normalize_hrefs(tmp_dir)
c2.catalog_type = CatalogType.RELATIVE_PUBLISHED
Expand All @@ -797,7 +797,7 @@ def test_extra_fields(self) -> None:

catalog.extra_fields["custom_field"] = "Special content"

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
p = os.path.join(tmp_dir, "catalog.json")
catalog.save_object(include_self_link=False, dest_href=p)
with open(p) as f:
Expand All @@ -822,7 +822,7 @@ def test_validate_all(self) -> None:
item = cat.get_item("area-1-1-labels", recursive=True)
assert item is not None
item.geometry = {"type": "INVALID", "coordinates": "NONE"}
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
cat.normalize_hrefs(tmp_dir)
cat.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)

Expand All @@ -843,7 +843,7 @@ def test_set_hrefs_manually(self) -> None:
year += 1
month += 1

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
for root, _, items in catalog.walk():

# Set root's HREF based off the parent
Expand Down Expand Up @@ -933,7 +933,7 @@ def test_reading_iterating_and_writing_works_as_expected(self) -> None:
for item in cat.get_all_items():
pass

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
new_stac_uri = os.path.join(tmp_dir, "test-case-6")
cat.normalize_hrefs(new_stac_uri)
cat.save(catalog_type=CatalogType.SELF_CONTAINED)
Expand Down Expand Up @@ -1003,7 +1003,7 @@ def check_catalog(self, c: Catalog, tag: str) -> None:
self.check_item(item, tag)

def test_full_copy_1(self) -> None:
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
cat = Catalog(id="test", description="test catalog")

item = Item(
Expand All @@ -1024,7 +1024,7 @@ def test_full_copy_1(self) -> None:
self.check_catalog(cat2, "dest")

def test_full_copy_2(self) -> None:
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
cat = Catalog(id="test", description="test catalog")
image_item = Item(
id="Imagery",
Expand Down Expand Up @@ -1071,7 +1071,7 @@ def test_full_copy_2(self) -> None:
self.check_catalog(cat2, "dest")

def test_full_copy_3(self) -> None:
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
root_cat = TestCases.test_case_1()
root_cat.normalize_hrefs(
os.path.join(tmp_dir, "catalog-full-copy-3-source")
Expand All @@ -1085,7 +1085,7 @@ def test_full_copy_3(self) -> None:
self.check_catalog(cat2, "dest")

def test_full_copy_4(self) -> None:
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
root_cat = TestCases.test_case_2()
root_cat.normalize_hrefs(
os.path.join(tmp_dir, "catalog-full-copy-4-source")
Expand Down
7 changes: 4 additions & 3 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import json
from datetime import datetime
from dateutil import tz
import tempfile

import pystac
from pystac.extensions.eo import EOExtension
from pystac.validation import validate_dict
from pystac import Collection, Item, Extent, SpatialExtent, TemporalExtent, CatalogType
from pystac.utils import datetime_to_str
from tests.utils import TestCases, ARBITRARY_GEOM, ARBITRARY_BBOX, get_temp_dir
from tests.utils import TestCases, ARBITRARY_GEOM, ARBITRARY_BBOX

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

Expand All @@ -34,7 +35,7 @@ def test_save_uses_previous_catalog_type(self) -> None:
collection = TestCases.test_case_8()
assert collection.STAC_OBJECT_TYPE == pystac.STACObjectType.COLLECTION
self.assertEqual(collection.catalog_type, CatalogType.SELF_CONTAINED)
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
collection.normalize_hrefs(tmp_dir)
href = collection.self_href
collection.save()
Expand Down Expand Up @@ -83,7 +84,7 @@ def test_extra_fields(self) -> None:

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

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
p = os.path.join(tmp_dir, "collection.json")
collection.save_object(include_self_link=False, dest_href=p)
with open(p) as f:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from datetime import datetime
import json
import tempfile
from typing import Any, Dict, List
import unittest

Expand All @@ -10,7 +11,7 @@
import pystac.serialization.common_properties
from pystac.item import CommonMetadata
from pystac.utils import datetime_to_str, get_opt, str_to_datetime, is_absolute_href
from tests.utils import TestCases, assert_to_from_dict, get_temp_dir
from tests.utils import TestCases, assert_to_from_dict


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

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

with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
p = os.path.join(tmp_dir, "item.json")
item.save_object(include_self_link=False, dest_href=p)
with open(p) as f:
Expand Down
9 changes: 5 additions & 4 deletions tests/test_stac_io.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import unittest
import warnings
import tempfile

import pystac
from pystac.stac_io import STAC_IO
from tests.utils import TestCases, get_temp_dir
from tests.utils import TestCases


class StacIOTest(unittest.TestCase):
Expand All @@ -25,14 +26,14 @@ def test_read_write_collection(self) -> None:
collection = pystac.read_file(
TestCases.get_path("data-files/collections/multi-extent.json")
)
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
dest_href = os.path.join(tmp_dir, "collection.json")
pystac.write_file(collection, dest_href=dest_href)
self.assertTrue(os.path.exists(dest_href), msg="File was not written.")

def test_read_item(self) -> None:
item = pystac.read_file(TestCases.get_path("data-files/item/sample-item.json"))
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
dest_href = os.path.join(tmp_dir, "item.json")
pystac.write_file(item, dest_href=dest_href)
self.assertTrue(os.path.exists(dest_href), msg="File was not written.")
Expand All @@ -41,7 +42,7 @@ def test_read_write_catalog(self) -> None:
catalog = pystac.read_file(
TestCases.get_path("data-files/catalogs/test-case-1/catalog.json")
)
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
dest_href = os.path.join(tmp_dir, "catalog.json")
pystac.write_file(catalog, dest_href=dest_href)
self.assertTrue(os.path.exists(dest_href), msg="File was not written.")
Expand Down
5 changes: 3 additions & 2 deletions tests/test_writing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import unittest
import tempfile
from typing import Any, List

import pystac
from pystac import Collection, CatalogType, HIERARCHICAL_LINKS
from pystac.utils import is_absolute_href, make_absolute_href, make_relative_href
from pystac.validation import validate_dict

from tests.utils import TestCases, get_temp_dir
from tests.utils import TestCases


class STACWritingTest(unittest.TestCase):
Expand Down Expand Up @@ -104,7 +105,7 @@ def validate_catalog_link_type(
def do_test(
self, catalog: pystac.Catalog, catalog_type: pystac.CatalogType
) -> None:
with get_temp_dir() as tmp_dir:
with tempfile.TemporaryDirectory() as tmp_dir:
catalog.normalize_hrefs(tmp_dir)
self.validate_catalog(catalog)

Expand Down
Loading

0 comments on commit f90d52b

Please sign in to comment.