Exemplo n.º 1
0
def _set_config(config: Config) -> None:
    # validate and set IO directories that are relative to project root
    config.input_glob = [
        str(Path(config.project_root).joinpath(str(pattern)).absolute())
        for pattern in ensure_list(config.input_glob)
    ]
    config.output_dir = str(
        Path(config.project_root).joinpath(config.output_dir).absolute())
Exemplo n.º 2
0
def run(config: Config):
    examples = {}
    failures = []

    # validate and set IO directories that are relative to project root
    config.input_glob = [
        os.path.abspath(os.path.join(config.project_root, pattern)) for pattern in ensure_list(config.input_glob)
    ]
    config.output_dir = os.path.abspath(os.path.join(config.project_root, config.output_dir))

    paths = file_wrangler.find_files(config)
    logger.debug('files to parse:\n%s', textwrap.indent('\n'.join(paths), prefix='  '))

    for path in paths:
        # load the file
        lines = wrap(config, failures, path, partial(
            file_wrangler.load_file_lines, path
        ), [])

        # extract snippets
        new_examples = wrap(config, failures, path, partial(
            extract_snippets, config, lines, path
        ), {})

        # store the new examples for analysis
        examples.update(new_examples)

    unique_example_names = dict()
    for (path, line_num, example_name), code_lines in examples.items():
        existing = unique_example_names.get(example_name)
        if existing:
            raise exceptions.DuplicateName('Example with duplicate name %s %s matches %s' % (path, line_num, existing))
        else:
            unique_example_names[example_name] = (path, line_num, example_name)

    for (path, line_num, example_name), code_lines in examples.items():
        example_block = '\n'.join(code_lines)
        logger.info('example: %r', example_name)
        logger.debug('example code: %s', example_block)

        wrap(config, failures, path, partial(
            file_wrangler.write_example, config, example_name, example_block
        ))

    return examples, paths, failures
Exemplo n.º 3
0
    def test_read(self):
        with open(self.tmp_fp, "w", encoding="utf8") as fh:
            fh.write(self.text)

        config = Config()
        config.stop_on_first_failure = True
        config.input_glob = self.tmp_fp
        config.output_dir = self.tmpdir.name

        examples, paths, failures = workflow.run(config)

        with self.subTest(part="found the file"):
            self.assertEqual([self.tmp_fp], paths)

        with self.subTest(part="no failures"):
            self.assertEqual([], failures)

        with self.subTest(part="one example extracted"):
            self.assertEqual(len(examples), self.expect_examples)

        for k, v in examples.items():
            with self.subTest(part="example matches"):
                self.assertEqual("\n".join(v), P.sample_output)
                self.assertIn(self.example_name, k[-1])