Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create directory for writing item json #152

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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