def test_Module_mpy_mismatch(): """ Ensure the ``outofdate`` property on a Module instance returns the expected boolean value to correctly indicate if the referenced module is, in fact, out of date. """ path = os.path.join("foo", "bar", "baz", "module.mpy") repo = "https://github.com/adafruit/SomeLibrary.git" with mock.patch("circup.CPY_VERSION", "6.1.2"): bundle = circup.Bundle(TEST_BUNDLE_NAME) m1 = circup.Module(path, repo, "1.2.3", "1.2.3", True, bundle, (None, None)) m2 = circup.Module( path, repo, "1.2.3", "1.2.3", True, bundle, ("7.0.0-alpha.1", None) ) m3 = circup.Module( path, repo, "1.2.3", "1.2.3", True, bundle, (None, "7.0.0-alpha.1") ) with mock.patch("circup.CPY_VERSION", "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.CPY_VERSION", "7.0.0"): assert m1.mpy_mismatch is False assert m1.outofdate is False assert m2.mpy_mismatch is False assert m2.outofdate is False assert m3.mpy_mismatch is True assert m3.outofdate is True
def test_Module_outofdate(): """ Ensure the ``outofdate`` property on a Module instance returns the expected boolean value to correctly indicate if the referenced module is, in fact, out of date. """ path = os.path.join("foo", "bar", "baz", "module.py") repo = "https://github.com/adafruit/SomeLibrary.git" bundle_path = os.path.join("baz", "bar", "foo", "module.py") m1 = circup.Module(path, repo, "1.2.3", "3.2.1", bundle_path) m2 = circup.Module(path, repo, "1.2.3", "1.2.3", bundle_path) # shouldn't happen! m3 = circup.Module(path, repo, "3.2.1", "1.2.3", bundle_path) assert m1.outofdate is True assert m2.outofdate is False assert m3.outofdate is False
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" mpy = True with mock.patch("circup.logger.info") as mock_logger, mock.patch( "circup.os.path.isfile", return_value=False), mock.patch( "circup.CPY_VERSION", "4.1.2"), mock.patch("circup.os.walk", return_value=[["lib", "", ""]]) as mock_walk: m = circup.Module(path, repo, device_version, bundle_version, mpy) 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 == os.path.join("lib", m.name) assert m.mpy is True mock_walk.assert_called_once_with(circup.BUNDLE_DIR.format("4mpy"))
def test_Module_repr(): """ Ensure the repr(dict) is returned (helps when logging). """ path = os.path.join("foo", "bar", "baz", "local_module.py") repo = "https://github.com/adafruit/SomeLibrary.git" device_version = "1.2.3" bundle_version = "3.2.1" with mock.patch("circup.os.path.isfile", return_value=True), mock.patch( "circup.CPY_VERSION", "4.1.2" ), mock.patch("circup.Bundle.lib_dir", return_value="tests"): bundle = circup.Bundle(TEST_BUNDLE_NAME) m = circup.Module( path, repo, device_version, bundle_version, False, bundle, (None, None) ) assert repr(m) == repr( { "path": path, "file": "local_module.py", "name": "local_module", "repo": repo, "device_version": device_version, "bundle_version": bundle_version, "bundle_path": os.path.join("tests", m.file), "mpy": False, "min_version": None, "max_version": None, } )
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", "dir_module", "") repo = "https://github.com/adafruit/SomeLibrary.git" device_version = "1.2.3" bundle_version = "3.2.1" mpy = True with mock.patch("circup.logger.info") as mock_logger, mock.patch( "circup.os.path.isfile", return_value=False ), mock.patch("circup.CPY_VERSION", "4.1.2"), mock.patch( "circup.DATA_DIR", "/tests/DATA_DIR" ), mock.patch( "circup.Bundle.lib_dir", return_value="tests" ): bundle = circup.Bundle(TEST_BUNDLE_NAME) m = circup.Module( path, repo, device_version, bundle_version, mpy, bundle, (None, None) ) mock_logger.assert_called_once_with(m) assert m.path == path assert m.file is None assert m.name == "dir_module" assert m.repo == repo assert m.device_version == device_version assert m.bundle_version == bundle_version assert m.bundle_path == os.path.join("tests", m.name) assert m.mpy is True
def test_Module_row(): """ Ensure the tuple contains the expected items to be correctly displayed in a table of version-related results. """ bundle = circup.Bundle(TEST_BUNDLE_NAME) path = os.path.join("foo", "bar", "baz", "module.py") repo = "https://github.com/adafruit/SomeLibrary.git" with mock.patch("circup.os.path.isfile", return_value=True), mock.patch( "circup.CPY_VERSION", "6.1.2" ): m = circup.Module(path, repo, "1.2.3", None, False, bundle, (None, None)) assert m.row == ("module", "1.2.3", "unknown", "Major Version") m = circup.Module(path, repo, "1.2.3", "1.3.4", False, bundle, (None, None)) assert m.row == ("module", "1.2.3", "1.3.4", "Minor Version") m = circup.Module(path, repo, "1.2.3", "1.2.3", True, bundle, ("9.0.0", None)) assert m.row == ("module", "1.2.3", "1.2.3", "MPY Format")
def test_Module_row(): """ Ensure the tuple contains the expected items to be correctly displayed in a table of version-related results. """ path = os.path.join("foo", "bar", "baz", "module.py") repo = "https://github.com/adafruit/SomeLibrary.git" device_version = "1.2.3" bundle_version = None bundle_path = os.path.join("baz", "bar", "foo", "module.py") 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")
def test_Module_outofdate_bad_versions(): """ Sometimes, the version is not a valid semver value. In this case, the ``outofdate`` property assumes the module should be updated (to correct this problem). Such a problem should be logged. """ path = os.path.join("foo", "bar", "baz", "module.py") repo = "https://github.com/adafruit/SomeLibrary.git" device_version = "hello" 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.logger.warning") as mock_logger: assert m.outofdate is True assert mock_logger.call_count == 2
def test_Module_update_dir(): """ Ensure if the module is a directory, the expected actions take place to update the module on the connected device. """ path = os.path.join("foo", "bar", "baz", "module.py") repo = "https://github.com/adafruit/SomeLibrary.git" 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.shutil") as mock_shutil, mock.patch( "circup.os.path.isdir", return_value=True): m.update() mock_shutil.rmtree.assert_called_once_with(m.path) mock_shutil.copytree.assert_called_once_with(m.bundle_path, m.path)
def test_Module_major_update_bad_versions(): """ Sometimes, the version is not a valid semver value. In this case, the ``major_update`` property assumes the module is a major update, so as not to block the user from getting the latest update. Such a problem should be logged. """ path = os.path.join("foo", "bar", "baz", "module.py") repo = "https://github.com/adafruit/SomeLibrary.git" device_version = "1.2.3" bundle_version = "version-3" 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.logger.warning") as mock_logger: assert m.major_update is True assert mock_logger.call_count == 2
def test_Module_update_file(): """ Ensure if the module is a file, the expected actions take place to update the module on the connected device. """ bundle = circup.Bundle(TEST_BUNDLE_NAME) path = os.path.join("foo", "bar", "baz", "module.py") repo = "https://github.com/adafruit/SomeLibrary.git" device_version = "1.2.3" bundle_version = None m = circup.Module(path, repo, device_version, bundle_version, False, bundle, (None, None)) with mock.patch("circup.shutil") as mock_shutil, mock.patch( "circup.os.remove") as mock_remove, mock.patch( "circup.os.path.isdir", return_value=False): m.update() mock_remove.assert_called_once_with(m.path) mock_shutil.copyfile.assert_called_once_with(m.bundle_path, m.path)
def test_Module_repr(): """ Ensure the repr(dict) is returned (helps when logging). """ 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" with mock.patch("circup.os.path.isfile", return_value=True), mock.patch( "circup.CPY_VERSION", "4.1.2"), mock.patch("circup.os.walk", return_value=[["lib", "", ""]]): m = circup.Module(path, repo, device_version, bundle_version, False) assert repr(m) == repr({ "path": path, "file": "module.py", "name": "module", "repo": repo, "device_version": device_version, "bundle_version": bundle_version, "bundle_path": os.path.join("lib", m.file), "mpy": False, })