Esempio n. 1
0
def test_lifecycle_bad_syntax():
    result = {}
    def load(filename):
        handler = ServerLifecycleHandler(filename=filename)
        result['handler'] = handler
    with_file_contents("This is a syntax error", load)

    handler = result['handler']
    assert handler.error is not None
    assert 'Invalid syntax' in handler.error
Esempio n. 2
0
def test_lifecycle_runtime_error():
    result = {}
    def load(filename):
        handler = ServerLifecycleHandler(filename=filename)
        result['handler'] = handler
    with_file_contents("raise RuntimeError('nope')", load)

    handler = result['handler']
    assert handler.error is not None
    assert 'nope' in handler.error
Esempio n. 3
0
def test_url_path():
    result = {}
    def load(filename):
        handler = ServerLifecycleHandler(filename=filename)
        result['handler'] = handler
    with_file_contents("""
def on_server_unloaded(server_context):
    pass
""", load)

    handler = result['handler']
    assert handler.error is None
    assert handler.url_path().startswith("/")
Esempio n. 4
0
def test_calling_lifecycle_hooks():
    result = {}
    def load(filename):
        handler = result['handler'] = ServerLifecycleHandler(filename=filename)
        if handler.failed:
            raise RuntimeError(handler.error)
    with_file_contents(script_adds_four_handlers, load)

    handler = result['handler']
    assert "on_server_loaded" == handler.on_server_loaded(None)
    assert "on_server_unloaded" == handler.on_server_unloaded(None)
    assert "on_session_created" == handler.on_session_created(None)
    assert "on_session_destroyed" == handler.on_session_destroyed(None)
Esempio n. 5
0
    def test_runner_script_with_encoding(self):
        doc = Document()
        source = "# -*- coding: utf-8 -*-\nimport os"
        result = {}
        def load(filename):
            handler = bahs.ScriptHandler(filename=filename)
            handler.modify_document(doc)
            result['handler'] = handler
            result['filename'] = filename
        with_file_contents(source, load)

        assert result['handler'].error is None
        assert result['handler'].failed is False
        assert not doc.roots
Esempio n. 6
0
    def test_runner_uses_source_from_filename(self):
        doc = Document()
        source = "# Test contents for script"
        result = {}
        def load(filename):
            handler = bahs.ScriptHandler(filename=filename)
            handler.modify_document(doc)
            result['handler'] = handler
            result['filename'] = filename
        with_file_contents(source, load)

        assert result['handler']._runner.path == result['filename']
        assert result['handler']._runner.source == source
        assert not doc.roots
Esempio n. 7
0
def test_lifecycle_bad_session_created_signature():
    result = {}
    def load(filename):
        handler = ServerLifecycleHandler(filename=filename)
        result['handler'] = handler
    with_file_contents("""
def on_session_created(a,b):
    pass
""", load)

    handler = result['handler']
    assert handler.error is not None
    assert 'on_session_created must have signature func(session_context)' in handler.error
    assert 'func(a, b)' in handler.error
Esempio n. 8
0
def test_empty_lifecycle():
    doc = Document()
    def load(filename):
        handler = ServerLifecycleHandler(filename=filename)
        handler.modify_document(doc)
        handler.on_server_loaded(None)
        handler.on_server_unloaded(None)
        handler.on_session_created(None)
        handler.on_session_destroyed(None)
        if handler.failed:
            raise RuntimeError(handler.error)
    with_file_contents("# This script does nothing", load)

    assert not doc.roots
Esempio n. 9
0
def test_url_path_failed():
    result = {}
    def load(filename):
        handler = ServerLifecycleHandler(filename=filename)
        result['handler'] = handler
    with_file_contents("""
# bad signature
def on_server_unloaded():
    pass
""", load)

    handler = result['handler']
    assert handler.error is not None
    assert handler.url_path() is None
def test_runner_uses_source_from_filename():
    doc = Document()
    source = "# Test contents for script"
    result = {}
    def load(filename):
        handler = ScriptHandler(filename=filename)
        handler.modify_document(doc)
        result['handler'] = handler
        result['filename'] = filename
    with_file_contents(source, load)

    assert result['handler']._runner.path == result['filename']
    assert result['handler']._runner.source == source
    assert not doc.roots
def test_runner_script_with_encoding():
    doc = Document()
    source = "# -*- coding: utf-8 -*-\nimport os"
    result = {}
    def load(filename):
        handler = ScriptHandler(filename=filename)
        handler.modify_document(doc)
        result['handler'] = handler
        result['filename'] = filename
    with_file_contents(source, load)

    assert result['handler'].error is None
    assert result['handler'].failed is False
    assert not doc.roots
Esempio n. 12
0
    def test_lifecycle_bad_server_loaded_signature(self):
        result = {}
        def load(filename):
            handler = bahs.ServerLifecycleHandler(filename=filename)
            result['handler'] = handler
        with_file_contents("""
def on_server_loaded(a,b):
    pass
""", load)

        handler = result['handler']
        assert handler.error is not None
        assert 'on_server_loaded must have signature func(server_context)' in handler.error
        assert 'func(a, b)' in handler.error
        assert "Traceback" in handler.error_detail