diff --git a/djclick/adapter.py b/djclick/adapter.py index e528d37..d8191d4 100644 --- a/djclick/adapter.py +++ b/djclick/adapter.py @@ -71,7 +71,7 @@ def run_from_argv(self, argv): if exit_code: sys.exit(exit_code) except click.ClickException as e: - if getattr(e.ctx, "traceback", False): # NOCOV + if getattr(e, "ctx", False) and getattr(e.ctx, "traceback", False): # NOCOV raise e.show() sys.exit(e.exit_code) diff --git a/djclick/test/test_adapter.py b/djclick/test/test_adapter.py index 7eb4bd7..b7fe7e3 100644 --- a/djclick/test/test_adapter.py +++ b/djclick/test/test_adapter.py @@ -147,9 +147,6 @@ def test_django_pythonpath(manage): ) == b"1" -@pytest.mark.xfail( - reason="Looks like CommandError no longer " "results in non-zero exit status" -) def test_django_traceback(manage): with pytest.raises(subprocess.CalledProcessError) as e: manage("errcmd") @@ -170,6 +167,13 @@ def test_django_traceback(manage): assert e.returncode == 1 +def test_click_exception(manage): + with pytest.raises(subprocess.CalledProcessError) as e: + manage("clickexceptioncmd") + assert e.value.output == b"Error: Raised error description\n" + assert e.value.returncode == 1 + + def test_django_settings(manage): # The --settings switch only works from the command line (or if the django # settings where not setup before)... this means that we have to call it diff --git a/djclick/test/testprj/testapp/management/commands/clickexceptioncmd.py b/djclick/test/testprj/testapp/management/commands/clickexceptioncmd.py new file mode 100644 index 0000000..6d66705 --- /dev/null +++ b/djclick/test/testprj/testapp/management/commands/clickexceptioncmd.py @@ -0,0 +1,6 @@ +import djclick as click + + +@click.command(version="20.0") +def command(): + raise click.ClickException("Raised error description")