Skip to content

Commit

Permalink
No traceback on missing configuration with towncrier check (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysle authored May 8, 2023
1 parent daac2d2 commit d626a90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/towncrier/_settings/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Mapping, Sequence

from click import ClickException

from .._settings import fragment_types as ft


Expand Down Expand Up @@ -60,7 +62,7 @@ class Config:
orphan_prefix: str = "+"


class ConfigError(Exception):
class ConfigError(ClickException):
def __init__(self, *args: str, **kwargs: str):
self.failing_option = kwargs.get("failing_option")
super().__init__(*args)
Expand Down
3 changes: 3 additions & 0 deletions src/towncrier/newsfragments/501.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Calling ``towncrier check`` without an existing configuration, will just show only an error message.

In previous versions, a traceback was generated instead of the error message.
21 changes: 20 additions & 1 deletion src/towncrier/test/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import os

from click.testing import CliRunner
from twisted.trial.unittest import TestCase

from .._settings import ConfigError, load_config
from .helpers import write
from .._shell import cli
from .helpers import with_isolated_runner, write


class TomlSettingsTests(TestCase):
Expand Down Expand Up @@ -192,6 +194,23 @@ def test_towncrier_toml_preferred(self):
config = load_config(project_dir)
self.assertEqual(config.package, "a")

@with_isolated_runner
def test_load_no_config(self, runner: CliRunner):
"""
Calling the root CLI without an existing configuration file in the base directory,
will exit with code 1 and an informative message is sent to standard output.
"""
temp = self.mktemp()
os.makedirs(temp)

result = runner.invoke(cli, ("--dir", temp))

self.assertEqual(
result.output,
f"No configuration file found.\nLooked in: {os.path.abspath(temp)}\n",
)
self.assertEqual(result.exit_code, 1)

def test_missing_template(self):
"""
Towncrier will raise an exception saying when it can't find a template.
Expand Down

0 comments on commit d626a90

Please sign in to comment.