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

add unit test coverage for pull-sources #629

Merged
merged 1 commit into from
Sep 20, 2022
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: 2 additions & 2 deletions charmtools/pullsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def download_item(args):
# Download the item
fetcher = fetchers.get_fetcher(args.item)
download_dir = fetcher.fetch(tempdir)
except fetchers.FetchError:
print("Can't find source for {}".format(args.item))
except fetchers.FetchError as e:
print("Can't find source for {} ({})".format(args.item, e))
return 1

# Copy download dir to final destination dir
Expand Down
44 changes: 44 additions & 0 deletions tests/test_pullsource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import re
from types import SimpleNamespace
import unittest
import unittest.mock as mock
from tempfile import NamedTemporaryFile, TemporaryDirectory

from charmtools import pullsource


class TestPullSource(unittest.TestCase):
@mock.patch('builtins.print')
def test__download_item_cs_prefix(self, mock_print):
"""Don't support downloads from any cs: prefix."""
args = SimpleNamespace(item="cs:fake-charm")
assert pullsource.download_item(args) == 1
mock_print.assert_called_once_with("Cannot download source for charm cs:fake-charm")

@mock.patch('builtins.print')
def test__download_fake_layer_prefix(self, mock_print):
"""Confirm supports downloads from layers: prefix."""
with TemporaryDirectory() as tmpdir:
args = SimpleNamespace(item="layer:fake-layer", dir=tmpdir)
assert pullsource.download_item(args) == 1
mock_print.assert_called_once_with("Can't find source for layer:fake-layer (No fetcher for url: layer:fake-layer)")

@mock.patch('builtins.print')
@mock.patch.object(pullsource, 'fetchers')
def test__download_layer_basic(self, mock_fetchers, mock_print):
"""Confirm supports downloads from layers: prefix."""
fetcher = mock_fetchers.get_fetcher.return_value
fetcher.revision = "abcdefghijklmnop"
with TemporaryDirectory() as tmpdir:
mkdir = fetcher.fetch.return_value = os.path.join(tmpdir, "source")
os.mkdir(mkdir)
with NamedTemporaryFile(dir=fetcher.fetch.return_value):
args = SimpleNamespace(item="layer:fake-layer", dir=tmpdir)
assert pullsource.download_item(args) is None
mock_print.assert_called_once()
args, _ = mock_print.call_args
rev_match = re.search(r"rev: ([a-z0-9]+)", args[0])
assert rev_match, f"'{args[0]}', didn't contain a match for rev: ([a-z0-9]+)"
assert rev_match.groups()[0] == fetcher.revision