| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from reno import config |
| 18 |
from reno import formatter |
| 19 |
from reno import loader |
| 20 |
from reno.tests import base |
| 21 |
|
| 22 |
|
| 23 |
class TestFormatterBase(base.TestCase): |
| 24 |
|
| 25 |
scanner_output = { |
| 26 |
'0.0.0': [('note1', 'shaA')], |
| 27 |
'1.0.0': [('note2', 'shaB'), ('note3', 'shaC')], |
| 28 |
} |
| 29 |
|
| 30 |
versions = ['0.0.0', '1.0.0'] |
| 31 |
|
| 32 |
def _get_note_body(self, reporoot, filename, sha): |
| 33 |
return self.note_bodies.get(filename, '') |
| 34 |
|
| 35 |
def setUp(self): |
| 36 |
super(TestFormatterBase, self).setUp() |
| 37 |
|
| 38 |
def _load(ldr): |
| 39 |
ldr._scanner_output = self.scanner_output |
| 40 |
ldr._cache = { |
| 41 |
'file-contents': self.note_bodies |
| 42 |
} |
| 43 |
|
| 44 |
self.c = config.Config('reporoot') |
| 45 |
|
| 46 |
with mock.patch('reno.loader.Loader._load_data', _load): |
| 47 |
self.ldr = loader.Loader( |
| 48 |
self.c, |
| 49 |
ignore_cache=False, |
| 50 |
) |
| 51 |
|
| 52 |
def tearDown(self): |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
self.ldr.close() |
| 57 |
super().tearDown() |
| 58 |
|
| 59 |
|
| 60 |
class TestFormatter(TestFormatterBase): |
| 61 |
|
| 62 |
note_bodies = { |
| 63 |
'note1': { |
| 64 |
'prelude': 'This is the prelude.', |
| 65 |
}, |
| 66 |
'note2': { |
| 67 |
'issues': [ |
| 68 |
'This is the first issue.', |
| 69 |
'This is the second issue.', |
| 70 |
], |
| 71 |
}, |
| 72 |
'note3': { |
| 73 |
'features': [ |
| 74 |
'We added a feature!', |
| 75 |
], |
| 76 |
'upgrade': None, |
| 77 |
}, |
| 78 |
} |
| 79 |
|
| 80 |
def test_with_title(self): |
| 81 |
result = formatter.format_report( |
| 82 |
loader=self.ldr, |
| 83 |
config=self.c, |
| 84 |
versions_to_include=self.versions, |
| 85 |
title='This is the title', |
| 86 |
) |
| 87 |
self.assertIn('This is the title', result) |
| 88 |
|
| 89 |
def test_versions(self): |
| 90 |
result = formatter.format_report( |
| 91 |
loader=self.ldr, |
| 92 |
config=self.c, |
| 93 |
versions_to_include=self.versions, |
| 94 |
title='This is the title', |
| 95 |
) |
| 96 |
self.assertIn('0.0.0\n=====', result) |
| 97 |
self.assertIn('1.0.0\n=====', result) |
| 98 |
|
| 99 |
def test_without_title(self): |
| 100 |
result = formatter.format_report( |
| 101 |
loader=self.ldr, |
| 102 |
config=self.c, |
| 103 |
versions_to_include=self.versions, |
| 104 |
title=None, |
| 105 |
) |
| 106 |
self.assertNotIn('This is the title', result) |
| 107 |
|
| 108 |
def test_default_section_order(self): |
| 109 |
result = formatter.format_report( |
| 110 |
loader=self.ldr, |
| 111 |
config=self.c, |
| 112 |
versions_to_include=self.versions, |
| 113 |
title=None, |
| 114 |
) |
| 115 |
prelude_pos = result.index('This is the prelude.') |
| 116 |
issues_pos = result.index('This is the first issue.') |
| 117 |
features_pos = result.index('We added a feature!') |
| 118 |
expected = [prelude_pos, features_pos, issues_pos] |
| 119 |
actual = list(sorted([prelude_pos, features_pos, issues_pos])) |
| 120 |
self.assertEqual(expected, actual) |
| 121 |
|
| 122 |
|
| 123 |
class TestFormatterCustomSections(TestFormatterBase): |
| 124 |
note_bodies = { |
| 125 |
'note1': { |
| 126 |
'prelude': 'This is the prelude.', |
| 127 |
}, |
| 128 |
'note2': { |
| 129 |
'features': [ |
| 130 |
'This is the first feature.', |
| 131 |
], |
| 132 |
'api': [ |
| 133 |
'This is the API change for the first feature.', |
| 134 |
], |
| 135 |
'features_subsubsection': [ |
| 136 |
'This is a subsubsection feature.', |
| 137 |
], |
| 138 |
}, |
| 139 |
'note3': { |
| 140 |
'api': [ |
| 141 |
'This is the API change for the second feature.', |
| 142 |
], |
| 143 |
'features': [ |
| 144 |
'This is the second feature.', |
| 145 |
], |
| 146 |
'features_subsection': [ |
| 147 |
'This is a subsection feature.', |
| 148 |
], |
| 149 |
}, |
| 150 |
} |
| 151 |
|
| 152 |
def setUp(self): |
| 153 |
super(TestFormatterCustomSections, self).setUp() |
| 154 |
self.c.override(sections=[ |
| 155 |
['features', 'New Features'], |
| 156 |
['features_subsection', 'Subsection', 2], |
| 157 |
['features_subsubsection', 'Subsubsection', 3], |
| 158 |
['api', 'API Changes'], |
| 159 |
]) |
| 160 |
|
| 161 |
def test_custom_section_order(self): |
| 162 |
result = formatter.format_report( |
| 163 |
loader=self.ldr, |
| 164 |
config=self.c, |
| 165 |
versions_to_include=self.versions, |
| 166 |
title=None, |
| 167 |
) |
| 168 |
prelude_pos = result.index('This is the prelude.') |
| 169 |
api_pos = result.index('API Changes') |
| 170 |
features_pos = result.index('New Features') |
| 171 |
features_subsection_pos = result.index('Subsection') |
| 172 |
features_subsubsection_pos = result.index('Subsubsection') |
| 173 |
expected = [ |
| 174 |
prelude_pos, |
| 175 |
features_pos, |
| 176 |
features_subsection_pos, |
| 177 |
features_subsubsection_pos, |
| 178 |
api_pos, |
| 179 |
] |
| 180 |
actual = sorted(expected) |
| 181 |
self.assertEqual(expected, actual) |
| 182 |
self.assertIn('.. _relnotes_1.0.0_API Changes:', result) |
| 183 |
|
| 184 |
def test_header_underlines(self): |
| 185 |
result = formatter.format_report( |
| 186 |
loader=self.ldr, |
| 187 |
config=self.c, |
| 188 |
versions_to_include=self.versions, |
| 189 |
title=None, |
| 190 |
).splitlines() |
| 191 |
|
| 192 |
def assert_header(header: str, char: str) -> None: |
| 193 |
pos = next( |
| 194 |
i for i, line in enumerate(result) if line.strip() == header |
| 195 |
) |
| 196 |
expected = char * len(header) |
| 197 |
self.assertEqual(result[pos + 1].strip(), expected) |
| 198 |
|
| 199 |
assert_header("New Features", "-") |
| 200 |
assert_header("Subsection", "^") |
| 201 |
assert_header("Subsubsection", "~") |
| 202 |
|
| 203 |
|
| 204 |
class TestFormatterCustomUnreleaseTitle(TestFormatterBase): |
| 205 |
|
| 206 |
note_bodies = { |
| 207 |
'note1': { |
| 208 |
'prelude': 'This is the prelude.', |
| 209 |
}, |
| 210 |
} |
| 211 |
|
| 212 |
scanner_output = { |
| 213 |
'0.1.0-1': [('note1', 'shaA')], |
| 214 |
} |
| 215 |
|
| 216 |
versions = ['0.1.0-1'] |
| 217 |
|
| 218 |
def test_with_title(self): |
| 219 |
self.c.override(unreleased_version_title='Not Released') |
| 220 |
result = formatter.format_report( |
| 221 |
loader=self.ldr, |
| 222 |
config=self.c, |
| 223 |
versions_to_include=self.versions, |
| 224 |
title='This is the title', |
| 225 |
) |
| 226 |
self.assertIn('Not Released', result) |
| 227 |
self.assertNotIn('0.1.0-1', result) |
| 228 |
self.assertIn('.. _This is the title_Not Released:', result) |
| 229 |
|
| 230 |
def test_without_title(self): |
| 231 |
result = formatter.format_report( |
| 232 |
loader=self.ldr, |
| 233 |
config=self.c, |
| 234 |
versions_to_include=self.versions, |
| 235 |
title='This is the title', |
| 236 |
) |
| 237 |
self.assertIn('0.1.0-1', result) |
| 238 |
self.assertIn('.. _This is the title_0.1.0-1:', result) |
| 239 |
|
| 240 |
|
| 241 |
class TestFormatterAnchors(TestFormatterBase): |
| 242 |
|
| 243 |
note_bodies = { |
| 244 |
'note1': { |
| 245 |
'prelude': 'This is the prelude.', |
| 246 |
}, |
| 247 |
'note2': { |
| 248 |
'issues': [ |
| 249 |
'This is the first issue.', |
| 250 |
'This is the second issue.', |
| 251 |
], |
| 252 |
}, |
| 253 |
'note3': { |
| 254 |
'features': [ |
| 255 |
'We added a feature!', |
| 256 |
], |
| 257 |
'upgrade': None, |
| 258 |
}, |
| 259 |
} |
| 260 |
|
| 261 |
def test_with_title(self): |
| 262 |
self.c.override(unreleased_version_title='Not Released') |
| 263 |
result = formatter.format_report( |
| 264 |
loader=self.ldr, |
| 265 |
config=self.c, |
| 266 |
versions_to_include=self.versions, |
| 267 |
title='This is the title', |
| 268 |
) |
| 269 |
self.assertIn('.. _This is the title_0.0.0:', result) |
| 270 |
self.assertIn('.. _This is the title_0.0.0_Prelude:', result) |
| 271 |
self.assertIn('.. _This is the title_1.0.0:', result) |
| 272 |
self.assertIn('.. _This is the title_1.0.0_Known Issues:', result) |
| 273 |
|
| 274 |
def test_without_title(self): |
| 275 |
result = formatter.format_report( |
| 276 |
loader=self.ldr, |
| 277 |
config=self.c, |
| 278 |
versions_to_include=self.versions, |
| 279 |
) |
| 280 |
self.assertIn('.. _relnotes_0.0.0:', result) |
| 281 |
self.assertIn('.. _relnotes_0.0.0_Prelude:', result) |
| 282 |
self.assertIn('.. _relnotes_1.0.0:', result) |
| 283 |
self.assertIn('.. _relnotes_1.0.0_Known Issues:', result) |
| 284 |
|
| 285 |
def test_with_branch_and_title(self): |
| 286 |
self.c.override(unreleased_version_title='Not Released') |
| 287 |
result = formatter.format_report( |
| 288 |
loader=self.ldr, |
| 289 |
config=self.c, |
| 290 |
versions_to_include=self.versions, |
| 291 |
title='This is the title', |
| 292 |
branch='stable/queens', |
| 293 |
) |
| 294 |
self.assertIn('.. _This is the title_0.0.0_stable_queens:', result) |
| 295 |
self.assertIn('.. _This is the title_0.0.0_stable_queens_Prelude:', |
| 296 |
result) |
| 297 |
self.assertIn('.. _This is the title_1.0.0_stable_queens:', result) |
| 298 |
self.assertIn( |
| 299 |
'.. _This is the title_1.0.0_stable_queens_Known Issues:', |
| 300 |
result) |
| 301 |
|
| 302 |
def test_with_branch(self): |
| 303 |
result = formatter.format_report( |
| 304 |
loader=self.ldr, |
| 305 |
config=self.c, |
| 306 |
versions_to_include=self.versions, |
| 307 |
branch='stable/queens', |
| 308 |
) |
| 309 |
self.assertIn('.. _relnotes_0.0.0_stable_queens:', result) |
| 310 |
self.assertIn('.. _relnotes_0.0.0_stable_queens_Prelude:', result) |
| 311 |
self.assertIn('.. _relnotes_1.0.0_stable_queens:', result) |
| 312 |
self.assertIn('.. _relnotes_1.0.0_stable_queens_Known Issues:', result) |