Fix bug in handling directory based modules. Include more extensive tests to exercise the various cases.

This commit is contained in:
Nicholas H.Tollervey 2019-09-06 15:56:25 +01:00
parent eb9c7d26bc
commit b6df5caa33
No known key found for this signature in database
GPG key ID: FD2A04F69841B6FA
6 changed files with 77 additions and 9 deletions

View file

@ -383,16 +383,19 @@ def get_modules(path):
metadata["path"] = sfm
result[os.path.basename(sfm)] = metadata
for dm in directory_mods:
name = os.path.basename(dm)
result[name] = {}
name = os.path.basename(os.path.dirname(dm))
metadata = {}
for source in glob.glob(os.path.join(dm, "*.py")):
with open(source, encoding="utf-8") as source_file:
source_code = source_file.read()
metadata = extract_metadata(source_code)
if "__version__":
if "__version__" in metadata:
metadata["path"] = dm
result[name] = metadata
break
else:
# No version metadata found.
result[name] = {"path": dm}
return result

View file

View file

@ -0,0 +1,3 @@
# A simple directory based Python module that's missing metadata.
def hello():
return "Hello, World!"

View file

View file

@ -0,0 +1,8 @@
# A simple directory based Python module containing expected "local" metadata.
__version__ = "3.2.1"
__repo__ = "https://github.com/adafruit/SomeModule.git"
def hello():
return "Hello, World!"

View file

@ -393,16 +393,70 @@ def test_get_device_versions_go_bang():
circup.get_device_versions()
def test_get_modules():
def test_get_modules_that_are_files():
"""
Check the expected dictionary containing metadata is returned given the
(mocked) results of glob and open.
(mocked) results of glob and open on file based Python modules.
"""
path = "foo"
mods = ["tests/local_module.py"]
with mock.patch("circup.glob.glob", return_value=mods):
path = "tests" # mocked away in function.
mods = [os.path.join("tests", "local_module.py")]
with mock.patch("circup.glob.glob", side_effect=[mods, []]):
result = circup.get_modules(path)
assert len(result) == 1 # dict key is reused.
assert len(result) == 1
assert "local_module.py" in result
assert result["local_module.py"]["path"] == os.path.join(
"tests", "local_module.py"
)
assert (
result["local_module.py"]["__version__"] == "1.2.3"
) # from fixture.
repo = "https://github.com/adafruit/SomeLibrary.git" # from fixture.
assert result["local_module.py"]["__repo__"] == repo
def test_get_modules_that_are_directories():
"""
Check the expected dictionary containing metadata is returned given the
(mocked) results of glob and open, on directory based Python modules.
"""
path = "tests" # mocked away in function.
mods = [os.path.join("tests", "dir_module", "")]
mod_files = [
"tests/dir_module/my_module.py",
"tests/dir_module/__init__.py",
]
with mock.patch("circup.glob.glob", side_effect=[[], mods, mod_files]):
result = circup.get_modules(path)
assert len(result) == 1
assert "dir_module" in result
assert result["dir_module"]["path"] == os.path.join(
"tests", "dir_module", ""
)
assert result["dir_module"]["__version__"] == "3.2.1" # from fixture.
repo = "https://github.com/adafruit/SomeModule.git" # from fixture.
assert result["dir_module"]["__repo__"] == repo
def test_get_modules_that_are_directories_with_no_metadata():
"""
Check the expected dictionary containing just the path is returned given
the (mocked) results of glob and open, on directory based Python modules.
"""
path = "tests" # mocked away in function.
mods = [os.path.join("tests", "bad_module", "")]
mod_files = [
"tests/bad_module/my_module.py",
"tests/bad_module/__init__.py",
]
with mock.patch("circup.glob.glob", side_effect=[[], mods, mod_files]):
result = circup.get_modules(path)
assert len(result) == 1
assert "bad_module" in result
assert result["bad_module"]["path"] == os.path.join(
"tests", "bad_module", ""
)
assert "__version__" not in result["bad_module"]
assert "__repo__" not in result["bad_module"]
def test_ensure_latest_bundle_no_bundle_data():