Skip to content

Commit

Permalink
Merge pull request #117 from sandialabs/ignore-assert-warnings-in-tes…
Browse files Browse the repository at this point in the history
…t-files

test: Ignore assert warnings in test files
  • Loading branch information
jmgate authored Apr 24, 2024
2 parents ec65408 + 01f97d0 commit 932c4a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 57 deletions.
22 changes: 10 additions & 12 deletions example/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_basic() -> None:
check=True,
text=True,
)
assert ( # noqa: S101
assert (
result.stdout
== """
The effective command line invocation was:
Expand All @@ -41,7 +41,7 @@ def test_default_values() -> None:
check=True,
text=True,
)
assert ( # noqa: S101
assert (
result.stdout
== """
The effective command line invocation was:
Expand All @@ -59,14 +59,14 @@ def test_relative_references() -> None:
check=True,
text=True,
)
assert ( # noqa: S101
assert (
"""
The effective command line invocation was:
relative_references.py --bar spam --baz 42 --src
""".strip()
in result.stdout
)
assert re.search(r"--src /\S+/bar\.txt", result.stdout) # noqa: S101
assert re.search(r"--src /\S+/bar\.txt", result.stdout)


def test_post_processing() -> None:
Expand All @@ -78,7 +78,7 @@ def test_post_processing() -> None:
check=True,
text=True,
)
assert ( # noqa: S101
assert (
"""
The effective command line invocation was:
post_processing.py --bar spam --baz 42 --before
Expand All @@ -89,9 +89,7 @@ def test_post_processing() -> None:
time_from_example = datetime.strptime(
shlex.split(result.stdout)[-1], "%Y-%m-%d %H:%M:%S.%f"
).astimezone(timezone.utc)
assert ( # noqa: S101
thirty_miutes_ago - time_from_example < timedelta(seconds=1)
)
assert thirty_miutes_ago - time_from_example < timedelta(seconds=1)


def test_pretty_printing() -> None:
Expand All @@ -111,7 +109,7 @@ def test_pretty_printing() -> None:
check=True,
text=True,
)
assert ( # noqa: S101
assert (
"""
The effective command line invocation was:
pretty_printing.py \\
Expand All @@ -121,13 +119,13 @@ def test_pretty_printing() -> None:
""".strip()
in result.stdout
)
assert re.search(r"--src /\S+/file\.txt", result.stdout) # noqa: S101
assert re.search(r"--src /\S+/file\.txt", result.stdout)
today = datetime.now(tz=timezone.utc)
time_from_example = datetime.strptime(
shlex.split(result.stdout.splitlines()[-1])[-1],
"%Y-%m-%d %H:%M:%S.%f",
).astimezone(timezone.utc)
assert today - time_from_example < timedelta(seconds=1) # noqa: S101
assert today - time_from_example < timedelta(seconds=1)


def test_subparsers() -> None:
Expand All @@ -139,7 +137,7 @@ def test_subparsers() -> None:
check=True,
text=True,
)
assert ( # noqa: S101
assert (
result.stdout
== """
The effective command line invocation was:
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"


[tool.bandit.assert_used]
skips = ["**/test_*.py"]


[tool.commitizen]
name = "cz_customize"

Expand Down Expand Up @@ -103,6 +99,10 @@ ignore = [
]


[tool.ruff.lint.per-file-ignores]
"**/test_*.py" = ["S101"]


[tool.ruff.lint.pydocstyle]
convention = "google"

Expand Down
68 changes: 27 additions & 41 deletions test/test_reverse_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def strip_first_entry(input_string: str) -> str:

def test_strip_first_entry() -> None:
"""Ensure :func:`strip_first_entry` works as expected."""
assert strip_first_entry("foo bar baz") == "bar baz" # noqa: S101
assert strip_first_entry("foo bar baz") == "bar baz"


def strip_first_line(input_string: str) -> str:
Expand All @@ -135,7 +135,7 @@ def strip_first_line(input_string: str) -> str:

def test_strip_first_line() -> None:
"""Ensure :func:`strip_first_line` works as expected."""
assert strip_first_line("foo\nbar\nbaz") == "bar\nbaz" # noqa: S101
assert strip_first_line("foo\nbar\nbaz") == "bar\nbaz"


@pytest.mark.parametrize("args", COMPLETE_ARGS)
Expand All @@ -162,7 +162,7 @@ def test_get_effective_command_line_invocation(parser, args) -> None:
result = strip_first_entry(
unparser.get_effective_command_line_invocation()
)
assert result == expected # noqa: S101
assert result == expected


@pytest.mark.parametrize("args", COMPLETE_ARGS)
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_get_pretty_command_line_invocation(parser, args) -> None:
expected += """\n pos1-val1 pos1-val2 \\
pos2-val"""
result = strip_first_line(unparser.get_pretty_command_line_invocation())
assert result == expected # noqa: S101
assert result == expected


def test_get_command_line_invocation_strip_spaces() -> None:
Expand All @@ -203,13 +203,11 @@ def test_get_command_line_invocation_strip_spaces() -> None:
unparser._args = ["program_name", " --foo", " ", " --bar"]
unparser._unparsed = [True]
expected = "program_name --foo --bar"
assert ( # noqa: S101
unparser.get_effective_command_line_invocation() == expected
)
assert unparser.get_effective_command_line_invocation() == expected
expected = """ --foo \\
--bar"""
result = strip_first_line(unparser.get_pretty_command_line_invocation())
assert result == expected # noqa: S101
assert result == expected


@pytest.mark.parametrize(
Expand Down Expand Up @@ -266,7 +264,7 @@ def test__unparse_args(add_args, add_kwargs, args, expected) -> None:
unparser._unparse_args()
else:
unparser._unparse_args()
assert unparser._args[1:] == expected # noqa: S101
assert unparser._args[1:] == expected


def test__unparse_args_boolean_optional_action() -> None:
Expand All @@ -285,7 +283,7 @@ def test__unparse_args_boolean_optional_action() -> None:
namespace = Namespace()
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_args()
assert unparser._args[1:] == [" --foo"] # noqa: S101
assert unparser._args[1:] == [" --foo"]


def test__unparse_args_already_unparsed() -> None:
Expand All @@ -297,7 +295,7 @@ def test__unparse_args_already_unparsed() -> None:
args_before = unparser._args.copy()
unparser._unparsed = [True]
unparser._unparse_args()
assert unparser._args == args_before # noqa: S101
assert unparser._args == args_before


def test__arg_is_default_and_help_is_suppressed() -> None:
Expand All @@ -309,7 +307,7 @@ def test__arg_is_default_and_help_is_suppressed() -> None:
result = strip_first_entry(
unparser.get_effective_command_line_invocation()
)
assert result == "" # noqa: S101
assert result == ""


@pytest.mark.parametrize(
Expand All @@ -323,7 +321,7 @@ def test__arg_is_default_and_help_is_suppressed() -> None:
def test__get_long_option_strings(strings, expected) -> None:
"""Ensure the long-form option is selected from a list."""
unparser = ReverseArgumentParser(ArgumentParser(), Namespace())
assert unparser._get_long_option_strings(strings) == expected # noqa: S101
assert unparser._get_long_option_strings(strings) == expected


@pytest.mark.parametrize(
Expand All @@ -337,9 +335,7 @@ def test__get_long_option_strings(strings, expected) -> None:
def test__get_short_option_strings(strings, expected) -> None:
"""Ensure the short-form option is selected from a list."""
unparser = ReverseArgumentParser(ArgumentParser(), Namespace())
assert ( # noqa: S101
unparser._get_short_option_strings(strings) == expected
)
assert unparser._get_short_option_strings(strings) == expected


@pytest.mark.parametrize(
Expand All @@ -355,7 +351,7 @@ def test__get_option_string(strings, expected) -> None:
parser = ArgumentParser()
action = parser.add_argument(*strings)
unparser = ReverseArgumentParser(parser, Namespace())
assert unparser._get_option_string(action) == expected # noqa: S101
assert unparser._get_option_string(action) == expected


@pytest.mark.parametrize(
Expand All @@ -371,9 +367,7 @@ def test__get_option_string_prefer_short(strings, expected) -> None:
parser = ArgumentParser()
action = parser.add_argument(*strings)
unparser = ReverseArgumentParser(parser, Namespace())
assert ( # noqa: S101
unparser._get_option_string(action, prefer_short=True) == expected
)
assert unparser._get_option_string(action, prefer_short=True) == expected


@pytest.mark.parametrize(
Expand Down Expand Up @@ -401,7 +395,7 @@ def test__unparse_store_action(add_args, add_kwargs, args, expected) -> None:
namespace = parser.parse_args(shlex.split(args))
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_store_action(action)
assert unparser._args[1:] == [expected] # noqa: S101
assert unparser._args[1:] == [expected]


@pytest.mark.parametrize(
Expand Down Expand Up @@ -437,9 +431,7 @@ def test__unparse_store_const_action(
namespace = parser.parse_args(shlex.split(args))
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_store_const_action(action)
assert ( # noqa: S101
unparser._args[1:] == ([expected] if expected is not None else [])
)
assert unparser._args[1:] == ([expected] if expected is not None else [])


@pytest.mark.parametrize(
Expand All @@ -452,9 +444,7 @@ def test__unparse_store_true_action(args, expected) -> None:
namespace = parser.parse_args(args)
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_store_true_action(action)
assert ( # noqa: S101
unparser._args[1:] == ([expected] if expected is not None else [])
)
assert unparser._args[1:] == ([expected] if expected is not None else [])


@pytest.mark.parametrize(
Expand All @@ -467,9 +457,7 @@ def test__unparse_store_false_action(args, expected) -> None:
namespace = parser.parse_args(args)
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_store_false_action(action)
assert ( # noqa: S101
unparser._args[1:] == ([expected] if expected is not None else [])
)
assert unparser._args[1:] == ([expected] if expected is not None else [])


@pytest.mark.parametrize(
Expand All @@ -496,7 +484,7 @@ def test__unparse_append_action(add_args, add_kwargs, args, expected) -> None:
namespace = parser.parse_args(shlex.split(args))
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_append_action(action)
assert unparser._args[1:] == expected # noqa: S101
assert unparser._args[1:] == expected


@pytest.mark.parametrize(
Expand All @@ -511,9 +499,7 @@ def test__unparse_append_const_action(args, expected) -> None:
namespace = parser.parse_args(shlex.split(args))
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_append_const_action(action)
assert ( # noqa: S101
unparser._args[1:] == ([expected] if expected is not None else [])
)
assert unparser._args[1:] == ([expected] if expected is not None else [])


@pytest.mark.parametrize(
Expand Down Expand Up @@ -546,7 +532,7 @@ def test__unparse_count_action(add_args, add_kwargs, args, expected) -> None:
namespace = parser.parse_args(shlex.split(args))
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_count_action(action)
assert unparser._args[1:] == [expected] # noqa: S101
assert unparser._args[1:] == [expected]


@pytest.mark.parametrize(
Expand Down Expand Up @@ -575,9 +561,9 @@ def test__unparse_sub_parsers_action(args, expected, pretty) -> None:
result = strip_first_entry(
unparser.get_effective_command_line_invocation()
)
assert result == expected # noqa: S101
assert result == expected
result = strip_first_line(unparser.get_pretty_command_line_invocation())
assert result == pretty # noqa: S101
assert result == pretty


def test__unparse_sub_parsers_action_nested() -> None:
Expand Down Expand Up @@ -617,9 +603,9 @@ def test__unparse_sub_parsers_action_nested() -> None:
result = strip_first_entry(
unparser.get_effective_command_line_invocation()
)
assert result == args # noqa: S101
assert result == args
result = strip_first_line(unparser.get_pretty_command_line_invocation())
assert result == pretty # noqa: S101
assert result == pretty


def test__unparse_extend_action() -> None:
Expand All @@ -630,7 +616,7 @@ def test__unparse_extend_action() -> None:
unparser = ReverseArgumentParser(parser, namespace)
expected = " --foo bar baz bif"
unparser._unparse_extend_action(action)
assert unparser._args[1:] == [expected] # noqa: S101
assert unparser._args[1:] == [expected]


@pytest.mark.parametrize(
Expand All @@ -657,6 +643,6 @@ def test__unparse_boolean_optional_action(default, args, expected) -> None:
namespace = parser.parse_args(shlex.split(args))
unparser = ReverseArgumentParser(parser, namespace)
unparser._unparse_boolean_optional_action(action)
assert unparser._args[1:] == ( # noqa: S101
assert unparser._args[1:] == (
[expected] if expected is not None else []
)

0 comments on commit 932c4a6

Please sign in to comment.