Handle directory based module names correctly (with tests).

This commit is contained in:
Nicholas H.Tollervey 2019-09-05 12:33:21 +01:00
parent 7a889638a5
commit c5e467e670
No known key found for this signature in database
GPG key ID: FD2A04F69841B6FA
2 changed files with 62 additions and 11 deletions

View file

@ -97,6 +97,11 @@ class Module:
self, path, repo, device_version, bundle_version, bundle_path
):
"""
The ``self.file`` and ``self.name`` attributes are constructed from
the ``path`` value. If the path is to a directory based module, the
resulting self.file value will be None, and the name will be the
basename of the directory path.
:param str path: The path to the module on the connected CIRCUITPYTHON
device.
:param str repo: The URL of the Git repository for this module.
@ -105,8 +110,14 @@ class Module:
:param str bundle_path: The path to the bundle version of the module.
"""
self.path = path
self.file = os.path.basename(path)
self.name = self.file[:-3]
if os.path.isfile(self.path):
# Single file module.
self.file = os.path.basename(path)
self.name = self.file[:-3]
else:
# Directory based module.
self.file = None
self.name = os.path.basename(os.path.dirname(self.path))
self.repo = repo
self.device_version = device_version
self.bundle_version = bundle_version
@ -322,7 +333,7 @@ def find_modules():
# If it's not possible to get the device and bundle metadata, bail out
# with a friendly message and indication of what's gone wrong.
logger.exception(ex)
click.echo("There was a problem, {} (check the logs)".format(ex))
click.echo("There was a problem: {}".format(ex))
sys.exit(1)
@ -501,8 +512,9 @@ def list(): # pragma: no cover
logger.info("List")
# Grab out of date modules.
data = [("Module", "Version", "Latest")]
data += [m.row for m in find_modules() if m.outofdate]
if data:
modules = [m.row for m in find_modules() if m.outofdate]
if modules:
data += modules
# Nice tabular display.
col_width = [0, 0, 0]
for row in data:

View file

@ -29,16 +29,19 @@ import json
from unittest import mock
def test_Module_init():
def test_Module_init_file_module():
"""
Ensure the Module instance is set up as expected and logged.
Ensure the Module instance is set up as expected and logged, is if for a
single file Python module.
"""
path = os.path.join("foo", "bar", "baz", "module.py")
repo = "https://github.com/adafruit/SomeLibrary.git"
device_version = "1.2.3"
bundle_version = "3.2.1"
bundle_path = os.path.join("baz", "bar", "foo", "module.py")
with mock.patch("circup.logger.info") as mock_logger:
with mock.patch("circup.logger.info") as mock_logger, mock.patch(
"circup.os.path.isfile", return_value=True
):
m = circup.Module(
path, repo, device_version, bundle_version, bundle_path
)
@ -52,6 +55,32 @@ def test_Module_init():
assert m.bundle_path == bundle_path
def test_Module_init_directory_module():
"""
Ensure the Module instance is set up as expected and logged, as if for a
directory based Python module.
"""
path = os.path.join("foo", "bar", "modulename", "")
repo = "https://github.com/adafruit/SomeLibrary.git"
device_version = "1.2.3"
bundle_version = "3.2.1"
bundle_path = os.path.join("baz", "bar", "foo", "")
with mock.patch("circup.logger.info") as mock_logger, mock.patch(
"circup.os.path.isfile", return_value=False
):
m = circup.Module(
path, repo, device_version, bundle_version, bundle_path
)
mock_logger.assert_called_once_with(m)
assert m.path == path
assert m.file is None
assert m.name == "modulename"
assert m.repo == repo
assert m.device_version == device_version
assert m.bundle_version == bundle_version
assert m.bundle_path == bundle_path
def test_Module_outofdate():
"""
Ensure the ``outofdate`` property on a Module instance returns the expected
@ -97,7 +126,10 @@ def test_Module_row():
device_version = "1.2.3"
bundle_version = None
bundle_path = os.path.join("baz", "bar", "foo", "module.py")
m = circup.Module(path, repo, device_version, bundle_version, bundle_path)
with mock.patch("circup.os.path.isfile", return_value=True):
m = circup.Module(
path, repo, device_version, bundle_version, bundle_path
)
assert m.row == ("module", "1.2.3", "unknown")
@ -148,7 +180,10 @@ def test_Module_repr():
device_version = "1.2.3"
bundle_version = "3.2.1"
bundle_path = os.path.join("baz", "bar", "foo", "module.py")
m = circup.Module(path, repo, device_version, bundle_version, bundle_path)
with mock.patch("circup.os.path.isfile", return_value=True):
m = circup.Module(
path, repo, device_version, bundle_version, bundle_path
)
assert repr(m) == repr(
{
"path": path,
@ -297,7 +332,11 @@ def test_find_modules():
bundle_modules = json.load(f)
with mock.patch(
"circup.get_device_versions", return_value=device_modules
), mock.patch("circup.get_bundle_versions", return_value=bundle_modules):
), mock.patch(
"circup.get_bundle_versions", return_value=bundle_modules
), mock.patch(
"circup.os.path.isfile", return_value=True
):
result = circup.find_modules()
assert len(result) == 1
assert result[0].name == "adafruit_74hc595"