Skip to content

Commit daac2d2

Browse files
authored
Provide a friendly message if version option is required (#507)
1 parent a8f4630 commit daac2d2

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/towncrier/build.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import click
1717

18-
from click import Context, Option
18+
from click import Context, Option, UsageError
1919

2020
from towncrier import _git
2121

@@ -151,6 +151,18 @@ def __main(
151151
base_directory, config = load_config_from_options(directory, config_file)
152152
to_err = draft
153153

154+
if project_version is None:
155+
project_version = config.version
156+
if project_version is None:
157+
if not config.package:
158+
raise UsageError(
159+
"'--version' is required since the config file does "
160+
"not contain 'version' or 'package'."
161+
)
162+
project_version = get_version(
163+
os.path.join(base_directory, config.package_dir), config.package
164+
).strip()
165+
154166
click.echo("Loading template...", err=to_err)
155167
if isinstance(config.template, tuple):
156168
template = resources.read_text(*config.template)
@@ -182,13 +194,6 @@ def __main(
182194
fragment_contents, config.types, all_bullets=config.all_bullets
183195
)
184196

185-
if project_version is None:
186-
project_version = config.version
187-
if project_version is None:
188-
project_version = get_version(
189-
os.path.join(base_directory, config.package_dir), config.package
190-
).strip()
191-
192197
if project_name is None:
193198
project_name = config.name
194199
if not project_name:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A friendly message is now provided, when it's necessary to pass the ``--version`` option explicitly.

src/towncrier/test/test_build.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,19 @@ def test_needs_config(self):
511511
self.assertEqual(1, result.exit_code, result.output)
512512
self.assertTrue(result.output.startswith("No configuration file found."))
513513

514+
@with_isolated_runner
515+
def test_needs_version(self, runner: CliRunner):
516+
"""
517+
If the configuration file doesn't specify a version or a package, the version
518+
option is required.
519+
"""
520+
write("towncrier.toml", "[tool.towncrier]")
521+
522+
result = runner.invoke(_main, ["--draft"], catch_exceptions=False)
523+
524+
self.assertEqual(2, result.exit_code)
525+
self.assertIn("Error: '--version' is required", result.output)
526+
514527
def test_projectless_changelog(self):
515528
"""In which a directory containing news files is built into a changelog
516529

0 commit comments

Comments
 (0)