From a163f0fb6cb92c4bda4c7f985da8912f1280c41c Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Mon, 8 May 2023 09:49:10 +1200 Subject: [PATCH 1/4] Provide a friendly message if version option is required --- src/towncrier/build.py | 21 +++++++++++++-------- src/towncrier/test/test_build.py | 12 ++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/towncrier/build.py b/src/towncrier/build.py index bb016443..9a41b40d 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/test/test_build.py b/src/towncrier/test/test_build.py index 4dbc07ce..06689871 100644 --- a/src/towncrier/test/test_build.py +++ b/src/towncrier/test/test_build.py @@ -511,6 +511,18 @@ 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. + """ + with open("towncrier.toml", "w") as f: + f.write("[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 From d64737b88efef2672b582f52768774f7d49edc1b Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Mon, 8 May 2023 09:49:10 +1200 Subject: [PATCH 2/4] Add newsfragment --- src/towncrier/newsfragments/507.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/towncrier/newsfragments/507.misc diff --git a/src/towncrier/newsfragments/507.misc b/src/towncrier/newsfragments/507.misc new file mode 100644 index 00000000..14ed0e2e --- /dev/null +++ b/src/towncrier/newsfragments/507.misc @@ -0,0 +1 @@ +Provide a friendly message if --version option is required (for non-Python projects that don't explicitly provide the version in the config) From 887937ba1ccff366d087668fdf39ecd4c6c18031 Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Mon, 8 May 2023 09:49:10 +1200 Subject: [PATCH 3/4] Simplify test and fragment --- src/towncrier/newsfragments/507.misc | 2 +- src/towncrier/test/test_build.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/towncrier/newsfragments/507.misc b/src/towncrier/newsfragments/507.misc index 14ed0e2e..b8f04783 100644 --- a/src/towncrier/newsfragments/507.misc +++ b/src/towncrier/newsfragments/507.misc @@ -1 +1 @@ -Provide a friendly message if --version option is required (for non-Python projects that don't explicitly provide the version in the config) +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 06689871..d4561263 100644 --- a/src/towncrier/test/test_build.py +++ b/src/towncrier/test/test_build.py @@ -517,9 +517,10 @@ def test_needs_version(self, runner: CliRunner): If the configuration file doesn't specify a version or a package, the version option is required. """ - with open("towncrier.toml", "w") as f: - f.write("[tool.towncrier]") + 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) From 2abd895a3b3b091a2baf70cf26ce4497b7985f73 Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Mon, 8 May 2023 16:54:34 +1200 Subject: [PATCH 4/4] Dedent --- src/towncrier/build.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/towncrier/build.py b/src/towncrier/build.py index 9a41b40d..ef7c5f35 100644 --- a/src/towncrier/build.py +++ b/src/towncrier/build.py @@ -153,15 +153,15 @@ def __main( 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() + 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):