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_install_with_additional_arguments(check_output, check_call): installer = Pip3BundleInstallerExtensionPoint() cache_dir = mkdtemp() prefix = mkdtemp() python_path = os.path.join(prefix, 'usr', 'bin', 'python3') context_args = Mock() context_args.pip3_args = [' --test-arg-1', '--test-arg-2'] context_args.pip3_requirements = None context = BundleInstallerContext(args=context_args, cache_path=cache_dir, prefix_path=prefix) check_output.return_value = 'pkg1==3.4.5\npkg2==3.1.2\n' try: installer.initialize(context) installer.add_to_install_list('pkg1==3.4.5') installer.add_to_install_list('pkg2>=3.1.2') installer.add_to_install_list('remove_me') installer.remove_from_install_list('remove_me') result = installer.install() assert result == { 'installed_packages': [{ 'name': 'pkg1', 'version': '3.4.5' }, { 'name': 'pkg2', 'version': '3.1.2' }] } finally: shutil.rmtree(cache_dir) shutil.rmtree(prefix)
def test_install_additional_requirements(check_output, check_call): """ This test should be mocking the read and write to the file and then reading the requirements file. Instead I am just checking the packages array after the install to verify the "requrements.txt" was read and added to the list. """ installer = Pip3BundleInstallerExtensionPoint() cache_dir = mkdtemp() prefix = mkdtemp() python_path = os.path.join(prefix, 'usr', 'bin', 'python3') context_args = Mock() context_args.pip3_args = [] context_args.pip3_requirements = 'requirements.txt' context = BundleInstallerContext(args=context_args, cache_path=cache_dir, prefix_path=prefix) check_output.return_value = 'pkg1==3.4.5\npkg2==3.1.2\nrpkg==1.2.3\n' try: installer.initialize(context) installer.add_to_install_list('pkg1==3.4.5') installer.add_to_install_list('pkg2==3.1.2') result = installer.install() assert installer._packages == [ 'pkg1==3.4.5', 'pkg2==3.1.2', 'rpkg==1.2.3' ] args_list = check_call.call_args_list args = args_list[0][0][0] assert args[0] == python_path # Ensure we upgrade pip/setuptools assert args[1:] == [ '-m', 'pip', 'install', '-U', 'pip', 'setuptools==44.0.0' ] args = args_list[1][0][0] assert args[0] == python_path assert args[1:-1] == [ '-m', 'pip', 'install', '--default-timeout=100', '--ignore-installed', '-r' ] assert result == { 'installed_packages': [{ 'name': 'pkg1', 'version': '3.4.5' }, { 'name': 'pkg2', 'version': '3.1.2' }, { 'name': 'rpkg', 'version': '1.2.3' }] } finally: shutil.rmtree(cache_dir) shutil.rmtree(prefix)
def test_install_nothing(): installer = Pip3BundleInstallerExtensionPoint() cache_dir = mkdtemp() prefix = mkdtemp() context = BundleInstallerContext( args=Mock(), cache_path=cache_dir, prefix_path=prefix) installer.initialize(context) result = installer.install() assert result == {'installed_packages': []}
def test_install_not_required(check_output, check_call): installer = Pip3BundleInstallerExtensionPoint() cache_dir = mkdtemp() prefix = mkdtemp() python_path = os.path.join(prefix, 'usr', 'bin', 'python3') context_args = Mock() context_args.pip3_args = [] context_args.pip3_requirements = None context = BundleInstallerContext( args=context_args, cache_path=cache_dir, prefix_path=prefix) check_output.return_value = 'pkg1==3.4.5\npkg2==3.1.2\n' try: installer.initialize(context) installer.add_to_install_list('pkg1==3.4.5') installer.add_to_install_list('pkg2>=3.1.2') installer.add_to_install_list('remove_me') installer.remove_from_install_list('remove_me') result = installer.install() args_list = check_call.call_args_list args = args_list[0][0][0] assert args[0] == python_path # Ensure we upgrade pip/setuptools assert args[1:] == [ '-m', 'pip', 'install', '-U', 'pip', 'setuptools==44.0.0'] args = args_list[1][0][0] assert args[0] == python_path assert args[1:-1] == [ '-m', 'pip', 'install', '--default-timeout=100', '--ignore-installed', '-r'] assert result == { 'installed_packages': [ { 'name': 'pkg1', 'version': '3.4.5' }, { 'name': 'pkg2', 'version': '3.1.2' } ] } result_2 = installer.install() assert result == result_2 # Verify we haven't called pip assert check_call.call_count == 3 # Verify we haven't called pip freeze assert check_output.call_count == 1 finally: shutil.rmtree(cache_dir) shutil.rmtree(prefix)
def _setup_installers(self, context, path_context): cache_path = path_context.installer_cache_path() prefix_path = os.path.abspath(path_context.staging_path()) installers = get_bundle_installer_extensions() self.installer_cache_dirs = {} for name, installer in installers.items(): installer_cache_dir = os.path.join(cache_path, name) self.installer_cache_dirs[name] = installer_cache_dir os.makedirs(installer_cache_dir, exist_ok=True) context = BundleInstallerContext(args=context.args, cache_path=installer_cache_dir, prefix_path=prefix_path) installer.initialize(context) return installers
def test_install_with_additional_arguments(check_output, check_call): installer = PipBundleInstallerExtensionPoint() cache_dir = mkdtemp() prefix = mkdtemp() python_path = os.path.join(prefix, 'usr', 'bin', 'python2') context_args = Mock() context_args.pip_args = [' --test-arg-1', '--test-arg-2'] context = BundleInstallerContext( args=context_args, cache_path=cache_dir, prefix_path=prefix) check_output.return_value = 'pkg1==3.4.5\npkg2==3.1.2\n' try: installer.initialize(context) installer.add_to_install_list('pkg1==3.4.5') installer.add_to_install_list('pkg2>=3.1.2') installer.add_to_install_list('remove_me') installer.remove_from_install_list('remove_me') result = installer.install() args_list = check_call.call_args_list args = args_list[0][0][0] assert args[0] == python_path # Ensure we upgrade pip/setuptools assert args[1:] == ['-m', 'pip', 'install', '-U', 'pip', 'setuptools'] args = args_list[1][0][0] assert args[0] == python_path assert args[1:-1] == [ '-m', 'pip', 'install', ' --test-arg-1', '--test-arg-2', '--default-timeout=100', '--ignore-installed', '-r'] assert result == { 'installed_packages': [ { 'name': 'pkg1', 'version': '3.4.5' }, { 'name': 'pkg2', 'version': '3.1.2' } ] } finally: shutil.rmtree(cache_dir) shutil.rmtree(prefix)
def test_install_not_required(check_output, check_call): installer = Pip3BundleInstallerExtensionPoint() cache_dir = mkdtemp() prefix = mkdtemp() python_path = os.path.join(prefix, 'usr', 'bin', 'python3') context_args = Mock() context_args.pip3_args = [] context_args.pip3_requirements = None context = BundleInstallerContext(args=context_args, cache_path=cache_dir, prefix_path=prefix) check_output.return_value = 'pkg1==3.4.5\npkg2==3.1.2\n' try: installer.initialize(context) installer.add_to_install_list('pkg1==3.4.5') installer.add_to_install_list('pkg2>=3.1.2') installer.add_to_install_list('remove_me') installer.remove_from_install_list('remove_me') result = installer.install() assert result == { 'installed_packages': [{ 'name': 'pkg1', 'version': '3.4.5' }, { 'name': 'pkg2', 'version': '3.1.2' }] } result_2 = installer.install() assert result == result_2 # Verify we haven't called pip assert check_call.call_count == 4 # Verify we haven't called pip freeze assert check_output.call_count == 1 finally: shutil.rmtree(cache_dir) shutil.rmtree(prefix)
def setup_installers(self, context: CommandContext): """ Initialize all of the installer extension points. This will initialize all installer extension points registered under 'colcon_bundle.installer' :param context: Context from the calling verb """ cache_path = self._path_context.installer_cache_path() self.installers = get_bundle_installer_extensions() for name, installer in self.installers.items(): installer_cache_dir = os.path.join(cache_path, name) self.installer_cache_dirs[name] = installer_cache_dir os.makedirs(installer_cache_dir, exist_ok=True) context = BundleInstallerContext(args=context.args, cache_path=installer_cache_dir, prefix_path=self.prefix_path) installer.initialize(context)
def test_install_addtional_requirements(check_output, check_call): >>>>>>> pip+apt+py3+tar """ This test should be mocking the read and write to the file and then reading the requirements file. Instead I am just checking the packages array after the install to verify the "requrements.txt" was read and added to the list. """ installer = Pip3BundleInstallerExtensionPoint() cache_dir = mkdtemp() prefix = mkdtemp() python_path = os.path.join(prefix, 'usr', 'bin', 'python3') context_args = Mock() context_args.pip3_args = [] context_args.pip3_requirements = 'requirements.txt' context = BundleInstallerContext( args=context_args, cache_path=cache_dir, prefix_path=prefix) check_output.return_value = 'pkg1==3.4.5\npkg2==3.1.2\nrpkg==1.2.3\n' try: installer.initialize(context) installer.add_to_install_list('pkg1==3.4.5') installer.add_to_install_list('pkg2==3.1.2') result = installer.install() assert installer._packages == [ 'pkg1==3.4.5', 'pkg2==3.1.2', 'rpkg==1.2.3' ] args_list = check_call.call_args_list args = args_list[0][0][0] assert args[0] == python_path # Ensure we upgrade pip/setuptools