| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
import argparse |
| 15 |
import os |
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
import fixtures |
| 19 |
from testtools import ExpectedException |
| 20 |
|
| 21 |
from reno import config |
| 22 |
from reno.config import Section |
| 23 |
from reno import defaults |
| 24 |
from reno import main |
| 25 |
from reno.tests import base |
| 26 |
|
| 27 |
|
| 28 |
def expected_options(**overrides): |
| 29 |
"""The default config options, along with any overrides set via kwargs.""" |
| 30 |
result = { |
| 31 |
o.name: ( |
| 32 |
Section.from_raw_yaml(o.default) |
| 33 |
if o.name == "sections" |
| 34 |
else o.default |
| 35 |
) |
| 36 |
for o in config._OPTIONS |
| 37 |
} |
| 38 |
result.update(**overrides) |
| 39 |
return result |
| 40 |
|
| 41 |
|
| 42 |
class TestConfig(base.TestCase): |
| 43 |
EXAMPLE_CONFIG = """ |
| 44 |
collapse_pre_releases: false |
| 45 |
""" |
| 46 |
|
| 47 |
def setUp(self): |
| 48 |
super(TestConfig, self).setUp() |
| 49 |
|
| 50 |
self.tempdir = self.useFixture(fixtures.TempDir()) |
| 51 |
|
| 52 |
def test_defaults(self): |
| 53 |
c = config.Config(self.tempdir.path) |
| 54 |
actual = c.options |
| 55 |
self.assertEqual(expected_options(), actual) |
| 56 |
|
| 57 |
def test_override(self): |
| 58 |
c = config.Config(self.tempdir.path) |
| 59 |
c.override( |
| 60 |
collapse_pre_releases=False, |
| 61 |
) |
| 62 |
actual = c.options |
| 63 |
expected = expected_options(collapse_pre_releases=False) |
| 64 |
self.assertEqual(expected, actual) |
| 65 |
|
| 66 |
def test_override_multiple(self): |
| 67 |
c = config.Config(self.tempdir.path) |
| 68 |
c.override( |
| 69 |
notesdir='value1', |
| 70 |
) |
| 71 |
c.override( |
| 72 |
notesdir='value2', |
| 73 |
) |
| 74 |
actual = c.options |
| 75 |
expected = expected_options(notesdir='value2') |
| 76 |
self.assertEqual(expected, actual) |
| 77 |
|
| 78 |
def test_override_sections_with_subsections(self): |
| 79 |
c = config.Config(self.tempdir.path) |
| 80 |
c.override( |
| 81 |
sections=[ |
| 82 |
["features", "Features"], |
| 83 |
["features_sub", "Sub", 2], |
| 84 |
["features_subsub", "Subsub", 3], |
| 85 |
["bugs", "Bugs"], |
| 86 |
["bugs_sub", "Sub", 2], |
| 87 |
["documentation", "Documentation", 1] |
| 88 |
], |
| 89 |
) |
| 90 |
actual = c.options |
| 91 |
expected = expected_options( |
| 92 |
sections=[ |
| 93 |
Section("features", "Features", section_level=1), |
| 94 |
Section("features_sub", "Sub", section_level=2), |
| 95 |
Section("features_subsub", "Subsub", section_level=3), |
| 96 |
Section("bugs", "Bugs", section_level=1), |
| 97 |
Section("bugs_sub", "Sub", section_level=2), |
| 98 |
Section("documentation", "Documentation", section_level=1), |
| 99 |
] |
| 100 |
) |
| 101 |
self.assertEqual(expected, actual) |
| 102 |
|
| 103 |
|
| 104 |
with ExpectedException(ValueError): |
| 105 |
c.override(sections=[["features"]]) |
| 106 |
with ExpectedException(ValueError): |
| 107 |
c.override(sections=[["features", "Features", 0]]) |
| 108 |
with ExpectedException(ValueError): |
| 109 |
c.override(sections=[["features", "Features", 5]]) |
| 110 |
|
| 111 |
def test_load_file_not_present(self): |
| 112 |
missing = 'reno.config.Config._report_missing_config_files' |
| 113 |
with mock.patch(missing) as error_handler: |
| 114 |
config.Config(self.tempdir.path) |
| 115 |
self.assertEqual(1, error_handler.call_count) |
| 116 |
|
| 117 |
def _test_load_file(self, config_path): |
| 118 |
with open(config_path, 'w') as fd: |
| 119 |
fd.write(self.EXAMPLE_CONFIG) |
| 120 |
self.addCleanup(os.unlink, config_path) |
| 121 |
c = config.Config(self.tempdir.path) |
| 122 |
self.assertEqual(False, c.collapse_pre_releases) |
| 123 |
|
| 124 |
def test_load_file_in_releasenotesdir(self): |
| 125 |
rn_path = self.tempdir.join('releasenotes') |
| 126 |
os.mkdir(rn_path) |
| 127 |
config_path = self.tempdir.join('releasenotes/config.yaml') |
| 128 |
self._test_load_file(config_path) |
| 129 |
|
| 130 |
def test_load_file_in_repodir(self): |
| 131 |
config_path = self.tempdir.join('reno.yaml') |
| 132 |
self._test_load_file(config_path) |
| 133 |
|
| 134 |
def test_load_file_empty(self): |
| 135 |
config_path = self.tempdir.join('reno.yaml') |
| 136 |
with open(config_path, 'w') as fd: |
| 137 |
fd.write('# Add reno config here') |
| 138 |
self.addCleanup(os.unlink, config_path) |
| 139 |
c = config.Config(self.tempdir.path) |
| 140 |
self.assertEqual(True, c.collapse_pre_releases) |
| 141 |
|
| 142 |
def test_get_default(self): |
| 143 |
d = config.Config.get_default('notesdir') |
| 144 |
self.assertEqual('notes', d) |
| 145 |
|
| 146 |
def test_get_default_unknown(self): |
| 147 |
self.assertRaises( |
| 148 |
ValueError, |
| 149 |
config.Config.get_default, |
| 150 |
'unknownopt', |
| 151 |
) |
| 152 |
|
| 153 |
def _run_override_from_parsed_args(self, argv): |
| 154 |
parser = argparse.ArgumentParser() |
| 155 |
main._build_query_arg_group(parser) |
| 156 |
args = parser.parse_args(argv) |
| 157 |
c = config.Config(self.tempdir.path) |
| 158 |
c.override_from_parsed_args(args) |
| 159 |
return c |
| 160 |
|
| 161 |
def test_override_from_parsed_args_empty(self): |
| 162 |
c = self._run_override_from_parsed_args([]) |
| 163 |
actual = { |
| 164 |
o.name: getattr(c, o.name) |
| 165 |
for o in config._OPTIONS |
| 166 |
} |
| 167 |
self.assertEqual(expected_options(), actual) |
| 168 |
|
| 169 |
def test_override_from_parsed_args_boolean_false(self): |
| 170 |
c = self._run_override_from_parsed_args([ |
| 171 |
'--no-collapse-pre-releases', |
| 172 |
]) |
| 173 |
actual = c.options |
| 174 |
expected = expected_options(collapse_pre_releases=False) |
| 175 |
self.assertEqual(expected, actual) |
| 176 |
|
| 177 |
def test_override_from_parsed_args_boolean_true(self): |
| 178 |
c = self._run_override_from_parsed_args([ |
| 179 |
'--collapse-pre-releases', |
| 180 |
]) |
| 181 |
actual = c.options |
| 182 |
expected = expected_options(collapse_pre_releases=True) |
| 183 |
self.assertEqual(expected, actual) |
| 184 |
|
| 185 |
def test_override_from_parsed_args_string(self): |
| 186 |
c = self._run_override_from_parsed_args([ |
| 187 |
'--earliest-version', '1.2.3', |
| 188 |
]) |
| 189 |
actual = c.options |
| 190 |
expected = expected_options(earliest_version='1.2.3') |
| 191 |
self.assertEqual(expected, actual) |
| 192 |
|
| 193 |
def test_override_from_parsed_args_ignore_non_options(self): |
| 194 |
parser = argparse.ArgumentParser() |
| 195 |
main._build_query_arg_group(parser) |
| 196 |
parser.add_argument('not_a_config_option') |
| 197 |
args = parser.parse_args(['value']) |
| 198 |
c = config.Config(self.tempdir.path) |
| 199 |
c.override_from_parsed_args(args) |
| 200 |
self.assertFalse(hasattr(c, 'not_a_config_option')) |
| 201 |
|
| 202 |
|
| 203 |
class TestConfigProperties(base.TestCase): |
| 204 |
|
| 205 |
def setUp(self): |
| 206 |
super(TestConfigProperties, self).setUp() |
| 207 |
|
| 208 |
self.tempdir = self.useFixture(fixtures.TempDir()) |
| 209 |
self.c = config.Config('releasenotes') |
| 210 |
|
| 211 |
def test_reporoot(self): |
| 212 |
self.c.reporoot = 'blah//' |
| 213 |
self.assertEqual('blah/', self.c.reporoot) |
| 214 |
self.c.reporoot = 'blah' |
| 215 |
self.assertEqual('blah/', self.c.reporoot) |
| 216 |
|
| 217 |
def test_notespath(self): |
| 218 |
self.assertEqual('releasenotes/notes', self.c.notespath) |
| 219 |
self.c.override(notesdir='thenotes') |
| 220 |
self.assertEqual('releasenotes/thenotes', self.c.notespath) |
| 221 |
|
| 222 |
def test_template(self): |
| 223 |
template = defaults.TEMPLATE.format(defaults.PRELUDE_SECTION_NAME) |
| 224 |
self.assertEqual(template, self.c.template) |
| 225 |
self.c.override(template='i-am-a-template') |
| 226 |
self.assertEqual('i-am-a-template', self.c.template) |
| 227 |
|
| 228 |
def test_prelude_override(self): |
| 229 |
template = defaults.TEMPLATE.format(defaults.PRELUDE_SECTION_NAME) |
| 230 |
self.assertEqual(template, self.c.template) |
| 231 |
self.c.override(prelude_section_name='fake_prelude_name') |
| 232 |
expected_template = defaults.TEMPLATE.format('fake_prelude_name') |
| 233 |
self.assertEqual(expected_template, self.c.template) |
| 234 |
|
| 235 |
def test_prelude_and_template_override(self): |
| 236 |
template = defaults.TEMPLATE.format(defaults.PRELUDE_SECTION_NAME) |
| 237 |
self.assertEqual(template, self.c.template) |
| 238 |
self.c.override(prelude_section_name='fake_prelude_name', |
| 239 |
template='i-am-a-template') |
| 240 |
self.assertEqual('fake_prelude_name', self.c.prelude_section_name) |
| 241 |
self.assertEqual('i-am-a-template', self.c.template) |