diff --git a/src/towncrier/build.py b/src/towncrier/build.py index bb016443..ef7c5f35 100644 --- a/src/towncrier/build.py +++ b/src/towncrier/build.py @@ -15,7 +15,7 @@ import click -from click import Context, Option +from click import Context, Option, UsageError from towncrier import _git @@ -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) @@ -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: diff --git a/src/towncrier/newsfragments/507.misc b/src/towncrier/newsfragments/507.misc new file mode 100644 index 00000000..b8f04783 --- /dev/null +++ b/src/towncrier/newsfragments/507.misc @@ -0,0 +1 @@ +A friendly message is now provided, when it's necessary to pass the ``--version`` option explicitly. diff --git a/src/towncrier/test/test_build.py b/src/towncrier/test/test_build.py index 4dbc07ce..d4561263 100644 --- a/src/towncrier/test/test_build.py +++ b/src/towncrier/test/test_build.py @@ -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