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

In Item.make_asset_hrefs_relative, leave relative hrefs alone. #229

Merged
merged 1 commit into from
Oct 30, 2020
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
In Item.make_asset_hrefs_relative, leave relative hrefs alone.
This fixes a bug where make_asset_hrefs_relative was modifying already
relative paths, which set the HREFs to invalid locations.
  • Loading branch information
lossyrob committed Oct 30, 2020
commit 12e872da27dd36667bb198563c45db203b293204
14 changes: 10 additions & 4 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 186,17 @@ def make_asset_hrefs_relative(self):
Returns:
Item: self
"""
self_href = self.get_self_href()
if self_href is None:
raise STACError('Cannot make asset HREFs relative if no self_href is set.')

self_href = None
for asset in self.assets.values():
asset.href = make_relative_href(asset.href, self_href)
href = asset.href
if is_absolute_href(href):
if self_href is None:
self_href = self.get_self_href()
if self_href is None:
raise STACError('Cannot make asset HREFs relative '
'if no self_href is set.')
asset.href = make_relative_href(asset.href, self_href)
return self

def make_asset_hrefs_absolute(self):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 556,20 @@ def test_make_all_asset_hrefs_absolute(self):
href = item.assets['cf73ec1a-d790-4b59-b077-e101738571ed'].href
self.assertTrue(is_absolute_href(href))

def test_make_all_asset_hrefs_relative(self):
cat = TestCases.test_case_2()
item = cat.get_item('cf73ec1a-d790-4b59-b077-e101738571ed', recursive=True)
asset = item.assets['cf73ec1a-d790-4b59-b077-e101738571ed']
original_href = asset.href
cat.make_all_asset_hrefs_absolute()

assert is_absolute_href(asset.href)

cat.make_all_asset_hrefs_relative()

self.assertFalse(is_absolute_href(asset.href))
self.assertEqual(asset.href, original_href)

def test_make_all_links_relative_or_absolute(self):
def check_all_relative(cat):
for root, catalogs, items in cat.walk():
Expand Down
10 changes: 10 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 177,16 @@ def test_clone_sets_asset_owner(self):
clone_asset = list(clone.assets.values())[0]
self.assertIs(clone_asset.owner, clone)

def test_make_asset_href_relative_is_noop_on_relative_hrefs(self):
cat = TestCases.test_case_2()
item = next(cat.get_all_items())
asset = list(item.assets.values())[0]
assert not is_absolute_href(asset.href)
original_href = asset.get_absolute_href()

item.make_asset_hrefs_relative()
self.assertEqual(asset.get_absolute_href(), original_href)


class CommonMetadataTest(unittest.TestCase):
def setUp(self):
Expand Down