Repository: pypa/pip
File: tests/unit/test_req_file.py (Python)

Code before:
        with caplog.at_level(logging.WARNING), mock.patch(
            "locale.getpreferredencoding", return_value=locale_encoding
        ):
            reqs = tuple(parse_reqfile(req_file.resolve(), session=session))

        assert len(caplog.records) == 1
        assert (
            caplog.records[0].msg
            == "unable to decode data from %s with default encoding %s, "
            "falling back to encoding from locale: %s. "
            "If this is intentional you should specify the encoding with a "
            "PEP-263 style comment, e.g. '# -*- coding: %s -*-'"
        )
        assert caplog.records[0].args == (
            str(req_file),
            "utf-8",
            locale_encoding,
            locale_encoding,
        )

        assert len(reqs) == 1
        assert reqs[0].name == "pip"
        assert str(reqs[0].specifier) == "<=24.0"

    @pytest.mark.parametrize("encoding", ["utf-8", "gbk"])
    def test_erorrs_on_non_decodable_data(
        self, encoding: str, tmpdir: Path, session: PipSession
    ) -> None:
        data = b"\xff"
        req_file = tmpdir / "requirements.txt"
        req_file.write_bytes(data)

        with pytest.raises(UnicodeDecodeError), mock.patch(
            "locale.getpreferredencoding", return_value=encoding
        ):
            next(parse_reqfile(req_file.resolve(), session=session))


Review comment (suggestion):
```suggestion
    def test_errors_on_non_decodable_data(
```
:slightly_smiling_face: 

Code after:
        self,
        tmpdir: Path,
        session: PipSession,
        caplog: pytest.LogCaptureFixture,
    ) -> None:
        # \xff is valid in latin-1 but not UTF-8
        data = b"pip<=24.0 # some comment\xff\n"
        locale_encoding = "latin-1"
        req_file = tmpdir / "requirements.txt"
        req_file.write_bytes(data)

        # it's hard to rely on a locale definitely existing for testing
        # so patch things out for simplicity
        with caplog.at_level(logging.WARNING), mock.patch(
            "locale.getpreferredencoding", return_value=locale_encoding
        ):
            reqs = tuple(parse_reqfile(req_file.resolve(), session=session))

        assert len(caplog.records) == 1
        assert (
            caplog.records[0].msg
            == "unable to decode data from %s with default encoding %s, "
            "falling back to encoding from locale: %s. "
            "If this is intentional you should specify the encoding with a "
            "PEP-263 style comment, e.g. '# -*- coding: %s -*-'"
        )
        assert caplog.records[0].args == (
            str(req_file),
            "utf-8",
            locale_encoding,
            locale_encoding,
        )

        assert len(reqs) == 1
        assert reqs[0].name == "pip"
        assert str(reqs[0].specifier) == "<=24.0"

    @pytest.mark.parametrize("encoding", ["utf-8", "gbk"])
    def test_errors_on_non_decodable_data(
        self, encoding: str, tmpdir: Path, session: PipSession
    ) -> None:
        data = b"\xff"
        req_file = tmpdir / "requirements.txt"
        req_file.write_bytes(data)

        with pytest.raises(UnicodeDecodeError), mock.patch(
            "locale.getpreferredencoding", return_value=encoding
        ):
            next(parse_reqfile(req_file.resolve(), session=session))