def test_apply_black_on_python_notebooks(nb_file, tmpdir): tmp_ipynb = str(tmpdir.join('notebook.ipynb')) tmp_py = str(tmpdir.join('notebook.py')) copyfile(nb_file, tmp_ipynb) jupytext(args=[tmp_ipynb, '--to', 'py:percent']) system('black', tmp_py) jupytext(args=[tmp_py, '--to', 'ipynb', '--update']) nb1 = readf(nb_file) nb2 = readf(tmp_ipynb) nb3 = readf(tmp_py) assert len(nb1.cells) == len(nb2.cells) assert len(nb1.cells) == len(nb3.cells) for c1, c2 in zip(nb1.cells, nb2.cells): # same content (almost) assert black_invariant(c1.source) == black_invariant(c2.source) # python representation is pep8 assert 'lines_to_next_cell' not in c2.metadata # outputs are preserved assert c1.cell_type == c2.cell_type if c1.cell_type == 'code': compare(c1.outputs, c2.outputs) compare(nb1.metadata, nb2.metadata)
def test_apply_black_on_python_notebooks(nb_file, tmpdir): tmp_ipynb = str(tmpdir.join("notebook.ipynb")) tmp_py = str(tmpdir.join("notebook.py")) copyfile(nb_file, tmp_ipynb) jupytext(args=[tmp_ipynb, "--to", "py:percent"]) system("black", tmp_py) jupytext(args=[tmp_py, "--to", "ipynb", "--update"]) nb1 = read(nb_file) nb2 = read(tmp_ipynb) nb3 = read(tmp_py) assert len(nb1.cells) == len(nb2.cells) assert len(nb1.cells) == len(nb3.cells) for c1, c2 in zip(nb1.cells, nb2.cells): # same content (almost) assert black_invariant(c1.source) == black_invariant(c2.source) # python representation is pep8 assert "lines_to_next_cell" not in c2.metadata # outputs are preserved assert c1.cell_type == c2.cell_type if c1.cell_type == "code": compare(c1.outputs, c2.outputs) compare(nb1.metadata, nb2.metadata)
def test_execute_readme_and_update_index_html(): nb_path = os.path.dirname(os.path.abspath(__file__)) readme = os.path.join(nb_path, '..', 'README.md') readme_ipynb = os.path.join(nb_path, '..', 'README.ipynb') index = os.path.join(nb_path, '..', 'index.html') jupytext_cli([readme, '--execute', '--to', 'ipynb']) system('jupyter', 'nbconvert', '--to', 'html', readme_ipynb, '--output', index)
def test_execute_readme_and_update_index_html(): nb_path = os.path.dirname(os.path.abspath(__file__)) readme = os.path.join(nb_path, "..", "README.md") index = os.path.join(nb_path, "..", "index.html") jupytext_cli([readme, "--execute", "--output", "README.ipynb"]) system("jupyter", "nbconvert", "--to", "html", "README.ipynb") os.remove("README.ipynb") if not os.path.isfile(index): os.rename("README.html", index)
def test_cli_expect_errors(tmp_ipynb): with pytest.raises(ValueError): jupytext([]) with pytest.raises(ValueError): jupytext(['--sync']) with pytest.raises(ValueError): jupytext([tmp_ipynb, tmp_ipynb, '--paired-paths']) with pytest.raises(ValueError): jupytext(['--pre-commit', 'notebook.ipynb']) with pytest.raises(ValueError): jupytext(['notebook.ipynb', '--from', 'py:percent', '--to', 'md']) with pytest.raises(SystemExit): jupytext_cli([]) with pytest.raises((SystemExit, TypeError)): # SystemExit on Windows, TypeError on Linux system('jupytext', ['notebook.ipynb', '--from', 'py:percent', '--to', 'md'])
def tool_version(tool): try: args = tool.split(" ") args.append("--version") return system(*args) except (OSError, SystemExit): # pragma: no cover return None
def test_cli_expect_errors(tmp_ipynb): with pytest.raises(ValueError): jupytext([]) with pytest.raises(ValueError): jupytext(["--sync"]) with pytest.raises(ValueError): jupytext([tmp_ipynb, tmp_ipynb, "--paired-paths"]) with pytest.raises(ValueError): jupytext(["--pre-commit", "notebook.ipynb"]) with pytest.raises(ValueError): jupytext(["notebook.ipynb", "--from", "py:percent", "--to", "md"]) with pytest.raises(ValueError): jupytext([]) with pytest.raises( (SystemExit, TypeError)): # SystemExit on Windows, TypeError on Linux system("jupytext", ["notebook.ipynb", "--from", "py:percent", "--to", "md"])
def test_pre_commit_hook_for_existing_changed_file(tmpdir): # get the path and revision of this repo, to use with pre-commit repo_root = str(Path(__file__).parent.parent.resolve()) repo_rev = system("git", "rev-parse", "HEAD", cwd=repo_root).strip() git = git_in_tmpdir(tmpdir) # set up the tmpdir repo with pre-commit pre_commit_config_yaml = dedent(f""" repos: - repo: {repo_root} rev: {repo_rev} hooks: - id: jupytext args: [--from, ipynb, --to, "py:light"] """) tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml) git("add", ".pre-commit-config.yaml") with tmpdir.as_cwd(): pre_commit(["install", "--install-hooks"]) # write test notebook and output file nb = new_notebook(cells=[new_markdown_cell("A short notebook")]) nb_file = str(tmpdir.join("test.ipynb")) write(nb, nb_file) py_file = str(tmpdir.join("test.py")) jupytext(["--from", "ipynb", "--to", "py:light", str(nb_file)]) git("add", ".") git("commit", "-m", "test") # make a change to the notebook nb = new_notebook(cells=[new_markdown_cell("Some other text")]) write(nb, nb_file) git("add", nb_file) # now a commit will fail, and keep failing until we add the new # changes made to the existing output to the index ourselves with pytest.raises(SystemExit): git("commit", "-m", "fails") with pytest.raises(SystemExit): git("commit", "-m", "fails again") # once we add the changes, it will pass git("add", py_file) git("commit", "-m", "succeeds") assert "test.ipynb" in git("ls-files") assert "test.py" in git("ls-files")
def test_pre_commit_hook_for_new_file(tmpdir): # get the path and revision of this repo, to use with pre-commit repo_root = str(Path(__file__).parent.parent.resolve()) repo_rev = system("git", "rev-parse", "HEAD", cwd=repo_root).strip() # set up the tmpdir repo with pre-commit git = git_in_tmpdir(tmpdir) pre_commit_config_yaml = dedent(f""" repos: - repo: {repo_root} rev: {repo_rev} hooks: - id: jupytext args: [--sync] """) tmpdir.join(".pre-commit-config.yaml").write(pre_commit_config_yaml) git("add", ".pre-commit-config.yaml") with tmpdir.as_cwd(): pre_commit(["install", "--install-hooks", "-f"]) # write test notebook and sync it to py:percent nb = new_notebook(cells=[new_markdown_cell("A short notebook")]) nb_file = str(tmpdir.join("test.ipynb")) py_file = str(tmpdir.join("test.py")) write(nb, nb_file) with tmpdir.as_cwd(): jupytext(["--set-formats", "ipynb,py:percent", nb_file]) # try to commit it, should fail since the hook runs and makes changes git("add", nb_file) with pytest.raises(SystemExit): git("commit", "-m", "failing") # try again, it will still fail because the output hasn't been added with pytest.raises(SystemExit): git("commit", "-m", "still failing") # add the new file, now the commit will succeed git("add", py_file) git("commit", "-m", "passing") assert "test.ipynb" in git("ls-files") assert "test.py" in git("ls-files")
def system_in_tmpdir(*args): return system(*args, cwd=str(tmpdir))
def git(*args): out = system("git", *args, cwd=str(tmpdir)) print(out) return out
def jupytext_repo_rev(jupytext_repo_root): """The local revision of this repo, to use in .pre-commit-config.yaml in tests""" return system("git", "rev-parse", "HEAD", cwd=jupytext_repo_root).strip()
def tool_version(tool): try: return system(tool, '--version') except (OSError, SystemExit): # pragma: no cover return None
def git(*args): print(system('git', *args, cwd=str(tmpdir)))