Ejemplo n.º 1
0
    def setUp(self):
        super().setUp()

        class Options:
            source = '.'
            node_packages = []
            node_engine = nodejs._NODEJS_VERSION
            npm_run = []
            node_package_manager = 'npm'
            source = '.'
        self.options = Options()

        self.project_options = snapcraft.ProjectOptions()

        self.useFixture(fixture_setup.CleanEnvironment())

        patcher = mock.patch('snapcraft.internal.common.run')
        self.run_mock = patcher.start()
        self.addCleanup(patcher.stop)

        patcher = mock.patch('snapcraft.internal.common.run_output')
        self.run_output_mock = patcher.start()
        self.addCleanup(patcher.stop)
        self.run_output_mock.return_value = '{"dependencies": []}'

        patcher = mock.patch('snapcraft.sources.Tar')
        self.tar_mock = patcher.start()
        self.addCleanup(patcher.stop)

        patcher = mock.patch('sys.stdout')
        patcher.start()
        self.addCleanup(patcher.stop)

        self.nodejs_url = nodejs.get_nodejs_release(
            nodejs._NODEJS_VERSION, self.project_options.deb_arch)
Ejemplo n.º 2
0
    def test_build(self, mock_base_build, mock_run, mock_run_output):
        self.options.requirements = 'requirements.txt'
        self.options.constraints = 'constraints.txt'
        self.options.python_packages = ['test', 'packages']

        class TempDir(tempfile.TemporaryDirectory):
            def __enter__(self):
                project_whl_path = os.path.join(self.name, 'project.whl')
                open(project_whl_path, 'w').close()
                return super().__enter__()

        patcher = mock.patch('tempfile.TemporaryDirectory',
                             new=mock.Mock(wraps=TempDir))
        patcher.start()
        self.addCleanup(patcher.stop)

        mock_run_output.return_value = 'yaml (1.2)\bextras (1.0)'

        self.useFixture(fixture_setup.CleanEnvironment())
        plugin = python.PythonPlugin('test-part', self.options,
                                     self.project_options)
        setup_directories(plugin, self.options.python_version)

        def build_side_effect():
            open(os.path.join(plugin.builddir, 'setup.py'), 'w').close()

        mock_base_build.side_effect = build_side_effect

        requirements_path = os.path.join(plugin.sourcedir, 'requirements.txt')
        constraints_path = os.path.join(plugin.sourcedir, 'constraints.txt')

        pip_command = [
            os.path.join(plugin.installdir, 'usr', 'bin', 'python3'), '-m',
            'pip'
        ]

        pip_wheel = [
            'wheel', '--disable-pip-version-check', '--no-index',
            '--find-links', plugin._python_package_dir, '--constraint',
            constraints_path, '--wheel-dir', mock.ANY
        ]

        pip_install = [
            'install', '--user', '--no-compile', '--disable-pip-version-check',
            '--no-index', '--find-links', plugin._python_package_dir,
            '--constraint', constraints_path
        ]

        calls = [
            mock.call(
                pip_command + pip_wheel +
                ['--requirement', requirements_path, '.', 'test', 'packages'],
                cwd=plugin.builddir,
                env=mock.ANY),
            mock.call(tests.ContainsList(pip_command + pip_install +
                                         ['project.whl']),
                      env=mock.ANY),
        ]
        plugin.build()
        mock_run.assert_has_calls(calls)
Ejemplo n.º 3
0
    def test_build_with_proivder_stage_dir(self):
        self.useFixture(fixture_setup.CleanEnvironment())

        plugin = plainbox_provider.PlainboxProviderPlugin(
            'test-part', self.options, self.project_options)

        os.makedirs(plugin.sourcedir)
        provider_path = os.path.join(self.project_options.stage_dir,
                                     'providers', 'test-provider')
        os.makedirs(provider_path)

        # Place a few files with bad shebangs, and some files that shouldn't be
        # changed.
        files = [{
            'path': os.path.join(plugin.installdir, 'baz'),
            'contents': '#!/foo/bar/baz/python3',
            'expected': '#!/usr/bin/env python3',
        }, {
            'path': os.path.join(plugin.installdir, 'bin', 'foobar'),
            'contents': '#!/foo/baz/python3.5',
            'expected': '#!/usr/bin/env python3.5',
        }, {
            'path': os.path.join(plugin.installdir, 'foo'),
            'contents': 'foo',
            'expected': 'foo',
        }, {
            'path': os.path.join(plugin.installdir, 'bar'),
            'contents': 'bar\n#!/usr/bin/python3',
            'expected': 'bar\n#!/usr/bin/python3',
        }]

        for file_info in files:
            os.makedirs(os.path.dirname(file_info['path']), exist_ok=True)
            with open(file_info['path'], 'w') as f:
                f.write(file_info['contents'])

        plugin.build()

        calls = [
            mock.call(['python3', 'manage.py', 'validate'],
                      env={'PROVIDERPATH': provider_path}),
            mock.call(['python3', 'manage.py', 'build']),
            mock.call(['python3', 'manage.py', 'i18n']),
            mock.call([
                'python3', 'manage.py', 'install', '--layout=relocatable',
                '--prefix=/providers/test-part',
                '--root={}'.format(plugin.installdir)
            ]),
        ]
        self.mock_run.assert_has_calls(calls)

        for file_info in files:
            with open(os.path.join(plugin.installdir, file_info['path']),
                      'r') as f:
                self.assertThat(f.read(), Equals(file_info['expected']))
Ejemplo n.º 4
0
    def test_build(self, mock_base_build):
        self.options.requirements = 'requirements.txt'
        self.options.constraints = 'constraints.txt'
        self.options.python_packages = ['test', 'packages']

        packages = collections.OrderedDict()
        packages['yaml'] = '1.2'
        packages['extras'] = '1.0'
        self.mock_pip.return_value.list.return_value = packages

        self.useFixture(fixture_setup.CleanEnvironment())
        plugin = python.PythonPlugin('test-part', self.options,
                                     self.project_options)
        setup_directories(plugin, self.options.python_version)

        for file_name in (self.options.requirements, self.options.constraints):
            path = os.path.join(plugin.sourcedir, file_name)
            open(path, 'w').close()

        def build_side_effect():
            open(os.path.join(plugin.builddir, 'setup.py'), 'w').close()
            os.mkdir(os.path.join(plugin.builddir, 'dist'))
            open(os.path.join(plugin.builddir, 'dist', 'package.tar'),
                 'w').close()

        mock_base_build.side_effect = build_side_effect

        requirements_path = os.path.join(plugin.sourcedir, 'requirements.txt')
        constraints_path = os.path.join(plugin.sourcedir, 'constraints.txt')

        pip_wheel = self.mock_pip.return_value.wheel
        pip_wheel.return_value = ['foo', 'bar']

        plugin.build()

        # Pip should not attempt to download again in build (only pull)
        pip_download = self.mock_pip.return_value.download
        pip_download.assert_not_called()

        pip_wheel.assert_called_once_with(['test', 'packages'],
                                          constraints={constraints_path},
                                          process_dependency_links=False,
                                          requirements={requirements_path},
                                          setup_py_dir=plugin.sourcedir)

        pip_install = self.mock_pip.return_value.install
        pip_install.assert_called_once_with(['foo', 'bar'],
                                            process_dependency_links=False,
                                            upgrade=True,
                                            install_deps=False)
Ejemplo n.º 5
0
    def setUp(self):
        super().setUp()

        self.useFixture(fixture_setup.CleanEnvironment())

        self.project_options = snapcraft.ProjectOptions()

        patcher = mock.patch('snapcraft.internal.common.run')
        self.run_mock = patcher.start()
        self.addCleanup(patcher.stop)

        patcher = mock.patch('snapcraft.internal.common.run_output')
        self.run_output_mock = patcher.start()
        self.addCleanup(patcher.stop)

        patcher = mock.patch('sys.stdout')
        patcher.start()
        self.addCleanup(patcher.stop)
Ejemplo n.º 6
0
    def test_build(self, platform_machine_mock, platform_architecture_mock):
        self.useFixture(fixture_setup.CleanEnvironment())
        self.useFixture(fixtures.EnvironmentVariable('PATH', '/bin'))

        class Options:
            source = '.'
            gulp_tasks = []
            node_engine = '4'

        platform_machine_mock.return_value = 'x86_64'
        platform_architecture_mock.return_value = ('64bit', 'ELF')
        plugin = gulp.GulpPlugin('test-part', Options(), self.project_options)

        os.makedirs(plugin.builddir)
        open(os.path.join(plugin.builddir, 'package.json'), 'w').close()

        plugin.build()

        path = '{}:/bin'.format(os.path.join(plugin._npm_dir, 'bin'))
        self.run_mock.assert_has_calls([
            mock.call(['npm', 'install', '-g', 'gulp-cli'],
                      cwd=plugin.builddir,
                      env={
                          'PATH': path,
                          'NPM_CONFIG_PREFIX': plugin._npm_dir
                      }),
            mock.call(['npm', 'install', '--only-development'],
                      cwd=plugin.builddir,
                      env={
                          'PATH': path,
                          'NPM_CONFIG_PREFIX': plugin._npm_dir
                      }),
        ])

        self.tar_mock.assert_has_calls([
            mock.call(
                nodejs.get_nodejs_release(plugin.options.node_engine,
                                          plugin.project.deb_arch),
                os.path.join(plugin._npm_dir)),
            mock.call().provision(plugin._npm_dir,
                                  clean_target=False,
                                  keep_tarball=True)
        ])
Ejemplo n.º 7
0
    def test_use_staged_python_extra_cppflags(self, run_mock, run_output_mock):
        self.useFixture(fixture_setup.CleanEnvironment())
        # Add some extra CPPFLAGS into the environment
        self.useFixture(
            fixtures.EnvironmentVariable('CPPFLAGS', '-I/opt/include'))

        plugin = python.PythonPlugin('test-part', self.options,
                                     self.project_options)

        setup_directories(plugin, self.options.python_version)
        # Create the necessary hints to detect a staged python
        staged_python_bin = os.path.join(plugin.project.stage_dir, 'usr',
                                         'bin', 'python3')
        os.makedirs(os.path.dirname(staged_python_bin))
        open(staged_python_bin, 'w').close()
        staged_python_include = os.path.join(plugin.project.stage_dir, 'usr',
                                             'include', 'python3.7')
        os.makedirs(staged_python_include)

        plugin.pull()

        pip_command = [
            os.path.join(plugin.project.stage_dir, 'usr', 'bin', 'python3'),
            '-m', 'pip', 'download', '--disable-pip-version-check', '--dest',
            os.path.join(plugin.partdir, 'packages'), '.'
        ]
        cwd = plugin.sourcedir
        env = {
            'PYTHONUSERBASE':
            plugin.installdir,
            'PYTHONHOME':
            os.path.join(plugin.project.stage_dir, 'usr'),
            'PATH':
            '{}/usr/bin:$PATH'.format(plugin.installdir),
            'CPPFLAGS':
            '-I{} -I/opt/include'.format(
                os.path.join(plugin.project.stage_dir, 'usr', 'include',
                             'python3.7'))
        }
        run_mock.assert_called_once_with(pip_command, cwd=cwd, env=env)