Skip to content

Commit

Permalink
Provide a friendly message if version option is required (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileyChris committed May 8, 2023
1 parent a8f4630 commit daac2d2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/towncrier/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import click

from click import Context, Option
from click import Context, Option, UsageError

from towncrier import _git

Expand Down Expand Up @@ -151,6 +151,18 @@ def __main(
base_directory, config = load_config_from_options(directory, config_file)
to_err = draft

if project_version is None:
project_version = config.version
if project_version is None:
if not config.package:
raise UsageError(
"'--version' is required since the config file does "
"not contain 'version' or 'package'."
)
project_version = get_version(
os.path.join(base_directory, config.package_dir), config.package
).strip()

click.echo("Loading template...", err=to_err)
if isinstance(config.template, tuple):
template = resources.read_text(*config.template)
Expand Down Expand Up @@ -182,13 +194,6 @@ def __main(
fragment_contents, config.types, all_bullets=config.all_bullets
)

if project_version is None:
project_version = config.version
if project_version is None:
project_version = get_version(
os.path.join(base_directory, config.package_dir), config.package
).strip()

if project_name is None:
project_name = config.name
if not project_name:
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/507.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A friendly message is now provided, when it's necessary to pass the ``--version`` option explicitly.
13 changes: 13 additions & 0 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,19 @@ def test_needs_config(self):
self.assertEqual(1, result.exit_code, result.output)
self.assertTrue(result.output.startswith("No configuration file found."))

@with_isolated_runner
def test_needs_version(self, runner: CliRunner):
"""
If the configuration file doesn't specify a version or a package, the version
option is required.
"""
write("towncrier.toml", "[tool.towncrier]")

result = runner.invoke(_main, ["--draft"], catch_exceptions=False)

self.assertEqual(2, result.exit_code)
self.assertIn("Error: '--version' is required", result.output)

def test_projectless_changelog(self):
"""In which a directory containing news files is built into a changelog
Expand Down

0 comments on commit daac2d2

Please sign in to comment.