|
3 | 3 |
|
4 | 4 | import os
|
5 | 5 |
|
| 6 | +from textwrap import dedent |
| 7 | + |
6 | 8 | from click.testing import CliRunner
|
7 | 9 | from twisted.trial.unittest import TestCase
|
8 | 10 |
|
@@ -112,22 +114,6 @@ def test_template_extended(self):
|
112 | 114 |
|
113 | 115 | self.assertEqual(config.template, ("towncrier.templates", "default.rst"))
|
114 | 116 |
|
115 |
| - def test_missing(self): |
116 |
| - """ |
117 |
| - If the config file doesn't have the correct toml key, we error. |
118 |
| - """ |
119 |
| - project_dir = self.mktemp_project( |
120 |
| - pyproject_toml=""" |
121 |
| - [something.else] |
122 |
| - blah='baz' |
123 |
| - """ |
124 |
| - ) |
125 |
| - |
126 |
| - with self.assertRaises(ConfigError) as e: |
127 |
| - load_config(project_dir) |
128 |
| - |
129 |
| - self.assertEqual(e.exception.failing_option, "all") |
130 |
| - |
131 | 117 | def test_incorrect_single_file(self):
|
132 | 118 | """
|
133 | 119 | single_file must be a bool.
|
@@ -194,6 +180,93 @@ def test_towncrier_toml_preferred(self):
|
194 | 180 | config = load_config(project_dir)
|
195 | 181 | self.assertEqual(config.package, "a")
|
196 | 182 |
|
| 183 | + def test_pyproject_only_pyproject_toml(self): |
| 184 | + """ |
| 185 | + Towncrier will fallback to the [project.name] value in pyproject.toml. |
| 186 | +
|
| 187 | + This tests asserts that the minimal configuration is to do *nothing* |
| 188 | + when using a pyproject.toml file. |
| 189 | + """ |
| 190 | + project_dir = self.mktemp_project( |
| 191 | + pyproject_toml=""" |
| 192 | + [project] |
| 193 | + name = "a" |
| 194 | + """, |
| 195 | + ) |
| 196 | + |
| 197 | + config = load_config(project_dir) |
| 198 | + self.assertEqual(config.package, "a") |
| 199 | + self.assertEqual(config.name, "a") |
| 200 | + |
| 201 | + def test_pyproject_assert_fallback(self): |
| 202 | + """ |
| 203 | + This test is an extensive test of the fallback scenarios |
| 204 | + for the `package` and `name` keys in the towncrier section. |
| 205 | +
|
| 206 | + It will fallback to pyproject.toml:name in any case. |
| 207 | + And as such it checks the various fallback mechanisms |
| 208 | + if the fields are not present in the towncrier.toml, nor |
| 209 | + in the pyproject.toml files. |
| 210 | +
|
| 211 | + This both tests when things are *only* in the pyproject.toml |
| 212 | + and default usage of the data in the towncrier.toml file. |
| 213 | + """ |
| 214 | + pyproject_toml = dedent( |
| 215 | + """ |
| 216 | + [project] |
| 217 | + name = "foo" |
| 218 | + [tool.towncrier] |
| 219 | + """ |
| 220 | + ) |
| 221 | + towncrier_toml = dedent( |
| 222 | + """ |
| 223 | + [tool.towncrier] |
| 224 | + """ |
| 225 | + ) |
| 226 | + tests = [ |
| 227 | + "", |
| 228 | + "name = '{name}'", |
| 229 | + "package = '{package}'", |
| 230 | + "name = '{name}'", |
| 231 | + "package = '{package}'", |
| 232 | + ] |
| 233 | + |
| 234 | + def factory(name, package): |
| 235 | + def func(test): |
| 236 | + return dedent(test).format(name=name, package=package) |
| 237 | + |
| 238 | + return func |
| 239 | + |
| 240 | + for pp_fields in map(factory(name="a", package="b"), tests): |
| 241 | + pp_toml = pyproject_toml + pp_fields |
| 242 | + for tc_fields in map(factory(name="c", package="d"), tests): |
| 243 | + tc_toml = towncrier_toml + tc_fields |
| 244 | + |
| 245 | + # Create the temporary project |
| 246 | + project_dir = self.mktemp_project( |
| 247 | + pyproject_toml=pp_toml, |
| 248 | + towncrier_toml=tc_toml, |
| 249 | + ) |
| 250 | + |
| 251 | + # Read the configuration file. |
| 252 | + config = load_config(project_dir) |
| 253 | + |
| 254 | + # Now the values depend on where the fallback |
| 255 | + # is. |
| 256 | + # If something is in towncrier.toml, it will be preferred |
| 257 | + # name fallsback to package |
| 258 | + if "package" in tc_fields: |
| 259 | + package = "d" |
| 260 | + else: |
| 261 | + package = "foo" |
| 262 | + self.assertEqual(config.package, package) |
| 263 | + |
| 264 | + if "name" in tc_fields: |
| 265 | + self.assertEqual(config.name, "c") |
| 266 | + else: |
| 267 | + # fall-back to package name |
| 268 | + self.assertEqual(config.name, package) |
| 269 | + |
197 | 270 | @with_isolated_runner
|
198 | 271 | def test_load_no_config(self, runner: CliRunner):
|
199 | 272 | """
|
|
0 commit comments