util: Add a cache for resolved git urls

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2018-11-21 10:15:52 +01:00
parent 4864a0f58e
commit 444af0396e
2 changed files with 38 additions and 0 deletions

View File

@ -285,6 +285,22 @@ def resolve_git_url(url):
return result
class GitUrlResolver(object):
"""A wrapper for resolve_git_url that checks each url only once. In offline
mode it does not actually do anything, returning original input urls.
"""
def __init__(self, offline=False):
self.offline = offline
self.cache = {}
def __call__(self, url):
if self.offline:
return url
if url not in self.cache:
self.cache[url] = resolve_git_url(url)
return self.cache[url]
# fomat: {arch|*: [data]}
def get_arch_data(conf, var_name, arch):
result = []

View File

@ -112,6 +112,28 @@ class TestGitRefResolver(unittest.TestCase):
[mock.call(['git', 'ls-remote', 'https://git.example.com/repo.git', 'HEAD'],
universal_newlines=True)] * 2)
@mock.patch("pungi.util.resolve_git_url")
def test_resolver_offline(self, mock_resolve):
resolver = util.GitUrlResolver(offline=True)
self.assertEqual(
resolver("http://example.com/repo.git#HEAD"),
"http://example.com/repo.git#HEAD",
)
self.assertEqual(mock_resolve.call_args_list, [])
@mock.patch("pungi.util.resolve_git_url")
def test_resolver_caches_calls(self, mock_resolve):
url1 = "http://example.com/repo.git#HEAD"
url2 = "http://example.com/repo.git#master"
mock_resolve.side_effect = ["1", "2"]
resolver = util.GitUrlResolver()
self.assertEqual(resolver(url1), "1")
self.assertEqual(resolver(url1), "1")
self.assertEqual(resolver(url2), "2")
self.assertEqual(resolver(url1), "1")
self.assertEqual(resolver(url2), "2")
self.assertEqual(mock_resolve.call_args_list, [mock.call(url1), mock.call(url2)])
class TestGetVariantData(unittest.TestCase):
def test_get_simple(self):