Beispiel #1
0
def _folded_skips(
    startdir: py.path.local,
    skipped: Sequence[CollectReport],
) -> List[Tuple[int, str, Optional[int], str]]:
    d = {}  # type: Dict[Tuple[str, Optional[int], str], List[CollectReport]]
    for event in skipped:
        assert event.longrepr is not None
        assert len(event.longrepr) == 3, (event, event.longrepr)
        fspath, lineno, reason = event.longrepr
        # For consistency, report all fspaths in relative form.
        fspath = startdir.bestrelpath(py.path.local(fspath))
        keywords = getattr(event, "keywords", {})
        # folding reports with global pytestmark variable
        # this is workaround, because for now we cannot identify the scope of a skip marker
        # TODO: revisit after marks scope would be fixed
        if (event.when == "setup" and "skip" in keywords
                and "pytestmark" not in keywords):
            key = (fspath, None, reason
                   )  # type: Tuple[str, Optional[int], str]
        else:
            key = (fspath, lineno, reason)
        d.setdefault(key, []).append(event)
    values = []  # type: List[Tuple[int, str, Optional[int], str]]
    for key, events in d.items():
        values.append((len(events), *key))
    return values
Beispiel #2
0
def run_autofix_test(
    tmpdir: py.path.local,
    method: typing.Callable[[typing.List[str]], int],
    not_pretty_formatted_path: str,
    formatted_path: str,
) -> None:
    tmpdir.mkdir("src")
    not_pretty_formatted_tmp_path = tmpdir.join("src").join(
        basename(not_pretty_formatted_path))

    # It is a relative paths as KTLint==0.41.0 dropped support for absolute paths
    not_pretty_formatted_tmp_strpath = str(
        tmpdir.bestrelpath(not_pretty_formatted_tmp_path))

    copyfile(not_pretty_formatted_path, not_pretty_formatted_tmp_path)
    with change_dir_context(tmpdir.strpath):
        parameters = ["--autofix", not_pretty_formatted_tmp_strpath]
        status_code = method(parameters)
        if status_code != 1:
            raise UnexpectedStatusCode(parameters=parameters,
                                       expected_status_code=1,
                                       actual_status_code=status_code)

    # file was formatted (shouldn't trigger linter again)
    with change_dir_context(tmpdir.strpath):
        parameters = ["--autofix", not_pretty_formatted_tmp_strpath]
        status_code = method(parameters)
        if status_code != 0:
            raise UnexpectedStatusCode(parameters=parameters,
                                       expected_status_code=0,
                                       actual_status_code=status_code)

    assert not_pretty_formatted_tmp_path.read_text("utf-8") == py.path.local(
        formatted_path).read_text("utf-8")