21
21
pytest_version_info = tuple (map (int , pytest .__version__ .split ("." )[:3 ]))
22
22
23
23
class TWMock :
24
+ WRITE = object ()
25
+
24
26
def __init__ (self ):
25
27
self .lines = []
28
+ self .is_writing = False
26
29
def sep (self , sep , line = None ):
27
30
self .lines .append ((sep , line ))
31
+ def write (self , msg , ** kw ):
32
+ self .lines .append ((TWMock .WRITE , msg ))
28
33
def line (self , line , ** kw ):
29
34
self .lines .append (line )
30
35
def markup (self , text , ** kw ):
31
36
return text
37
+ def get_write_msg (self , idx ):
38
+ flag , msg = self .lines [idx ]
39
+ assert flag == TWMock .WRITE
40
+ return msg
32
41
33
42
fullwidth = 80
34
43
@@ -755,14 +764,18 @@ def f():
755
764
assert tw .lines [0 ] == " def f():"
756
765
assert tw .lines [1 ] == "> g(3)"
757
766
assert tw .lines [2 ] == ""
758
- assert tw .lines [3 ].endswith ("mod.py:5: " )
759
- assert tw .lines [4 ] == ("_ " , None )
760
- assert tw .lines [5 ] == ""
761
- assert tw .lines [6 ] == " def g(x):"
762
- assert tw .lines [7 ] == "> raise ValueError(x)"
763
- assert tw .lines [8 ] == "E ValueError: 3"
764
- assert tw .lines [9 ] == ""
765
- assert tw .lines [10 ].endswith ("mod.py:3: ValueError" )
767
+ line = tw .get_write_msg (3 )
768
+ assert line .endswith ("mod.py" )
769
+ assert tw .lines [4 ] == (":5: " )
770
+ assert tw .lines [5 ] == ("_ " , None )
771
+ assert tw .lines [6 ] == ""
772
+ assert tw .lines [7 ] == " def g(x):"
773
+ assert tw .lines [8 ] == "> raise ValueError(x)"
774
+ assert tw .lines [9 ] == "E ValueError: 3"
775
+ assert tw .lines [10 ] == ""
776
+ line = tw .get_write_msg (11 )
777
+ assert line .endswith ("mod.py" )
778
+ assert tw .lines [12 ] == ":3: ValueError"
766
779
767
780
def test_toterminal_long_missing_source (self , importasmod , tmpdir ):
768
781
mod = importasmod ("""
@@ -781,13 +794,17 @@ def f():
781
794
tw .lines .pop (0 )
782
795
assert tw .lines [0 ] == "> ???"
783
796
assert tw .lines [1 ] == ""
784
- assert tw .lines [2 ].endswith ("mod.py:5: " )
785
- assert tw .lines [3 ] == ("_ " , None )
786
- assert tw .lines [4 ] == ""
787
- assert tw .lines [5 ] == "> ???"
788
- assert tw .lines [6 ] == "E ValueError: 3"
789
- assert tw .lines [7 ] == ""
790
- assert tw .lines [8 ].endswith ("mod.py:3: ValueError" )
797
+ line = tw .get_write_msg (2 )
798
+ assert line .endswith ("mod.py" )
799
+ assert tw .lines [3 ] == ":5: "
800
+ assert tw .lines [4 ] == ("_ " , None )
801
+ assert tw .lines [5 ] == ""
802
+ assert tw .lines [6 ] == "> ???"
803
+ assert tw .lines [7 ] == "E ValueError: 3"
804
+ assert tw .lines [8 ] == ""
805
+ line = tw .get_write_msg (9 )
806
+ assert line .endswith ("mod.py" )
807
+ assert tw .lines [10 ] == ":3: ValueError"
791
808
792
809
def test_toterminal_long_incomplete_source (self , importasmod , tmpdir ):
793
810
mod = importasmod ("""
@@ -806,13 +823,17 @@ def f():
806
823
tw .lines .pop (0 )
807
824
assert tw .lines [0 ] == "> ???"
808
825
assert tw .lines [1 ] == ""
809
- assert tw .lines [2 ].endswith ("mod.py:5: " )
810
- assert tw .lines [3 ] == ("_ " , None )
811
- assert tw .lines [4 ] == ""
812
- assert tw .lines [5 ] == "> ???"
813
- assert tw .lines [6 ] == "E ValueError: 3"
814
- assert tw .lines [7 ] == ""
815
- assert tw .lines [8 ].endswith ("mod.py:3: ValueError" )
826
+ line = tw .get_write_msg (2 )
827
+ assert line .endswith ("mod.py" )
828
+ assert tw .lines [3 ] == ":5: "
829
+ assert tw .lines [4 ] == ("_ " , None )
830
+ assert tw .lines [5 ] == ""
831
+ assert tw .lines [6 ] == "> ???"
832
+ assert tw .lines [7 ] == "E ValueError: 3"
833
+ assert tw .lines [8 ] == ""
834
+ line = tw .get_write_msg (9 )
835
+ assert line .endswith ("mod.py" )
836
+ assert tw .lines [10 ] == ":3: ValueError"
816
837
817
838
def test_toterminal_long_filenames (self , importasmod ):
818
839
mod = importasmod ("""
@@ -826,15 +847,18 @@ def f():
826
847
try :
827
848
repr = excinfo .getrepr (abspath = False )
828
849
repr .toterminal (tw )
829
- line = tw .lines [- 1 ]
830
850
x = py .path .local ().bestrelpath (path )
831
851
if len (x ) < len (str (path )):
832
- assert line == "mod.py:3: ValueError"
852
+ msg = tw .get_write_msg (- 2 )
853
+ assert msg == "mod.py"
854
+ assert tw .lines [- 1 ] == ":3: ValueError"
833
855
834
856
repr = excinfo .getrepr (abspath = True )
835
857
repr .toterminal (tw )
858
+ msg = tw .get_write_msg (- 2 )
859
+ assert msg == path
836
860
line = tw .lines [- 1 ]
837
- assert line == "%s :3: ValueError" % ( path ,)
861
+ assert line == ":3: ValueError"
838
862
finally :
839
863
old .chdir ()
840
864
@@ -891,24 +915,32 @@ def i():
891
915
r = excinfo .getrepr (style = "long" )
892
916
tw = TWMock ()
893
917
r .toterminal (tw )
918
+ print 'x' * 80 #XXX DIRTY
894
919
for line in tw .lines : print (line )
920
+ print ')' * 80 #XXX DIRTY
895
921
assert tw .lines [0 ] == ""
896
922
assert tw .lines [1 ] == " def f():"
897
923
assert tw .lines [2 ] == "> g()"
898
924
assert tw .lines [3 ] == ""
899
- assert tw .lines [4 ].endswith ("mod.py:3: " )
900
- assert tw .lines [5 ] == ("_ " , None )
901
- assert tw .lines [6 ].endswith ("in g" )
902
- assert tw .lines [7 ] == " h()"
903
- assert tw .lines [8 ].endswith ("in h" )
904
- assert tw .lines [9 ] == " i()"
905
- assert tw .lines [10 ] == ("_ " , None )
906
- assert tw .lines [11 ] == ""
907
- assert tw .lines [12 ] == " def i():"
908
- assert tw .lines [13 ] == "> raise ValueError()"
909
- assert tw .lines [14 ] == "E ValueError"
910
- assert tw .lines [15 ] == ""
911
- assert tw .lines [16 ].endswith ("mod.py:9: ValueError" )
925
+ msg = tw .get_write_msg (4 )
926
+ assert msg .endswith ("mod.py" )
927
+ assert tw .lines [5 ] == ":3: "
928
+ assert tw .lines [6 ] == ("_ " , None )
929
+ tw .get_write_msg (7 )
930
+ assert tw .lines [8 ].endswith ("in g" )
931
+ assert tw .lines [9 ] == " h()"
932
+ tw .get_write_msg (10 )
933
+ assert tw .lines [11 ].endswith ("in h" )
934
+ assert tw .lines [12 ] == " i()"
935
+ assert tw .lines [13 ] == ("_ " , None )
936
+ assert tw .lines [14 ] == ""
937
+ assert tw .lines [15 ] == " def i():"
938
+ assert tw .lines [16 ] == "> raise ValueError()"
939
+ assert tw .lines [17 ] == "E ValueError"
940
+ assert tw .lines [18 ] == ""
941
+ msg = tw .get_write_msg (19 )
942
+ msg .endswith ("mod.py" )
943
+ assert tw .lines [20 ] == ":9: ValueError"
912
944
913
945
914
946
@pytest .mark .parametrize ("style" , ["short" , "long" ])
0 commit comments