Debian Codegraph @

test/test_cmdline.py

raw ◀ ▶
2 Debian patches touch this file
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_ignore_unknown_debhelper_options():
57 parser = cmdline.DebhelperOptionParser()
58 parser.add_option('-O')
59 opts, args = parser.parse_args(['-O--buildsystem=none'])
60 eq_([], args)
61
62
63 def test_parser_picks_up_DH_OPTIONS_from_environ():
64 with patch.dict(os.environ, {'DH_OPTIONS': '--sourcedirectory=/tmp/'}):
65 parser = cmdline.get_default_parser()
66 opts, args = parser.parse_args()
67 eq_('/tmp/', opts.sourcedirectory)
68
69 def test_get_default_parser():
70 parser = cmdline.get_default_parser()
71 opts, args = parser.parse_args([
72 '-O--sourcedirectory', '/tmp/foo',
73 '--extra-index-url', 'http://example.com'
74 ])
75 eq_('/tmp/foo', opts.sourcedirectory)
76 eq_(['http://example.com'], opts.extra_index_url)
77
78
79 def test_pypi_url_creates_deprecation_warning():
80 # This test needs to be the first one that uses '--pypi-url' flag.
81 # Otherwise the 'default' for warnings.simplefilter will exclude
82 # the subsequent warnings. Another option would be to use 'always'
83 # in cmdline.py, but that's quite wide cannon to shoot with.
84 parser = cmdline.get_default_parser()
85 with warnings.catch_warnings(record=True) as w:
86 parser.parse_args([
87 '--pypi-url=http://example.com',
88 ])
89 eq_(len(w), 1)
90 ok_(issubclass(w[0].category, DeprecationWarning))
91 eq_(str(w[0].message), 'Use of --pypi-url is deprecated. Use --index-url instead')
92
93
94 def test_no_test_creates_deprecation_warning():
95 parser = cmdline.get_default_parser()
96 with warnings.catch_warnings(record=True) as w:
97 parser.parse_args([
98 '--no-test',
99 ])
100 eq_(len(w), 1)
101 ok_(issubclass(w[0].category, DeprecationWarning))
102 eq_(str(w[0].message),
103 'Use of --no-test is deprecated and has no effect. '
104 'Use --setuptools-test if you want to execute '
105 '`setup.py test` during package build.')
106
107
108 @patch('sys.exit')
109 def test_pypi_url_index_url_conflict(exit_):
110 parser = cmdline.get_default_parser()
111 f = get_mocked_stderr()
112 with patch('sys.stderr', f):
113 parser.parse_args([
114 '--pypi-url=http://example.com',
115 '--index-url=http://example.org']
116 )
117 ok_('Deprecated --pypi-url and the new --index-url are mutually exclusive'
118 in f.getvalue())
119 exit_.assert_called_once_with(2)
120
121
122 @patch('sys.exit')
123 def test_test_flag_conflict(exit_):
124 parser = cmdline.get_default_parser()
125 f = get_mocked_stderr()
126 with patch('sys.stderr', f):
127 parser.parse_args([
128 '--no-test',
129 '--setuptools-test']
130 )
131 ok_('Deprecated --no-test and the new --setuptools-test are mutually '
132 'exclusive'
133 in f.getvalue())
134 exit_.assert_called_once_with(2)
135
136
137 @patch('sys.exit')
138 def test_pypi_url_index_url_conflict_independent_from_order(exit_):
139 parser = cmdline.get_default_parser()
140 f = get_mocked_stderr()
141 with patch('sys.stderr', f):
142 parser.parse_args([
143 '--index-url=http://example.org',
144 '--pypi-url=http://example.com']
145 )
146 ok_('Deprecated --pypi-url and the new --index-url are mutually exclusive'
147 in f.getvalue())
148 exit_.assert_called_once_with(2)
149
150
151 def test_that_default_test_option_should_be_false():
152 parser = cmdline.get_default_parser()
153 opts, args = parser.parse_args()
154 eq_(False, opts.setuptools_test)
155
156
157 def test_that_test_option_can_be_true():
158 parser = cmdline.get_default_parser()
159 opts, args = parser.parse_args(['--setuptools-test'])
160 eq_(True, opts.setuptools_test)
161
162
163 def test_that_no_test_option_has_no_effect():
164 parser = cmdline.get_default_parser()
165 opts, args = parser.parse_args(['--no-test'])
166 eq_(False, opts.setuptools_test)
167
168
169 def test_that_default_use_system_packages_option_should_be_false():
170 parser = cmdline.get_default_parser()
171 opts, args = parser.parse_args()
172 eq_(False, opts.use_system_packages)
173
174
175 def test_that_use_system_packages_option_can_be_true():
176 parser = cmdline.get_default_parser()
177 opts, args = parser.parse_args(['--use-system-packages'])
178 eq_(True, opts.use_system_packages)
•179
180
181 def test_builtin_venv_and_setuptools_conflict():
182 error_message = '--setuptools flag is not supported by builtin venv module'
183 args_list = [
184 ['--builtin-venv', '--setuptools'],
185 ['--setuptools', '--builtin-venv'],
186 ]
187
188 for args in args_list:
189 f = get_mocked_stderr()
190 with patch('sys.stderr', f), patch('sys.exit') as sysexit:
191 parser = cmdline.get_default_parser()
192 parser.parse_args(args)
193 ok_(error_message in f.getvalue())
194 sysexit.assert_called_once_with(2)