aboutsummaryrefslogtreecommitdiffstats
path: root/community/py3-sybil/fix-tests-for-python-3-10.patch
blob: b261140173c404fb4dc533b287da7dab3290db8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
From 3650f5e609a6da7e114224a578e4182e262cdec5 Mon Sep 17 00:00:00 2001
From: Chris Withers <chris@simplistix.co.uk>
Date: Tue, 5 Oct 2021 08:29:21 +0100
Subject: [PATCH] More specific pattern matching to help with Python 3.10

---
 tests/test_functional.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tests/test_functional.py b/tests/test_functional.py
index 6e77053..5bebfdb 100644
--- a/tests/test_functional.py
+++ b/tests/test_functional.py
@@ -15,7 +15,7 @@ def __init__(self, text):
         self.index = 0
 
     def then_find(self, substring):
-        assert substring in self.text[self.index:]
+        assert substring in self.text[self.index:], self.text[self.index:]
         self.index = self.text.index(substring, self.index)
 
 
@@ -99,14 +99,14 @@ def pytest_sessionfinish(self, session):
                   'sybil teardown 4\n'
                   'session_fixture teardown')
     out.then_find('_ fail.rst line=1 column=1 _')
-    out.then_find(  ">   raise Exception('the start!')")
+    out.then_find("raise Exception('the start!')")
     out.then_find('_ fail.rst line=8 column=1 _')
     out.then_find('Y count was 3 instead of 2')
     out.then_find('fail.rst:8: SybilFailure')
     out.then_find('_ fail.rst line=10 column=1 _')
     out.then_find('ValueError: X count was 3 instead of 4')
     out.then_find('_ fail.rst line=14 column=1 _')
-    out.then_find(">       raise Exception('boom!')")
+    out.then_find("raise Exception('boom!')")
     out.then_find('fail.rst:18: Exception')
 
 
From 4c12bfc0340a77cbe6496ebcef2fc777006a9c0e Mon Sep 17 00:00:00 2001
From: Chris Withers <chris@simplistix.co.uk>
Date: Tue, 5 Oct 2021 10:42:15 +0100
Subject: [PATCH] Restructure tests to clarify that the line numbers and
 filenames need to be correct in exceptions.

---
 tests/helpers.py                           | 10 ++++++++
 tests/samples/codeblock_future_imports.txt |  4 +--
 tests/test_codeblock.py                    | 30 ++++++++--------------
 3 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/tests/helpers.py b/tests/helpers.py
index 1f3d7dc..d96fe56 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -1,5 +1,8 @@
 from io import open
 from os.path import dirname, join
+from traceback import TracebackException
+
+from _pytest._code import ExceptionInfo
 
 from sybil.document import Document
 from sybil.example import Example
@@ -23,3 +26,10 @@ def evaluate_region(region, namespace):
         region=region,
         namespace=namespace
     ))
+
+
+def check_excinfo(excinfo: ExceptionInfo, text: str, *, lineno: int, filename: str = '/the/path'):
+    assert str(excinfo.value) == text, f'{str(excinfo.value)!r} == {text!r}'
+    details = TracebackException.from_exception(excinfo.value, lookup_lines=False).stack[-1]
+    assert details.filename == filename, f'{details.filename!r} == {filename!r}'
+    assert details.lineno == lineno, f'{details.lineno} == {lineno}'
diff --git a/tests/samples/codeblock_future_imports.txt b/tests/samples/codeblock_future_imports.txt
index c59a541..22412ed 100644
--- a/tests/samples/codeblock_future_imports.txt
+++ b/tests/samples/codeblock_future_imports.txt
@@ -1,9 +1,9 @@
 .. invisible-code-block: python
 
-  print('pathalogical worst case for line numbers', file=buffer)
+  raise Exception('Boom 1')
 
 More likely is one down here:
 
 .. code-block:: python
 
-  print('still should work and have good line numbers', file=buffer)
+  raise Exception('Boom 2')
diff --git a/tests/test_codeblock.py b/tests/test_codeblock.py
index 9196fb5..9aab10e 100644
--- a/tests/test_codeblock.py
+++ b/tests/test_codeblock.py
@@ -2,7 +2,7 @@
 from io import StringIO
 from sybil.document import Document
 from sybil.parsers.codeblock import CodeBlockParser, compile_codeblock
-from tests.helpers import document_from_sample, evaluate_region
+from .helpers import document_from_sample, evaluate_region, check_excinfo
 
 
 def test_basic():
@@ -16,7 +16,7 @@ def test_basic():
     assert namespace['z'] == 0
     with pytest.raises(Exception) as excinfo:
         evaluate_region(regions[1], namespace)
-    assert str(excinfo.value) == 'boom!'
+    check_excinfo(excinfo, 'boom!', lineno=11)
     assert evaluate_region(regions[2], namespace) is None
     assert namespace['y'] == 1
     assert namespace['z'] == 1
@@ -32,27 +32,17 @@ def test_basic():
     assert '__builtins__' not in namespace
 
 
-def test_future_imports():
+def test_line_numbers_correct_with_future_imports():
     document = document_from_sample('codeblock_future_imports.txt')
     regions = list(CodeBlockParser(['print_function'])(document))
     assert len(regions) == 2
-    buffer = StringIO()
-    namespace = {'buffer': buffer}
-    assert evaluate_region(regions[0], namespace) is None
-    assert buffer.getvalue() == (
-        'pathalogical worst case for line numbers\n'
-    )
-    # the future import line drops the firstlineno by 1
-    code = compile_codeblock(regions[0].parsed, document.path)
-    assert code.co_firstlineno == 2
-    assert evaluate_region(regions[1], namespace) is None
-    assert buffer.getvalue() == (
-        'pathalogical worst case for line numbers\n'
-        'still should work and have good line numbers\n'
-    )
-    # the future import line drops the firstlineno by 1
-    code = compile_codeblock(regions[1].parsed, document.path)
-    assert code.co_firstlineno == 8
+    namespace = {}
+    with pytest.raises(Exception) as excinfo:
+        evaluate_region(regions[0], namespace)
+    check_excinfo(excinfo, 'Boom 1', lineno=3)
+    with pytest.raises(Exception) as excinfo:
+        evaluate_region(regions[1], namespace)
+    check_excinfo(excinfo, 'Boom 2', lineno=9)
 
 
 def test_windows_line_endings(tmp_path):