Skip to content

Comments

feat: add rename() override and autovivify for directory mocks (#135)#222

Open
Koan-Bot wants to merge 3 commits intocpanel:masterfrom
atoomic:koan.atoomic/fix-issue-135
Open

feat: add rename() override and autovivify for directory mocks (#135)#222
Koan-Bot wants to merge 3 commits intocpanel:masterfrom
atoomic:koan.atoomic/fix-issue-135

Conversation

@Koan-Bot
Copy link
Contributor

@Koan-Bot Koan-Bot commented Feb 24, 2026

What

Add rename() override for mocked files and an autovivify option for directory mocks that automatically creates mocked file entries for any file operation under the directory.

Why

Issue #135: the common pattern of writing to a temp file then renaming it into place wasn't possible with Test::MockFile. You had to pre-declare every file path. With autovivify, a single new_dir('/data', { autovivify => 1 }) covers all files created under that path.

How

  • rename() CORE::GLOBAL override: transfers contents, mode, ownership, timestamps between mocked paths. Proper errno for error cases.
  • autovivify option on dir() / new_dir(): registers the directory in a tracking hash. _maybe_autovivify() is called from _mock_stat, __open, __sysopen, __mkdir, and __rename to create on-demand file mocks.
  • Auto-vivified children hold strong refs from the parent — cleanup happens automatically on scope exit.

Testing

t/autovivify.t covers: basic open/write/read, temp+rename pattern, stat on non-existent, mkdir subdirectory, unlink, sysopen, scope cleanup, readdir, glob, and dir()+mkdir flow.

Closes #135

Koan-Bot and others added 2 commits February 24, 2026 12:56
Override CORE::GLOBAL::rename to intercept rename operations on mocked
files. This enables renaming between mocked paths without touching the
real filesystem.

Supports renaming files, symlinks, and empty directories. Properly
sets errno for error cases (ENOENT, EISDIR, ENOTDIR). Transfers
contents, mode, ownership, and timestamps from source to destination.

Prerequisite for the autovivify feature (issue cpanel#135) which enables
the common "write temp file, rename into place" pattern.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When autovivify is enabled on a dir() or new_dir() mock, any file
operation (open, sysopen, stat, mkdir, rename) on a path under that
directory automatically creates a mocked file entry. This supports
the common pattern of writing to a temp file then renaming into place.

Auto-vivified children are tied to the parent directory's lifetime
and cleaned up when it goes out of scope.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Koan-Bot Koan-Bot changed the title feat: add rename() override for mocked files feat: add rename() override and autovivify for directory mocks (#135) Feb 24, 2026
@atoomic
Copy link
Contributor

atoomic commented Feb 24, 2026

view failure from CI

#   Failed test 'POD test for blib/lib/Test/MockFile.pm'
#   at /usr/local/share/perl/5.38.2/Test/Pod.pm line 184.
Wide character in print at /usr/local/share/perl/5.38.2/Test2/Formatter/TAP.pm line 125.
# blib/lib/Test/MockFile.pm (838): Non-ASCII character seen before =encoding in '—'. Assuming UTF-8
# Looks like you failed 1 test of 6.
t/pod.t ............................. 

@atoomic
Copy link
Contributor

atoomic commented Feb 24, 2026

@Koan-Bot rebase

@Koan-Bot
Copy link
Contributor Author

❌ Permission denied. Only users with write access can trigger bot commands.

@Koan-Bot
Copy link
Contributor Author

Rebase: feat: add rename() override and autovivify for directory mocks (#135)

Branch koan.atoomic/fix-issue-135 has been rebased onto master and force-pushed.

Actions

  • Read PR comments and review feedback
  • Rebased koan.atoomic/fix-issue-135 onto origin/master
  • Applied review feedback
  • Force-pushed koan.atoomic/fix-issue-135 to origin

Automated by Kōan

@Koan-Bot
Copy link
Contributor Author

CI is green across all 18 Perl versions. The POD encoding issue (em-dash before =encoding) was resolved in the rebase.

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.

Provide a way to mock a whole dir or tree.

2 participants