Commit 7b5add47 authored by Jérome Perrin's avatar Jérome Perrin

loadNXDTestFile: use `compile` for better tracebacks on errors

When using compile with the actual file path, we can have better tracebacks
in case of errors.

before:

    Traceback (most recent call last):
      File "/srv/slapgrid/slappart3/srv/runner/software/9544feb19475590d240ba2d32743c0a0/bin/nxdtest", line 22, in <module>
        sys.exit(nxdtest.main())
      File "/srv/slapgrid/slappart3/srv/runner/software/9544feb19475590d240ba2d32743c0a0/parts/nxdtest/nxdtest/__init__.py", line 142, in main
        tenv = loadNXDTestFile('.nxdtest')
      File "/srv/slapgrid/slappart3/srv/runner/software/9544feb19475590d240ba2d32743c0a0/parts/nxdtest/nxdtest/__init__.py", line 75, in loadNXDTestFile
        six.exec_(src, g)
      File "<string>", line 77, in <module>
    NameError: name 'Pylint' is not defined

after:

    Traceback (most recent call last):
      File "/srv/slapgrid/slappart3/srv/runner/software/9544feb19475590d240ba2d32743c0a0/bin/nxdtest", line 22, in <module>
        sys.exit(nxdtest.main())
      File "/srv/slapgrid/slappart3/srv/runner/software/9544feb19475590d240ba2d32743c0a0/parts/nxdtest/nxdtest/__init__.py", line 142, in main
        tenv = loadNXDTestFile('.nxdtest')
      File "/srv/slapgrid/slappart3/srv/runner/software/9544feb19475590d240ba2d32743c0a0/parts/nxdtest/nxdtest/__init__.py", line 75, in loadNXDTestFile
        six.exec_(compile(src, os.path.realpath(path), 'exec'), g)
      File "/srv/slapgrid/slappart3/srv/runner/instance/slappart8/var/nxdtest/.nxdtest", line 77, in <module>
        summaryf=Pylint.summary,
    NameError: name 'Pylint' is not defined

/reviewed-by @kirr
/reviewed-in nexedi/nxdtest!10
parent 0ad45a9c
...@@ -73,7 +73,7 @@ def loadNXDTestFile(path): # -> TestEnv ...@@ -73,7 +73,7 @@ def loadNXDTestFile(path): # -> TestEnv
'UnitTest': UnitTest,} 'UnitTest': UnitTest,}
with open(path, "r") as f: with open(path, "r") as f:
src = f.read() src = f.read()
six.exec_(src, g) six.exec_(compile(src, os.path.realpath(path), 'exec'), g)
return t return t
# TestCase defines one test case to run. # TestCase defines one test case to run.
......
...@@ -89,6 +89,18 @@ TestCase('TESTNAME', ['echo'], summaryf="error") ...@@ -89,6 +89,18 @@ TestCase('TESTNAME', ['echo'], summaryf="error")
assert "TypeError" in captured.err assert "TypeError" in captured.err
def test_error_execing_nxdtest_file(run_nxdtest, capsys):
with pytest.raises(ZeroDivisionError) as excinfo:
run_nxdtest(
"""\
1 / 0
"""
)
assert '1 / 0' in str(excinfo.traceback[-1])
# The actual .nxdtest filename is also included in traceback
assert ".nxdtest':1" in str(excinfo.traceback[-1])
def test_run_argument(run_nxdtest, capsys): def test_run_argument(run_nxdtest, capsys):
run_nxdtest( run_nxdtest(
"""\ """\
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment