Repository: pypa/pip
File: tests/lib/__init__.py (Python)

Code before:
    ``created``
        Dictionary of files/directories found only in the end state.

    ``updated``
        Dictionary of files whose size has changed (FIXME not entirely
        reliable, but comparing contents is not possible because
        FoundFile.bytes is lazy, and comparing mtime doesn't help if
        we want to know if a file has been returned to its earlier
        state).

    Ignores mtime and other file attributes; only presence/absence and
    size are considered.

    """

    def prefix_match(path: str, prefix_path: StrPath) -> bool:
        prefix = os.fspath(prefix_path)
        if path == prefix:
            return True
        prefix = prefix.rstrip(os.path.sep) + os.path.sep
        return path.startswith(prefix)

    start_keys = {
        k for k in start.keys() if not any(prefix_match(k, i) for i in ignore)
    }
    end_keys = {k for k in end.keys() if not any(prefix_match(k, i) for i in ignore)}
    deleted = {k: start[k] for k in start_keys.difference(end_keys)}
    created = {k: end[k] for k in end_keys.difference(start_keys)}
    updated = {}
    for k in start_keys.intersection(end_keys):
        if start[k].size != end[k].size:
            updated[k] = end[k]
    return {"deleted": deleted, "created": created, "updated": updated}


def assert_all_changes(
    start_state: Union[_FilesState, TestPipResult],
    end_state: Union[_FilesState, TestPipResult],
    expected_changes: List[StrPath],
) -> Dict[str, _FilesState]:
    """
    Fails if anything changed that isn't listed in the
    expected_changes.

    start_state is either a dict mapping paths to
    scripttest.[FoundFile|FoundDir] objects or a TestPipResult whose
    files_before we'll test.  end_state is either a similar dict or a
    TestPipResult whose files_after we'll test.

    Note: listing a directory means anything below
    that directory can be expected to have changed.

Review comment (suggestion):
```suggestion
    end_keys = {k for k in end if not any(prefix_match(k, i) for i in ignore)}
```

And other `keys()` calls in this file? Not entirely sure.

Code after:
    ``created``
        Dictionary of files/directories found only in the end state.

    ``updated``
        Dictionary of files whose size has changed (FIXME not entirely
        reliable, but comparing contents is not possible because
        FoundFile.bytes is lazy, and comparing mtime doesn't help if
        we want to know if a file has been returned to its earlier
        state).

    Ignores mtime and other file attributes; only presence/absence and
    size are considered.

    """

    def prefix_match(path: str, prefix_path: StrPath) -> bool:
        prefix = os.fspath(prefix_path)
        if path == prefix:
            return True
        prefix = prefix.rstrip(os.path.sep) + os.path.sep
        return path.startswith(prefix)

    start_keys = {k for k in start if not any(prefix_match(k, i) for i in ignore)}
    end_keys = {k for k in end if not any(prefix_match(k, i) for i in ignore)}
    deleted = {k: start[k] for k in start_keys.difference(end_keys)}
    created = {k: end[k] for k in end_keys.difference(start_keys)}
    updated = {}
    for k in start_keys.intersection(end_keys):
        if start[k].size != end[k].size:
            updated[k] = end[k]
    return {"deleted": deleted, "created": created, "updated": updated}


def assert_all_changes(
    start_state: Union[_FilesState, TestPipResult],
    end_state: Union[_FilesState, TestPipResult],
    expected_changes: List[StrPath],
) -> Dict[str, _FilesState]:
    """
    Fails if anything changed that isn't listed in the
    expected_changes.

    start_state is either a dict mapping paths to
    scripttest.[FoundFile|FoundDir] objects or a TestPipResult whose
    files_before we'll test.  end_state is either a similar dict or a
    TestPipResult whose files_after we'll test.

    Note: listing a directory means anything below
    that directory can be expected to have changed.
    """
    __tracebackhide__ = True