Skip to content

refactor: reduce duplication in MockFile.pm#284

Open
Koan-Bot wants to merge 1 commit intocpanel:masterfrom
atoomic:koan.atoomic/refactor-readability
Open

refactor: reduce duplication in MockFile.pm#284
Koan-Bot wants to merge 1 commit intocpanel:masterfrom
atoomic:koan.atoomic/refactor-readability

Conversation

@Koan-Bot
Copy link
Contributor

What

Extract shared patterns and remove dead code in lib/Test/MockFile.pm to improve maintainability.

Why

The 4188-line main module had three identical mock-construction blocks (20+ lines each) and 50 instances of a two-line autodie check-and-throw pattern. Any change to mock defaults required synchronizing three locations — a maintenance trap. The review also found a dead variable and a POD typo.

How

  • _default_mock_attrs(): centralizes the default attribute hash used by new(), _create_file_through_broken_symlink(), and _maybe_autovivify().
  • _new_nonexistent_file_mock($path): shared constructor for the two internal sites that create placeholder mocks (broken-symlink targets and autovivified files). Reduces 40 lines to 2 call sites.
  • _maybe_throw_autodie($func, @args): combines _caller_has_autodie_for() + _throw_autodie() into a single call, replacing 50 two-line patterns.
  • Removed dead $set_error variable in __chown (declared but never set).
  • Fixed CAEATSCAVEATS typo in POD.

Skipped extracting the CORE::open fallback dispatch — @_ aliasing constraints prevent clean extraction (documented in project learnings).

Testing

Full local test suite: 86 files, 1457 tests — all pass.
Net change: -23 lines (110 insertions, 133 deletions).

🤖 Generated with Claude Code

Extract _default_mock_attrs() and _new_nonexistent_file_mock() to
centralize mock object construction defaults that were duplicated
across new(), _create_file_through_broken_symlink(), and
_maybe_autovivify(). Changes to defaults now only need to happen
in one place.

Add _maybe_throw_autodie() helper that combines the check-and-throw
pattern (_throw_autodie + _caller_has_autodie_for) into a single
call, reducing 50 two-line boilerplate patterns to one-liners.

Remove dead $set_error variable in __chown — it was declared but
never set to true, so the conditional guards were always no-ops.

Fix CAEATS typo in POD (now CAVEATS).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@atoomic atoomic marked this pull request as ready for review March 12, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants