Ejemplo n.º 1
0
    def test_script_runtime_error(self):
        doc = Document()
        handler = CodeHandler(source="raise RuntimeError('nope')", filename="/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert "nope" in handler.error
Ejemplo n.º 2
0
    def test_script_sys_path(self):
        doc = Document()
        handler = CodeHandler(source="""import sys; raise RuntimeError("path: '%s'" % sys.path[0])""", filename="/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert "path: ''" in handler.error
Ejemplo n.º 3
0
    def test_script_sys_path(self):
        doc = Document()
        handler = CodeHandler(source="""import sys; raise RuntimeError("path: '%s'" % sys.path[0])""", filename="path/to/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert "path: 'path/to'" in handler.error
Ejemplo n.º 4
0
    def test_script_cwd(self):
        doc = Document()
        handler = CodeHandler(source="""import os; raise RuntimeError("cwd: '%s'" % os.getcwd())""", filename="/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert "cwd: '%s'" % abspath(sep) in handler.error
Ejemplo n.º 5
0
    def test_script_bad_syntax(self):
        doc = Document()
        handler = CodeHandler(source="This is a syntax error", filename="/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert 'Invalid syntax' in handler.error
Ejemplo n.º 6
0
    def test_script_runtime_error(self):
        doc = Document()
        handler = CodeHandler(source="raise RuntimeError('nope')", filename="/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert 'nope' in handler.error
Ejemplo n.º 7
0
    def test_script_cwd(self):
        doc = Document()
        handler = CodeHandler("""import os; raise RuntimeError("cwd: '%s'" % os.getcwd())""", "/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert "cwd: '/'" in handler.error
Ejemplo n.º 8
0
    def test_script_bad_syntax(self):
        doc = Document()
        handler = CodeHandler(source="This is a syntax error", filename="/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert "Invalid syntax" in handler.error
Ejemplo n.º 9
0
    def test_script_adds_roots(self):
        doc = Document()
        handler = CodeHandler(source=script_adds_two_roots, filename="/test_filename")
        handler.modify_document(doc)
        if handler.failed:
            raise RuntimeError(handler.error)

        assert len(doc.roots) == 2
Ejemplo n.º 10
0
    def test_empty_script(self):
        doc = Document()
        handler = CodeHandler(source="# This script does nothing", filename="/test_filename")
        handler.modify_document(doc)
        if handler.failed:
            raise RuntimeError(handler.error)

        assert not doc.roots
Ejemplo n.º 11
0
 def test_safe_to_fork(self):
     doc = Document()
     handler = CodeHandler(source="# This script does nothing", filename="path/to/test_filename")
     assert handler.safe_to_fork
     handler.modify_document(doc)
     if handler.failed:
         raise RuntimeError(handler.error)
     assert not handler.safe_to_fork
Ejemplo n.º 12
0
 def test_safe_to_fork(self):
     doc = Document()
     handler = CodeHandler(source="# This script does nothing", filename="path/to/test_filename")
     assert handler.safe_to_fork
     handler.modify_document(doc)
     if handler.failed:
         raise RuntimeError(handler.error)
     assert not handler.safe_to_fork
Ejemplo n.º 13
0
    def test_script_adds_roots(self):
        doc = Document()
        handler = CodeHandler(source=script_adds_two_roots, filename="/test_filename")
        handler.modify_document(doc)
        if handler.failed:
            raise RuntimeError(handler.error)

        assert len(doc.roots) == 2
Ejemplo n.º 14
0
    def test_empty_script(self):
        doc = Document()
        handler = CodeHandler(source="# This script does nothing", filename="/test_filename")
        handler.modify_document(doc)
        if handler.failed:
            raise RuntimeError(handler.error)

        assert not doc.roots
Ejemplo n.º 15
0
    def test_exec_and___future___flags(self):
        doc = Document()
        handler = CodeHandler(source="exec(\"print \\\"XXX\\\"\")", filename="path/to/test_filename")
        handler.modify_document(doc)
        if handler.failed:
            raise RuntimeError(handler.error)

        assert not doc.roots
Ejemplo n.º 16
0
 def test_failed_handler(self, caplog) -> None:
     a = baa.Application()
     handler = CodeHandler(filename="junk", source="bad(")
     a.add(handler)
     d = Document()
     with caplog.at_level(logging.ERROR):
         assert len(caplog.records) == 0
         a.initialize_document(d)
         assert len(caplog.records) == 1
Ejemplo n.º 17
0
    def test_script_argv(self):
        doc = Document()
        handler = CodeHandler(source="""import sys; raise RuntimeError("argv: %r" % sys.argv)""", filename="/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert "argv: ['test_filename']" in handler.error

        doc = Document()
        handler = CodeHandler(source="""import sys; raise RuntimeError("argv: %r" % sys.argv)""", filename="/test_filename", argv=[10, 20, 30])
        handler.modify_document(doc)

        assert handler.error is not None
        assert "argv: ['test_filename', 10, 20, 30]" in handler.error
Ejemplo n.º 18
0
    def test_script_argv(self):
        doc = Document()
        handler = CodeHandler(source="""import sys; raise RuntimeError("argv: %r" % sys.argv)""", filename="path/to/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert "argv: ['test_filename']" in handler.error

        doc = Document()
        handler = CodeHandler(source="""import sys; raise RuntimeError("argv: %r" % sys.argv)""", filename="path/to/test_filename", argv=[10, 20, 30])
        handler.modify_document(doc)

        assert handler.error is not None
        assert "argv: ['test_filename', 10, 20, 30]" in handler.error