|
12 | 12 | import pytest |
13 | 13 | from click.testing import CliRunner |
14 | 14 |
|
15 | | -from jupyter_jcli import pair_baseline |
16 | 15 | from jupyter_jcli.cli import main |
17 | 16 | from jupyter_jcli.drift import DriftResult |
| 17 | +from jupyter_jcli import pair_baseline |
18 | 18 |
|
19 | 19 |
|
20 | 20 | # --------------------------------------------------------------------------- |
@@ -678,6 +678,67 @@ def test_pre_merge_updates_ref_so_following_post_does_not_conflict( |
678 | 678 | assert [cell.source for cell in nb_after.cells if cell.source.strip()] == ["x = 40"] |
679 | 679 |
|
680 | 680 |
|
| 681 | +class TestGcPairSyncRefsCLI: |
| 682 | + def test_dry_run_reports_without_deleting(self, git_repo: Path, monkeypatch: pytest.MonkeyPatch): |
| 683 | + py, _ipynb = _make_pair(git_repo, ["x = 1"], ["x = 1"]) |
| 684 | + _git(git_repo, "add", "nb.py") |
| 685 | + _git(git_repo, "commit", "-m", "init", env=_git_env(100)) |
| 686 | + |
| 687 | + monkeypatch.setenv("GIT_AUTHOR_DATE", "@150 +0000") |
| 688 | + monkeypatch.setenv("GIT_COMMITTER_DATE", "@150 +0000") |
| 689 | + assert pair_baseline.write_baseline(py, py.read_text(encoding="utf-8")) is True |
| 690 | + py.write_text(py.read_text(encoding="utf-8").replace("x = 1", "x = 2"), encoding="utf-8") |
| 691 | + _git(git_repo, "add", "nb.py") |
| 692 | + _git(git_repo, "commit", "-m", "new head", env=_git_env(200)) |
| 693 | + |
| 694 | + runner = CliRunner() |
| 695 | + monkeypatch.chdir(git_repo) |
| 696 | + result = runner.invoke(main, ["_hooks", "gc-pair-sync-refs", "--dry-run"], catch_exceptions=False) |
| 697 | + |
| 698 | + assert result.exit_code == 0 |
| 699 | + assert "would-remove" in (result.stderr or result.output) |
| 700 | + assert "removed 1, kept 0" in (result.stderr or result.output) |
| 701 | + refs = _git(git_repo, "for-each-ref", "refs/jcli/pair-sync/", "--format=%(refname)") |
| 702 | + assert refs.stdout.strip() != "" |
| 703 | + |
| 704 | + def test_cli_removes_orphan_ref(self, git_repo: Path, monkeypatch: pytest.MonkeyPatch): |
| 705 | + _git(git_repo, "commit", "--allow-empty", "-m", "init", env=_git_env(100)) |
| 706 | + ghost_path = git_repo / "ghost.py" |
| 707 | + monkeypatch.setenv("GIT_AUTHOR_DATE", "@150 +0000") |
| 708 | + monkeypatch.setenv("GIT_COMMITTER_DATE", "@150 +0000") |
| 709 | + assert pair_baseline.write_baseline(ghost_path, "# %%\nx = 1\n") is True |
| 710 | + |
| 711 | + runner = CliRunner() |
| 712 | + monkeypatch.chdir(git_repo) |
| 713 | + result = runner.invoke(main, ["_hooks", "gc-pair-sync-refs"], catch_exceptions=False) |
| 714 | + |
| 715 | + assert result.exit_code == 0 |
| 716 | + assert "remove" in (result.stderr or result.output) |
| 717 | + refs = _git(git_repo, "for-each-ref", "refs/jcli/pair-sync/", "--format=%(refname)") |
| 718 | + assert refs.stdout.strip() == "" |
| 719 | + |
| 720 | + def test_cli_removes_stale_head_older_ref(self, git_repo: Path, monkeypatch: pytest.MonkeyPatch): |
| 721 | + py, _ipynb = _make_pair(git_repo, ["x = 1"], ["x = 1"]) |
| 722 | + _git(git_repo, "add", "nb.py") |
| 723 | + _git(git_repo, "commit", "-m", "init", env=_git_env(100)) |
| 724 | + |
| 725 | + monkeypatch.setenv("GIT_AUTHOR_DATE", "@150 +0000") |
| 726 | + monkeypatch.setenv("GIT_COMMITTER_DATE", "@150 +0000") |
| 727 | + assert pair_baseline.write_baseline(py, py.read_text(encoding="utf-8")) is True |
| 728 | + py.write_text(py.read_text(encoding="utf-8").replace("x = 1", "x = 5"), encoding="utf-8") |
| 729 | + _git(git_repo, "add", "nb.py") |
| 730 | + _git(git_repo, "commit", "-m", "head newer", env=_git_env(200)) |
| 731 | + |
| 732 | + runner = CliRunner() |
| 733 | + monkeypatch.chdir(git_repo) |
| 734 | + result = runner.invoke(main, ["_hooks", "gc-pair-sync-refs"], catch_exceptions=False) |
| 735 | + |
| 736 | + assert result.exit_code == 0 |
| 737 | + assert "removed 1, kept 0" in (result.stderr or result.output) |
| 738 | + refs = _git(git_repo, "for-each-ref", "refs/jcli/pair-sync/", "--format=%(refname)") |
| 739 | + assert refs.stdout.strip() == "" |
| 740 | + |
| 741 | + |
681 | 742 | # --------------------------------------------------------------------------- |
682 | 743 | # --debug smoke tests for pair-drift-guard-pre and pair-drift-guard-post |
683 | 744 | # --------------------------------------------------------------------------- |
|
0 commit comments