From 34cc1e350b25e4915d3fa83ebc0e30b392734f44 Mon Sep 17 00:00:00 2001 From: Austin Macdonald Date: Wed, 28 Aug 2024 09:39:08 -0500 Subject: [PATCH] Add tests for correct handling of args Fixes https://github.com/con/duct/issues/58 --- test/test_arg_parsing.py | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/test_arg_parsing.py diff --git a/test/test_arg_parsing.py b/test/test_arg_parsing.py new file mode 100644 index 00000000..1d4a95c1 --- /dev/null +++ b/test/test_arg_parsing.py @@ -0,0 +1,42 @@ +import subprocess +import pytest + + +def test_duct_help() -> None: + out = subprocess.check_output(["duct", "--help", "ps"]) + assert "usage: duct [-h]" in str(out) + + +def test_cmd_help() -> None: + out = subprocess.check_output(["duct", "ps", "--help"]) + assert "ps [options]" in str(out) + assert "usage: duct [-h]" not in str(out) + + +@pytest.mark.parametrize( + "args", + [ + ["duct", "--unknown", "ps"], + ["duct", "--unknown", "ps", "--shouldhavenoeffect"], + ], +) +def test_duct_unrecognized_arg(args: list) -> None: + try: + subprocess.check_output(args, stderr=subprocess.STDOUT) + pytest.fail("Command should have failed with a non-zero exit code") + except subprocess.CalledProcessError as e: + assert e.returncode == 2 + assert "duct: error: unrecognized arguments: --unknown" in str(e.stdout) + + +def test_duct_missing_cmd() -> None: + try: + subprocess.check_output( + ["duct", "--sample-interval", "1"], stderr=subprocess.STDOUT + ) + pytest.fail("Command should have failed with a non-zero exit code") + except subprocess.CalledProcessError as e: + assert e.returncode == 2 + assert "duct: error: the following arguments are required: command" in str( + e.stdout + )