2
2
import logging
3
3
import os
4
4
import textwrap
5
- import warnings
6
5
from optparse import Values
7
6
from pathlib import Path
8
7
from typing import Any , Iterator , List , Optional , Protocol , Tuple , Union
@@ -886,7 +885,10 @@ def test_install_requirements_with_options(
886
885
assert req .global_options == [global_option ]
887
886
888
887
def test_warns_on_decode_fail_in_locale (
889
- self , tmpdir : Path , session : PipSession
888
+ self ,
889
+ tmpdir : Path ,
890
+ session : PipSession ,
891
+ caplog : pytest .LogCaptureFixture ,
890
892
) -> None :
891
893
# \xe3\x80\x82 encodes to 'IDEOGRAPHIC FULL STOP' in UTF-8
892
894
# the lone \x82 byte is invalid in the gbk encoding
@@ -897,32 +899,31 @@ def test_warns_on_decode_fail_in_locale(
897
899
898
900
# it's hard to rely on a locale definitely existing for testing
899
901
# so patch things out for simplicity
900
- with pytest . warns ( UnicodeWarning ) as records , mock .patch (
902
+ with caplog . at_level ( logging . WARNING , "pip._internal.req.req_file" ) , mock .patch (
901
903
"locale.getpreferredencoding" , return_value = locale_encoding
902
904
):
903
905
reqs = tuple (parse_reqfile (req_file .resolve (), session = session ))
904
906
905
- assert len (records ) == 1
907
+ assert len (caplog . records ) == 1
906
908
assert (
907
- str ( records [0 ].message )
908
- == "unable to decode data with gbk, falling back to utf-8 "
909
+ caplog . records [0 ].msg == "unable to decode data from %s with encoding %s, "
910
+ " falling back to encoding %s "
909
911
)
912
+ assert caplog .records [0 ].args == (str (req_file ), locale_encoding , "utf-8" )
913
+
910
914
assert len (reqs ) == 1
911
915
assert reqs [0 ].name == "pip"
912
916
assert str (reqs [0 ].specifier ) == "<=24.0"
913
917
914
- @pytest .mark .parametrize ("encoding" , ( "utf-8" , "gbk" ) )
918
+ @pytest .mark .parametrize ("encoding" , [ "utf-8" , "gbk" ] )
915
919
def test_erorrs_on_non_decodable_data (
916
920
self , encoding : str , tmpdir : Path , session : PipSession
917
921
) -> None :
918
922
data = b"\xff "
919
923
req_file = tmpdir / "requirements.txt"
920
924
req_file .write_bytes (data )
921
925
922
- with warnings . catch_warnings (), pytest .raises (UnicodeDecodeError ), mock .patch (
926
+ with pytest .raises (UnicodeDecodeError ), mock .patch (
923
927
"locale.getpreferredencoding" , return_value = encoding
924
928
):
925
- warnings .simplefilter (
926
- "ignore" , category = UnicodeWarning
927
- ) # suppress warning not under test here
928
929
next (parse_reqfile (req_file .resolve (), session = session ))
0 commit comments