|
| 1 | +# Test::MockFile |
| 2 | + |
| 3 | +Perl module for mocking file system operations in unit tests. Intercepts `stat`, `lstat`, `-X` operators, `open`, `sysopen`, `opendir`, and related calls so tests run without touching disk. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +- **Core**: `lib/Test/MockFile.pm` (~2530 lines) — main module, CORE::GLOBAL overrides, strict mode |
| 8 | +- **FileHandle**: `lib/Test/MockFile/FileHandle.pm` — tied file handle for mocked files |
| 9 | +- **DirHandle**: `lib/Test/MockFile/DirHandle.pm` — directory handle for mocked dirs |
| 10 | +- **Plugin system**: `lib/Test/MockFile/Plugin.pm`, `Plugins.pm`, `Plugin/FileTemp.pm` |
| 11 | +- **Key dependency**: `Overload::FileCheck` (XS) — enables `-X` operator interception |
| 12 | + |
| 13 | +## How It Works |
| 14 | + |
| 15 | +1. `Overload::FileCheck` hooks `-X` operators and `stat`/`lstat` via XS |
| 16 | +2. `CORE::GLOBAL::*` overrides intercept `open`, `sysopen`, `opendir`, `readdir`, etc. |
| 17 | +3. Mocked files stored in `%files_being_mocked` hash (path → blessed object, weakref) |
| 18 | +4. Strict mode (default ON) dies on unmocked file access — configurable via rules |
| 19 | + |
| 20 | +## Build & Test |
| 21 | + |
| 22 | +```bash |
| 23 | +perl Makefile.PL && make && make test |
| 24 | +``` |
| 25 | + |
| 26 | +Dependencies installed via `cpanfile` (for CI: `cpm install -g`). |
| 27 | + |
| 28 | +## Conventions |
| 29 | + |
| 30 | +- **Perl style**: `.perltidyrc` in repo root — run `perltidy` before committing |
| 31 | +- **POD**: `.podtidy-opts` — documentation inline in `.pm` files |
| 32 | +- **Minimum Perl**: 5.14 (code uses `goto` on CORE functions, available 5.16+; workaround for 5.14) |
| 33 | +- **Branch naming**: feature branches off `master` |
| 34 | + |
| 35 | +## CI |
| 36 | + |
| 37 | +- GitHub Actions: `.github/workflows/linux.yml` |
| 38 | +- Matrix: Perl 5.14–5.40 on `perldocker/perl-tester` |
| 39 | +- Env: `PERL_USE_UNSAFE_INC=0`, `AUTHOR_TESTING=1` |
| 40 | + |
| 41 | +## Key Internals |
| 42 | + |
| 43 | +- `_upgrade_barewords()` — converts bareword filehandles to typeglobs |
| 44 | +- `_find_file_or_fh()` — resolves paths/handles, follows symlinks (max depth 10) |
| 45 | +- `_abs_path_to_file()` — normalizes paths (strips `//`, `./`, trailing `/`) |
| 46 | +- `_strict_mode_violation()` — enforces strict mode with stack inspection |
| 47 | +- `_goto_is_available()` — detects Perl versions where `goto \&CORE::func` works |
| 48 | +- Strict rules: `@STRICT_RULES` array, evaluated in order (first match wins) |
| 49 | + |
| 50 | +## Open Issues (25) |
| 51 | + |
| 52 | +Notable: #158 (glob corruption), #112 (flock), #77 (seek/truncate/tell), #44 (autodie compat), #27 (two handles same file). Full list: https://github.com/cpanel/Test-MockFile/issues |
0 commit comments