Debian Codegraph @

lib/Test2/Harness/Runner/DepTracer.pm

raw
1 package Test2::Harness::Runner::DepTracer;
2 use strict;
3 use warnings;
4
5 use Carp qw/croak/;
6
7 our $VERSION = '1.000179';
8
9 use Test2::Harness::Util::HashBase qw/
10 -_on
11 -exclude
12 -dep_map
13 -loaded
14 -my_require
15 -real_require
16 -_my_inc
17 -callbacks
18 /;
19
20 my %DEFAULT_EXCLUDE = (
21 'warnings.pm' => 1,
22 'strict.pm' => 1,
23 );
24
25 my $ACTIVE;
26
27 sub ACTIVE { $ACTIVE }
28
29 sub start {
30 my $self = shift;
31
32 croak "There is already an active DepTracer" if $ACTIVE;
33
34 $ACTIVE = $self;
35
36 unshift @INC => $self->my_inc;
37
38 $self->{+_ON} = 1;
39 }
40
41 sub stop {
42 my $self = shift;
43
44 croak "DepTracer is not active" unless $ACTIVE;
45 croak "Different DepTracer is active" unless "$ACTIVE" eq "$self";
46 $ACTIVE = undef;
47
48 $self->{+_ON} = 0;
49
50 my $inc = $self->{+_MY_INC} or return 0;
51
52 @INC = grep { !(ref($_) && $inc == $_) } @INC;
53 return 0;
54 }
55
56 sub my_inc {
57 my $self = shift;
58
59 return $self->{+_MY_INC} if $self->{+_MY_INC};
60
61 my $exclude = $self->{+EXCLUDE} ||= {%DEFAULT_EXCLUDE};
62 my $dep_map = $self->{+DEP_MAP} ||= {};
63 my $loaded = $self->{+LOADED} ||= {};
64
65 return $self->{+_MY_INC} ||= sub {
66 my ($this, $file) = @_;
67
68 return unless $self->{+_ON};
69 return unless $file =~ m/^[_a-z]/i;
70 return if $exclude->{$file};
71
72 my $loaded_by = $self->loaded_by;
73 push @{$dep_map->{$file}} => $loaded_by;
74 $loaded->{$file}++;
75
76 return;
77 };
78 }
79
80 sub clear_loaded { %{$_[0]->{+LOADED}} = () }
81
82 my %REQUIRE_CACHE;
83
84 sub add_callbacks {
85 my $self = shift;
86 my %watch = @_;
87 for my $file (keys %watch) {
88 my $cb = $watch{$file};
89 $self->add_callback($file => $cb);
90 }
91 }
92
93 sub add_callback {
94 my $self = shift;
95 my ($file, $cb) = @_;
96 $self->{+LOADED}->{$file}++;
97 $self->{+CALLBACKS}->{$file} = $cb;
98 }
99
100 sub init {
101 my $self = shift;
102
103 my $exclude = $self->{+EXCLUDE} ||= { %DEFAULT_EXCLUDE };
104
105 my $stash = \%CORE::GLOBAL::;
106 # We use a string in the reference below to prevent the glob slot from
107 # being auto-vivified by the compiler.
108 $self->{+REAL_REQUIRE} = exists $stash->{require} ? \&{'CORE::GLOBAL::require'} : undef;
109
110 $self->{+CALLBACKS} //= {};
111 my $dep_map = $self->{+DEP_MAP} ||= {};
112 my $loaded = $self->{+LOADED} ||= {};
113 my $inc = $self->my_inc;
114
115 my $require = $self->{+MY_REQUIRE} = sub {
116 my ($file) = @_;
117
118 my $loaded_by = $self->loaded_by;
119
120 my $real_require = $self->{+REAL_REQUIRE};
121 unless($real_require) {
122 my $caller = $loaded_by->[0];
123 $real_require = $REQUIRE_CACHE{$caller} ||= eval "package $caller; sub { CORE::require(\$_[0]) }" or die $@;
124 }
125
126 goto &$real_require unless $self->{+_ON};
127
128 if ($file =~ m/^[_a-z]/i) {
129 unless ($exclude->{$file}) {
130 push @{$dep_map->{$file}} => $loaded_by;
131 $loaded->{$file}++;
132 }
133 }
134
135 if (!ref($INC[0]) || $INC[0] != $inc) {
136 @INC = (
137 $inc,
138 grep { !(ref($_) && $inc == $_) } @INC,
139 );
140 }
141
142 local @INC = @INC[1 .. $#INC];
143
144 $real_require->(@_);
145 };
146
147 {
148 no strict 'refs';
149 no warnings 'redefine';
150 *{'CORE::GLOBAL::require'} = $require;
151 }
152 }
153
154 sub loaded_by {
155 my $level = 1;
156
157 while(my @caller = caller($level++)) {
158 next if $caller[0] eq __PACKAGE__;
159
160 return [$caller[0], $caller[1]];
161 }
162
163 return ['', ''];
164 }
165
166 1;
167
168 __END__
169
170
171 =pod
172
173 =encoding UTF-8
174
175 =head1 NAME
176
177 Test2::Harness::Runner::DepTracer - Tool for tracing module dependencies as
178 they are loaded.
179
180 =head1 DESCRIPTION
181
182 This tool is used by Test2::Harness to build a graph of dependencies which can
183 then be used to blacklist modified modules (and anything that uses them) when
184 they change under a preloaded runner.
185
186 =head1 SYNOPSIS
187
188 use Test2::Harness::Runner::DepTracer;
189
190 my $dt = Test2::Harness::Runner::DepTracer->new();
191
192 $dt->start();
193
194 require Some::Thing;
195
196 # You can always check for and retrieve an active DepTrace this way:
197 my $dt_reference = Test2::Harness::Runner::DepTracer->ACTIVE;
198
199 $dt->stop();
200
201 my $dep_map = $dt->dep_map;
202
203 my $loaded_by = $dep_map->{'Some/Thing.pm'};
204 print "Some::Thing was directly or indirectly loaded by:\n" . join("\n" => @$loaded_by) . "\n";
205
206 =head1 ATTRIBUTES
207
208 These can be specified at construction, and will be populated during use.
209
210 =over 4
211
212 =item $hashref = $dt->exclude
213
214 A hashref of files/modules to exclude from dep tracking. By default C<strict>
215 and C<warnings> are excluded.
216
217 =item $hashref = $dt->dep_map
218
219 Every file which is loaded while the tool is started will have an entry in this
220 hash, each value is an array of all files which loaded the key file directly or
221 indirectly.
222
223 =item $hashref = $dt->loaded
224
225 How many times each file was directly loaded.
226
227 =back
228
229 =head1 METHODS
230
231 =over 4
232
233 =item $dt->start
234
235 Start tracking modules which are loaded.
236
237 =item $dt->stop
238
239 Stop tracking modules that are loaded.
240
241 =back
242
243 =head1 CLASS METHODS
244
245 =over 4
246
247 =item $dt_or_undef = Test2::Harness::Runner::DepTracer->ACTIVE();
248
249 Get the currently active DepTracer, if any.
250
251 =back
252
253 =head1 SOURCE
254
255 The source code repository for Test2-Harness can be found at
256 F<http://github.com/Test-More/Test2-Harness/>.
257
258 =head1 MAINTAINERS
259
260 =over 4
261
262 =item Chad Granum E<lt>[email protected]<gt>
263
264 =back
265
266 =head1 AUTHORS
267
268 =over 4
269
270 =item Chad Granum E<lt>[email protected]<gt>
271
272 =back
273
274 =head1 COPYRIGHT
275
276 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
277
278 This program is free software; you can redistribute it and/or
279 modify it under the same terms as Perl itself.
280
281 See F<http://dev.perl.org/licenses/>
282
283 =cut