Esempio n. 1
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.")
Esempio n. 2
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
Esempio n. 3
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
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.")
Esempio n. 5
0
def test_working_directory(tmp_path):
    cwd = Path.cwd()
    assert cwd != tmp_path
    with working_directory(tmp_path):
        assert Path.cwd() == tmp_path
    assert Path.cwd() == cwd