def test_file_exists_in_directory_check_fragments(reset_results_dictionary, tmpdir): """Check that the checking of fragments in a file works correctly.""" reflection_file = tmpdir.mkdir("sub").join("reflection.md") reflection_file.write( "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" ) assert ( reflection_file.read() == "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" ) assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "sub" reflection_file = "reflection.md" invoke.invoke_all_fragment_checks("hello", 1, reflection_file, directory, "") details = report.get_result() assert details is not None report.reset() invoke.invoke_all_fragment_checks("@name", 1, reflection_file, directory, "") details = report.get_result() assert details is not None report.reset() invoke.invoke_all_fragment_checks("again", 2, reflection_file, directory, "") details = report.get_result() assert details is not None report.reset() invoke.invoke_all_fragment_checks("planet", 2, reflection_file, directory, "") details = report.get_result() assert details is not None
def test_act_produces_output(commandline_arguments, expected_result, load_checker): """Ensure 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"] != ""
def test_set_result(reset_results_dictionary): """Set the result dictionary and check if it keeps the values.""" report.set_result("Command executes", True, "") assert report.get_result()[ constants.results.Description] == "Command executes" assert report.get_result()[constants.results.Outcome] is True assert report.get_result()[constants.results.Diagnostic] == ""
def test_file_exists_in_directory_check_words_wildcards( reset_results_dictionary, tmpdir): """Check that the checking of words works correctly.""" reflection_file = tmpdir.mkdir("sub").join("reflection.md") reflection_file.write( "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" ) assert ( reflection_file.read() == "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" ) assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "sub" reflection_file = "*.md" invoke.invoke_all_minimum_word_count_checks( reflection_file, directory, 4, fragments.count_minimum_words, constants.words.Minimum, ) details = report.get_result() assert details is not None report.reset() invoke.invoke_all_minimum_word_count_checks( reflection_file, directory, 200, fragments.count_minimum_words, constants.words.Minimum, ) details = report.get_result() assert details is not None
def check(system_arguments): """Orchestrate a full check of the specified deliverables.""" # *Section: Initialize # step_results = [] check_results = [] # **Step: Parse and then verify the arguments, extract remaining arguments parsed_arguments, remaining_arguments = parse_arguments(system_arguments) verification_status = verify_arguments(parsed_arguments) # **Step: Get the source of all the checkers available from either: # --> the internal directory of checkers (e.g., "./gator/checks") # --> the directory specified on the command-line external_checker_directory = checkers.get_checker_dir(parsed_arguments) checker_source = checkers.get_source([external_checker_directory]) # **Step: Get and perform the preliminary actions before running a checker # if the arguments did not parse or verify correctly, then: # --> argparse will cause the program to crash with an error OR # --> one of the actions will be to display the help message and exit actions = get_actions(parsed_arguments, verification_status) perform_actions(actions) # *Section: Perform the check # **Step: Get and transform the name of the chosen checker and # then prepare for running it by ensuring that it is: # --> available for use (i.e., pluginbase found and loaded it) check_name = checkers.get_chosen_check(parsed_arguments) check_file = checkers.transform_check(check_name) check_exists = checkers.verify_check_existence(check_file, checker_source) # **Step: Load the check and verify that it is valid: check_verified = False check = None if check_exists: check = checker_source.load_plugin(check_file) check_verified = checkers.verify_check_functions(check) # produce error message and exit because the check is not valid if not check_exists or not check_verified: # do not potentially produce the welcome message again parsed_arguments.nowelcome = True actions = get_actions(parsed_arguments, check_verified) perform_actions(actions) # **Step: Perform the check since it exists and it is verified check_result = check.act(parsed_arguments, remaining_arguments) check_results.extend(check_result) # *Section: Output the report # **Step: get the report's details result = report.get_result() # **Step: Override the result's description if a user-provided description exists result = description.transform_result_dictionary(parsed_arguments, result) # **Step: produce the output produced_output = report.output(report.get_result(), OUTPUT_TYPE) # **Step: display the output display.message(produced_output) # Section: Return control back to __main__ in gatorgrader # Only step: determine the correct exit code for the checks correct_exit_code = leave.get_code(check_results) return correct_exit_code
def test_act_produces_output( commandline_arguments, chosen_file, containing_directory, provided_count, expected_result, tmpdir, load_checker, ): """Check that using the check produces output.""" testargs = [os.getcwd()] with patch.object(sys, "argv", testargs): new_file = tmpdir.mkdir(containing_directory).join(chosen_file) new_file.write( "paragraph one \n\n paragraph two two \n\n paragraph three three three" ) assert ( new_file.read() == "paragraph one \n\n paragraph two two \n\n paragraph three three three" ) assert len(tmpdir.listdir()) == 1 overall_directory = (tmpdir.dirname + "/" + tmpdir.basename + "/" + containing_directory) commandline_arguments = [ "CountParagraphWords", "--file", "file_to_find", "--directory", overall_directory, "--count", provided_count, ] 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"] != ""
def test_file_exists_in_directory_check_paragraphs(reset_results_dictionary, tmpdir): """Checks that the checking of paragraphs works correctly""" reflection_file = tmpdir.mkdir("sub").join("reflection.md") reflection_file.write("hello world 44\n\nhi\n\nff!$@name\n\n^^44") assert reflection_file.read() == "hello world 44\n\nhi\n\nff!$@name\n\n^^44" assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "sub" reflection_file = "reflection.md" invoke.invoke_all_paragraph_checks(reflection_file, directory, 4) details = report.get_result() assert details is not None report.reset() invoke.invoke_all_paragraph_checks(reflection_file, directory, 200) details = report.get_result() assert details is not None
def test_comment_counts_check_multiple_java(reset_results_dictionary, tmpdir): """Checks that invocation of comment counting check works correctly""" hello_file = tmpdir.mkdir("subdirectory").join("Hello.java") hello_file.write("/* hello world */") assert hello_file.read() == "/* hello world */" assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "subdirectory" hello_file = "Hello.java" invoke.invoke_file_in_directory_check(hello_file, directory) details = report.get_result() assert details is not None report.reset() invoke.invoke_all_comment_checks(hello_file, directory, 1, "multiple-line", "Java") details = report.get_result() assert details is not None
def test_output_text_diagnostic(reset_results_dictionary): """Set the report and check the textual output.""" report.set_result("Command executes", False, "Missing trailing slash") output = report.output(report.get_result(), report.TEXT) assert "Command executes" in output assert "Missing trailing slash" in output assert "\n" in output
def test_content_string_check_fragments_with_threshold(reset_results_dictionary): """Checks that the checking of words works correctly""" value = "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" invoke.invoke_all_count_checks(1, contents=value) invoke.invoke_all_count_checks(2, contents=value) invoke.invoke_all_count_checks(7, contents=value) details = report.get_result() assert details is not None
def test_content_string_check_fragments(reset_results_dictionary): """Check that the checking of words works correctly.""" value = "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" invoke.invoke_all_fragment_checks("hello", 1, contents=value) invoke.invoke_all_fragment_checks("@name", 1, contents=value) invoke.invoke_all_fragment_checks("@name", 2, contents=value) invoke.invoke_all_fragment_checks("planet", 2, contents=value) details = report.get_result() assert details is not None
def test_comment_counts_check_multiple_python_not_enough( reset_results_dictionary, tmpdir): """Check that invocation of comment counting check works correctly.""" hello_file = tmpdir.mkdir("subdirectory").join("Hello.py") hello_file.write('""" hello world """') assert hello_file.read() == '""" hello world """' assert len(tmpdir.listdir()) == 1 # directory = tmpdir.dirname + '"""' + tmpdir.basename + '"""' + "subdirectory" directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "subdirectory" hello_file = "Hello.py" invoke.invoke_file_in_directory_check(hello_file, directory) details = report.get_result() assert details is not None report.reset() invoke.invoke_all_comment_checks(hello_file, directory, 100, "multiple-line", "Python") details = report.get_result() assert details is not None
def test_comment_counts_check_multiple_java_invalid_check( reset_results_dictionary, tmpdir): """Check that invocation of comment counting check works correctly.""" hello_file = tmpdir.mkdir("subdirectory").join("Hello.java") hello_file.write("/* hello world */") assert hello_file.read() == "/* hello world */" assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "subdirectory" hello_file = "Hello.java" invoke.invoke_file_in_directory_check(hello_file, directory) details = report.get_result() assert details is not None report.reset() # this check cannot pass because of the fact that it asks # for "standard-line" comments, which are not supported by this function invoke.invoke_all_comment_checks(hello_file, directory, 1, "standard-line", "Java") details = report.get_result() assert details is not None
def test_output_json(reset_results_dictionary): """Set the report and check output as JSON-based text.""" report.set_result("Command executes", True, "") output = report.output(report.get_result(), report.JSON) assert output is not None assert "\n" not in output assert "Command executes" in output assert "true" in output assert f'"{constants.results.Check}":' in output assert f'"{constants.results.Outcome}":' in output assert f'"{constants.results.Diagnostic}":' in output
def test_output_json(reset_results_dictionary): """Set the report and check output as JSON-based text""" report.set_result("Command executes", True, "") output = report.output(report.get_result(), report.JSON) assert output is not None assert "\n" not in output assert "Command executes" in output assert "true" in output assert f'"{report.CHECK}":' in output assert f'"{report.OUTCOME}":' in output assert f'"{report.DIAGNOSTIC}":' in output
def test_file_exists_in_directory_check_words_exact(reset_results_dictionary, tmpdir): """Checks that the checking of words works exact correctly""" reflection_file = tmpdir.mkdir("sub").join("reflection.md") reflection_file.write( "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" ) assert ( reflection_file.read() == "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" ) assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "sub" reflection_file = "reflection.md" invoke.invoke_all_word_count_checks(reflection_file, directory, 4, True) details = report.get_result() assert details is not None report.reset() invoke.invoke_all_word_count_checks(reflection_file, directory, 200, True) details = report.get_result() assert details is not None
def test_file_exists_in_directory_check_total_words_exact( reset_results_dictionary, tmpdir): """Check that the checking of total words works exactly correctly.""" reflection_file = tmpdir.mkdir("sub").join("reflection.md") reflection_file.write( "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" ) assert ( reflection_file.read() == "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" ) assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "sub" reflection_file = "reflection.md" # invoke.invoke_all_total_word_count_checks( # reflection_file, directory, 12, exact=True # ) invoke.invoke_all_total_word_count_checks( reflection_file, directory, 12, fragments.count_total_words, constants.words.Total, exact=True, ) details = report.get_result() assert details is not None report.reset() # invoke.invoke_all_total_word_count_checks( # reflection_file, directory, 100, exact=True # ) invoke.invoke_all_total_word_count_checks( reflection_file, directory, 100, fragments.count_total_words, constants.words.Total, exact=True, ) details = report.get_result() assert details is not None
def test_file_exists_in_directory_check_test_file(reset_results_dictionary): """Check that invocation of file existence check works correctly.""" testargs = [os.getcwd()] with patch.object(sys, "argv", testargs): directory = "tests" test_file = "test_invoke.py" invoke.invoke_file_in_directory_check(test_file, directory) details = report.get_result() # file is found in the specified directory assert details is not None assert details[constants.results.Outcome] is True assert "exists in" in details[constants.results.Description] assert details[constants.results.Diagnostic] == ""
def test_file_exists_in_directory_check(reset_results_dictionary, tmpdir): """Checks to that invocation of file existence check works correctly""" hello_file = tmpdir.mkdir("sub").join("hello.txt") hello_file.write("content") assert hello_file.read() == "content" assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "sub" hello_file = "hello.txt" invoke.invoke_file_in_directory_check(hello_file, directory) details = report.get_result() assert details is not None # assert details["outcome"] is True assert details["diagnostic"] is not None assert details["diagnostic"]
def test_run_command_grab_output_as_string_count_lines( reset_results_dictionary, tmpdir ): """Checks that invocation of command produces correct captured output for line count""" tmpdir.mkdir("Hello1") tmpdir.mkdir("Hello2") tmpdir.mkdir("Hello3") assert len(tmpdir.listdir()) == 3 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" met_or_exceeded_count = invoke.invoke_all_command_count_checks("ls " + directory, 1) assert met_or_exceeded_count is True met_or_exceeded_count = invoke.invoke_all_command_count_checks("ls " + directory, 4) assert met_or_exceeded_count is False details = report.get_result() assert details is not None
def test_file_exists_in_directory_check(reset_results_dictionary, tmpdir): """Check that invocation of file existence check works correctly.""" hello_file = tmpdir.mkdir("sub").join("hello.txt") hello_file.write("content") assert hello_file.read() == "content" assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "sub" hello_file = "hello.txt" invoke.invoke_file_in_directory_check(hello_file, directory) details = report.get_result() # file is found in the specified directory assert details is not None assert details[constants.results.Outcome] is True assert "exists in" in details[constants.results.Description] assert details[constants.results.Diagnostic] == ""
def check(system_arguments): """Orchestrate a full check of the specified deliverables""" # Section: Initialize # Only step: check the arguments step_results = [] check_results = [] gg_arguments, arguments_actions = check_arguments(system_arguments) step_results = perform(arguments_actions) check_results.extend(step_results) # Section: Perform one of these checks checks = [ "check_commits", "check_exists", "check_single", "check_multiple", "check_paragraphs", "check_words", "check_markdown_file", "check_fragment_file", "check_fragment_command", "check_count_file", "check_count_command", "check_executes_command", "check_regex_file", "check_regex_command", ] # iterate through all of the possible checks for a_check in checks: # reflectively create the checking function check_to_invoke = getattr(ORCHESTRATE, a_check) # call the checking function and get actions actions = check_to_invoke(gg_arguments) # perform the actions and get results step_results = perform(actions) # store the results from these actions check_results.extend(step_results) # Section: Output the report # Only step: get the report's details, produce the output, and display it produced_output = report.output(report.get_result(), OUTPUT_TYPE) display.message(produced_output) # Section: Return control back to __main__ in gatorgrader # Only step: determine the correct exit code for the checks correct_exit_code = leave.get_code(check_results) return correct_exit_code
def test_output_text(reset_results_dictionary): """Set the report and check the textual output.""" report.set_result("Command executes", True, "") output = report.output(report.get_result(), report.TEXT) assert "Command executes" in output assert "\n" not in output
def test_set_result(reset_results_dictionary): """Set the result dictionary and check if it keeps the values""" report.set_result("Command executes", True, "") assert report.get_result()[report.CHECK] == "Command executes" assert report.get_result()[report.OUTCOME] is True assert report.get_result()[report.DIAGNOSTIC] == ""
def test_content_string_check_regex_exact(reset_results_dictionary): """Check that the checking of exact regex works correctly.""" value = "hello world 44 fine\n\nhi there nice again\n\nff! Is now $@name again\n\n" invoke.invoke_all_regex_checks("hello", 1, contents=value, exact=True) details = report.get_result() assert details is not None
def test_commit_checks(reset_results_dictionary): """Checks to that invocation of commit check works correctly""" invoke.invoke_commits_check(".", sys.maxsize) details = report.get_result() assert details is not None
def test_act_produces_output_with_exact( commandline_arguments, chosen_file, containing_directory, provided_count, expected_result, tmpdir, load_checker, ): """Check that using the check produces output.""" test_contents = """ ## What Do People Think about GatorGrader? GatorGrader addresses many of the challenges that an instructor faces when designing automated checks for the source code or technical writing that a ## Installing GatorGrader Installing GatorGrader is not necessary if you intend to use it through its [Gradle plugin](https://github.com/GatorEducator/gatorgradle). If you want to ## Running GatorGrader Students and instructors normally use GatorGrader through its [Gradle plugin](https://github.com/GatorEducator/gatorgradle), specifying the requested ## Testing GatorGrader ### Automated Testing The developers use [Pytest](https://docs.pytest.org/en/latest/) for the testing of GatorGrader. Depending on your goals, there are several different...""" testargs = [os.getcwd()] with patch.object(sys, "argv", testargs): new_file = tmpdir.mkdir(containing_directory).join(chosen_file) new_file.write(test_contents) assert new_file.read() == test_contents assert len(tmpdir.listdir()) == 1 overall_directory = ( tmpdir.dirname + "/" + tmpdir.basename + "/" + containing_directory ) commandline_arguments = [ "CountMarkdownTags", "--file", "file_to_find", "--directory", overall_directory, "--count", provided_count, "--tag", "heading", "--exact", ] 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"] != ""
def test_commit_checks_exact(reset_results_dictionary): """Check that invocation of commit check exacted works correctly.""" invoke.invoke_commits_check(".", sys.maxsize, True) details = report.get_result() assert details is not None