def test_install_dependencies_with_preinstall_with_requirements(callmock): d = Deployment("test", preinstall=["foobar"]) d.pip_prefix = ["pip", "install"] d.install_dependencies() callmock.assert_has_calls( [call(["pip", "install", "foobar"]), call(["pip", "install", "-r", "./requirements.txt"])] )
def test_upgrade_pip_with_preinstall(callmock): d = Deployment('test', upgrade_pip=True, preinstall=['foobar']) d.pip_prefix = ['pip', 'install'] d.install_dependencies() callmock.assert_has_calls([ call(['pip', 'install', '-U', 'pip']), call(['pip', 'install', 'foobar'])])
def test_create_venv_with_setuptools(callmock): d = Deployment('test', setuptools=True) d.create_virtualenv() eq_(TEST_VENV_PATH, d.package_dir) callmock.assert_called_with(['virtualenv', '--setuptools', TEST_VENV_PATH]) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', LOG_ARG], d.pip_args)
def test_upgrade_pip(callmock): d = Deployment('test', upgrade_pip=True) d.pip_prefix = d.pip_preinstall_prefix = ['pip'] d.pip_args = ['install'] d.install_dependencies() callmock.assert_called_with( ['pip', 'install', ANY, '-U', 'pip'])
def test_install_dependencies_with_preinstall(callmock): d = Deployment('test', preinstall=['foobar']) d.pip_prefix = d.pip_preinstall_prefix = ['pip'] d.pip_args = ['install'] d.install_dependencies() callmock.assert_called_with( ['pip', 'install', 'foobar'])
def test_create_venv_with_verbose(callmock): d = Deployment('test', verbose=True) d.create_virtualenv() eq_('debian/test/opt/venvs/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', '--verbose', 'debian/test/opt/venvs/test'])
def test_create_venv_with_extra_virtualenv(callmock): d = Deployment('test', extra_virtualenv_arg=["--never-download"]) d.create_virtualenv() eq_('debian/test/opt/venvs/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', '--never-download', 'debian/test/opt/venvs/test'])
def test_install_dependencies_with_requirements(callmock): d = Deployment('test') d.pip_prefix = ['pip'] d.pip_args = ['install'] d.install_dependencies() callmock.assert_called_with( ['pip', 'install', '-r', './requirements.txt'])
def test_fix_activate_path(): deployment = Deployment('test') temp = tempfile.NamedTemporaryFile() with open(temp.name, 'w') as fh: fh.write(textwrap.dedent(""" other things VIRTUAL_ENV="/this/path/is/wrong/and/longer/than/new/path" more other things """)) expected = textwrap.dedent(""" other things VIRTUAL_ENV="/usr/share/python/test" more other things """) with patch('dh_virtualenv.deployment.os.path.join', return_value=temp.name): deployment.fix_activate_path() with open(temp.name) as fh: eq_(expected, temp.read())
def test_venv_with_custom_python(callmock): d = Deployment('test', python='/tmp/python') d.create_virtualenv() eq_(TEST_VENV_PATH, d.package_dir) callmock.assert_called_with(['virtualenv', '--python', '/tmp/python', TEST_VENV_PATH]) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', LOG_ARG], d.pip_args)
def test_fix_activate_path(): deployment = Deployment('test') temp = tempfile.NamedTemporaryFile() with open(temp.name, 'w') as fh: fh.write( textwrap.dedent(""" other things VIRTUAL_ENV="/this/path/is/wrong/and/longer/than/new/path" more other things """)) expected = textwrap.dedent(""" other things VIRTUAL_ENV="/usr/share/python/test" more other things """) with patch('dh_virtualenv.deployment.os.path.join', return_value=temp.name): deployment.fix_activate_path() with open(temp.name) as fh: eq_(expected, temp.read())
def test_create_venv_with_extra_virtualenv(callmock): d = Deployment('test', extra_virtualenv_arg=["--never-download"]) d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', '--never-download', 'debian/test/usr/share/python/test'])
def test_create_venv_with_system_packages(callmock): d = Deployment('test', use_system_packages=True) d.create_virtualenv() eq_('debian/test/opt/venvs/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--system-site-packages', 'debian/test/opt/venvs/test']) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', LOG_ARG], d.pip_args)
def test_install_dependencies_with_preinstall_with_requirements(callmock): d = Deployment('test', preinstall=['foobar']) d.pip_prefix = ['pip', 'install'] d.install_dependencies() callmock.assert_has_calls([ call(['pip', 'install', 'foobar']), call(['pip', 'install', '-r', './requirements.txt']) ])
def test_testrunner(callmock): d = Deployment('test') d.run_tests() callmock.assert_called_once_with([ PY_CMD, 'setup.py', 'test', ], cwd='.')
def test_testrunner(callmock): d = Deployment('test') d.run_tests() callmock.assert_called_once_with([ 'debian/test/usr/share/python/test/bin/python', 'setup.py', 'test', ])
def test_create_venv_with_extra_urls(callmock): d = Deployment('test', extra_urls=['foo', 'bar']) d.create_virtualenv() eq_(TEST_VENV_PATH, d.package_dir) callmock.assert_called_with(['virtualenv', TEST_VENV_PATH]) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', '--extra-index-url=foo', '--extra-index-url=bar', LOG_ARG], d.pip_args)
def test_install_dependencies_with_preinstall_with_requirements(callmock): d = Deployment('test', preinstall=['foobar']) d.pip_prefix = ['pip', 'install'] d.install_dependencies() callmock.assert_has_calls([ call(['pip', 'install', 'foobar']), call(['pip', 'install', '-r', 'requirements.txt']) ])
def test_create_venv_with_system_packages(callmock): d = Deployment('test', use_system_packages=True) d.create_virtualenv() eq_('debian/test/opt/venvs/test', d.package_dir) callmock.assert_called_with( ['virtualenv', '--system-site-packages', 'debian/test/opt/venvs/test']) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', LOG_ARG], d.pip_args)
def test_install_package(callmock): d = Deployment('test') d.bin_dir = 'derp' d.pip_prefix = ['derp/python', 'derp/pip'] d.install_package() callmock.assert_called_with([ 'derp/python', 'derp/pip', '.', ], cwd=os.getcwd())
def test_upgrade_pip_with_preinstall(callmock): d = Deployment('test', upgrade_pip=True, preinstall=['foobar']) d.pip_prefix = d.pip_preinstall_prefix = ['pip'] d.pip_args = ['install'] d.install_dependencies() callmock.assert_has_calls([ call(['pip', 'install', ANY, '-U', 'pip']), call(['pip', 'install', 'foobar'])])
def test_create_venv(callmock): d = Deployment('test') d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with([ 'virtualenv', '--no-site-packages', 'debian/test/usr/share/python/test' ]) eq_([PY_CMD, PIP_CMD, 'install', '--log=' + os.path.abspath('foo')], d.pip_prefix)
def test_install_package(callmock): d = Deployment('test') d.bin_dir = 'derp' d.install_package() callmock.assert_called_with([ 'derp/python', 'setup.py', 'install', '--record', 'foo', '--install-headers', 'debian/test/usr/share/python/test/include/site/python2.6' ])
def test_create_venv_with_extra_urls(callmock): d = Deployment('test', extra_urls=['foo', 'bar']) d.create_virtualenv() eq_('debian/test/opt/venvs/test', d.package_dir) callmock.assert_called_with( ['virtualenv', '--no-site-packages', 'debian/test/opt/venvs/test']) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', '--extra-index-url=foo', '--extra-index-url=bar', LOG_ARG], d.pip_args)
def test_venv_with_custom_python(callmock): d = Deployment('test', python='/tmp/python') d.create_virtualenv() eq_('debian/test/opt/venvs/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', '--python', '/tmp/python', 'debian/test/opt/venvs/test']) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', LOG_ARG], d.pip_args)
def test_create_venv_with_extra_pip_arg(callmock): d = Deployment('test', extra_pip_arg=['--no-compile']) d.create_virtualenv() d.install_dependencies() eq_('debian/test/opt/venvs/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', 'debian/test/opt/venvs/test']) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', LOG_ARG, '--no-compile'], d.pip_args)
def test_create_venv_with_extra_urls(callmock): d = Deployment('test', extra_urls=['foo', 'bar']) d.create_virtualenv() eq_('debian/test/opt/venvs/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', 'debian/test/opt/venvs/test']) eq_([PY_CMD, PIP_CMD], d.pip_prefix) eq_(['install', '--extra-index-url=foo', '--extra-index-url=bar', LOG_ARG], d.pip_args)
def test_create_builtin_venv_with_unsupported_options(callmock): d = Deployment('test', python='python_interpreter', builtin_venv=True, setuptools=True, verbose=True) d.create_virtualenv() eq_(TEST_VENV_PATH, d.package_dir) callmock.assert_called_with( ['python_interpreter', '-m', 'venv', TEST_VENV_PATH])
def test_create_venv_with_system_packages(callmock): d = Deployment('test', use_system_packages=True) d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--system-site-packages', 'debian/test/usr/share/python/test']) eq_([PY_CMD, PIP_CMD, 'install', '--log=' + os.path.abspath('foo')], d.pip_prefix)
def test_create_venv(callmock): d = Deployment('test') d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', 'debian/test/usr/share/python/test']) eq_(['debian/test/usr/share/python/test/bin/python', 'debian/test/usr/share/python/test/bin/pip', 'install', '--log=foo'], d.pip_prefix)
def test_custom_src_dir(callmock): d = Deployment("test") d.pip_prefix = ["pip", "install"] d.sourcedirectory = "root/srv/application" d.create_virtualenv() d.install_dependencies() callmock.assert_called_with( [ "debian/test/usr/share/python/test/bin/python", "debian/test/usr/share/python/test/bin/pip", "install", "--log=foo", "-r", "root/srv/application/requirements.txt", ] ) d.install_package() callmock.assert_called_with( [ "debian/test/usr/share/python/test/bin/python", "debian/test/usr/share/python/test/bin/pip", "install", "--log=foo", "root/srv/application", ] )
def test_create_venv(callmock): d = Deployment('test') d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with([ 'virtualenv', '--no-site-packages', 'debian/test/usr/share/python/test' ]) eq_([ 'debian/test/usr/share/python/test/bin/python', 'debian/test/usr/share/python/test/bin/pip', 'install', '--log=foo' ], d.pip_prefix)
def test_create_venv_with_extra_urls(callmock): d = Deployment('test', extra_urls=['foo', 'bar']) d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', 'debian/test/usr/share/python/test']) eq_(['debian/test/usr/share/python/test/bin/python', 'debian/test/usr/share/python/test/bin/pip', 'install', '--extra-index-url=foo', '--extra-index-url=bar', '--log=foo'], d.pip_prefix)
def test_venv_with_custom_python(callmock): d = Deployment('test', python='/tmp/python') d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', '--python', '/tmp/python', 'debian/test/usr/share/python/test']) eq_([PY_CMD, PIP_CMD, 'install', '--log=' + os.path.abspath('foo')], d.pip_prefix)
def test_fix_local_symlinks(deployment_dir): d = Deployment('testing') d.package_dir = deployment_dir local = os.path.join(deployment_dir, 'local') os.makedirs(local) target = os.path.join(deployment_dir, 'sometarget') symlink = os.path.join(local, 'symlink') os.symlink(target, symlink) d.fix_local_symlinks() eq_(os.readlink(symlink), '../sometarget')
def test_fix_local_symlinks(deployment_dir): d = Deployment("testing") d.package_dir = deployment_dir local = os.path.join(deployment_dir, "local") os.makedirs(local) target = os.path.join(deployment_dir, "sometarget") symlink = os.path.join(local, "symlink") os.symlink(target, symlink) d.fix_local_symlinks() eq_(os.readlink(symlink), "../sometarget")
def test_fix_local_symlinks_with_relative_links(deployment_dir): # Runs shouldn't ruin the already relative symlinks. d = Deployment('testing') d.package_dir = deployment_dir local = os.path.join(deployment_dir, 'local') os.makedirs(local) symlink = os.path.join(local, 'symlink') os.symlink('../target', symlink) d.fix_local_symlinks() eq_(os.readlink(symlink), '../target')
def test_custom_pip_tool_used_for_installation(callmock, _): d = Deployment( 'test', preinstall=['pip-custom-platform'], pip_tool='pip-custom-platform', ) d.install_dependencies() d.install_package() callmock.assert_has_calls([ call([PY_CMD, PIP_CMD, 'install', LOG_ARG, 'pip-custom-platform']), call([PY_CMD, CUSTOM_PIP_CMD, 'install', LOG_ARG, '-r', './requirements.txt']), call([PY_CMD, CUSTOM_PIP_CMD, 'install', LOG_ARG, '.'], cwd=os.path.abspath('.')), ])
def test_create_venv_with_extra_urls(callmock): d = Deployment('test', extra_urls=['foo', 'bar']) d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with([ 'virtualenv', '--no-site-packages', 'debian/test/usr/share/python/test' ]) eq_([ 'debian/test/usr/share/python/test/bin/python', 'debian/test/usr/share/python/test/bin/pip', 'install', '--extra-index-url=foo', '--extra-index-url=bar', '--log=foo' ], d.pip_prefix)
def test_create_venv_with_extra_pip_arg(callmock): d = Deployment('test', extra_pip_arg=['--no-compile']) d.create_virtualenv() d.install_dependencies() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', 'debian/test/usr/share/python/test']) eq_([PY_CMD, PIP_CMD, 'install', '--log=' + os.path.abspath('foo'), '--no-compile'], d.pip_prefix)
def test_fix_local_symlinks_with_relative_links(deployment_dir): # Runs shouldn't ruin the already relative symlinks. d = Deployment("testing") d.package_dir = deployment_dir local = os.path.join(deployment_dir, "local") os.makedirs(local) symlink = os.path.join(local, "symlink") os.symlink("../target", symlink) d.fix_local_symlinks() eq_(os.readlink(symlink), "../target")
def test_create_venv_with_custom_index_url(callmock): d = Deployment('test', extra_urls=['foo', 'bar'], pypi_url='http://example.com/simple') d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with([ 'virtualenv', '--no-site-packages', 'debian/test/usr/share/python/test' ]) eq_([ PY_CMD, PIP_CMD, 'install', '--pypi-url=http://example.com/simple', '--extra-index-url=foo', '--extra-index-url=bar', '--log=' + os.path.abspath('foo') ], d.pip_prefix)
def test_find_script_files_normal_shebang(bin_dir): d = Deployment('testing') d.bin_dir = bin_dir script_files = [os.path.join(bin_dir, s) for s in ('s1', 's2', 's3')] for script in script_files: with open(os.path.join(bin_dir, script), 'w') as f: f.write('#!/usr/bin/python\n') with open(os.path.join(bin_dir, 'n1'), 'w') as f: f.write('#!/bin/bash') found_files = sorted(d.find_script_files()) eq_(found_files, script_files)
def test_create_venv(callmock): d = Deployment("test") d.create_virtualenv() eq_("debian/test/usr/share/python/test", d.package_dir) callmock.assert_called_with(["virtualenv", "--no-site-packages", "debian/test/usr/share/python/test"]) eq_( [ "debian/test/usr/share/python/test/bin/python", "debian/test/usr/share/python/test/bin/pip", "install", "--log=foo", ], d.pip_prefix, )
def test_create_venv_with_custom_index_url(callmock): d = Deployment('test', extra_urls=['foo', 'bar'], pypi_url='http://example.com/simple') d.create_virtualenv() eq_('debian/test/usr/share/python/test', d.package_dir) callmock.assert_called_with(['virtualenv', '--no-site-packages', 'debian/test/usr/share/python/test']) eq_(['debian/test/usr/share/python/test/bin/python', 'debian/test/usr/share/python/test/bin/pip', 'install', '--pypi-url=http://example.com/simple', '--extra-index-url=foo', '--extra-index-url=bar', '--log=foo'], d.pip_prefix)
def test_deployment_from_options_with_verbose(): options, _ = get_default_parser().parse_args([ '--verbose' ]) d = Deployment.from_options('foo', options) eq_(d.package, 'foo') eq_(d.verbose, True)
def test_find_script_files_long_shebang(bin_dir): d = Deployment('testing') d.bin_dir = bin_dir script_files = [os.path.join(bin_dir, s) for s in ('s1', 's2', 's3')] for script in script_files: with open(os.path.join(bin_dir, script), 'w') as f: # It does not really matter what we write into the # exec statement as executable here f.write(create_new_style_shebang('/usr/bin/python')) with open(os.path.join(bin_dir, 'n1'), 'w') as f: f.write('#!/bin/bash') found_files = sorted(d.find_script_files()) eq_(found_files, script_files)
def check_shebangs_fix(interpreter, path): """Checks shebang substitution for the given interpreter""" deployment = Deployment('test') temp = tempfile.NamedTemporaryFile() # We cheat here a little. The fix_shebangs walks through the # project directory, however we can just point to a single # file, as the underlying mechanism is just grep -r. deployment.bin_dir = temp.name expected_shebang = '#!' + os.path.join(path, 'bin/python') + '\n' with open(temp.name, 'w') as f: f.write('#!/usr/bin/{0}\n'.format(interpreter)) deployment.fix_shebangs() with open(temp.name) as f: eq_(f.read(), expected_shebang) with open(temp.name, 'w') as f: f.write('#!/usr/bin/env {0}\n'.format(interpreter)) deployment.fix_shebangs() with open(temp.name) as f: eq_(f.readline(), expected_shebang)
def test_deployment_from_options(): options, _ = get_default_parser().parse_args([ '--extra-index-url', 'http://example.com', '-O--pypi-url', 'http://example.org' ]) d = Deployment.from_options('foo', options) eq_(d.package, 'foo') eq_(d.pypi_url, 'http://example.org') eq_(d.extra_urls, ['http://example.com'])
def test_custom_src_dir(callmock): d = Deployment('test') d.pip_prefix = ['pip', 'install'] d.sourcedirectory = 'root/srv/application' d.create_virtualenv() d.install_dependencies() callmock.assert_called_with([ 'debian/test/usr/share/python/test/bin/python', 'debian/test/usr/share/python/test/bin/pip', 'install', '--log=foo', '-r', 'root/srv/application/requirements.txt' ], ) d.install_package() callmock.assert_called_with([ 'debian/test/usr/share/python/test/bin/python', 'debian/test/usr/share/python/test/bin/pip', 'install', '--log=foo', 'root/srv/application', ])
def test_custom_src_dir(callmock): d = Deployment('test') d.pip_prefix = ['pip', 'install'] d.sourcedirectory = 'root/srv/application' d.create_virtualenv() d.install_dependencies() callmock.assert_called_with([ PY_CMD, PIP_CMD, 'install', '--log=' + os.path.abspath('foo'), '-r', 'root/srv/application/requirements.txt' ], ) d.install_package() callmock.assert_called_with([ PY_CMD, PIP_CMD, 'install', '--log=' + os.path.abspath('foo'), '.', ], cwd=os.path.abspath('root/srv/application'))