Skip to content

Commit

Permalink
fix: create dir before writing stac item
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Varner authored and gadomski committed Apr 23, 2024
1 parent 338e42e commit 0dacfd0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- `open_href` ([#123](https://github.com/stac-utils/stac-asset/pull/123))

### Fixed

- Directory for writing Item JSON is always created ([#152](https://github.com/stac-utils/stac-asset/pull/152))

## [0.2.3] - 2023-10-20

### Added
Expand Down
1 change: 1 addition & 0 deletions src/stac_asset/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ async def download_item(
if self_href:
make_asset_hrefs_relative(item)
d = item.to_dict(include_self_link=True, transform_hrefs=False)
os.makedirs(os.path.dirname(self_href), exist_ok=True)
with open(self_href, "w") as f:
json.dump(d, f)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ async def test_download_missing_asset_delete(tmp_path: Path, item: Item) -> None
assert "does-not-exist" not in item.assets


async def test_download_nonexistent_asset(tmp_path: Path, item: Item) -> None:
# this previously had a bug where the code assumed the directory had
# been created by downloading the assets when trying to write the item json,
# but it hadn't, so a failure occurred
await stac_asset.download_item(
item,
tmp_path / "dir-that-doesnt-exist",
file_name="item.json",
config=Config(include=["non-existent-asset"]),
)


async def test_download_missing_asset_fail_fast(tmp_path: Path, item: Item) -> None:
item.assets["does-not-exist"] = Asset("not-a-file.md5")
with pytest.raises(FileNotFoundError):
Expand Down

0 comments on commit 0dacfd0

Please sign in to comment.