def _run_add_to_install_list_test(self, package_name, package_version): # We import apt inside the method it is used so we can't @patch it like # a normal import apt = mock.MagicMock() with patch.dict("sys.modules", apt=apt): cache_dir = mkdtemp() prefix = mkdtemp() _, sources_list = mkstemp() try: context_args = Mock() context_args.apt_sources_list = sources_list context = BundleInstallerContext(args=context_args, cache_path=cache_dir, prefix_path=prefix) installer = AptBundleInstallerExtension() installer.initialize(context) package_mock = mock.MagicMock() default_candidate = package_mock.candidate apt.Cache().__getitem__.return_value = package_mock candidate_mock = mock.MagicMock() package_mock.versions.get.return_value = candidate_mock installer.add_to_install_list(package_name + "=" + package_version) apt.Cache().__getitem__.assert_called_with(package_name) package_mock.versions.get.assert_called_with(package_version, default_candidate) self.assertEqual(package_mock.candidate, candidate_mock) package_mock.mark_install.assert_called_with(auto_fix=False, from_user=False) finally: shutil.rmtree(cache_dir) shutil.rmtree(prefix) os.remove(sources_list)
def test_should_load_on_ubuntu_no_python3_apt(self, _): with patch.dict("sys.modules", apt=None): apt = AptBundleInstallerExtension() with self.assertRaises(RuntimeError): apt.should_load()
def test_should_load_on_ubuntu(self, _): with patch.dict("sys.modules", apt=mock.MagicMock()): apt = AptBundleInstallerExtension() assert apt.should_load() == True
def test_should_load_not_on_ubuntu(self, get_version): apt = AptBundleInstallerExtension() get_version.side_effect = ValueError() assert apt.should_load() == False
def test_should_load_on_ubuntu_no_python3_apt(self, _): sys.modules['apt'] = None apt = AptBundleInstallerExtension() with self.assertRaises(RuntimeError): apt.should_load()
def test_should_load_on_ubuntu(self, _): sys.modules['apt'] = mock.MagicMock() apt = AptBundleInstallerExtension() assert apt.should_load() == True