Commit 495fed6e 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
parent 0ad45a9c
Pipeline #16943 passed with stage
in 0 seconds
......@@ -73,7 +73,7 @@ def loadNXDTestFile(path): # -> TestEnv
'UnitTest': UnitTest,}
with open(path, "r") as f:
src = f.read()
six.exec_(src, g)
six.exec_(compile(src, os.path.realpath(path), 'exec'), g)
return t
# TestCase defines one test case to run.
......
......@@ -89,6 +89,18 @@ TestCase('TESTNAME', ['echo'], summaryf="error")
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):
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