26
26
pytest_version_info = tuple (map (int , pytest .__version__ .split ("." )[:3 ]))
27
27
28
28
class TWMock :
29
+ WRITE = object ()
30
+
29
31
def __init__ (self ):
30
32
self .lines = []
33
+ self .is_writing = False
31
34
def sep (self , sep , line = None ):
32
35
self .lines .append ((sep , line ))
36
+ def write (self , msg , ** kw ):
37
+ self .lines .append ((TWMock .WRITE , msg ))
33
38
def line (self , line , ** kw ):
34
39
self .lines .append (line )
35
40
def markup (self , text , ** kw ):
36
41
return text
42
+ def get_write_msg (self , idx ):
43
+ flag , msg = self .lines [idx ]
44
+ assert flag == TWMock .WRITE
45
+ return msg
37
46
38
47
fullwidth = 80
39
48
@@ -803,14 +812,18 @@ def f():
803
812
assert tw .lines [0 ] == " def f():"
804
813
assert tw .lines [1 ] == "> g(3)"
805
814
assert tw .lines [2 ] == ""
806
- assert tw .lines [3 ].endswith ("mod.py:5: " )
807
- assert tw .lines [4 ] == ("_ " , None )
808
- assert tw .lines [5 ] == ""
809
- assert tw .lines [6 ] == " def g(x):"
810
- assert tw .lines [7 ] == "> raise ValueError(x)"
811
- assert tw .lines [8 ] == "E ValueError: 3"
812
- assert tw .lines [9 ] == ""
813
- assert tw .lines [10 ].endswith ("mod.py:3: ValueError" )
815
+ line = tw .get_write_msg (3 )
816
+ assert line .endswith ("mod.py" )
817
+ assert tw .lines [4 ] == (":5: " )
818
+ assert tw .lines [5 ] == ("_ " , None )
819
+ assert tw .lines [6 ] == ""
820
+ assert tw .lines [7 ] == " def g(x):"
821
+ assert tw .lines [8 ] == "> raise ValueError(x)"
822
+ assert tw .lines [9 ] == "E ValueError: 3"
823
+ assert tw .lines [10 ] == ""
824
+ line = tw .get_write_msg (11 )
825
+ assert line .endswith ("mod.py" )
826
+ assert tw .lines [12 ] == ":3: ValueError"
814
827
815
828
def test_toterminal_long_missing_source (self , importasmod , tmpdir ):
816
829
mod = importasmod ("""
@@ -829,13 +842,17 @@ def f():
829
842
tw .lines .pop (0 )
830
843
assert tw .lines [0 ] == "> ???"
831
844
assert tw .lines [1 ] == ""
832
- assert tw .lines [2 ].endswith ("mod.py:5: " )
833
- assert tw .lines [3 ] == ("_ " , None )
834
- assert tw .lines [4 ] == ""
835
- assert tw .lines [5 ] == "> ???"
836
- assert tw .lines [6 ] == "E ValueError: 3"
837
- assert tw .lines [7 ] == ""
838
- assert tw .lines [8 ].endswith ("mod.py:3: ValueError" )
845
+ line = tw .get_write_msg (2 )
846
+ assert line .endswith ("mod.py" )
847
+ assert tw .lines [3 ] == ":5: "
848
+ assert tw .lines [4 ] == ("_ " , None )
849
+ assert tw .lines [5 ] == ""
850
+ assert tw .lines [6 ] == "> ???"
851
+ assert tw .lines [7 ] == "E ValueError: 3"
852
+ assert tw .lines [8 ] == ""
853
+ line = tw .get_write_msg (9 )
854
+ assert line .endswith ("mod.py" )
855
+ assert tw .lines [10 ] == ":3: ValueError"
839
856
840
857
def test_toterminal_long_incomplete_source (self , importasmod , tmpdir ):
841
858
mod = importasmod ("""
@@ -854,13 +871,17 @@ def f():
854
871
tw .lines .pop (0 )
855
872
assert tw .lines [0 ] == "> ???"
856
873
assert tw .lines [1 ] == ""
857
- assert tw .lines [2 ].endswith ("mod.py:5: " )
858
- assert tw .lines [3 ] == ("_ " , None )
859
- assert tw .lines [4 ] == ""
860
- assert tw .lines [5 ] == "> ???"
861
- assert tw .lines [6 ] == "E ValueError: 3"
862
- assert tw .lines [7 ] == ""
863
- assert tw .lines [8 ].endswith ("mod.py:3: ValueError" )
874
+ line = tw .get_write_msg (2 )
875
+ assert line .endswith ("mod.py" )
876
+ assert tw .lines [3 ] == ":5: "
877
+ assert tw .lines [4 ] == ("_ " , None )
878
+ assert tw .lines [5 ] == ""
879
+ assert tw .lines [6 ] == "> ???"
880
+ assert tw .lines [7 ] == "E ValueError: 3"
881
+ assert tw .lines [8 ] == ""
882
+ line = tw .get_write_msg (9 )
883
+ assert line .endswith ("mod.py" )
884
+ assert tw .lines [10 ] == ":3: ValueError"
864
885
865
886
def test_toterminal_long_filenames (self , importasmod ):
866
887
mod = importasmod ("""
@@ -874,15 +895,18 @@ def f():
874
895
try :
875
896
repr = excinfo .getrepr (abspath = False )
876
897
repr .toterminal (tw )
877
- line = tw .lines [- 1 ]
878
898
x = py .path .local ().bestrelpath (path )
879
899
if len (x ) < len (str (path )):
880
- assert line == "mod.py:3: ValueError"
900
+ msg = tw .get_write_msg (- 2 )
901
+ assert msg == "mod.py"
902
+ assert tw .lines [- 1 ] == ":3: ValueError"
881
903
882
904
repr = excinfo .getrepr (abspath = True )
883
905
repr .toterminal (tw )
906
+ msg = tw .get_write_msg (- 2 )
907
+ assert msg == path
884
908
line = tw .lines [- 1 ]
885
- assert line == "%s :3: ValueError" % ( path ,)
909
+ assert line == ":3: ValueError"
886
910
finally :
887
911
old .chdir ()
888
912
@@ -929,19 +953,25 @@ def i():
929
953
assert tw .lines [1 ] == " def f():"
930
954
assert tw .lines [2 ] == "> g()"
931
955
assert tw .lines [3 ] == ""
932
- assert tw .lines [4 ].endswith ("mod.py:3: " )
933
- assert tw .lines [5 ] == ("_ " , None )
934
- assert tw .lines [6 ].endswith ("in g" )
935
- assert tw .lines [7 ] == " h()"
936
- assert tw .lines [8 ].endswith ("in h" )
937
- assert tw .lines [9 ] == " i()"
938
- assert tw .lines [10 ] == ("_ " , None )
939
- assert tw .lines [11 ] == ""
940
- assert tw .lines [12 ] == " def i():"
941
- assert tw .lines [13 ] == "> raise ValueError()"
942
- assert tw .lines [14 ] == "E ValueError"
943
- assert tw .lines [15 ] == ""
944
- assert tw .lines [16 ].endswith ("mod.py:9: ValueError" )
956
+ msg = tw .get_write_msg (4 )
957
+ assert msg .endswith ("mod.py" )
958
+ assert tw .lines [5 ] == ":3: "
959
+ assert tw .lines [6 ] == ("_ " , None )
960
+ tw .get_write_msg (7 )
961
+ assert tw .lines [8 ].endswith ("in g" )
962
+ assert tw .lines [9 ] == " h()"
963
+ tw .get_write_msg (10 )
964
+ assert tw .lines [11 ].endswith ("in h" )
965
+ assert tw .lines [12 ] == " i()"
966
+ assert tw .lines [13 ] == ("_ " , None )
967
+ assert tw .lines [14 ] == ""
968
+ assert tw .lines [15 ] == " def i():"
969
+ assert tw .lines [16 ] == "> raise ValueError()"
970
+ assert tw .lines [17 ] == "E ValueError"
971
+ assert tw .lines [18 ] == ""
972
+ msg = tw .get_write_msg (19 )
973
+ msg .endswith ("mod.py" )
974
+ assert tw .lines [20 ] == ":9: ValueError"
945
975
946
976
@pytest .mark .skipif ("sys.version_info[0] < 3" )
947
977
def test_exc_chain_repr (self , importasmod ):
0 commit comments