lib/Devel/App/Test/Generator/LCSAJ/Runtime.pm

Structural Coverage (Approximate)

TER1 (Statement): 89.66%
TER2 (Branch): 81.25%
TER3 (LCSAJ): 0.0% (0/2)
Approximate LCSAJ segments: 17

LCSAJ Legend

โ— Covered โ€” this LCSAJ path was executed during testing.

โ— Not covered โ€” this LCSAJ path was never executed. These are the paths to focus on.

Multiple dots on a line indicate that multiple control-flow paths begin at that line. Hovering over any dot shows:

        start โ†’ end โ†’ jump
        

Uncovered paths show [NOT COVERED] in the tooltip.

Mutant Testing Legend

Survived (tests missed this) Killed (tests detected this) No mutation
    1: package Devel::App::Test::Generator::LCSAJ::Runtime;
    2: 
    3: use strict;
    4: use warnings;
    5: use autodie     qw(open close);
    6: use Carp        qw(croak);
    7: use Config;
    8: use Cwd         qw(abs_path);
    9: use JSON::MaybeXS;
   10: use File::Path  qw(make_path);
   11: use Readonly;
   12: 
   13: # --------------------------------------------------
   14: # Output directory for per-process hit JSON files.
   15: # One file is written per process (PID) so parallel
   16: # test runs do not overwrite each other's output.
   17: # --------------------------------------------------
   18: Readonly my $OUT_DIR => 'cover_html/lcsaj_hits';
   19: 
   20: =head1 NAME
   21: 
   22: Devel::App::Test::Generator::LCSAJ::Runtime - Debugger backend for LCSAJ coverage
   23: 
   24: =head1 VERSION
   25: 
   26: Version 0.52
   27: 
   28: =cut
   29: 
   30: our $VERSION = '0.52';
   31: 
   32: =head1 SYNOPSIS
   33: 
   34:   PERL5OPT='-d:App::Test::Generator::LCSAJ::Runtime -Mblib' prove -l t
   35: 
   36: =head1 DESCRIPTION
   37: 
   38: This module is loaded as a Perl debugger backend using the C<-d:Module> flag.
   39: 
   40: When Perl sees C<-d:App::Test::Generator::LCSAJ::Runtime> it prepends C<Devel::>
   41: and loads C<Devel/App/Test/Generator/LCSAJ/Runtime.pm> from C<@INC>.
   42: The file must therefore live at that path - typically C<lib/Devel/App/Test/Generator/LCSAJ/Runtime.pm>.
   43: 
   44: Perl automatically calls C<DB::DB> before executing each statement while the
   45: debugger is active. We record (file, line) pairs to build runtime hit data for
   46: later LCSAJ analysis.
   47: 
   48: Results are written to C<cover_html/lcsaj_hits/hits_PID.json> at process exit,
   49: one file per process so that parallel test runs do not overwrite each other.
   50: 
   51: =head1 ENVIRONMENT
   52: 
   53: =over 4
   54: 
   55: =item LCSAJ_TARGETS
   56: 
   57: Optional list of B<absolute> paths (as produced by C<realpath>), separated by
   58: C<$Config{path_sep}> (C<:> on Unix, C<;> on Windows, as in C<PATH>), to
   59: restrict recording to specific source files. When empty or unset every
   60: non-internal file is recorded.
   61: 
   62: =back
   63: 
   64: =cut
   65: 
   66: # --------------------------------------------------
   67: # %HITS       - { normalised_path => { line_number => hit_count } }
   68: # %TARGET     - set of normalised paths to record (empty means record everything)
   69: # %NORM_CACHE - { raw_file => normalised_path }, memoises abs_path()
   70: #               since DB::DB sees the same $file on every consecutive
   71: #               statement within a source file
   72: #
   73: # These must be package globals (our) rather than lexicals because DB::DB
   74: # is called by the Perl debugger infrastructure and needs to access them
   75: # without a closure. Lexical vars would not be visible in DB::DB.
   76: # --------------------------------------------------
   77: our %HITS;
   78: our %TARGET;
   79: our %NORM_CACHE;
   80: 
   81: # --------------------------------------------------
   82: # Populate %TARGET from LCSAJ_TARGETS at compile time.
   83: # The env var contains absolute realpath() output
   84: # separated by $Config{path_sep}; a colon would split
   85: # a Windows drive letter off its path. Stray newlines
   86: # from broken shell pipelines are stripped defensively.
   87: # --------------------------------------------------
   88: BEGIN {
โ—[NOT COVERED] 89 โ†’ 92 โ†’ 0   89: 	my $targets_env = $ENV{LCSAJ_TARGETS} // '';
   90: 	$targets_env =~ s/\n//g;
   91: 
   92: 	for my $t (split /\Q$Config{path_sep}\E/, $targets_env) {
   93: 		next unless length $t;
   94: 
   95: 		# Inline normalisation — cannot call _normalize here since
   96: 		# BEGIN runs before named subs are compiled when BEGIN
   97: 		# appears at the top of the file
   98: 		my $f = $t;
   99: 		$f =~ tr{\\}{/};
  100: 		$f =~ s{^.*/blib/lib/}{lib/};
  101: 		$f =~ s{^.*/lib/}{lib/};
  102: 		$TARGET{$f} = 1;
  103: 	}
  104: }
  105: 
  106: END {
  107: 	_write_results();
  108: }
  109: 
  110: # --------------------------------------------------
  111: # _normalize
  112: #
  113: # Convert an absolute or build-tree path
  114: #             to a canonical lib-relative form so that
  115: #             paths recorded at runtime match the
  116: #             targets derived from LCSAJ_TARGETS.
  117: #
  118: # Entry:      $path - an absolute or relative file path.
  119: #
  120: # Exit:       Returns a lib-relative path string,
  121: #             e.g. lib/Foo/Bar.pm
  122: #
  123: # Notes:      Must be defined before the BEGIN block
  124: #             that calls it, since BEGIN runs at compile
  125: #             time and later subs may not yet be compiled.
  126: #
  127: # Examples:
  128: #   /home/user/proj/blib/lib/Foo/Bar.pm  ->  lib/Foo/Bar.pm
  129: #   /home/user/proj/lib/Foo/Bar.pm       ->  lib/Foo/Bar.pm
  130: #   D:\a\proj\blib\lib\Foo\Bar.pm        ->  lib/Foo/Bar.pm
  131: # --------------------------------------------------

					
Mutants (Total: 2, Killed: 0, Survived: 2)
132: sub _normalize { 133: my $f = $_[0]; 134: 135: # Windows paths (abs_path, %INC, caller) may use either separator 136: $f =~ tr{\\}{/}; 137: 138: # Strip everything up to and including blib/lib/ or lib/ 139: $f =~ s{^.*/blib/lib/}{lib/}; 140: $f =~ s{^.*/lib/}{lib/}; 141: return $f; 142: } 143: 144: # -------------------------------------------------- 145: # DB::DB 146: # 147: # Called by the Perl debugger before every 148: # statement. Records (file, line) hits for 149: # later LCSAJ coverage analysis. 150: # 151: # Entry: No arguments — caller(0) is used to get 152: # the current file and line number. 153: # 154: # Exit: Returns nothing. Updates %HITS in place. 155: # 156: # Side effects: Increments %HITS{$norm}{$line}. 157: # 158: # Notes: This sub lives in the DB:: package as 159: # required by Perl's debugger protocol. 160: # It is called for every statement executed 161: # while the debugger is active, so it must 162: # be as fast as possible. 163: # Internal files and out-of-target files 164: # are skipped immediately. 165: # abs_path() resolution is memoised in 166: # %NORM_CACHE per raw $file, since the same 167: # file is seen on every consecutive statement. 168: # -------------------------------------------------- 169: =head2 DB::DB 170:
Mutants (Total: 1, Killed: 0, Survived: 1)
171: Perl debugger hook, automatically invoked by the interpreter before every 172: statement while this module is active as a C<-d:> debugger backend. 173: Records a per-(file, line) hit count used later for LCSAJ coverage 174: analysis. 175: 176: =head3 Arguments 177: 178: None. Perl calls this sub directly; the current execution location is 179: obtained internally via C<caller(0)>. 180: 181: =head3 Returns 182: 183: Nothing meaningful - this is a void debugger callback. 184: 185: =head3 Side effects 186: 187: Increments C<%HITS{$norm}{$line}> for the normalised path and line number 188: of the statement about to execute. Resolves each distinct raw filename 189: via C<Cwd::abs_path> once, memoising the result in C<%NORM_CACHE>. 190: 191: =head3 Usage example 192: 193: Not called directly - activated via the Perl debugger flag: 194: 195: PERL5OPT='-d:App::Test::Generator::LCSAJ::Runtime -Mblib' prove -l t 196: 197: =head3 API specification 198: 199: =head4 input 200: 201: { } 202: 203: =head4 output 204: 205: { type => UNDEF } 206: 207: =cut 208: 209: sub DB::DB { โ—[NOT COVERED] 210 โ†’ 223 โ†’ 227 210: my (undef, $file, $line) = caller(0); 211: 212: return unless defined $file && defined $line; 213: 214: # Resolve symlinks and relative components to a stable absolute path, 215: # cached per raw $file to avoid a stat() on every statement 216: my $norm = $NORM_CACHE{$file} //= _normalize(abs_path($file) // $file); 217: 218: # Never record hits inside this module itself — suffix match is used 219: # so it works regardless of CWD or install prefix 220: return if $norm =~ m{(?:^|/)Devel/App/Test/Generator/LCSAJ/Runtime\.pm$}; 221: 222: # If a target list was provided, skip files not in it 223: if(%TARGET) { 224: return unless $TARGET{$norm}; 225: } 226: 227: $HITS{$norm}{$line}++; 228: } 229: 230: # -------------------------------------------------- 231: # _write_results 232: # 233: # Serialise %HITS to a per-process JSON 234: # file in the output directory. 235: # 236: # Entry: None. Reads %HITS and $OUT_DIR. 237: # 238: # Exit: Returns nothing. Writes a JSON file. 239: # Returns immediately if %HITS is empty. 240: # 241: # Side effects: Creates $OUT_DIR if absent. 242: # Writes cover_html/lcsaj_hits/hits_PID.json 243: # 244: # Notes: Called from END so it runs even when 245: # prove exits non-zero — mutation tests 246: # are expected to fail. PID is included 247: # in the filename so parallel test runs 248: # produce separate files without collision. 249: # -------------------------------------------------- 250: sub _write_results { 251: return unless %HITS; 252: 253: # Include PID in filename to support parallel test runs 254: my $out_file = "$OUT_DIR/hits_$$.json"; 255: 256: make_path($OUT_DIR) unless -d $OUT_DIR; 257: 258: # autodie is disabled for this open -- under "use autodie qw(open)" 259: # open() never returns false on failure, it throws its own exception 260: # instead, which would silently make the "or croak" below dead code 261: no autodie qw(open); 262: open my $fh, '>', $out_file or croak "Cannot write $out_file: $!"; 263: 264: print $fh encode_json(\%HITS); 265: close $fh; 266: } 267: 268: 1; 269: 270: __END__ 271: 272: =head1 OUTPUT FORMAT 273: 274: C<cover_html/lcsaj_hits/hits_PID.json> is a JSON object of the form: 275: 276: { 277: "lib/Foo/Bar.pm": { "12": 3, "15": 1, ... }, 278: ... 279: } 280: 281: Keys are lib-relative paths (C<lib/...>); values are objects mapping line 282: numbers (as strings) to hit counts. One file is written per process so 283: parallel test runs produce separate files. 284: 285: =head1 NOTES ON FILE PLACEMENT 286: 287: The C<-d:App::Test::Generator::LCSAJ::Runtime> flag causes Perl to load 288: C<Devel::App::Test::Generator::LCSAJ::Runtime>, which it finds at: 289: 290: lib/Devel/App/Test/Generator/LCSAJ/Runtime.pm 291: 292: Ensure this path is on C<@INC> (C<-Mblib> or C<-Ilib> in PERL5OPT). 293: 294: =head1 SEE ALSO 295: 296: L<Devel::Cover>, L<App::Test::Generator> 297: 298: =head1 AUTHOR 299: 300: Nigel Horne, C<< <njh at nigelhorne.com> >> 301: 302: Portions of this module's initial design and documentation were created 303: with the assistance of AI. 304: 305: =encoding UTF-8 306: 307: =head1 FORMAL SPECIFICATION 308: 309: Let H be the hits relation (file x line) → â„•, T be the target-file set, 310: and I be the internal-file predicate (true only for this module's own 311: source path). 312: 313: ┌ DB_DB ────────────────────────────────────────── 314: │ ΔH 315: │ file? : FilePath 316: │ line? : â„• 317: ├───────────────────────────────────────────────── 318: │ norm == normalize(file?) 319: │ ¬I(norm) ∧ (T = ∅ ∨ norm ∈ T) 320: │ ⟹ H′(norm, line?) = H(norm, line?) + 1 321: │ I(norm) ∨ (T ≠ ∅ ∧ norm ∉ T) 322: │ ⟹ H′ = H 323: └───────────────────────────────────────────────── 324: 325: =head1 LICENCE AND COPYRIGHT 326: 327: Copyright 2025-2026 Nigel Horne. 328: 329: Usage is subject to the terms of GPL2. 330: If you use it, 331: please let me know. 332: 333: =cut