Beispiel #1
0
def test_export_with_config_with_cli_opts(notebooks_dir, input_type, organize_by):
    """Test that export works with both config and CLI options. CLI options should overide config.
    """
    expected_notebooks = find_notebooks(notebooks_dir)
    assert len(expected_notebooks) == len(EXPECTED_NOTEBOOKS)

    if input_type == "dir":
        expected_to_convert = expected_notebooks
        input_path = str(notebooks_dir)
    elif input_type == "notebook":
        expected_to_convert = expected_notebooks[:1]
        input_path = str(expected_notebooks[0].path)

    sentinel_path = notebooks_dir / SAVE_PROGRESS_INDICATOR_FILE
    written_config = NbAutoexportConfig()
    with sentinel_path.open("w") as fp:
        fp.write(written_config.json())

    expected_config = NbAutoexportConfig(export_formats=EXPECTED_FORMATS, organize_by=organize_by)
    assert expected_config != written_config

    flags = list(chain(["-b", organize_by], *(["-f", fmt] for fmt in EXPECTED_FORMATS)))
    result = CliRunner().invoke(app, ["export", input_path] + flags)
    assert result.exit_code == 0

    expected_notebook_files = {nb.path for nb in expected_notebooks}
    expected_exports = set(get_expected_exports(expected_to_convert, expected_config))

    expected_exports_from_written = set(get_expected_exports(expected_to_convert, written_config))
    assert expected_exports != expected_exports_from_written

    all_expected = expected_notebook_files | expected_exports | {sentinel_path}
    assert set(notebooks_dir.glob("**/*")) == all_expected
Beispiel #2
0
def test_clean_relative(notebooks_dir, organize_by):
    """ Test that cleaning works relative to current working directory.
    """
    with working_directory(notebooks_dir):
        sentinel_path = Path(SAVE_PROGRESS_INDICATOR_FILE)
        config = NbAutoexportConfig(export_formats=EXPECTED_FORMATS,
                                    organize_by=organize_by)
        with sentinel_path.open("w") as fp:
            fp.write(config.json())

        result = CliRunner().invoke(app, ["clean", "."], input="y")
        assert result.exit_code == 0

        expected_notebooks = [
            JupyterNotebook.from_file(f"{nb}.ipynb")
            for nb in EXPECTED_NOTEBOOKS
        ]
        expected_exports = set(get_expected_exports(expected_notebooks,
                                                    config))

        all_expected = {nb.path
                        for nb in expected_notebooks
                        } | expected_exports | {sentinel_path}
        assert set(Path().glob("**/*")) == all_expected

        # Run clean again, there should be nothing to do
        result_rerun = CliRunner().invoke(app, ["clean", "."])
        assert result_rerun.exit_code == 0
        assert result_rerun.stdout.strip().endswith(
            "No files identified for cleaning. Exiting.")
Beispiel #3
0
def test_clean(notebooks_dir, need_confirmation, organize_by):
    sentinel_path = notebooks_dir / SAVE_PROGRESS_INDICATOR_FILE
    config = NbAutoexportConfig(export_formats=EXPECTED_FORMATS,
                                organize_by=organize_by)
    with sentinel_path.open("w") as fp:
        fp.write(config.json())

    if need_confirmation:
        result = CliRunner().invoke(app, ["clean", str(notebooks_dir)],
                                    input="y")
    else:
        result = CliRunner().invoke(
            app, ["clean", str(notebooks_dir), "--yes"])
    assert result.exit_code == 0

    expected_notebooks = [
        JupyterNotebook.from_file(notebooks_dir / f"{nb}.ipynb")
        for nb in EXPECTED_NOTEBOOKS
    ]
    expected_exports = set(get_expected_exports(expected_notebooks, config))

    all_expected = {nb.path
                    for nb in expected_notebooks
                    } | expected_exports | {sentinel_path}
    assert set(notebooks_dir.glob("**/*")) == all_expected

    # Run clean again, there should be nothing to do
    result_rerun = CliRunner().invoke(app, ["clean", str(notebooks_dir)])
    assert result_rerun.exit_code == 0
    assert result_rerun.stdout.strip().endswith(
        "No files identified for cleaning. Exiting.")
Beispiel #4
0
def test_export_with_config_no_cli_opts(notebooks_dir, input_type, organize_by):
    """Test that export works with a config and no CLI options. Should use config options.
    """
    expected_notebooks = find_notebooks(notebooks_dir)
    assert len(expected_notebooks) == len(EXPECTED_NOTEBOOKS)

    if input_type == "dir":
        expected_to_convert = expected_notebooks
        input_path = str(notebooks_dir)
    elif input_type == "notebook":
        expected_to_convert = expected_notebooks[:1]
        input_path = str(expected_notebooks[0].path)

    sentinel_path = notebooks_dir / SAVE_PROGRESS_INDICATOR_FILE
    config = NbAutoexportConfig(export_formats=EXPECTED_FORMATS, organize_by=organize_by)
    with sentinel_path.open("w") as fp:
        fp.write(config.json())

    result = CliRunner().invoke(app, ["export", input_path])
    assert result.exit_code == 0

    expected_notebook_files = {nb.path for nb in expected_notebooks}
    expected_exports = set(get_expected_exports(expected_to_convert, config))

    all_expected = expected_notebook_files | expected_exports | {sentinel_path}
    assert set(notebooks_dir.glob("**/*")) == all_expected
Beispiel #5
0
def test_export_no_config_with_cli_opts(notebooks_dir, input_type, organize_by):
    """Test export command with no config file and CLI options. Should use CLI options.
    """
    expected_notebooks = find_notebooks(notebooks_dir)
    assert len(expected_notebooks) == len(EXPECTED_NOTEBOOKS)

    if input_type == "dir":
        expected_to_convert = expected_notebooks
        input_path = str(notebooks_dir)
    elif input_type == "notebook":
        expected_to_convert = expected_notebooks[:1]
        input_path = str(expected_notebooks[0].path)

    flags = list(chain(["-b", organize_by], *(["-f", fmt] for fmt in EXPECTED_FORMATS)))
    result = CliRunner().invoke(app, ["export", input_path] + flags)
    assert result.exit_code == 0

    assert set(EXPECTED_FORMATS) != set(DEFAULT_EXPORT_FORMATS)  # make sure test is meaningful

    expected_config = NbAutoexportConfig(export_formats=EXPECTED_FORMATS, organize_by=organize_by)
    expected_notebook_files = {nb.path for nb in expected_notebooks}
    expected_exports = set(get_expected_exports(expected_to_convert, expected_config))

    all_expected = expected_notebook_files | expected_exports
    assert set(notebooks_dir.glob("**/*")) == all_expected
Beispiel #6
0
def test_clean_relative_subdirectory(notebooks_dir, input_type, organize_by):
    """ Test that export works for subdirectory relative to current working directory.
    """
    with working_directory(notebooks_dir):
        # Set up subdirectory
        subdir = Path("subdir")
        subdir.mkdir()
        for subfile in Path().iterdir():
            shutil.move(str(subfile), str(subdir))

        sentinel_path = subdir / SAVE_PROGRESS_INDICATOR_FILE
        config = NbAutoexportConfig(export_formats=EXPECTED_FORMATS, organize_by=organize_by)
        with sentinel_path.open("w") as fp:
            fp.write(config.json())

        expected_notebooks = find_notebooks(subdir)
        assert len(expected_notebooks) == len(EXPECTED_NOTEBOOKS)

        if input_type == "dir":
            expected_to_convert = expected_notebooks
            input_path = "subdir"
        elif input_type == "notebook":
            expected_to_convert = expected_notebooks[:1]
            input_path = str(subdir / f"{expected_notebooks[0].path.name}")

        result = CliRunner().invoke(app, ["export", input_path])
        assert result.exit_code == 0

        expected_notebook_files = {nb.path for nb in expected_notebooks}
        expected_exports = set(get_expected_exports(expected_to_convert, config))

        all_expected = expected_notebook_files | expected_exports | {sentinel_path}
        assert set(subdir.glob("**/*")) == all_expected
Beispiel #7
0
def test_export_relative(notebooks_dir, input_type, organize_by):
    """ Test that export works relative to current working directory.
    """
    with working_directory(notebooks_dir):
        expected_notebooks = find_notebooks(Path())
        assert len(expected_notebooks) == len(EXPECTED_NOTEBOOKS)

        sentinel_path = Path(SAVE_PROGRESS_INDICATOR_FILE)
        config = NbAutoexportConfig(export_formats=EXPECTED_FORMATS, organize_by=organize_by)
        with sentinel_path.open("w") as fp:
            fp.write(config.json())

        if input_type == "dir":
            expected_to_convert = expected_notebooks
            input_path = "."
        elif input_type == "notebook":
            expected_to_convert = expected_notebooks[:1]
            input_path = f"{expected_notebooks[0].path.name}"

        result = CliRunner().invoke(app, ["export", input_path])
        assert result.exit_code == 0

        expected_notebook_files = {nb.path for nb in expected_notebooks}
        expected_exports = set(get_expected_exports(expected_to_convert, config))

        all_expected = expected_notebook_files | expected_exports | {sentinel_path}
        assert set(Path().glob("**/*")) == all_expected
def test_clean_exclude(notebooks_dir):
    """Test that cleaning works with exclude"""
    with working_directory(notebooks_dir):
        # Set up subdirectory
        subdir = Path("subdir")
        subdir.mkdir()
        for subfile in Path().iterdir():
            shutil.move(str(subfile), str(subdir))

        # Set up extra files
        extra_files = [
            subdir / "keep.txt",
            subdir / "delete.txt",
            subdir / "images" / "keep.jpg",
            subdir / "images" / "delete.png",
            subdir / "pictures" / "delete.jpg",
            subdir / "keep.md",
            subdir / "docs" / "keep.md",
        ]
        for extra_file in extra_files:
            extra_file.parent.mkdir(exist_ok=True)
            extra_file.touch()

        sentinel_path = subdir / SAVE_PROGRESS_INDICATOR_FILE
        config = NbAutoexportConfig(
            export_formats=EXPECTED_FORMATS,
            organize_by="extension",
            clean=CleanConfig(exclude=["keep.txt", "images/*.jpg"]),
        )
        with sentinel_path.open("w", encoding="utf-8") as fp:
            fp.write(config.json())

        command = ["clean", "subdir", "-e", "**/*.md"]
        result = CliRunner().invoke(app, command, input="y")
        assert result.exit_code == 0

        expected_notebooks = [
            JupyterNotebook.from_file(subdir / f"{nb}.ipynb")
            for nb in EXPECTED_NOTEBOOKS
        ]
        expected_exports = set(get_expected_exports(expected_notebooks,
                                                    config))
        expected_extra = {
            extra_file
            for extra_file in extra_files if extra_file.match("keep.*")
        } | {subdir / "images", subdir / "docs"}

        all_expected = ({nb.path
                         for nb in expected_notebooks}
                        | expected_exports
                        | {sentinel_path}
                        | expected_extra)
        assert set(subdir.glob("**/*")) == all_expected

        # Run clean again, there should be nothing to do
        result_rerun = CliRunner().invoke(app, command)
        assert result_rerun.exit_code == 0
        assert result_rerun.stdout.strip().endswith(
            "No files identified for cleaning. Exiting.")
Beispiel #9
0
def test_export_no_config_no_cli_opts(notebooks_dir, input_type):
    """Test export command with no config file and no CLI options. Should use default options.
    """
    expected_notebooks = find_notebooks(notebooks_dir)
    assert len(expected_notebooks) == len(EXPECTED_NOTEBOOKS)

    if input_type == "dir":
        expected_to_convert = expected_notebooks
        input_path = str(notebooks_dir)
    elif input_type == "notebook":
        expected_to_convert = expected_notebooks[:1]
        input_path = str(expected_notebooks[0].path)

    result = CliRunner().invoke(app, ["export", input_path])
    assert result.exit_code == 0

    expected_notebook_files = {nb.path for nb in expected_notebooks}
    expected_exports = set(get_expected_exports(expected_to_convert, NbAutoexportConfig()))

    all_expected = expected_notebook_files | expected_exports
    assert set(notebooks_dir.glob("**/*")) == all_expected