test_util.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. (HP)
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import io
  15. import textwrap
  16. import six
  17. from six.moves import configparser
  18. import sys
  19. from pbr.tests import base
  20. from pbr import util
  21. class TestExtrasRequireParsingScenarios(base.BaseTestCase):
  22. scenarios = [
  23. ('simple_extras', {
  24. 'config_text': """
  25. [extras]
  26. first =
  27. foo
  28. bar==1.0
  29. second =
  30. baz>=3.2
  31. foo
  32. """,
  33. 'expected_extra_requires': {
  34. 'first': ['foo', 'bar==1.0'],
  35. 'second': ['baz>=3.2', 'foo'],
  36. 'test': ['requests-mock'],
  37. "test:(python_version=='2.6')": ['ordereddict'],
  38. }
  39. }),
  40. ('with_markers', {
  41. 'config_text': """
  42. [extras]
  43. test =
  44. foo:python_version=='2.6'
  45. bar
  46. baz<1.6 :python_version=='2.6'
  47. zaz :python_version>'1.0'
  48. """,
  49. 'expected_extra_requires': {
  50. "test:(python_version=='2.6')": ['foo', 'baz<1.6'],
  51. "test": ['bar', 'zaz']}}),
  52. ('no_extras', {
  53. 'config_text': """
  54. [metadata]
  55. long_description = foo
  56. """,
  57. 'expected_extra_requires':
  58. {}
  59. })]
  60. def config_from_ini(self, ini):
  61. config = {}
  62. if sys.version_info >= (3, 2):
  63. parser = configparser.ConfigParser()
  64. else:
  65. parser = configparser.SafeConfigParser()
  66. ini = textwrap.dedent(six.u(ini))
  67. parser.readfp(io.StringIO(ini))
  68. for section in parser.sections():
  69. config[section] = dict(parser.items(section))
  70. return config
  71. def test_extras_parsing(self):
  72. config = self.config_from_ini(self.config_text)
  73. kwargs = util.setup_cfg_to_setup_kwargs(config)
  74. self.assertEqual(self.expected_extra_requires,
  75. kwargs['extras_require'])
  76. class TestInvalidMarkers(base.BaseTestCase):
  77. def test_invalid_marker_raises_error(self):
  78. config = {'extras': {'test': "foo :bad_marker>'1.0'"}}
  79. self.assertRaises(SyntaxError, util.setup_cfg_to_setup_kwargs, config)