second try
[vsorcdistro/.git] / ryu / .eggs / pbr-5.3.1-py2.7.egg / pbr / tests / test_util.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. (HP)
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import io
17 import tempfile
18 import textwrap
19
20 import six
21 from six.moves import configparser
22 import sys
23
24 from pbr.tests import base
25 from pbr import util
26
27
28 def config_from_ini(ini):
29     config = {}
30     if sys.version_info >= (3, 2):
31         parser = configparser.ConfigParser()
32     else:
33         parser = configparser.SafeConfigParser()
34     ini = textwrap.dedent(six.u(ini))
35     parser.readfp(io.StringIO(ini))
36     for section in parser.sections():
37         config[section] = dict(parser.items(section))
38     return config
39
40
41 class TestExtrasRequireParsingScenarios(base.BaseTestCase):
42
43     scenarios = [
44         ('simple_extras', {
45             'config_text': """
46                 [extras]
47                 first =
48                     foo
49                     bar==1.0
50                 second =
51                     baz>=3.2
52                     foo
53                 """,
54             'expected_extra_requires': {
55                 'first': ['foo', 'bar==1.0'],
56                 'second': ['baz>=3.2', 'foo'],
57                 'test': ['requests-mock'],
58                 "test:(python_version=='2.6')": ['ordereddict'],
59             }
60         }),
61         ('with_markers', {
62             'config_text': """
63                 [extras]
64                 test =
65                     foo:python_version=='2.6'
66                     bar
67                     baz<1.6 :python_version=='2.6'
68                     zaz :python_version>'1.0'
69                 """,
70             'expected_extra_requires': {
71                 "test:(python_version=='2.6')": ['foo', 'baz<1.6'],
72                 "test": ['bar', 'zaz']}}),
73         ('no_extras', {
74             'config_text': """
75             [metadata]
76             long_description = foo
77             """,
78             'expected_extra_requires':
79             {}
80         })]
81
82     def test_extras_parsing(self):
83         config = config_from_ini(self.config_text)
84         kwargs = util.setup_cfg_to_setup_kwargs(config)
85
86         self.assertEqual(self.expected_extra_requires,
87                          kwargs['extras_require'])
88
89
90 class TestInvalidMarkers(base.BaseTestCase):
91
92     def test_invalid_marker_raises_error(self):
93         config = {'extras': {'test': "foo :bad_marker>'1.0'"}}
94         self.assertRaises(SyntaxError, util.setup_cfg_to_setup_kwargs, config)
95
96
97 class TestMapFieldsParsingScenarios(base.BaseTestCase):
98
99     scenarios = [
100         ('simple_project_urls', {
101             'config_text': """
102                 [metadata]
103                 project_urls =
104                     Bug Tracker = https://bugs.launchpad.net/pbr/
105                     Documentation = https://docs.openstack.org/pbr/
106                     Source Code = https://git.openstack.org/cgit/openstack-dev/pbr/
107                 """,  # noqa: E501
108             'expected_project_urls': {
109                 'Bug Tracker': 'https://bugs.launchpad.net/pbr/',
110                 'Documentation': 'https://docs.openstack.org/pbr/',
111                 'Source Code': 'https://git.openstack.org/cgit/openstack-dev/pbr/',  # noqa: E501
112             },
113         }),
114         ('query_parameters', {
115             'config_text': """
116                 [metadata]
117                 project_urls =
118                     Bug Tracker = https://bugs.launchpad.net/pbr/?query=true
119                     Documentation = https://docs.openstack.org/pbr/?foo=bar
120                     Source Code = https://git.openstack.org/cgit/openstack-dev/pbr/commit/?id=hash
121                 """,  # noqa: E501
122             'expected_project_urls': {
123                 'Bug Tracker': 'https://bugs.launchpad.net/pbr/?query=true',
124                 'Documentation': 'https://docs.openstack.org/pbr/?foo=bar',
125                 'Source Code': 'https://git.openstack.org/cgit/openstack-dev/pbr/commit/?id=hash',  # noqa: E501
126             },
127         }),
128     ]
129
130     def test_project_url_parsing(self):
131         config = config_from_ini(self.config_text)
132         kwargs = util.setup_cfg_to_setup_kwargs(config)
133
134         self.assertEqual(self.expected_project_urls, kwargs['project_urls'])
135
136
137 class TestKeywordsParsingScenarios(base.BaseTestCase):
138
139     scenarios = [
140         ('keywords_list', {
141             'config_text': """
142                 [metadata]
143                 keywords =
144                     one
145                     two
146                     three
147                 """,  # noqa: E501
148             'expected_keywords': ['one', 'two', 'three'],
149             },
150         ),
151         ('inline_keywords', {
152             'config_text': """
153                 [metadata]
154                 keywords = one, two, three
155                 """,  # noqa: E501
156             'expected_keywords': ['one, two, three'],
157         }),
158     ]
159
160     def test_keywords_parsing(self):
161         config = config_from_ini(self.config_text)
162         kwargs = util.setup_cfg_to_setup_kwargs(config)
163
164         self.assertEqual(self.expected_keywords, kwargs['keywords'])
165
166
167 class TestProvidesExtras(base.BaseTestCase):
168     def test_provides_extras(self):
169         ini = """
170         [metadata]
171         provides_extras = foo
172                           bar
173         """
174         config = config_from_ini(ini)
175         kwargs = util.setup_cfg_to_setup_kwargs(config)
176         self.assertEqual(['foo', 'bar'], kwargs['provides_extras'])
177
178
179 class TestDataFilesParsing(base.BaseTestCase):
180
181     scenarios = [
182         ('data_files', {
183             'config_text': """
184             [files]
185             data_files =
186                 'i like spaces/' =
187                     'dir with space/file with spc 2'
188                     'dir with space/file with spc 1'
189             """,
190             'data_files': [
191                 ('i like spaces/', ['dir with space/file with spc 2',
192                                     'dir with space/file with spc 1'])
193             ]
194         })]
195
196     def test_handling_of_whitespace_in_data_files(self):
197         config = config_from_ini(self.config_text)
198         kwargs = util.setup_cfg_to_setup_kwargs(config)
199
200         self.assertEqual(self.data_files,
201                          list(kwargs['data_files']))
202
203
204 class TestUTF8DescriptionFile(base.BaseTestCase):
205     def test_utf8_description_file(self):
206         _, path = tempfile.mkstemp()
207         ini_template = """
208         [metadata]
209         description_file = %s
210         """
211         # Two \n's because pbr strips the file content and adds \n\n
212         # This way we can use it directly as the assert comparison
213         unicode_description = u'UTF8 description: é"…-ʃŋ\'\n\n'
214         ini = ini_template % path
215         with io.open(path, 'w', encoding='utf8') as f:
216             f.write(unicode_description)
217         config = config_from_ini(ini)
218         kwargs = util.setup_cfg_to_setup_kwargs(config)
219         self.assertEqual(unicode_description, kwargs['long_description'])