Validate path arg, fix non-cwd resolution, refactor _fix_file#7
Merged
Conversation
- Reject non-directory paths up front via `parser.error` instead of letting `pathspec` raise an obscure error later (#6). - Open files `rb` in `--check` mode so read-only files don't raise `OSError`; the write paths in `_fix_file` were already guarded (#8). - Resolve walked filenames against the supplied `path`, not cwd. Previously `eof-fixer /some/dir` from elsewhere raised `FileNotFoundError` on every file. - Split `_fix_file` into `_detect_trailing` (pure inspection) + dispatch, dropping `# noqa: C901, PLR0911` (#10). - README: note that an appended terminator is always LF regardless of the file's existing CRLF/CR style (#5). - Add tests for CRLF / CR / BOM / late null byte / symlinks / read-only-in-check / arg validation / non-cwd invocation, all encoding current behavior (#9). Coverage stays at 100%. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Second round of code-review followups.
Summary
match_tree_filesyields paths relative to the suppliedpath, but the code opened them relative to cwd. Runningeof-fixer /some/other/dirfrom anywhere except inside that dir raisedFileNotFoundError. The existing test suite masked this because it alwaysos.chdir(tempdir)before invoking. Resolve withpath / filename. Adds a regression test.pathwas declared "path to directory" but never validated. Passing a file raised an opaquepathspecerror; passing a nonexistent path raisedFileNotFoundError. Now exits viaparser.error("path is not a directory: <p>")with code 2.--checkmode (require 100% cov #8): every file was openedrb+even in check mode, which blew up on read-only files for no reason (no writes were going to happen). Now opensrbwhencheck=True. Adds a test that chmods a file to0o444and verifies--checkreports it without raising._fix_filecarried# noqa: C901, PLR0911because it did binary detection, end-of-file inspection, and writing all in one body. Split into_detect_trailing(file_obj) -> tuple[str, int](pure inspection — returns("none", 0)/("append_lf", 0)/("truncate", offset)) and a small dispatcher in_fix_file. Both suppressions removed. No behavior change.pre-commit-hooksbehavior.--check, and the non-cwd regression. Factored a small_run_main_in()context manager that handles cwd/argv/stdout/stderr swapping; existing tests left untouched. Coverage stays at 100%.No public API or output-format changes.
Test plan
just lint— ruff format, ruff check, ty — all pass with no new suppressions.just test— 15/15 pass, 100% coverage oneof_fixer/main.pyand tests.grep -nE "noqa: (C901 |PLR0911)" eof_fixer/main.py— empty.eof-fixer pyproject.toml→ exit 2, stderr "path is not a directory: …".eof-fixer /nonexistent→ exit 2, stderr "path is not a directory: …".tmp=$(mktemp -d) && printf a > "$tmp/ro.txt" && chmod 444 "$tmp/ro.txt" && eof-fixer "$tmp" --check→ printsFixing ro.txt, exit 1, no traceback.🤖 Generated with Claude Code