示例#1
0
    def test_script_argv(self):
        doc = Document()
        handler = bahc.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 = bahc.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
示例#2
0
    def test_script_runtime_error(self):
        doc = Document()
        handler = bahc.CodeHandler(source="raise RuntimeError('nope')", filename="path/to/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert 'nope' in handler.error
示例#3
0
    def test_script_sys_path(self):
        doc = Document()
        handler = bahc.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
示例#4
0
    def test_script_bad_syntax(self):
        doc = Document()
        handler = bahc.CodeHandler(source="This is a syntax error", filename="path/to/test_filename")
        handler.modify_document(doc)

        assert handler.error is not None
        assert 'Invalid syntax' in handler.error
示例#5
0
    def test_script_adds_roots(self):
        doc = Document()
        handler = bahc.CodeHandler(source=script_adds_two_roots, filename="path/to/test_filename")
        handler.modify_document(doc)
        if handler.failed:
            raise RuntimeError(handler.error)

        assert len(doc.roots) == 2
示例#6
0
    def test_empty_script(self):
        doc = Document()
        handler = bahc.CodeHandler(source="# This script does nothing", filename="path/to/test_filename")
        handler.modify_document(doc)
        if handler.failed:
            raise RuntimeError(handler.error)

        assert not doc.roots
示例#7
0
    def test_exec_and___future___flags(self):
        doc = Document()
        handler = bahc.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
示例#8
0
 def test_safe_to_fork(self) -> None:
     doc = Document()
     handler = bahc.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
示例#9
0
 def test_missing_filename(self) -> None:
     with pytest.raises(ValueError) as e:
         bahc.CodeHandler(source="# This script does nothing")
         assert str(e) == "Must pass filename to CodeHandler"
示例#10
0
 def test_missing_source(self) -> None:
     with pytest.raises(ValueError) as e:
         bahc.CodeHandler()
         assert str(e) == "Must pass source to CodeHandler"