Skip to content

Commit

Permalink
Merge pull request canonical#165 from fnordahl/issue/164
Browse files Browse the repository at this point in the history
Fix package/version parsing
  • Loading branch information
johnsca authored and George Kraft committed Jun 2, 2020
1 parent 955e853 commit f761da8
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 5 deletions.
2 changes: 1 addition & 1 deletion layer.yaml
@@ -1,5 +1,5 @@
includes: ['layer:options']
exclude: ['.travis.yml', 'tests', 'tox.ini', 'test-requirements.txt']
exclude: ['.travis.yml', 'tests', 'tox.ini', 'test-requirements.txt', 'unit_tests']
defines:
packages:
type: array
Expand Down
2 changes: 1 addition & 1 deletion lib/charms/layer/basic.py
Expand Up @@ -233,7 +233,7 @@ def _load_installed_versions(pip):
def _load_wheelhouse_versions():
versions = {}
for wheel in glob('wheelhouse/*'):
pkg, ver = os.path.basename(wheel).split('-')
pkg, ver = os.path.basename(wheel).rsplit('-', 1)
# nb: LooseVersion ignores the file extension
versions[pkg.replace('_', '-')] = LooseVersion(ver)
return versions
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -1,2 +1,3 @@
mock
flake8
pytest
4 changes: 1 addition & 3 deletions tox.ini
Expand Up @@ -4,9 +4,7 @@ envlist = flake8, py35, py36, py37
skip_missing_interpreters = True

[testenv]
# This is not pretty, but pytest will return 0 if all tests worked and 5 if no tests are found.
# We want to consider no tests as an indication of success.
commands = /bin/bash -c 'py.test -v || if [[ $? == 5 ]]; then true; else false; fi'
commands = /bin/bash -c 'py.test -v'

deps =
-r{toxinidir}/requirements.txt
Expand Down
15 changes: 15 additions & 0 deletions unit_tests/__init__.py
@@ -0,0 +1,15 @@
# Copyright 2020 Canonical Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys

sys.path.append('lib')
28 changes: 28 additions & 0 deletions unit_tests/test_lib_charms_layer_basic.py
@@ -0,0 +1,28 @@
import mock

import lib.charms.layer.basic as basic

import unit_tests.utils as test_utils


class TestLayerBasic(test_utils.BaseTestCase):

def test__load_wheelhouse_versions(self):
self.patch_object(basic, 'glob')
self.patch_object(basic, 'LooseVersion')
self.glob.return_value = [
'python-dateutil-2.8.1.tar.gz',
'setuptools_scm-1.17.0.tar.gz',
'wheel-0.33.6.tar.gz',
]
self.assertDictEqual(
basic._load_wheelhouse_versions(), {
'setuptools-scm': mock.ANY,
'python-dateutil': mock.ANY,
'wheel': mock.ANY,
})
self.LooseVersion.assert_has_calls([
mock.call('0.33.6.tar.gz'),
mock.call('2.8.1.tar.gz'),
mock.call('1.17.0.tar.gz'),
], any_order=True)
79 changes: 79 additions & 0 deletions unit_tests/utils.py
@@ -0,0 +1,79 @@
# Copyright 2016 Canonical Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit test helpers from https://github.com/openstack/charms.openstack/"""

import contextlib
import io
import mock
import unittest


@contextlib.contextmanager
def patch_open():
'''Patch open() to allow mocking both open() itself and the file that is
yielded.
Yields the mock for "open" and "file", respectively.'''
mock_open = mock.MagicMock(spec=open)
mock_file = mock.MagicMock(spec=io.FileIO)

@contextlib.contextmanager
def stub_open(*args, **kwargs):
mock_open(*args, **kwargs)
yield mock_file

with mock.patch('builtins.open', stub_open):
yield mock_open, mock_file


class BaseTestCase(unittest.TestCase):

def setUp(self):
self._patches = {}
self._patches_start = {}

def tearDown(self):
for k, v in self._patches.items():
v.stop()
setattr(self, k, None)
self._patches = None
self._patches_start = None

def patch_object(self, obj, attr, return_value=None, name=None, new=None,
**kwargs):
if name is None:
name = attr
if new is not None:
mocked = mock.patch.object(obj, attr, new=new, **kwargs)
else:
mocked = mock.patch.object(obj, attr, **kwargs)
self._patches[name] = mocked
started = mocked.start()
if new is None:
started.return_value = return_value
self._patches_start[name] = started
setattr(self, name, started)

def patch(self, item, return_value=None, name=None, new=None, **kwargs):
if name is None:
raise RuntimeError("Must pass 'name' to .patch()")
if new is not None:
mocked = mock.patch(item, new=new, **kwargs)
else:
mocked = mock.patch(item, **kwargs)
self._patches[name] = mocked
started = mocked.start()
if new is None:
started.return_value = return_value
self._patches_start[name] = started
setattr(self, name, started)

0 comments on commit f761da8

Please sign in to comment.