import glob
import re


def create_index(test_folder: str = "graph_test_set", wildcard= "*.adoc", recursive: bool = False) -> str:
    """
    Create automatic index of the tests inside test_folder. Recursivity is turned off by default.
    :param test_folder: Folder where the tests are located
    :param wildcard: Wildcard to look for the extension of the documents (e.g. *.adoc)
    :param recursive: boolean indicating if recursivity is expected. If recursive is true, please use proper wildcard
    :returns buffer: Content of the index part of the readme
    """
    buffer = ""
    for test in glob.iglob(f"{test_folder}/{wildcard}", recursive=recursive):
        with open(test, "r") as test_file:
            for line in test_file.readlines():
                if re.search("#### The test", line):
                    break
                elif re.search("^----", line):
                    break
                elif line == "\n":
                    continue
                else:
                    if re.search("^## Test: ", line):
                        line = "##" + line[8:]
                    buffer += line
            buffer += "\n"
    return buffer