fixing more tests

This commit is contained in:
foamyguy 2024-04-07 12:27:58 -05:00
parent eef8326519
commit fe326c84a4

View file

@ -67,12 +67,14 @@ def test_Bundle_init():
"key": TEST_BUNDLE_NAME,
"url": "https://github.com/" + TEST_BUNDLE_NAME,
"urlzip": "adafruit-circuitpython-bundle-{platform}-{tag}.zip",
"dir": circup.shared.DATA_DIR + "/adafruit/adafruit-circuitpython-bundle-{platform}",
"zip": circup.shared.DATA_DIR + "/adafruit-circuitpython-bundle-{platform}.zip",
"dir": circup.shared.DATA_DIR
+ "/adafruit/adafruit-circuitpython-bundle-{platform}",
"zip": circup.shared.DATA_DIR
+ "/adafruit-circuitpython-bundle-{platform}.zip",
"url_format": "https://github.com/"
+ TEST_BUNDLE_NAME
+ "/releases/download/{tag}/"
"adafruit-circuitpython-bundle-{platform}-{tag}.zip",
+ TEST_BUNDLE_NAME
+ "/releases/download/{tag}/"
"adafruit-circuitpython-bundle-{platform}-{tag}.zip",
"current": None,
"latest": None,
}
@ -88,14 +90,14 @@ def test_Bundle_lib_dir():
bundle = circup.Bundle(TEST_BUNDLE_NAME)
assert bundle.current_tag == "TESTTAG"
assert bundle.lib_dir("py") == (
circup.shared.DATA_DIR + "/"
"adafruit/adafruit-circuitpython-bundle-py/"
"adafruit-circuitpython-bundle-py-TESTTAG/lib"
circup.shared.DATA_DIR + "/"
"adafruit/adafruit-circuitpython-bundle-py/"
"adafruit-circuitpython-bundle-py-TESTTAG/lib"
)
assert bundle.lib_dir("8mpy") == (
circup.shared.DATA_DIR + "/"
"adafruit/adafruit-circuitpython-bundle-8mpy/"
"adafruit-circuitpython-bundle-8.x-mpy-TESTTAG/lib"
circup.shared.DATA_DIR + "/"
"adafruit/adafruit-circuitpython-bundle-8mpy/"
"adafruit-circuitpython-bundle-8.x-mpy-TESTTAG/lib"
)
@ -105,10 +107,8 @@ def test_Bundle_latest_tag():
"""
bundle_data = {TEST_BUNDLE_NAME: "TESTTAG"}
with mock.patch(
"circup.bundle.get_latest_release_from_url", return_value="BESTESTTAG"
), mock.patch(
"circup.bundle.tags_data_load", return_value=bundle_data
):
"circup.bundle.get_latest_release_from_url", return_value="BESTESTTAG"
), mock.patch("circup.bundle.tags_data_load", return_value=bundle_data):
bundle = circup.Bundle(TEST_BUNDLE_NAME)
assert bundle.latest_tag == "BESTESTTAG"
@ -117,14 +117,16 @@ def test_get_bundles_dict():
"""
Check we are getting the bundles list from BUNDLE_CONFIG_FILE.
"""
with mock.patch("circup.shared.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON), mock.patch(
"circup.shared.BUNDLE_CONFIG_LOCAL", ""
):
with mock.patch(
"circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON
), mock.patch("circup.shared.BUNDLE_CONFIG_LOCAL", ""):
bundles_dict = get_bundles_dict()
assert bundles_dict == TEST_BUNDLE_DATA
with mock.patch("circup.shared.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON), mock.patch(
"circup.shared.BUNDLE_CONFIG_LOCAL", TEST_BUNDLE_CONFIG_LOCAL_JSON
with mock.patch(
"circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON
), mock.patch(
"circup.command_utils.BUNDLE_CONFIG_LOCAL", TEST_BUNDLE_CONFIG_LOCAL_JSON
):
bundles_dict = get_bundles_dict()
expected_dict = {**TEST_BUNDLE_LOCAL_DATA, **TEST_BUNDLE_DATA}
@ -135,14 +137,16 @@ def test_get_bundles_local_dict():
"""
Check we are getting the bundles list from BUNDLE_CONFIG_LOCAL.
"""
with mock.patch("circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON), mock.patch(
"circup.command_utils.BUNDLE_CONFIG_LOCAL", ""
):
with mock.patch(
"circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON
), mock.patch("circup.command_utils.BUNDLE_CONFIG_LOCAL", ""):
bundles_dict = get_bundles_dict()
assert bundles_dict == TEST_BUNDLE_DATA
with mock.patch("circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON), mock.patch(
"circup.command_utils.BUNDLE_CONFIG_LOCAL", TEST_BUNDLE_CONFIG_LOCAL_JSON
with mock.patch(
"circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON
), mock.patch(
"circup.command_utils.BUNDLE_CONFIG_LOCAL", TEST_BUNDLE_CONFIG_LOCAL_JSON
):
bundles_dict = get_bundles_dict()
expected_dict = {**TEST_BUNDLE_LOCAL_DATA, **TEST_BUNDLE_DATA}
@ -153,9 +157,9 @@ def test_get_bundles_list():
"""
Check we are getting the bundles list from BUNDLE_CONFIG_FILE.
"""
with mock.patch("circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON), mock.patch(
"circup.command_utils.BUNDLE_CONFIG_LOCAL", ""
):
with mock.patch(
"circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON
), mock.patch("circup.command_utils.BUNDLE_CONFIG_LOCAL", ""):
bundles_list = circup.get_bundles_list()
bundle = circup.Bundle(TEST_BUNDLE_NAME)
assert repr(bundles_list) == repr([bundle])
@ -165,9 +169,11 @@ def test_save_local_bundles():
"""
Pretend to save local bundles.
"""
with mock.patch("circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON), mock.patch(
"circup.command_utils.BUNDLE_CONFIG_LOCAL", ""
), mock.patch("circup.os.unlink") as mock_unlink, mock.patch(
with mock.patch(
"circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON
), mock.patch("circup.command_utils.BUNDLE_CONFIG_LOCAL", ""), mock.patch(
"circup.os.unlink"
) as mock_unlink, mock.patch(
"circup.command_utils.json.dump"
) as mock_dump, mock.patch(
"circup.command_utils.open", mock.mock_open()
@ -182,9 +188,13 @@ def test_save_local_bundles_reset():
"""
Pretend to reset the local bundles.
"""
with mock.patch("circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON), mock.patch(
"circup.command_utils.BUNDLE_CONFIG_LOCAL", "test/NOTEXISTS"
), mock.patch("circup.os.path.isfile", return_value=True), mock.patch(
with mock.patch(
"circup.command_utils.BUNDLE_CONFIG_FILE", TEST_BUNDLE_CONFIG_JSON
), mock.patch(
"circup.command_utils.BUNDLE_CONFIG_LOCAL", "test/NOTEXISTS"
), mock.patch(
"circup.os.path.isfile", return_value=True
), mock.patch(
"circup.os.unlink"
) as mock_unlink, mock.patch(
"circup.command_utils.json.load", return_value=TEST_BUNDLE_DATA
@ -208,7 +218,7 @@ def test_Module_init_file_module():
bundle_version = "3.2.1"
with mock.patch("circup.logger.info") as mock_logger, mock.patch(
"circup.os.path.isfile", return_value=True
"circup.os.path.isfile", return_value=True
), mock.patch(
"circup.bundle.Bundle.lib_dir",
return_value="tests",
@ -248,7 +258,7 @@ def test_Module_init_directory_module():
bundle_version = "3.2.1"
mpy = True
with mock.patch("circup.logger.info") as mock_logger, mock.patch(
"circup.bundle.Bundle.lib_dir", return_value="tests"
"circup.bundle.Bundle.lib_dir", return_value="tests"
):
backend = DiskBackend("tests/mock_device", mock_logger)
bundle = circup.Bundle(TEST_BUNDLE_NAME)
@ -331,9 +341,7 @@ def test_Module_mpy_mismatch():
"""
name = "module.py"
repo = "https://github.com/adafruit/SomeLibrary.git"
with mock.patch(
"circup.logger.warning"
) as mock_logger:
with mock.patch("circup.logger.warning") as mock_logger:
backend = DiskBackend("tests/mock_device", mock_logger)
bundle = circup.Bundle(TEST_BUNDLE_NAME)
m1 = Module(name, backend, repo, "1.2.3", "1.2.3", True, bundle, (None, None))
@ -350,14 +358,20 @@ def test_Module_mpy_mismatch():
m3 = Module(
name, backend, repo, "1.2.3", "1.2.3", True, bundle, (None, "7.0.0-alpha.1")
)
with mock.patch("circup.backends.DiskBackend.get_circuitpython_version", return_value=("6.2.0", "")):
with mock.patch(
"circup.backends.DiskBackend.get_circuitpython_version",
return_value=("6.2.0", ""),
):
assert m1.mpy_mismatch is False
assert m1.outofdate is False
assert m2.mpy_mismatch is True
assert m2.outofdate is True
assert m3.mpy_mismatch is False
assert m3.outofdate is False
with mock.patch("circup.backends.DiskBackend.get_circuitpython_version", return_value=("8.0.0", "")):
with mock.patch(
"circup.backends.DiskBackend.get_circuitpython_version",
return_value=("8.0.0", ""),
):
assert m1.mpy_mismatch is False
assert m1.outofdate is False
assert m2.mpy_mismatch is False
@ -404,7 +418,8 @@ def test_Module_row():
name = "module.py"
repo = "https://github.com/adafruit/SomeLibrary.git"
with mock.patch("circup.os.path.isfile", return_value=True), mock.patch(
"circup.backends.DiskBackend.get_circuitpython_version", return_value=("8.0.0", "")
"circup.backends.DiskBackend.get_circuitpython_version",
return_value=("8.0.0", ""),
), mock.patch("circup.logger.warning") as mock_logger:
backend = DiskBackend("mock_device", mock_logger)
m = Module(name, backend, repo, "1.2.3", None, False, bundle, (None, None))
@ -425,7 +440,9 @@ def test_Module_update_dir():
repo = "https://github.com/adafruit/SomeLibrary.git"
device_version = "1.2.3"
bundle_version = None
with mock.patch("circup.backends.shutil") as mock_shutil, mock.patch("circup.logger.warning") as mock_logger:
with mock.patch("circup.backends.shutil") as mock_shutil, mock.patch(
"circup.logger.warning"
) as mock_logger:
backend = DiskBackend("mock_device", mock_logger)
m = Module(
name,
@ -455,10 +472,8 @@ def test_Module_update_file():
bundle_version = None
with mock.patch("circup.backends.shutil") as mock_shutil, mock.patch(
"circup.os.remove"
) as mock_remove, mock.patch(
"circup.logger.warning"
) as mock_logger:
"circup.os.remove"
) as mock_remove, mock.patch("circup.logger.warning") as mock_logger:
backend = circup.DiskBackend("tests/mock_device", mock_logger)
m = Module(
name,
@ -485,7 +500,8 @@ def test_Module_repr():
device_version = "1.2.3"
bundle_version = "3.2.1"
with mock.patch("circup.os.path.isfile", return_value=True), mock.patch(
"circup.backends.DiskBackend.get_circuitpython_version", return_value=("4.1.2", "")
"circup.backends.DiskBackend.get_circuitpython_version",
return_value=("4.1.2", ""),
), mock.patch("circup.Bundle.lib_dir", return_value="tests"), mock.patch(
"circup.logger.warning"
) as mock_logger:
@ -538,7 +554,9 @@ def test_find_device_posix_no_mount_command():
with open("tests/mount_exists.txt", "rb") as fixture_file:
fixture = fixture_file.read()
mock_check = mock.MagicMock(side_effect=[FileNotFoundError, fixture])
with mock.patch("os.name", "posix"), mock.patch("circup.command_utils.check_output", mock_check):
with mock.patch("os.name", "posix"), mock.patch(
"circup.command_utils.check_output", mock_check
):
assert find_device() == "/media/ntoll/CIRCUITPY"
assert mock_check.call_count == 2
assert mock_check.call_args_list[0][0][0] == "mount"
@ -553,7 +571,7 @@ def test_find_device_posix_missing():
with open("tests/mount_missing.txt", "rb") as fixture_file:
fixture = fixture_file.read()
with mock.patch("os.name", "posix"), mock.patch(
"circup.command_utils.check_output", return_value=fixture
"circup.command_utils.check_output", return_value=fixture
):
assert find_device() is None
@ -569,7 +587,7 @@ def test_find_device_nt_exists():
mock_windll.kernel32.GetVolumeInformationW.return_value = None
fake_buffer = ctypes.create_unicode_buffer("CIRCUITPY")
with mock.patch("os.name", "nt"), mock.patch(
"os.path.exists", return_value=True
"os.path.exists", return_value=True
), mock.patch("ctypes.create_unicode_buffer", return_value=fake_buffer):
ctypes.windll = mock_windll
assert find_device() == "A:\\"
@ -586,7 +604,7 @@ def test_find_device_nt_missing():
mock_windll.kernel32.GetVolumeInformationW.return_value = None
fake_buffer = ctypes.create_unicode_buffer(1024)
with mock.patch("os.name", "nt"), mock.patch(
"os.path.exists", return_value=True
"os.path.exists", return_value=True
), mock.patch("ctypes.create_unicode_buffer", return_value=fake_buffer):
ctypes.windll = mock_windll
assert find_device() is None
@ -610,7 +628,7 @@ def test_get_latest_release_from_url():
response = mock.MagicMock()
response.headers = {
"Location": "https://github.com/adafruit"
"/Adafruit_CircuitPython_Bundle/releases/tag/20190903"
"/Adafruit_CircuitPython_Bundle/releases/tag/20190903"
}
expected_url = "https://github.com/" + TEST_BUNDLE_NAME + "/releases/latest"
with mock.patch("circup.shared.requests.head", return_value=response) as mock_get:
@ -632,7 +650,7 @@ def test_extract_metadata_python():
)
path = "foo.py"
with mock.patch(
"builtins.open", mock.mock_open(read_data=code)
"builtins.open", mock.mock_open(read_data=code)
) as mock_open, mock.patch("circup.logger.warning") as mock_logger:
result = circup.extract_metadata(path, mock_logger)
mock_open.assert_called_once_with(path, "r", encoding="utf-8")
@ -678,11 +696,9 @@ def test_find_modules():
bundle_modules = json.load(f)
with mock.patch(
"circup.DiskBackend.get_device_versions", return_value=device_modules
"circup.DiskBackend.get_device_versions", return_value=device_modules
), mock.patch(
"circup.get_bundle_versions", return_value=bundle_modules
), mock.patch(
"circup.os.path.isfile", return_value=True
"circup.command_utils.get_bundle_versions", return_value=bundle_modules
), mock.patch(
"circup.logger.warning"
) as mock_logger:
@ -696,8 +712,8 @@ def test_find_modules():
assert len(result) == 1
assert result[0].name == "adafruit_74hc595"
assert (
result[0].repo
== "https://github.com/adafruit/Adafruit_CircuitPython_74HC595.git"
result[0].repo
== "https://github.com/adafruit/Adafruit_CircuitPython_74HC595.git"
)
@ -707,8 +723,8 @@ def test_find_modules_goes_bang():
and the utility exists with an error code of 1.
"""
with mock.patch(
"circup.DiskBackend.get_device_versions", side_effect=Exception("BANG!")
), mock.patch("circup.click") as mock_click, mock.patch(
"circup.DiskBackend.get_device_versions", side_effect=Exception("BANG!")
), mock.patch("circup.command_utils.click") as mock_click, mock.patch(
"circup.sys.exit"
) as mock_exit, mock.patch(
"circup.logger.warning"
@ -726,14 +742,19 @@ def test_get_bundle_versions():
Ensure get_modules is called with the path for the library bundle.
Ensure ensure_latest_bundle is called even if lib_dir exists.
"""
with mock.patch("ensure_latest_bundle") as mock_elb, mock.patch(
"circup._get_modules_file", return_value={"ok": {"name": "ok"}}
) as mock_gm, mock.patch("circup.CPY_VERSION", "4.1.2"), mock.patch(
"circup.Bundle.lib_dir", return_value="foo/bar/lib"
with mock.patch(
"circup.command_utils.ensure_latest_bundle"
) as mock_elb, mock.patch(
"circup.command_utils._get_modules_file", return_value={"ok": {"name": "ok"}}
) as mock_gm, mock.patch(
"circup.backends.DiskBackend.get_circuitpython_version",
return_value=("4.1.2", ""),
), mock.patch(
"circup.bundle.Bundle.lib_dir", return_value="foo/bar/lib"
), mock.patch(
"circup.os.path.isdir", return_value=True
), mock.patch(
"circup.logger"
"circup.command_utils.logger"
) as mock_logger:
bundle = circup.Bundle(TEST_BUNDLE_NAME)
bundles_list = [bundle]
@ -749,12 +770,17 @@ def test_get_bundle_versions_avoid_download():
When avoid_download is True and lib_dir exists, don't ensure_latest_bundle.
Testing both cases: lib_dir exists and lib_dir doesn't exists.
"""
with mock.patch("ensure_latest_bundle") as mock_elb, mock.patch(
"circup._get_modules_file", return_value={"ok": {"name": "ok"}}
) as mock_gm, mock.patch("circup.CPY_VERSION", "4.1.2"), mock.patch(
with mock.patch(
"circup.command_utils.ensure_latest_bundle"
) as mock_elb, mock.patch(
"circup.command_utils._get_modules_file", return_value={"ok": {"name": "ok"}}
) as mock_gm, mock.patch(
"circup.backends.DiskBackend.get_circuitpython_version",
return_value=("4.1.2", ""),
), mock.patch(
"circup.Bundle.lib_dir", return_value="foo/bar/lib"
), mock.patch(
"circup.logger"
"circup.command_utils.logger"
) as mock_logger:
bundle = circup.Bundle(TEST_BUNDLE_NAME)
bundles_list = [bundle]
@ -780,7 +806,7 @@ def test_get_circuitpython_version():
with mock.patch("circup.logger.warning") as mock_logger:
backend = DiskBackend("tests/mock_device", mock_logger)
assert backend.get_circuitpython_version() == (
"4.1.0",
"8.1.0",
"this_is_a_board",
)
@ -790,11 +816,11 @@ def test_get_device_versions():
Ensure get_modules is called with the path for the attached device.
"""
with mock.patch(
"circup.DiskBackend.get_modules", return_value="ok"
"circup.DiskBackend.get_modules", return_value="ok"
) as mock_gm, mock.patch("circup.logger.warning") as mock_logger:
backend = circup.DiskBackend("mock_device", mock_logger)
backend = circup.DiskBackend("tests/mock_device", mock_logger)
assert backend.get_device_versions() == "ok"
mock_gm.assert_called_once_with(os.path.join("mock_device", "lib"))
mock_gm.assert_called_once_with(os.path.join("tests", "mock_device", "lib"))
def test_get_modules_empty_path():
@ -817,8 +843,8 @@ def test_get_modules_that_are_files():
os.path.join("tests", "local_module.py"),
os.path.join("tests", ".hidden_module.py"),
]
with mock.patch("circup.glob.glob", side_effect=[mods, [], []]), mock.patch(
"circup.logger.warning"
with mock.patch("circup.shared.glob.glob", side_effect=[mods, [], []]), mock.patch(
"circup.logger.warning"
) as mock_logger:
backend = circup.DiskBackend("mock_device", mock_logger)
result = backend.get_modules(path)
@ -844,7 +870,7 @@ def test_get_modules_that_are_directories():
]
mod_files = ["tests/dir_module/my_module.py", "tests/dir_module/__init__.py"]
with mock.patch(
"circup.glob.glob", side_effect=[[], [], mods, mod_files, []]
"circup.shared.glob.glob", side_effect=[[], [], mods, mod_files, []]
), mock.patch("circup.logger.warning") as mock_logger:
backend = circup.DiskBackend("mock_device", mock_logger)
result = backend.get_modules(path)
@ -865,7 +891,7 @@ def test_get_modules_that_are_directories_with_no_metadata():
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, []]
"circup.shared.glob.glob", side_effect=[[], [], mods, mod_files, []]
), mock.patch("circup.logger.warning") as mock_logger:
backend = circup.DiskBackend("mock_device", mock_logger)
result = backend.get_modules(path)
@ -881,12 +907,12 @@ def test_ensure_latest_bundle_no_bundle_data():
If there's no BUNDLE_DATA file (containing previous current version of the
bundle) then default to update.
"""
with mock.patch("circup.Bundle.latest_tag", "12345"), mock.patch(
"circup.os.path.isfile", return_value=False
), mock.patch("circup.get_bundle") as mock_gb, mock.patch(
"circup.json"
with mock.patch("circup.bundle.Bundle.latest_tag", "12345"), mock.patch(
"circup.os.path.isfile", return_value=False
), mock.patch("circup.command_utils.get_bundle") as mock_gb, mock.patch(
"circup.command_utils.json"
) as mock_json, mock.patch(
"circup.open"
"circup.command_utils.open"
):
bundle = circup.Bundle(TEST_BUNDLE_NAME)
ensure_latest_bundle(bundle)
@ -900,23 +926,22 @@ def test_ensure_latest_bundle_bad_bundle_data():
bundle) but it has been corrupted (which has sometimes happened during
manual testing) then default to update.
"""
with mock.patch("circup.Bundle.latest_tag", "12345"), mock.patch(
"circup.os.path.isfile", return_value=True
), mock.patch("circup.open"), mock.patch(
"circup.get_bundle"
) as mock_gb, mock.patch(
"circup.json.load", side_effect=json.decoder.JSONDecodeError("BANG!", "doc", 1)
with mock.patch("circup.bundle.Bundle.latest_tag", "12345"), mock.patch(
"circup.command_utils.open"
), mock.patch("circup.command_utils.get_bundle") as mock_gb, mock.patch(
"circup.command_utils.json.load",
side_effect=json.decoder.JSONDecodeError("BANG!", "doc", 1),
), mock.patch(
"circup.json.dump"
"circup.command_utils.json.dump"
), mock.patch(
"circup.logger"
"circup.command_utils.logger"
) as mock_logger:
bundle = circup.Bundle(TEST_BUNDLE_NAME)
ensure_latest_bundle(bundle)
mock_gb.assert_called_once_with(bundle, "12345")
# wrong file is opened twice (one at __init__, one at save())
assert mock_logger.error.call_count == 2
assert mock_logger.exception.call_count == 2
assert mock_logger.error.call_count == 1
assert mock_logger.exception.call_count == 1
def test_ensure_latest_bundle_to_update():
@ -924,12 +949,10 @@ def test_ensure_latest_bundle_to_update():
If the version found in the BUNDLE_DATA is out of date, then cause an
update to the bundle.
"""
with mock.patch("circup.Bundle.latest_tag", "54321"), mock.patch(
"circup.os.path.isfile", return_value=True
), mock.patch("circup.open"), mock.patch(
"circup.get_bundle"
) as mock_gb, mock.patch(
"circup.json"
with mock.patch("circup.bundle.Bundle.latest_tag", "54321"), mock.patch(
"circup.command_utils.open"
), mock.patch("circup.command_utils.get_bundle") as mock_gb, mock.patch(
"circup.command_utils.json"
) as mock_json:
mock_json.load.return_value = {TEST_BUNDLE_NAME: "12345"}
bundle = circup.Bundle(TEST_BUNDLE_NAME)
@ -945,14 +968,15 @@ def test_ensure_latest_bundle_to_update_http_error():
"""
tags_data = {TEST_BUNDLE_NAME: "12345"}
with mock.patch("circup.Bundle.latest_tag", "54321"), mock.patch(
# "circup.tags_data_load", return_value=tags_data
# ), mock.patch(
"circup.os.path.isfile",
return_value=True,
), mock.patch("circup.open"), mock.patch(
"circup.get_bundle", side_effect=requests.exceptions.HTTPError("404")
# "circup.tags_data_load", return_value=tags_data
# ), mock.patch(
"circup.os.path.isfile",
return_value=True,
), mock.patch("circup.command_utils.open"), mock.patch(
"circup.command_utils.get_bundle",
side_effect=requests.exceptions.HTTPError("404"),
) as mock_gb, mock.patch(
"circup.json"
"circup.command_utils.json"
) as mock_json, mock.patch(
"circup.click.secho"
) as mock_click:
@ -970,16 +994,14 @@ def test_ensure_latest_bundle_no_update():
If the version found in the BUNDLE_DATA is NOT out of date, just log the
fact and don't update.
"""
with mock.patch("circup.Bundle.latest_tag", "12345"), mock.patch(
"circup.os.path.isfile", return_value=True
), mock.patch("circup.os.path.isdir", return_value=True), mock.patch(
"circup.open"
), mock.patch(
"circup.get_bundle"
with mock.patch("circup.bundle.Bundle.latest_tag", "12345"), mock.patch(
"circup.os.path.isdir", return_value=True
), mock.patch("circup.command_utils.open"), mock.patch(
"circup.command_utils.get_bundle"
) as mock_gb, mock.patch(
"circup.json"
"circup.shared.json"
) as mock_json, mock.patch(
"circup.logger"
"circup.command_utils.logger"
) as mock_logger:
mock_json.load.return_value = {TEST_BUNDLE_NAME: "12345"}
bundle = circup.Bundle(TEST_BUNDLE_NAME)
@ -999,16 +1021,14 @@ def test_get_bundle():
mock_progress = mock.MagicMock()
mock_progress().__enter__ = mock.MagicMock(return_value=["a", "b", "c"])
mock_progress().__exit__ = mock.MagicMock()
with mock.patch("circup.requests") as mock_requests, mock.patch(
"circup.click"
with mock.patch("circup.command_utils.requests") as mock_requests, mock.patch(
"circup.click"
) as mock_click, mock.patch(
"circup.open", mock.mock_open()
"circup.command_utils.open", mock.mock_open()
) as mock_open, mock.patch(
"circup.os.path.isdir", return_value=True
), mock.patch(
"circup.shutil"
"circup.command_utils.shutil"
) as mock_shutil, mock.patch(
"circup.zipfile"
"circup.command_utils.zipfile"
) as mock_zipfile:
mock_click.progressbar = mock_progress
mock_requests.get().status_code = mock_requests.codes.ok
@ -1030,9 +1050,9 @@ def test_get_bundle_network_error():
Ensure that if there is a network related error when grabbing the bundle
then the error is logged and re-raised for the HTTP status code.
"""
with mock.patch("circup.requests") as mock_requests, mock.patch(
"circup.tags_data_load", return_value=dict()
), mock.patch("circup.logger") as mock_logger:
with mock.patch("circup.command_utils.requests") as mock_requests, mock.patch(
"circup.shared.tags_data_load", return_value=dict()
), mock.patch("circup.command_utils.logger") as mock_logger:
# Force failure with != requests.codes.ok
mock_requests.get().status_code = mock_requests.codes.BANG
# Ensure raise_for_status actually raises an exception.
@ -1044,9 +1064,8 @@ def test_get_bundle_network_error():
get_bundle(bundle, tag)
assert ex.value.args[0] == "Bang!"
url = (
"https://github.com/" + TEST_BUNDLE_NAME + "/releases/download"
"/{tag}/adafruit-circuitpython-bundle-py-{tag}.zip".format(
tag=tag)
"https://github.com/" + TEST_BUNDLE_NAME + "/releases/download"
"/{tag}/adafruit-circuitpython-bundle-py-{tag}.zip".format(tag=tag)
)
mock_requests.get.assert_called_once_with(url, stream=True, timeout=mock.ANY)
assert mock_logger.warning.call_count == 1
@ -1059,7 +1078,9 @@ def test_show_command():
"""
runner = CliRunner()
test_bundle_modules = ["one.py", "two.py", "three.py"]
with mock.patch("circup.get_bundle_versions", return_value=test_bundle_modules):
with mock.patch(
"circup.commands.get_bundle_versions", return_value=test_bundle_modules
):
result = runner.invoke(circup.show)
assert result.exit_code == 0
assert all(m.replace(".py", "") in result.output for m in test_bundle_modules)
@ -1071,7 +1092,9 @@ def test_show_match_command():
"""
runner = CliRunner()
test_bundle_modules = ["one.py", "two.py", "three.py"]
with mock.patch("circup.get_bundle_versions", return_value=test_bundle_modules):
with mock.patch(
"circup.commands.get_bundle_versions", return_value=test_bundle_modules
):
result = runner.invoke(circup.show, ["t"])
assert result.exit_code == 0
assert "one" not in result.output
@ -1079,11 +1102,13 @@ def test_show_match_command():
def test_show_match_py_command():
"""
Check that py does not match the .py extention in the module names
Check that py does not match the .py extension in the module names
"""
runner = CliRunner()
test_bundle_modules = ["one.py", "two.py", "three.py"]
with mock.patch("circup.get_bundle_versions", return_value=test_bundle_modules):
with mock.patch(
"circup.commands.get_bundle_versions", return_value=test_bundle_modules
):
result = runner.invoke(circup.show, ["py"])
assert result.exit_code == 0
assert "0 shown" in result.output
@ -1118,7 +1143,9 @@ def test_libraries_from_imports_bad():
TEST_BUNDLE_MODULES = {"one.py": {}, "two.py": {}, "three.py": {}}
runner = CliRunner()
with mock.patch("circup.get_bundle_versions", return_value=TEST_BUNDLE_MODULES):
with mock.patch(
"circup.commands.get_bundle_versions", return_value=TEST_BUNDLE_MODULES
):
result = runner.invoke(
circup.main,
[