From d1f67f87b2b5c748874024b71dbb09e2c8b01aa0 Mon Sep 17 00:00:00 2001 From: burgess01 Date: Wed, 5 Oct 2022 15:21:43 -0400 Subject: [PATCH 01/16] feature/ Add base failing command --- gator/checks/check_ExecuteFailingCommand.py | 54 +++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 gator/checks/check_ExecuteFailingCommand.py diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py new file mode 100644 index 000000000..3e9115b2c --- /dev/null +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -0,0 +1,54 @@ +"""Check that a command executes with an expected error.""" + +import argparse + +from gator import checkers +from gator import invoke + + +def get_parser(): + """Get a parser for the arguments provided on the command-line.""" + # create the parser with the default help formatter + # use a new description since this is a stand-alone check + parser = argparse.ArgumentParser( + prog="ExecuteFailingCommand", + description="Check Provided by GatorGrader: ExecuteFailingCommand", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + + # Required Named Checker Arguments {{{ + + required_group = parser.add_argument_group("required checker arguments") + + # COMMAND: the command to execute + # REQUIRED? Yes + required_group.add_argument( + "--command", type=str, help="command to execute", required=True + ) + + # }}} + + # Optional Named Checker Arguments {{{ + + # None required for this checker + + # }}} + return parser + + +def parse(args, parser=None): + """Use the parser on the provided arguments.""" + return checkers.parse(get_parser, args, parser) + + +# pylint: disable=unused-argument +def act(main_parsed_arguments, check_remaining_arguments): + """Perform the action for this check.""" + # extract the two arguments for this check: + # --> command is required to specify the commit count threshold + check_parsed_arguments = parse(check_remaining_arguments) + # Directly run the check since at least one of the argument's for it is mandatory. + # This means that the use of check_ExecuteCommand would have already failed by this + # point since argparse will exit the program if a command-line argument is not provided + command = check_parsed_arguments.command + return [invoke.invoke_all_command_executes_checks(command)] From 61fdffc1beefdbd540d9e922a815a48b0a0ae695 Mon Sep 17 00:00:00 2001 From: Jeff Normile Date: Wed, 5 Oct 2022 15:30:23 -0400 Subject: [PATCH 02/16] Adjust logic to 'invert' boolean values of command checks --- gator/checks/check_ExecuteFailingCommand.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py index 3e9115b2c..8e9eb87e1 100644 --- a/gator/checks/check_ExecuteFailingCommand.py +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -51,4 +51,10 @@ def act(main_parsed_arguments, check_remaining_arguments): # This means that the use of check_ExecuteCommand would have already failed by this # point since argparse will exit the program if a command-line argument is not provided command = check_parsed_arguments.command - return [invoke.invoke_all_command_executes_checks(command)] + invocations = [invoke.invoke_all_command_executes_checks(command)] + for invocation in invocations: + if invocation: + invocation = False: + if not invocation: + invocation = True: + return invocations \ No newline at end of file From 7316787af86f29a4e8639bf09a5694b1bb1d8607 Mon Sep 17 00:00:00 2001 From: burgess01 Date: Wed, 5 Oct 2022 15:31:49 -0400 Subject: [PATCH 03/16] fix/ correct syntax on if statement --- gator/checks/check_ExecuteFailingCommand.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py index 8e9eb87e1..ec46eea9c 100644 --- a/gator/checks/check_ExecuteFailingCommand.py +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -54,7 +54,7 @@ def act(main_parsed_arguments, check_remaining_arguments): invocations = [invoke.invoke_all_command_executes_checks(command)] for invocation in invocations: if invocation: - invocation = False: + invocation = False if not invocation: - invocation = True: - return invocations \ No newline at end of file + invocation = True + return invocations From ed86bbe9c57a056482916e1e6f676570d1e9e4b1 Mon Sep 17 00:00:00 2001 From: burgess01 Date: Wed, 5 Oct 2022 15:48:46 -0400 Subject: [PATCH 04/16] feature/ Add test file for failing command --- .../test_check_ExecuteFailingCommand.py | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/checks/test_check_ExecuteFailingCommand.py diff --git a/tests/checks/test_check_ExecuteFailingCommand.py b/tests/checks/test_check_ExecuteFailingCommand.py new file mode 100644 index 000000000..19e964401 --- /dev/null +++ b/tests/checks/test_check_ExecuteFailingCommand.py @@ -0,0 +1,99 @@ +"""Tests for ExecuteFailingCommand's input and verification of command-line arguments.""" + +import pytest +import os +import sys + +from unittest.mock import patch + + +from gator import arguments +from gator import report +from gator.checks import check_ExecuteFailingCommand + + +def test_no_arguments_incorrect_system_exit(capsys): + """No command-line arguments causes SystemExit crash of argparse with error output.""" + with pytest.raises(SystemExit): + _ = check_ExecuteFailingCommand.parse([]) + captured = capsys.readouterr() + # there is no standard output + counted_newlines = captured.out.count("\n") + assert counted_newlines == 0 + # standard error has two lines from pytest + assert "usage:" in captured.err + counted_newlines = captured.err.count("\n") + assert counted_newlines == 2 + + +@pytest.mark.parametrize( + "commandline_arguments", + [(["--commandWRONG", "echo"]), (["--command", "run", "--WRONG"]), (["--command"])], +) +def test_required_commandline_arguments_cannot_parse(commandline_arguments, capsys): + """Check that incorrect optional command-line arguments check correctly.""" + with pytest.raises(SystemExit): + _ = check_ExecuteFailingCommand.parse(commandline_arguments) + captured = capsys.readouterr() + # there is no standard output + counted_newlines = captured.out.count("\n") + assert counted_newlines == 0 + # standard error has two lines from pytest + assert "usage:" in captured.err + counted_newlines = captured.err.count("\n") + assert counted_newlines == 2 + + +@pytest.mark.parametrize( + "commandline_arguments", + [(["--command", "run_command_first"]), (["--command", "run_command_second"])], +) +def test_required_commandline_arguments_can_parse(commandline_arguments, not_raises): + """Check that correct optional command-line arguments check correctly.""" + with not_raises(SystemExit): + _ = check_ExecuteFailingCommand.parse(commandline_arguments) + + +@pytest.mark.parametrize( + "commandline_arguments", + [(["--command", "run_command_first"]), (["--command", "run_command_second"])], +) +def test_optional_commandline_arguments_can_parse_created_parser( + commandline_arguments, not_raises +): + """Check that correct optional command-line arguments check correctly.""" + with not_raises(SystemExit): + parser = check_ExecuteFailingCommand.get_parser() + _ = check_ExecuteFailingCommand.parse(commandline_arguments, parser) + + +@pytest.mark.parametrize( + "commandline_arguments, expected_result", + [ + (["ExecuteCommand", "--command", "WrongCommand"], False), + (["ExecuteCommand", "--command", 'echo "CorrectCommand"'], True), + ], +) +def test_act_produces_output(commandline_arguments, expected_result, load_checker): + """Check that using the check produces output.""" + testargs = [os.getcwd()] + with patch.object(sys, "argv", testargs): + parsed_arguments, remaining_arguments = arguments.parse(commandline_arguments) + args_verified = arguments.verify(parsed_arguments) + assert args_verified is True + check_exists, checker_source, check_file = load_checker(parsed_arguments) + assert check_exists is True + check = checker_source.load_plugin(check_file) + check_result = check.act(parsed_arguments, remaining_arguments) + # check the result + assert check_result is not None + assert len(check_result) == 1 + assert check_result[0] is expected_result + # check the contents of the report + assert report.get_result() is not None + assert len(report.get_result()["check"]) > 1 + assert report.get_result()["outcome"] is expected_result + if expected_result: + assert report.get_result()["diagnostic"] == "" + else: + assert report.get_result()["diagnostic"] != "" From d23cd60a7a7480bea86e7043cc915554ced859aa Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Fri, 7 Oct 2022 13:53:33 -0400 Subject: [PATCH 05/16] Bug fix: direct parametrize test to correct file --- tests/checks/test_check_ExecuteFailingCommand.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/checks/test_check_ExecuteFailingCommand.py b/tests/checks/test_check_ExecuteFailingCommand.py index 19e964401..25e35f879 100644 --- a/tests/checks/test_check_ExecuteFailingCommand.py +++ b/tests/checks/test_check_ExecuteFailingCommand.py @@ -70,8 +70,8 @@ def test_optional_commandline_arguments_can_parse_created_parser( @pytest.mark.parametrize( "commandline_arguments, expected_result", [ - (["ExecuteCommand", "--command", "WrongCommand"], False), - (["ExecuteCommand", "--command", 'echo "CorrectCommand"'], True), + (["ExecuteFailingCommand", "--command", "WrongCommand"], False), + (["ExecuteFailingCommand", "--command", 'echo "CorrectCommand"'], True), ], ) def test_act_produces_output(commandline_arguments, expected_result, load_checker): From 6bce9502c2e0571b089e60f7b04bf7be75b99c85 Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Tue, 11 Oct 2022 21:58:45 -0400 Subject: [PATCH 06/16] Update test_check_ExecuteFailingCommand file --- tests/checks/test_check_ExecuteFailingCommand.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/checks/test_check_ExecuteFailingCommand.py b/tests/checks/test_check_ExecuteFailingCommand.py index 25e35f879..6e3116a33 100644 --- a/tests/checks/test_check_ExecuteFailingCommand.py +++ b/tests/checks/test_check_ExecuteFailingCommand.py @@ -70,8 +70,8 @@ def test_optional_commandline_arguments_can_parse_created_parser( @pytest.mark.parametrize( "commandline_arguments, expected_result", [ - (["ExecuteFailingCommand", "--command", "WrongCommand"], False), - (["ExecuteFailingCommand", "--command", 'echo "CorrectCommand"'], True), + (["ExecuteFailingCommand", "--command", "WrongCommand"], True), + (["ExecuteFailingCommand", "--command", 'echo "CorrectCommand"'], False), ], ) def test_act_produces_output(commandline_arguments, expected_result, load_checker): From 4fedcb018c6b57c46e1bfa0ef575b73ced21b9fd Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Tue, 11 Oct 2022 22:01:53 -0400 Subject: [PATCH 07/16] Add inverse mode for function in invoke.py Add one more input inverse_check to run ExecuteFailingCommand correctly. For it, command with return code 1 should pass. Command with return code 0 shouldn't --- gator/invoke.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/gator/invoke.py b/gator/invoke.py index 49e2af2d8..598e4de45 100644 --- a/gator/invoke.py +++ b/gator/invoke.py @@ -694,7 +694,7 @@ def invoke_all_command_regex_checks( ) -def invoke_all_command_executes_checks(command): +def invoke_all_command_executes_checks(command, inverse_check = False): """Perform the check for whether or not a command runs without error.""" # pylint: disable=unused-variable # note that the program does not use all of these @@ -704,11 +704,18 @@ def invoke_all_command_executes_checks(command): # this is the opposite of what is used for processes # but, all other GatorGrader checks return 0 on failure and 1 on success command_passed = False - if ( - command_error == constants.markers.Empty - and command_returncode == constants.codes.Success - ): - command_passed = True + if inverse_check is False: + if ( + command_error == constants.markers.Empty + and command_returncode == constants.codes.Success + ): + command_passed = True + else: + if ( + command_error != constants.markers.Empty + and command_returncode != constants.codes.Success + ): + command_passed = True # create the message and diagnostic and report the result message = "The command '" + str(command) + "'" + " executes correctly" diagnostic = "The command returned the error code " + str(command_returncode) From 70a215b69371d969f5318c400205b8aed0a815c7 Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Tue, 11 Oct 2022 22:03:41 -0400 Subject: [PATCH 08/16] Modify act function in ExecuteFailingCommand.py Add argument to call function in invoke.py in the inverse mode (command returning 1 should pass) --- gator/checks/check_ExecuteFailingCommand.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py index ec46eea9c..6a8976ce0 100644 --- a/gator/checks/check_ExecuteFailingCommand.py +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -5,6 +5,7 @@ from gator import checkers from gator import invoke +# import subprocess def get_parser(): """Get a parser for the arguments provided on the command-line.""" @@ -51,10 +52,18 @@ def act(main_parsed_arguments, check_remaining_arguments): # This means that the use of check_ExecuteCommand would have already failed by this # point since argparse will exit the program if a command-line argument is not provided command = check_parsed_arguments.command - invocations = [invoke.invoke_all_command_executes_checks(command)] - for invocation in invocations: - if invocation: - invocation = False - if not invocation: - invocation = True + + # # Run a command and return the output and error code. + # process = subprocess.Popen( + # command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True + # ) + # # run the command and return the results + # output, error = process.communicate() + # return_code = process.returncode + + # if process.returncode == 0: + # command = "commandWrong" + # else: + # command = 'echo "CorrectCommand"' + invocations = [invoke.invoke_all_command_executes_checks(command, inverse_check=True)] return invocations From dfc4e1e89f00f69e97721f6127fa58ecc86ba12b Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Tue, 11 Oct 2022 22:04:06 -0400 Subject: [PATCH 09/16] Add inverse mode test for invoke.py --- tests/test_invoke.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_invoke.py b/tests/test_invoke.py index 8e72c9f5f..f020508f6 100644 --- a/tests/test_invoke.py +++ b/tests/test_invoke.py @@ -803,6 +803,21 @@ def test_command_executes_checks_does_execute_correctly(): status_code = invoke.invoke_all_command_executes_checks("true") assert status_code is True +def test_command_executes_checks_not_pass_with_correct_command_in_inverse_mode(): + """Check to see if a command does run correctly and gets a non-zero return value.""" + # note that a zero-code means that the command did not work + # this is the opposite of what is used for processes + # but, all other GatorGrader checks return 0 on failure and 1 on success + status_code = invoke.invoke_all_command_executes_checks("true",inverse_check=True) + assert status_code is False + +def test_command_executes_checks_pass_with_wrong_command_in_inverse_mode(): + """Check to see if a command does not run correctly and gets a zero return value.""" + # note that a zero-code means that the command did not work + # this is the opposite of what is used for processes + # but, all other GatorGrader checks return 0 on failure and 1 on success + status_code = invoke.invoke_all_command_executes_checks("willnotwork",inverse_check=True) + assert status_code is True # pylint: disable=unused-argument # pylint: disable=redefined-outer-name From 45511baf2b1d16067bcdea9417d445d7f8e5dd95 Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Wed, 12 Oct 2022 23:36:31 -0400 Subject: [PATCH 10/16] Delete unnecessary codes --- gator/checks/check_ExecuteFailingCommand.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py index 6a8976ce0..4f29127d7 100644 --- a/gator/checks/check_ExecuteFailingCommand.py +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -52,18 +52,5 @@ def act(main_parsed_arguments, check_remaining_arguments): # This means that the use of check_ExecuteCommand would have already failed by this # point since argparse will exit the program if a command-line argument is not provided command = check_parsed_arguments.command - - # # Run a command and return the output and error code. - # process = subprocess.Popen( - # command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True - # ) - # # run the command and return the results - # output, error = process.communicate() - # return_code = process.returncode - - # if process.returncode == 0: - # command = "commandWrong" - # else: - # command = 'echo "CorrectCommand"' invocations = [invoke.invoke_all_command_executes_checks(command, inverse_check=True)] return invocations From b80f2b7923b5c19535e7d15e791af9ca309e8648 Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Wed, 12 Oct 2022 23:48:01 -0400 Subject: [PATCH 11/16] Add comment for executefailing command check --- gator/checks/check_ExecuteFailingCommand.py | 1 + gator/invoke.py | 1 + tests/test_invoke.py | 8 ++++---- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py index 4f29127d7..7677be95b 100644 --- a/gator/checks/check_ExecuteFailingCommand.py +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -52,5 +52,6 @@ def act(main_parsed_arguments, check_remaining_arguments): # This means that the use of check_ExecuteCommand would have already failed by this # point since argparse will exit the program if a command-line argument is not provided command = check_parsed_arguments.command + # Invoke the command with the inverse check so that the command with return code 1 should pass the check invocations = [invoke.invoke_all_command_executes_checks(command, inverse_check=True)] return invocations diff --git a/gator/invoke.py b/gator/invoke.py index 598e4de45..7ca36480e 100644 --- a/gator/invoke.py +++ b/gator/invoke.py @@ -704,6 +704,7 @@ def invoke_all_command_executes_checks(command, inverse_check = False): # this is the opposite of what is used for processes # but, all other GatorGrader checks return 0 on failure and 1 on success command_passed = False + # Assume that command passes when it makes error and return code 1 in inverse check mode if inverse_check is False: if ( command_error == constants.markers.Empty diff --git a/tests/test_invoke.py b/tests/test_invoke.py index f020508f6..d650bf54d 100644 --- a/tests/test_invoke.py +++ b/tests/test_invoke.py @@ -787,7 +787,7 @@ def test_run_command_grab_output_as_string_count_lines_exact( def test_command_executes_checks_does_not_execute_correctly(): - """Check to see if a command does not run correctly and gets a zero return value.""" + """Check to see if a command does not run correctly and gets a zero return value and fails to pass.""" # note that a zero-code means that the command did not work # this is the opposite of what is used for processes # but, all other GatorGrader checks return 0 on failure and 1 on success @@ -796,7 +796,7 @@ def test_command_executes_checks_does_not_execute_correctly(): def test_command_executes_checks_does_execute_correctly(): - """Check to see if a command does run correctly and gets a non-zero return value.""" + """Check to see if a command does run correctly and gets a non-zero return value and succeeds to pass.""" # note that a zero-code means that the command did not work # this is the opposite of what is used for processes # but, all other GatorGrader checks return 0 on failure and 1 on success @@ -804,7 +804,7 @@ def test_command_executes_checks_does_execute_correctly(): assert status_code is True def test_command_executes_checks_not_pass_with_correct_command_in_inverse_mode(): - """Check to see if a command does run correctly and gets a non-zero return value.""" + """Check to see if a command does run correctly and gets a non-zero return value but not pass.""" # note that a zero-code means that the command did not work # this is the opposite of what is used for processes # but, all other GatorGrader checks return 0 on failure and 1 on success @@ -812,7 +812,7 @@ def test_command_executes_checks_not_pass_with_correct_command_in_inverse_mode() assert status_code is False def test_command_executes_checks_pass_with_wrong_command_in_inverse_mode(): - """Check to see if a command does not run correctly and gets a zero return value.""" + """Check to see if a command does not run correctly and gets a zero return value but pass.""" # note that a zero-code means that the command did not work # this is the opposite of what is used for processes # but, all other GatorGrader checks return 0 on failure and 1 on success From 7e2307d330d78e1653fe3136b55a743179ad7d22 Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Wed, 12 Oct 2022 23:51:32 -0400 Subject: [PATCH 12/16] Lint format polish --- gator/checks/check_ExecuteFailingCommand.py | 5 ++++- gator/invoke.py | 2 +- tests/test_invoke.py | 9 +++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py index 7677be95b..806e3abe1 100644 --- a/gator/checks/check_ExecuteFailingCommand.py +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -7,6 +7,7 @@ # import subprocess + def get_parser(): """Get a parser for the arguments provided on the command-line.""" # create the parser with the default help formatter @@ -53,5 +54,7 @@ def act(main_parsed_arguments, check_remaining_arguments): # point since argparse will exit the program if a command-line argument is not provided command = check_parsed_arguments.command # Invoke the command with the inverse check so that the command with return code 1 should pass the check - invocations = [invoke.invoke_all_command_executes_checks(command, inverse_check=True)] + invocations = [ + invoke.invoke_all_command_executes_checks(command, inverse_check=True) + ] return invocations diff --git a/gator/invoke.py b/gator/invoke.py index 7ca36480e..57e1d0570 100644 --- a/gator/invoke.py +++ b/gator/invoke.py @@ -694,7 +694,7 @@ def invoke_all_command_regex_checks( ) -def invoke_all_command_executes_checks(command, inverse_check = False): +def invoke_all_command_executes_checks(command, inverse_check=False): """Perform the check for whether or not a command runs without error.""" # pylint: disable=unused-variable # note that the program does not use all of these diff --git a/tests/test_invoke.py b/tests/test_invoke.py index d650bf54d..709d71d4f 100644 --- a/tests/test_invoke.py +++ b/tests/test_invoke.py @@ -803,22 +803,27 @@ def test_command_executes_checks_does_execute_correctly(): status_code = invoke.invoke_all_command_executes_checks("true") assert status_code is True + def test_command_executes_checks_not_pass_with_correct_command_in_inverse_mode(): """Check to see if a command does run correctly and gets a non-zero return value but not pass.""" # note that a zero-code means that the command did not work # this is the opposite of what is used for processes # but, all other GatorGrader checks return 0 on failure and 1 on success - status_code = invoke.invoke_all_command_executes_checks("true",inverse_check=True) + status_code = invoke.invoke_all_command_executes_checks("true", inverse_check=True) assert status_code is False + def test_command_executes_checks_pass_with_wrong_command_in_inverse_mode(): """Check to see if a command does not run correctly and gets a zero return value but pass.""" # note that a zero-code means that the command did not work # this is the opposite of what is used for processes # but, all other GatorGrader checks return 0 on failure and 1 on success - status_code = invoke.invoke_all_command_executes_checks("willnotwork",inverse_check=True) + status_code = invoke.invoke_all_command_executes_checks( + "willnotwork", inverse_check=True + ) assert status_code is True + # pylint: disable=unused-argument # pylint: disable=redefined-outer-name def test_run_command_does_execute_correctly_would_make_output( From 9b3743123303057365fdac9109f325997f77156e Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Thu, 13 Oct 2022 10:21:04 -0400 Subject: [PATCH 13/16] Delete useless comment --- gator/checks/check_ExecuteFailingCommand.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py index 806e3abe1..a5f008212 100644 --- a/gator/checks/check_ExecuteFailingCommand.py +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -5,8 +5,6 @@ from gator import checkers from gator import invoke -# import subprocess - def get_parser(): """Get a parser for the arguments provided on the command-line.""" From 7256770abc47c076a9156c506f136bee81c19735 Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Mon, 17 Oct 2022 09:47:47 -0400 Subject: [PATCH 14/16] Modify to use new interface created by master --- gator/checks/check_ExecuteFailingCommand.py | 8 +-- .../test_check_ExecuteFailingCommand.py | 63 ++++++------------- 2 files changed, 21 insertions(+), 50 deletions(-) diff --git a/gator/checks/check_ExecuteFailingCommand.py b/gator/checks/check_ExecuteFailingCommand.py index a5f008212..67c1f0460 100644 --- a/gator/checks/check_ExecuteFailingCommand.py +++ b/gator/checks/check_ExecuteFailingCommand.py @@ -36,9 +36,9 @@ def get_parser(): return parser -def parse(args, parser=None): +def parse(args): """Use the parser on the provided arguments.""" - return checkers.parse(get_parser, args, parser) + return checkers.parse(get_parser, args) # pylint: disable=unused-argument @@ -52,7 +52,5 @@ def act(main_parsed_arguments, check_remaining_arguments): # point since argparse will exit the program if a command-line argument is not provided command = check_parsed_arguments.command # Invoke the command with the inverse check so that the command with return code 1 should pass the check - invocations = [ - invoke.invoke_all_command_executes_checks(command, inverse_check=True) - ] + invocations = invoke.invoke_all_command_executes_checks(command, inverse_check=True) return invocations diff --git a/tests/checks/test_check_ExecuteFailingCommand.py b/tests/checks/test_check_ExecuteFailingCommand.py index 6e3116a33..cc74cc007 100644 --- a/tests/checks/test_check_ExecuteFailingCommand.py +++ b/tests/checks/test_check_ExecuteFailingCommand.py @@ -9,39 +9,30 @@ from gator import arguments from gator import report +from gator.exceptions import InvalidCheckArgumentsError from gator.checks import check_ExecuteFailingCommand -def test_no_arguments_incorrect_system_exit(capsys): - """No command-line arguments causes SystemExit crash of argparse with error output.""" - with pytest.raises(SystemExit): - _ = check_ExecuteFailingCommand.parse([]) - captured = capsys.readouterr() - # there is no standard output - counted_newlines = captured.out.count("\n") - assert counted_newlines == 0 - # standard error has two lines from pytest - assert "usage:" in captured.err - counted_newlines = captured.err.count("\n") - assert counted_newlines == 2 - - @pytest.mark.parametrize( "commandline_arguments", - [(["--commandWRONG", "echo"]), (["--command", "run", "--WRONG"]), (["--command"])], + [ + ([]), + (["--commandWRONG", "echo"]), + (["--command", "run", "--WRONG"]), + (["--command"]), + ], ) def test_required_commandline_arguments_cannot_parse(commandline_arguments, capsys): """Check that incorrect optional command-line arguments check correctly.""" - with pytest.raises(SystemExit): + with pytest.raises(InvalidCheckArgumentsError) as excinfo: _ = check_ExecuteFailingCommand.parse(commandline_arguments) captured = capsys.readouterr() - # there is no standard output - counted_newlines = captured.out.count("\n") - assert counted_newlines == 0 - # standard error has two lines from pytest - assert "usage:" in captured.err - counted_newlines = captured.err.count("\n") - assert counted_newlines == 2 + # there is no standard output or error + assert captured.err == "" + assert captured.out == "" + assert excinfo.value.check_name == "ExecuteFailingCommand" + assert excinfo.value.usage + assert excinfo.value.error @pytest.mark.parametrize( @@ -50,23 +41,9 @@ def test_required_commandline_arguments_cannot_parse(commandline_arguments, caps ) def test_required_commandline_arguments_can_parse(commandline_arguments, not_raises): """Check that correct optional command-line arguments check correctly.""" - with not_raises(SystemExit): + with not_raises(InvalidCheckArgumentsError): _ = check_ExecuteFailingCommand.parse(commandline_arguments) - -@pytest.mark.parametrize( - "commandline_arguments", - [(["--command", "run_command_first"]), (["--command", "run_command_second"])], -) -def test_optional_commandline_arguments_can_parse_created_parser( - commandline_arguments, not_raises -): - """Check that correct optional command-line arguments check correctly.""" - with not_raises(SystemExit): - parser = check_ExecuteFailingCommand.get_parser() - _ = check_ExecuteFailingCommand.parse(commandline_arguments, parser) - - @pytest.mark.parametrize( "commandline_arguments, expected_result", [ @@ -74,21 +51,17 @@ def test_optional_commandline_arguments_can_parse_created_parser( (["ExecuteFailingCommand", "--command", 'echo "CorrectCommand"'], False), ], ) -def test_act_produces_output(commandline_arguments, expected_result, load_checker): +def test_act_produces_output(commandline_arguments, expected_result, load_check): """Check that using the check produces output.""" testargs = [os.getcwd()] with patch.object(sys, "argv", testargs): parsed_arguments, remaining_arguments = arguments.parse(commandline_arguments) args_verified = arguments.verify(parsed_arguments) assert args_verified is True - check_exists, checker_source, check_file = load_checker(parsed_arguments) - assert check_exists is True - check = checker_source.load_plugin(check_file) + check = load_check(parsed_arguments) check_result = check.act(parsed_arguments, remaining_arguments) # check the result - assert check_result is not None - assert len(check_result) == 1 - assert check_result[0] is expected_result + assert check_result is expected_result # check the contents of the report assert report.get_result() is not None assert len(report.get_result()["check"]) > 1 From a1641530dff2f56ff1c1995a9e9c5dc9d194e040 Mon Sep 17 00:00:00 2001 From: Yanqiao4396 Date: Mon, 17 Oct 2022 09:52:29 -0400 Subject: [PATCH 15/16] Pass black format check --- tests/checks/test_check_ExecuteFailingCommand.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/checks/test_check_ExecuteFailingCommand.py b/tests/checks/test_check_ExecuteFailingCommand.py index cc74cc007..49126ac7a 100644 --- a/tests/checks/test_check_ExecuteFailingCommand.py +++ b/tests/checks/test_check_ExecuteFailingCommand.py @@ -44,6 +44,7 @@ def test_required_commandline_arguments_can_parse(commandline_arguments, not_rai with not_raises(InvalidCheckArgumentsError): _ = check_ExecuteFailingCommand.parse(commandline_arguments) + @pytest.mark.parametrize( "commandline_arguments, expected_result", [ From 382596d9fa8bd90b0ded013c61f075889c44eca7 Mon Sep 17 00:00:00 2001 From: Katie Burgess <70536041+burgess01@users.noreply.github.com> Date: Fri, 18 Nov 2022 15:46:11 -0500 Subject: [PATCH 16/16] fix: address why true failures wouldn't flag --- gator/invoke.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gator/invoke.py b/gator/invoke.py index dbd8bcd5d..e49099e35 100644 --- a/gator/invoke.py +++ b/gator/invoke.py @@ -716,7 +716,7 @@ def invoke_all_command_executes_checks(command, inverse_check=False): else: if ( command_error != constants.markers.Empty - and command_returncode != constants.codes.Success + or command_returncode != constants.codes.Success ): command_passed = True # create the message and diagnostic and report the result