Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a friendly message if version option is required #507

Merged
merged 4 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
SmileyChris marked this conversation as resolved.
Show resolved Hide resolved

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