Debian Codegraph @

tests/unit/test_exec_code.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 """In this file, we test to ensure that the output of
2 exec_code is as expected. The tests we do here
3 are almost identical to those for check_syntax,
4 found in test_check_syntax.py
5
6 We also test to ensure that exec_code does not accidentally
7 change any existing error handling settings.
8
9 """
10
11 import friendly_traceback as ft
12
13
14 def test_exec_code():
15 # set-up
16 bad_code_syntax = "True = 1"
17 bad_code_exec = "a = b" # Not a syntax error, but a NameError
18 good_code = "c = 1"
19
20 ft.set_stream("capture")
21 original_include = ft.get_include()
22 installed = ft.is_installed()
23 # ----- end of set-up
24
25 # When a SyntaxError is raised, exec_code returns False
26
27 assert not ft.editors_helpers.exec_code(source=bad_code_syntax)
28 result = ft.get_output() # content is flushed
29 assert "SyntaxError" in result
30
31 assert not ft.get_output() # confirm that content was flushed
32
33 ft.editors_helpers.exec_code(source=bad_code_exec)
34 result = ft.get_output()
35 assert "NameError" in result
36
37 assert ft.editors_helpers.exec_code(source=good_code)
38 assert not ft.get_output() # no new exceptions recorded
39
40 try:
41 exec(bad_code_syntax, {})
42 except Exception:
43 assert not ft.get_output()
44
45 # Ensure that a call to exec_code only install() temporarily
46 # if it was not installed before.
47 ft.uninstall()
48 ft.editors_helpers.exec_code(source=bad_code_syntax)
49 assert not ft.is_installed()
50 ft.editors_helpers.exec_code(source=bad_code_syntax, include="no_tb")
51 assert not ft.is_installed()
52
53 # When friendly is "installed", a call to exec_code
54 # leaves its include unchanged.
55 ft.install(redirect="capture")
56
57 ft.set_include("friendly_tb")
58 ft.editors_helpers.exec_code(source=bad_code_syntax)
59 assert ft.get_include() == "friendly_tb"
60 ft.editors_helpers.exec_code(source=bad_code_syntax, include="no_tb")
61 assert ft.get_include() == "friendly_tb"
62
63 # A call to exec_code, with a language specified as an argument
64 # should leave the previous language unchanged.
65
66 ft.set_lang("en")
67 ft.editors_helpers.exec_code(source=bad_code_exec, lang="fr", include="explain")
68 result = ft.get_output()
69 assert "Une exception `NameError` indique" in result
70 assert ft.get_lang() == "en"
71
72 # Clean up and restore for other tests
73 ft.get_output()
74 ft.set_stream(None)
75 if installed:
76 ft.uninstall()
77 ft.set_include(original_include)
78
79
80 def test_source_cache():
81 """
82 Test that all relevant caches are updated by `source_cache.cache.add`.
83 This test would fail without the clearing of the stack_data cache.
84 """
85
86 ft.set_stream("capture")
87 try:
88 for n in range(5):
89 source = f"{n}/0"
90 ft.editors_helpers.exec_code(source=source)
91 result = ft.get_output()
92 assert source in result
93 finally:
94 ft.set_stream(None)
95
96
97 if __name__ == "__main__":
98 test_exec_code()
99 test_source_cache()
100 print("Success!")