Repository navigation
Commit d74c104
test(util): cover
`git.util.rmfile` exists so that a read-only file can be deleted on
Windows, where `os.remove` fails on files with the read-only attribute.
On Windows it calls `os.chmod` to clear that attribute before removing
the file. Nothing tested this. Before this change, the only callers of
`rmfile` were `LockFile._release_lock` and a lock-file helper in
`test/test_repo.py`, and no test called either of them on a read-only
file.
Add `TestRmfile.test_deletes_readonly_file`, alongside the analogous
`TestRmtree.test_deletes_dir_with_readonly_files`. Its initial state
comes from a new fixture script, `tests/fixtures/rmfile.sh`. The script
creates a file named `readonly` and removes its write permissions with
`chmod a-w`, as `tests/fixtures/cleanup.sh` does for its read-only
case. On Windows this should give the file the read-only attribute. The
test uses a copy of the cached fixture output, which keeps the file's
mode, so it needs no `execute=True`. The script is separate from
`tests/fixtures/rmtree.sh`, whose cases are keyed by `TestRmtree` test
names through the `tmp_path` fixture in `test/test_util.py`, and which
that fixture runs in place for all but one of them.
`rmfile` silently does nothing if the path is not a file, so the test
would pass vacuously if the fixture made no file. It therefore first
checks that the file exists and is not owner-writable, and only then
that `rmfile` removes it. On Windows, `os.stat` reports the read-only
attribute as the absence of write bits, so this also confirms that the
fixture produced the condition the test is about, rather than assuming
it.
The test runs on every platform, but it is only meaningful on native
Windows. Unlike the `rmtree` test, it is not skipped on Cygwin. That
skip's reason, that Cygwin can't set the needed permissions, hasn't
been checked for this test. On Cygwin `sys.platform` is `"cygwin"`, so
`rmfile` uses no special handling there, and the test only checks that
a read-only file can be removed, as on Unix.
This adds coverage ahead of changing the mode `rmfile` passes to
`os.chmod`: the test passes with the current `0o777`, and on Windows it
would fail if the mode lacked the owner-write bit.
Validation: on macOS with Python 3.12, the four `TestRmtree` and
`TestRmfile` tests pass, the copied fixture file has mode `0o444`, and
the precondition fails when the path does not exist. `shellcheck`
passes on the new script. Neither Windows nor Cygwin was run locally.
The `import-cygwin` workflow does not run the test suite, so Cygwin is
not tested in CI either.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>rmfile deleting a read-only file1 parent 38992bc commit d74c104
2 files changed
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
216 | 232 | | |
217 | 233 | | |
218 | 234 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
0 commit comments