Skip to content

Commit 41342f0

Browse files
EliahKaganclaude
andcommitted
Add test that _rm_lock_files actually removes lock files
This test creates a real `.lock` file in the repository's `.git/` directory and asserts that `_rm_lock_files()` removes it. As of this commit (i.e. without the follow-up fix), the test fails: `_rm_lock_files` has been a silent no-op for several years because of a stale path expression in `_tc_lock_fpaths` just above it. The follow-up commit ("Fix stale path in `_tc_lock_fpaths`") corrects the path; with that fix in place this test passes. This is added test-first (failing-then-passing) so the regression guard is recorded as a real demonstration of the bug, the way `test/test_fixture_health.py` was added test-first earlier this year for the Cygwin safe.directory issue. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e196f44 commit 41342f0

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎test/test_repo.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,41 @@ def _rm_lock_files():
6262
rmfile(lfp)
6363

6464

65+
def test_rm_lock_files_actually_removes_lock_files():
66+
"""Verify ``_rm_lock_files`` actually finds and removes ``.lock`` files.
67+
68+
Regression test for a path bug introduced when the tests directory was
69+
moved from ``<repo>/git/test/`` to ``<repo>/test/`` in commit 0374d7cf
70+
(2020-07-12, "tests: move to root dir"; see also d5f0d487 for the
71+
related revert that landed alongside it). ``_tc_lock_fpaths`` above
72+
was originally written for a file two directory levels deep
73+
(``../../.git/...``); after the move it should have become
74+
``../.git/...``. The miscount caused :func:`glob.glob` to look in
75+
``<parent-of-repo>/.git/``, which usually does not exist, so the
76+
helper has been a silent no-op for several years.
77+
78+
This test depends on ``__file__`` resolving to the real
79+
``<repo>/test/test_repo.py`` at runtime (i.e. running the tests from
80+
the source tree, as CI does). When ``<repo>/.git`` is not a directory
81+
-- e.g. in a linked worktree where it is a gitfile -- the test is
82+
skipped, since the cleanup helper isn't meaningful in that context.
83+
"""
84+
repo_dot_git = osp.join(osp.dirname(osp.dirname(osp.abspath(__file__))), ".git")
85+
if not osp.isdir(repo_dot_git):
86+
pytest.skip(f"{repo_dot_git} is not a directory; cleanup helper isn't meaningful here")
87+
88+
canary = osp.join(repo_dot_git, "_test_rm_lock_files_canary.lock")
89+
open(canary, "w").close()
90+
try:
91+
_rm_lock_files()
92+
assert not osp.exists(canary), (
93+
f"_rm_lock_files() left {canary} in place; _tc_lock_fpaths is probably pointing outside the repo's .git"
94+
)
95+
finally:
96+
if osp.exists(canary):
97+
os.unlink(canary)
98+
99+
65100
class TestRepo(TestBase):
66101
def setUp(self):
67102
_rm_lock_files()

0 commit comments

Comments
 (0)