Example #1
0
def test_initialize_post_save_execution(monkeypatch, caplog):
    """Test that post_save initialization works as expected and bound post_save executes."""
    caplog.set_level(logging.DEBUG)

    jupyter_config_obj = Config(FileContentsManager=FileContentsManager())

    def mocked_post_save(model, os_path, contents_manager):
        """Append a token to os_path to certify that function ran."""
        os_path.append("nbautoexport")

    monkeypatch.setattr(nbautoexport_root, "post_save", mocked_post_save)

    # Initialize post_save hook
    jupyter_config.initialize_post_save_hook(jupyter_config_obj)

    assert caplog_contains(
        caplog,
        level=logging.INFO,
        in_msg="nbautoexport | Successfully registered post-save hook",
    )
    assert isinstance(jupyter_config_obj.FileContentsManager,
                      FileContentsManager)
    assert callable(jupyter_config_obj.FileContentsManager.post_save_hook)

    # Execute post_save hook
    os_path_list = []
    jupyter_config_obj.FileContentsManager.run_post_save_hook(
        model=None, os_path=os_path_list)
    assert os_path_list == ["nbautoexport"]
Example #2
0
def test_initialize_post_save_import_error_caught(monkeypatch, caplog,
                                                  jupyter_app):
    """Test that missing nbautoexport error is caught and properly logged."""

    real_import = __builtins__["__import__"]

    def mock_import(name, globals=None, locals=None, fromlist=(), level=0):
        if name == "nbautoexport":
            raise ModuleNotFoundError("No module named 'nbautoexport'")
        return real_import(name=name,
                           globals=globals,
                           locals=locals,
                           fromlist=fromlist,
                           level=level)

    monkeypatch.setattr(builtins, "__import__", mock_import)
    monkeypatch.delitem(sys.modules, "nbautoexport")

    jupyter_config_obj = Config(FileContentsManager=FileContentsManager())

    # Initialize post_save hook
    # Should run through since error is caught
    jupyter_config.initialize_post_save_hook(jupyter_config_obj)

    assert caplog_contains(
        caplog,
        name=jupyter_app.log.name,
        level=logging.ERROR,
        in_msg="ModuleNotFoundError: No module named 'nbautoexport'",
    )
Example #3
0
def test_initialize_post_save_binding():
    """Test that post_save hook can be successfully bound to a Jupyter config."""
    jupyter_config_obj = Config(FileContentsManager=FileContentsManager())
    jupyter_config.initialize_post_save_hook(jupyter_config_obj)
    assert isinstance(jupyter_config_obj.FileContentsManager,
                      FileContentsManager)
    assert jupyter_config_obj.FileContentsManager.post_save_hook is export.post_save
Example #4
0
def test_initialize_post_save_existing(monkeypatch):
    """Test that handling of existing post_save hook works properly."""

    jupyter_config_obj = Config(FileContentsManager=FileContentsManager())

    def old_post_save(model, os_path, contents_manager):
        """Append a token to os_path to certify that function ran."""
        os_path.append("old_post_save")

    jupyter_config_obj.FileContentsManager.post_save_hook = old_post_save

    def mocked_post_save(model, os_path, contents_manager):
        """Append a token to os_path to certify that function ran."""
        os_path.append("nbautoexport")

    monkeypatch.setattr(nbautoexport_root, "post_save", mocked_post_save)

    jupyter_config.initialize_post_save_hook(jupyter_config_obj)

    assert isinstance(jupyter_config_obj.FileContentsManager,
                      FileContentsManager)
    assert callable(jupyter_config_obj.FileContentsManager.post_save_hook)
    os_path_list = []
    jupyter_config_obj.FileContentsManager.run_post_save_hook(
        model=None, os_path=os_path_list)
    assert os_path_list == ["old_post_save", "nbautoexport"]
Example #5
0
def test_initialize_post_save_import_error_caught(monkeypatch):
    """Test that bound post_save hook with given signature can be successfully run."""

    jupyter_config_obj = Config(FileContentsManager=FileContentsManager())

    monkeypatch.delattr(nbautoexport_root, "post_save")

    # Expect: ImportError: cannot import name 'post_save' from 'nbautoexport'
    jupyter_config.initialize_post_save_hook(jupyter_config_obj)
Example #6
0
def test_initialize_post_save_execution(monkeypatch):
    """Test that bound post_save hook with given signature can be successfully run."""

    jupyter_config_obj = Config(FileContentsManager=FileContentsManager())

    def mocked_post_save(model, os_path, contents_manager):
        """Append a token to os_path to certify that function ran."""
        os_path.append("nbautoexport")

    monkeypatch.setattr(nbautoexport_root, "post_save", mocked_post_save)

    jupyter_config.initialize_post_save_hook(jupyter_config_obj)

    assert isinstance(jupyter_config_obj.FileContentsManager,
                      FileContentsManager)
    assert callable(jupyter_config_obj.FileContentsManager.post_save_hook)
    os_path_list = []
    jupyter_config_obj.FileContentsManager.run_post_save_hook(
        model=None, os_path=os_path_list)
    assert os_path_list == ["nbautoexport"]
Example #7
0
def test_initialize_post_save_double_import_error_caught(
        monkeypatch, caplog, capsys, jupyter_app):
    """Test that both missing nbautoexport error and missing jupyer_core are caught and properly
    logged."""

    real_import = __builtins__["__import__"]

    def mock_import(name, globals=None, locals=None, fromlist=(), level=0):
        if name == "nbautoexport":
            raise ModuleNotFoundError("No module named 'nbautoexport'")
        if name == "jupyter_core.application":
            raise ModuleNotFoundError(
                "No module named 'jupyter_core.application'")
        return real_import(name=name,
                           globals=globals,
                           locals=locals,
                           fromlist=fromlist,
                           level=level)

    monkeypatch.setattr(builtins, "__import__", mock_import)
    monkeypatch.delitem(sys.modules, "nbautoexport")
    monkeypatch.delitem(sys.modules, "jupyter_core.application")

    jupyter_config_obj = Config(FileContentsManager=FileContentsManager())

    # Initialize post_save hook
    # Should run through since error is caught
    jupyter_config.initialize_post_save_hook(jupyter_config_obj)

    # Caplog should be empty, since logger didn't work
    assert len(caplog.record_tuples) == 0

    # Errors should be in stderr
    captured = capsys.readouterr()
    assert "ModuleNotFoundError: No module named 'jupyter_core.application'" in captured.err
    assert "ModuleNotFoundError: No module named 'nbautoexport'" in captured.err
Example #8
0
def file_contents_manager(notebook_file):
    config = Config(FileContentsManager=FileContentsManager())
    config.FileContentsManager.root_dir = str(notebook_file.parent)
    initialize_post_save_hook(config)
    return config.FileContentsManager