Skip to content

Comments

feat: add :trace import tag for logging file accesses#217

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

feat: add :trace import tag for logging file accesses#217
Koan-Bot wants to merge 3 commits intocpanel:masterfrom
atoomic:koan.atoomic/fix-issue-9

Conversation

@Koan-Bot
Copy link
Contributor

Summary

Adds a :trace import tag that installs a built-in file access hook logging all unmocked file operations to STDERR. This helps developers discover which files their code touches during tests, so they know what to mock.

Fixes #9

Changes

  • Add :trace (and trace) import tag that enables trace mode
  • Trace hook logs [trace] open('/path') at file.pl line N to STDERR for every unmocked file operation
  • Hook is installed before _strict_mode_violation so it fires even when strict mode will die
  • Add :nostrict as a tag-style alias for nostrict (as suggested in the issue)
  • Uses print STDERR instead of warn to avoid triggering Test2::Plugin::NoWarnings
  • POD documentation for the new feature
  • New test file t/trace.t covering: trace with strict, trace with nostrict, colon and non-colon syntax, caller location in output, mocked files not traced, open tracing

Test plan

  • t/trace.t — tests trace output via subprocess execution (tmf_test_code) verifying STDERR output
  • t/import.t — existing import tests should still pass (no behavioral change to existing tags)

Generated by Kōan /fix

Koan-Bot and others added 2 commits February 23, 2026 19:42
Add a trace mode that logs all unmocked file access operations to STDERR,
helping developers discover which files their code touches during tests.

    use Test::MockFile qw< :trace >;
    use Test::MockFile qw< :trace :nostrict >;

The trace hook is installed as an internal hook before _strict_mode_violation,
so it logs the access even when strict mode will subsequently die.

Also adds :nostrict as a tag-style alias for the existing nostrict import.

Closes cpanel#9

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
$TRACE_ENABLED, @_internal_access_hooks, and @_public_access_hooks were
declared at line ~1657 but referenced at line ~627 in import(). Perl
compilation fails because my-declared variables are only visible from
the point of declaration onwards. Move them before import().

Fixes CI compilation error:
  Global symbol "$TRACE_ENABLED" requires explicit package name

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@atoomic
Copy link
Contributor

atoomic commented Feb 24, 2026

Failure from CI

    # Failed test 'Exit Value Check'
    # at /home/runner/work/Test-MockFile/Test-MockFile/t/./lib/Test/TMF.pm line 87.
    # +-------+----+-------+
    # | GOT   | OP | CHECK |
    # +-------+----+-------+
    # | 65280 | eq | 0     |
    # +-------+----+-------+
    (If this table is too small, you can use the TABLE_TERM_SIZE=### env var to set a larger size, detected size is '74')

    # Command = /usr/bin/perl -I/home/runner/work/Test-MockFile/Test-MockFile/t/../lib/ /tmp/QSw4itglhG/FmGtLV9l8w
    # Exit = 65280
    # ==== Output ====
    # [trace] stat('/no/such/trace/loc_test') at /tmp/QSw4itglhG/FmGtLV9l8w line 10
    # 1..0
    # # No tests run!
    # # Seeded srand with seed '20260224' from local date.
    # 
    # ========

# Failed test 'trace output includes caller location'
# at /home/runner/work/Test-MockFile/Test-MockFile/t/./lib/Test/TMF.pm line 87.
    # Failed test 'Exit Value Check'
    # at /home/runner/work/Test-MockFile/Test-MockFile/t/./lib/Test/TMF.pm line 87.
    # +-------+----+-------+
    # | GOT   | OP | CHECK |
    # +-------+----+-------+
    # | 65280 | eq | 0     |
    # +-------+----+-------+
    (If this table is too small, you can use the TABLE_TERM_SIZE=### env var to set a larger size, detected size is '74')

    # Command = /usr/bin/perl -I/home/runner/work/Test-MockFile/Test-MockFile/t/../lib/ /tmp/QSw4itglhG/U3JKrCnepH
    # Exit = 65280
    # ==== Output ====
    # [trace] open('/no/such/trace/openfile') at /tmp/QSw4itglhG/U3JKrCnepH line 10
    # 1..0
    # # No tests run!
    # # Seeded srand with seed '20260224' from local date.
    # 
    # ========

# Failed test 'trace fires for open on unmocked files'
# at /home/runner/work/Test-MockFile/Test-MockFile/t/./lib/Test/TMF.pm line 87.
# Seeded srand with seed '20260224' from local date.
t/trace.t ........................... 
Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/7 subtests 
t/unlink.t .......................... ok
t/writeline.t ....................... ok

Test Summary Report
-------------------
t/trace.t                         (Wstat: 512 (exited 2) Tests: 7 Failed: 2)
  Failed tests:  5, 7
  Non-zero exit status: 2

@atoomic
Copy link
Contributor

atoomic commented Feb 24, 2026

@Koan-Bot rebase

@Koan-Bot
Copy link
Contributor Author

Rebase: feat: add :trace import tag for logging file accesses

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

Actions

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

Automated by Kōan

@atoomic atoomic marked this pull request as ready for review February 24, 2026 21:01
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.

Trace mode

2 participants