Exemplo n.º 1
0
def update_test_from_md(md_file, test):
    """
    update test cases and variables for the test
    """
    ret = False
    test_cases = []
    variables = {}
    with open(md_file, encoding='utf-8') as f:
        parser = mistune.BlockLexer()
        text = f.read()
        parser.parse(mistune.preprocessing(text))
        for t in parser.tokens:
            if t["type"] == "table":
                table_header = t["header"][0].lower()
                if table_header == 'test case' or table_header == 'test cases':
                    for c in t["cells"]:
                        if not c[0] == '---':
                            test_cases.append(c[0])
                            break
                if table_header == 'variable' or table_header == 'variables':
                    list_var = None
                    for c in t["cells"]:
                        if c[0].startswith('#') or c[0].startswith('---'):
                            continue
                        if c[0].startswith('${'):
                            list_var = None
                            dict_var = None
                            variables[filter_kw(
                                c[0])] = c[1] if len(c) > 1 else ''
                        elif c[0].startswith('@'):
                            dict_var = None
                            list_var = filter_kw(c[0])
                            variables[list_var] = c[1:]
                        elif c[0].startswith('...'):
                            if list_var:
                                variables[list_var].extend(c[1:])
                            elif dict_var:
                                for i in c[1:]:
                                    if not i:
                                        continue
                                    k, v = i.split('=')
                                    variables[dict_var][k] = v
                        elif c[0].startswith('&'):
                            list_var = None
                            dict_var = filter_kw(c[0])
                            variables[dict_var] = {}
                            for i in c[1:]:
                                if not i:
                                    continue
                                k, v = i.split('=')
                                variables[dict_var][k] = v
                        else:
                            logger.error('Unknown tag: ' + c[0])
    if test.test_cases != test_cases:
        test.test_cases = test_cases
        ret = True
    if test.variables != variables:
        test.variables = variables
        ret = True
    return ret
Exemplo n.º 2
0
    def validate_markdown(self, markdown):
        m = mistune.Markdown()
        blocks = m.block(mistune.preprocessing(markdown))

        for block in blocks:
            if block['type'] == 'heading':
                # we dont want colon after section names
                assert not block['text'].endswith(':')
                if block['text'] in self.required_sections:
                    self.required_sections.remove(block['text'])
        try:
            assert len(self.required_sections) == 0
        except AssertionError as e:
            print("Missing required sections: {}".format(self.required_sections))
            raise e
Exemplo n.º 3
0
    def validate_markdown(self, markdown):
        r"""
        Validate MarkDown with required sections.
        """
        m = mistune.Markdown()
        blocks = m.block(mistune.preprocessing(markdown))

        for block in blocks:
            if block['type'] == 'heading':
                # we dont want colon after section names
                assert not block['text'].endswith(':')
                if block['text'] in self.required_sections:
                    self.required_sections.remove(block['text'])

        if self.required_sections:
            raise ValueError("Missing required sections: {}".format(self.required_sections))
Exemplo n.º 4
0
def fix(src, dst):
    file_content = preprocessing(src.read())
    text_elements = transform(file_content)

    correction_handler = ErrorCorrectionHandler(file_content, text_elements)
    for text_element in text_elements:
        process_errors(correction_handler, text_element)

    try:
        correction_executor = DiffOperationExecutor(
            correction_handler.diffs,
            correction_handler.raw_lines,
        )
        fixed = correction_executor.apply_diff_operations()
    except RuntimeError:
        click.echo('Something Wrong.')
        sys.exit(1)

    if not dst:
        click.echo(fixed)
    else:
        write_file(dst, fixed)
    sys.exit(0)
Exemplo n.º 5
0
def load_test_md(name):
    return preprocessing(
        open(os.path.join(DATA, name), encoding='utf-8').read(), )
Exemplo n.º 6
0
 def parse(self, text):
     return self.output(preprocessing(text))