def test_count_fragments_from_incorrect_wildcard(tmpdir): """Check that counting tags in a file works correctly if wildcard is incorrect.""" hello_file = tmpdir.mkdir("subdirectory").join("Hello.md") assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "subdirectory" hello_file = "%*#@@--(*.md)Hello.md" ( ( exceeds_threshold, actual_count, ), count_dictionary, ) = markdown.specified_tag_greater_than_count("code", markdown.count_specified_tag, 3, hello_file, directory) assert actual_count == 0 assert exceeds_threshold is False hello_file = "Hello.md%*#@@--(*.md)" ( ( exceeds_threshold, actual_count, ), count_dictionary, ) = markdown.specified_tag_greater_than_count("code", markdown.count_specified_tag, 3, hello_file, directory) assert actual_count == 0 assert exceeds_threshold is False
def test_count_fragments_from_file(tmpdir): """Check that counting tags in a file works correctly.""" test_contents = """ # Section One Some text with `code` in them. #Section Two With more `code blocks` and maybe an ![Image](www.example.com).""" hello_file = tmpdir.mkdir("subdirectory").join("Hello.md") hello_file.write(test_contents) assert hello_file.read() == test_contents assert len(tmpdir.listdir()) == 1 directory = tmpdir.dirname + "/" + tmpdir.basename + "/" + "subdirectory" hello_file = "Hello.md" ( (exceeds_threshold, actual_count,), count_dictionary, ) = markdown.specified_tag_greater_than_count( "code", markdown.count_specified_tag, 3, hello_file, directory ) assert actual_count == 2 assert exceeds_threshold is False ( (exceeds_threshold, actual_count,), count_dictionary, ) = markdown.specified_tag_greater_than_count( "code", markdown.count_specified_tag, 1, hello_file, directory, False ) assert actual_count == 2 assert exceeds_threshold is True ( (exactly, actual_count,), count_dictionary, ) = markdown.specified_tag_greater_than_count( "code", markdown.count_specified_tag, 1, hello_file, directory, True ) assert actual_count == 2 assert exactly is False ( (exactly, actual_count,), count_dictionary, ) = markdown.specified_tag_greater_than_count( "code", markdown.count_specified_tag, 2, hello_file, directory, True ) assert actual_count == 2 assert exactly is True
def invoke_all_markdown_checks(markdown_tag, expected_count, filecheck, directory, exact=False): """Perform the check for a markdown tag existence in a file and return the results.""" met_or_exceeded_count = 0 # perform the count, saving the details in a way that preserves information if the # filecheck was given as a wildcard (i.e., "*.py") ( ( met_or_exceeded_count, actual_count, ), count_dictionary, ) = markdown.specified_tag_greater_than_count( markdown_tag, markdown.count_specified_tag, expected_count, filecheck, directory, exact, ) # create an "at least" message which is the default if exact is not True: message = ("The " + filecheck + " in " + directory + " has at least " + str(expected_count) + " of the '" + markdown_tag + "' tag") # create an "exact" message which is an opt-in else: message = ("The " + filecheck + " in " + directory + " has exactly " + str(expected_count) + " of the '" + markdown_tag + "' tag") # Produce the diagnostic and report the result. # If a wildcard (i.e., "*.py") was given for the filename, then # this diagnostic is customized for the file that first breaks the check. fragment_diagnostic = util.get_file_diagnostic(count_dictionary) diagnostic = ("Found " + str(actual_count) + constants.markers.Space + "tag(s)" + constants.markers.Space + fragment_diagnostic + constants.markers.Space + constants.markers.File) # create the diagnostic and report the result report_result(met_or_exceeded_count, message, diagnostic) return met_or_exceeded_count
def invoke_all_markdown_checks( markdown_tag, expected_count, filecheck, directory, exact=False ): """Perform the check for a markdown tag existence in a file and return the results""" met_or_exceeded_count = 0 met_or_exceeded_count, actual_count = markdown.specified_tag_greater_than_count( markdown_tag, markdown.count_specified_tag, expected_count, filecheck, directory, exact, ) # create a message for a file in directory if exact is not True: message = ( "The " + filecheck + " in " + directory + " has at least " + str(expected_count) + " of the '" + markdown_tag + "' elements" ) else: message = ( "The " + filecheck + " in " + directory + " has exactly " + str(expected_count) + " of the '" + markdown_tag + "' elements" ) diagnostic = "Found " + str(actual_count) + " element(s) in the specified file" report_result(met_or_exceeded_count, message, diagnostic) return met_or_exceeded_count