| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# Copyright (c) 2014 Spotify AB |
| 4 |
|
| 5 |
# This file is part of dh-virtualenv. |
| 6 |
|
| 7 |
# dh-virtualenv is free software: you can redistribute it and/or |
| 8 |
# modify it under the terms of the GNU General Public License as |
| 9 |
# published by the Free Software Foundation, either version 2 of the |
| 10 |
# License, or (at your option) any later version. |
| 11 |
|
| 12 |
# dh-virtualenv is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
# General Public License for more details. |
| 16 |
|
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with dh-virtualenv. If not, see |
| 19 |
# <http://www.gnu.org/licenses/>. |
| 20 |
import io |
| 21 |
import os |
| 22 |
import warnings |
| 23 |
|
| 24 |
from dh_virtualenv import cmdline |
| 25 |
from mock import patch |
| 26 |
from nose.tools import eq_, ok_ |
| 27 |
|
| 28 |
|
| 29 |
def get_mocked_stderr(): |
| 30 |
# optparse will try to print str(err) to sys.stderr, both on Python 2 and 3 |
| 31 |
# so we need to mock sys.stderr with the right *IO class |
| 32 |
if str == bytes: |
| 33 |
# We're on Python 2, where str(foo) is a bytes string |
| 34 |
return io.BytesIO() |
| 35 |
|
| 36 |
else: |
| 37 |
# We're on Python 3, where str(foo) is a unicode string |
| 38 |
return io.StringIO() |
| 39 |
|
| 40 |
|
| 41 |
@patch.object(cmdline.DebhelperOptionParser, 'error') |
| 42 |
def test_unknown_argument_is_error(error_mock): |
| 43 |
parser = cmdline.DebhelperOptionParser(usage='foo') |
| 44 |
parser.parse_args(['-f']) |
| 45 |
eq_(1, error_mock.call_count) |
| 46 |
|
| 47 |
|
| 48 |
def test_test_debhelper_option_parsing(): |
| 49 |
parser = cmdline.DebhelperOptionParser() |
| 50 |
parser.add_option('--sourcedirectory') |
| 51 |
opts, args = parser.parse_args(['-O--sourcedirectory', '/tmp']) |
| 52 |
eq_('/tmp', opts.sourcedirectory) |
| 53 |
eq_([], args) |
| 54 |
|
| 55 |
|
| 56 |
def test_parser_picks_up_DH_OPTIONS_from_environ(): |
| 57 |
with patch.dict(os.environ, {'DH_OPTIONS': '--sourcedirectory=/tmp/'}): |
| 58 |
parser = cmdline.get_default_parser() |
| 59 |
opts, args = parser.parse_args() |
| 60 |
eq_('/tmp/', opts.sourcedirectory) |
| 61 |
|
| 62 |
|
| 63 |
def test_get_default_parser(): |
| 64 |
parser = cmdline.get_default_parser() |
| 65 |
opts, args = parser.parse_args([ |
| 66 |
'-O--sourcedirectory', '/tmp/foo', |
| 67 |
'--extra-index-url', 'http://example.com' |
| 68 |
]) |
| 69 |
eq_('/tmp/foo', opts.sourcedirectory) |
| 70 |
eq_(['http://example.com'], opts.extra_index_url) |
| 71 |
|
| 72 |
|
| 73 |
def test_pypi_url_creates_deprecation_warning(): |
| 74 |
# This test needs to be the first one that uses '--pypi-url' flag. |
| 75 |
# Otherwise the 'default' for warnings.simplefilter will exclude |
| 76 |
# the subsequent warnings. Another option would be to use 'always' |
| 77 |
# in cmdline.py, but that's quite wide cannon to shoot with. |
| 78 |
parser = cmdline.get_default_parser() |
| 79 |
with warnings.catch_warnings(record=True) as w: |
| 80 |
parser.parse_args([ |
| 81 |
'--pypi-url=http://example.com', |
| 82 |
]) |
| 83 |
eq_(len(w), 1) |
| 84 |
ok_(issubclass(w[0].category, DeprecationWarning)) |
| 85 |
eq_(str(w[0].message), 'Use of --pypi-url is deprecated. Use --index-url instead') |
| 86 |
|
| 87 |
|
| 88 |
def test_no_test_creates_deprecation_warning(): |
| 89 |
parser = cmdline.get_default_parser() |
| 90 |
with warnings.catch_warnings(record=True) as w: |
| 91 |
parser.parse_args([ |
| 92 |
'--no-test', |
| 93 |
]) |
| 94 |
eq_(len(w), 1) |
| 95 |
ok_(issubclass(w[0].category, DeprecationWarning)) |
| 96 |
eq_(str(w[0].message), |
| 97 |
'Use of --no-test is deprecated and has no effect. ' |
| 98 |
'Use --setuptools-test if you want to execute ' |
| 99 |
'`setup.py test` during package build.') |
| 100 |
|
| 101 |
|
| 102 |
@patch('sys.exit') |
| 103 |
def test_pypi_url_index_url_conflict(exit_): |
| 104 |
parser = cmdline.get_default_parser() |
| 105 |
f = get_mocked_stderr() |
| 106 |
with patch('sys.stderr', f): |
| 107 |
parser.parse_args([ |
| 108 |
'--pypi-url=http://example.com', |
| 109 |
'--index-url=http://example.org'] |
| 110 |
) |
| 111 |
ok_('Deprecated --pypi-url and the new --index-url are mutually exclusive' |
| 112 |
in f.getvalue()) |
| 113 |
exit_.assert_called_once_with(2) |
| 114 |
|
| 115 |
|
| 116 |
@patch('sys.exit') |
| 117 |
def test_test_flag_conflict(exit_): |
| 118 |
parser = cmdline.get_default_parser() |
| 119 |
f = get_mocked_stderr() |
| 120 |
with patch('sys.stderr', f): |
| 121 |
parser.parse_args([ |
| 122 |
'--no-test', |
| 123 |
'--setuptools-test'] |
| 124 |
) |
| 125 |
ok_('Deprecated --no-test and the new --setuptools-test are mutually ' |
| 126 |
'exclusive' |
| 127 |
in f.getvalue()) |
| 128 |
exit_.assert_called_once_with(2) |
| 129 |
|
| 130 |
|
| 131 |
@patch('sys.exit') |
| 132 |
def test_pypi_url_index_url_conflict_independent_from_order(exit_): |
| 133 |
parser = cmdline.get_default_parser() |
| 134 |
f = get_mocked_stderr() |
| 135 |
with patch('sys.stderr', f): |
| 136 |
parser.parse_args([ |
| 137 |
'--index-url=http://example.org', |
| 138 |
'--pypi-url=http://example.com'] |
| 139 |
) |
| 140 |
ok_('Deprecated --pypi-url and the new --index-url are mutually exclusive' |
| 141 |
in f.getvalue()) |
| 142 |
exit_.assert_called_once_with(2) |
| 143 |
|
| 144 |
|
| 145 |
def test_that_default_test_option_should_be_false(): |
| 146 |
parser = cmdline.get_default_parser() |
| 147 |
opts, args = parser.parse_args() |
| 148 |
eq_(False, opts.setuptools_test) |
| 149 |
|
| 150 |
|
| 151 |
def test_that_test_option_can_be_true(): |
| 152 |
parser = cmdline.get_default_parser() |
| 153 |
opts, args = parser.parse_args(['--setuptools-test']) |
| 154 |
eq_(True, opts.setuptools_test) |
| 155 |
|
| 156 |
|
| 157 |
def test_that_no_test_option_has_no_effect(): |
| 158 |
parser = cmdline.get_default_parser() |
| 159 |
opts, args = parser.parse_args(['--no-test']) |
| 160 |
eq_(False, opts.setuptools_test) |
| 161 |
|
| 162 |
|
| 163 |
def test_that_default_use_system_packages_option_should_be_false(): |
| 164 |
parser = cmdline.get_default_parser() |
| 165 |
opts, args = parser.parse_args() |
| 166 |
eq_(False, opts.use_system_packages) |
| 167 |
|
| 168 |
|
| 169 |
def test_that_use_system_packages_option_can_be_true(): |
| 170 |
parser = cmdline.get_default_parser() |
| 171 |
opts, args = parser.parse_args(['--use-system-packages']) |
| 172 |
eq_(True, opts.use_system_packages) |
| 173 |
|
| 174 |
|
| 175 |
def test_builtin_venv_and_setuptools_conflict(): |
| 176 |
error_message = '--setuptools flag is not supported by builtin venv module' |
| 177 |
args_list = [ |
| 178 |
['--builtin-venv', '--setuptools'], |
| 179 |
['--setuptools', '--builtin-venv'], |
| 180 |
] |
| 181 |
|
| 182 |
for args in args_list: |
| 183 |
f = get_mocked_stderr() |
| 184 |
with patch('sys.stderr', f), patch('sys.exit') as sysexit: |
| 185 |
parser = cmdline.get_default_parser() |
| 186 |
parser.parse_args(args) |
| 187 |
ok_(error_message in f.getvalue()) |
| 188 |
sysexit.assert_called_once_with(2) |