def test_run_code(): # set-up bad_code_syntax = "True = 1" bad_code_exec = "a = b" # Not a syntax error, but a NameError good_code = "c = 1" friendly.set_stream("capture") original_level = friendly.get_level() installed = friendly.is_installed() # ----- end of set-up # When a SyntaxError is raised, run_code returns False assert not friendly.run_code(source=bad_code_syntax) result = friendly.get_output() # content is flushed assert "Python exception" in result assert "SyntaxError" in result assert not friendly.get_output() # confirm that content was flushed assert not friendly.run_code(source=bad_code_exec) result = friendly.get_output() assert "Python exception" in result assert "NameError" in result assert friendly.run_code(source=good_code) assert not friendly.get_output() # no new exceptions recorded try: exec(bad_code_syntax, {}) except Exception: assert not friendly.get_output() # When friendly-traceback is not installed, a call to run_code # will end with level set to 0, which corresponds to normal Python # tracebacks friendly.uninstall() friendly.run_code(source=bad_code_syntax) assert friendly.get_level() == 0 friendly.run_code(source=bad_code_syntax, level=4) assert friendly.get_level() == 0 # When friendly-traceback is "installed", a call to run_code # leaves its level unchanged. friendly.install() friendly.set_level(3) friendly.run_code(source=bad_code_syntax) assert friendly.get_level() == 3 friendly.run_code(source=bad_code_syntax, level=4) assert friendly.get_level() == 3 # Clean up and restore for other tests friendly.get_output() friendly.set_stream(None) if installed: friendly.uninstall() friendly.set_level(original_level)
def test_run_error_en(): friendly_traceback.run( "tests/name_error.py", include="explain", # comprehensive console=False, redirect="capture", ) result = friendly_traceback.get_output() friendly_traceback.uninstall() assert "The similar name `pi` was found in the local scope." in result
def test_run_error_fr(): friendly_traceback.run( "tests/name_error.py", lang="fr", include="why", # more restricted than the English test console=False, redirect="capture", ) result = friendly_traceback.get_output() friendly_traceback.set_lang('en') friendly_traceback.uninstall() assert "Le nom semblable `pi` a été trouvé dans la portée locale." in result
def test_uncleaned_traceback(): """Assert this test filename appear in tracebacks if we don't exclude it. """ friendly.install(redirect="capture") try: raise ValueError except ValueError: friendly.explain() output = friendly.get_output() assert "test_clean_traceback" in output # cleanup for other tests friendly.uninstall()
def test_cleaned_traceback(): """Assert this test filename does not appear in tracebacks if we exclude it. """ friendly.install(redirect="capture") friendly.exclude_file_from_traceback(__file__) try: raise ValueError except ValueError: friendly.explain() output = friendly.get_output() assert "test_clean_traceback" not in output # cleanup for other tests friendly.include_file_in_traceback(__file__) friendly.uninstall()
def test_check_syntax(): # set-up bad_code_syntax = "True = 1" bad_code_exec = "a = b" # Not a syntax error, but a NameError good_code = "c = 1" friendly.set_stream("capture") original_verbosity = friendly.get_verbosity() installed = friendly.is_installed() # ----- end of set-up # When a SyntaxError is raised, check_syntax returns False assert not friendly.advanced_check_syntax(source=bad_code_syntax) result = friendly.get_output() # content is flushed assert "Python exception" in result assert "SyntaxError" in result assert not friendly.get_output() # confirm that content was flushed # When no SyntaxError is raised, check_syntax returns a tuple # containing a code object and a file name assert friendly.advanced_check_syntax(source=bad_code_exec) assert friendly.advanced_check_syntax(source=good_code) assert not friendly.get_output() # no new exceptions recorded try: exec(bad_code_syntax, {}) except Exception: assert not friendly.get_output() # When friendly-traceback is not installed, a call to check_syntax # will end with verbosity set to 0, which corresponds to normal Python # tracebacks friendly.uninstall() friendly.advanced_check_syntax(source=bad_code_syntax) assert friendly.get_verbosity() == 0 friendly.advanced_check_syntax(source=bad_code_syntax, verbosity=4) assert friendly.get_verbosity() == 0 # When friendly-traceback is "installed", a call to check_syntax # leaves its verbosity unchanged. friendly.install(redirect="capture") friendly.set_verbosity(3) friendly.advanced_check_syntax(source=bad_code_syntax) assert friendly.get_verbosity() == 3 friendly.advanced_check_syntax(source=bad_code_syntax, verbosity=4) assert friendly.get_verbosity() == 3 # A call to advanced_code_syntax, with a language specified as an argument # should leave the previous language unchanged. friendly.set_lang("en") assert not friendly.advanced_check_syntax(source=bad_code_syntax, lang="fr") result = friendly.get_output() assert "Exception Python" in result # French heading assert friendly.get_lang() == "en" # Clean up and restore for other tests friendly.get_output() friendly.set_stream(None) if installed: friendly.uninstall() friendly.set_verbosity(original_verbosity)
def test_exec_code(): # set-up bad_code_syntax = "True = 1" bad_code_exec = "a = b" # Not a syntax error, but a NameError good_code = "c = 1" friendly.set_stream("capture") original_include = friendly.get_include() installed = friendly.is_installed() # ----- end of set-up # When a SyntaxError is raised, exec_code returns False assert not friendly.editors_helper.exec_code(source=bad_code_syntax) result = friendly.get_output() # content is flushed assert "SyntaxError" in result assert not friendly.get_output() # confirm that content was flushed friendly.editors_helper.exec_code(source=bad_code_exec) result = friendly.get_output() assert "NameError" in result assert friendly.editors_helper.exec_code(source=good_code) assert not friendly.get_output() # no new exceptions recorded try: exec(bad_code_syntax, {}) except Exception: assert not friendly.get_output() # Ensure that a call to exec_code only install() temporarily # if it was not installed before. friendly.uninstall() friendly.editors_helper.exec_code(source=bad_code_syntax) assert not friendly.is_installed() friendly.editors_helper.exec_code(source=bad_code_syntax, include="no_tb") assert not friendly.is_installed() # When friendly-traceback is "installed", a call to exec_code # leaves its include unchanged. friendly.install(redirect="capture") friendly.set_include("friendly_tb") friendly.editors_helper.exec_code(source=bad_code_syntax) assert friendly.get_include() == "friendly_tb" friendly.editors_helper.exec_code(source=bad_code_syntax, include="no_tb") assert friendly.get_include() == "friendly_tb" # A call to exec_code, with a language specified as an argument # should leave the previous language unchanged. friendly.set_lang("en") friendly.editors_helper.exec_code(source=bad_code_exec, lang="fr", include="explain") result = friendly.get_output() assert "Une exception `NameError` indique" in result assert friendly.get_lang() == "en" # Clean up and restore for other tests friendly.get_output() friendly.set_stream(None) if installed: friendly.uninstall() friendly.set_include(original_include)