Debian Codegraph @

t/0LIB/MyTest.pm

raw
1 package MyTest;
2 use strict;
3 use warnings;
4 require Exporter;
5 #require UUID; # require'd in note() to enable coverage testing
6 use vars qw(@EXPORT $TMPDIR $TMPFILE $TESTS_RUN);
7 use constant USE_ITHREADS => !!$ENV{USE_ITHREADS};
8
9 # this block serves two functions.
10 # 1) provides an out-of-band means of tracking tests run via tmpfiles
11 # since some of the tests call into here from within threads.
12 # 2) speeds up the non-threaded case by not loading Fcntl / File::Temp.
13 BEGIN {
14 if (USE_ITHREADS) {
15 require File::Temp;
16 require Fcntl;
17 Fcntl->import(qw(
18 O_APPEND O_CREAT O_WRONLY LOCK_EX LOCK_UN SEEK_END
19 ));
20 my $tdo = File::Temp->newdir('UUID-test-XXXXXXXX', TMPDIR => 1, CLEANUP => 0);
21 $TMPDIR = $tdo->dirname;
22 $TMPFILE = File::Temp::tempnam($TMPDIR, 'UUID.runs.');
23 }
24 else {
25 # dummys so the compiler dont whine later
26 *O_APPEND = *O_CREAT = *O_WRONLY = sub {};
27 *LOCK_EX = *LOCK_UN = *SEEK_END = sub {};
28 }
29 $TESTS_RUN = 0;
30 }
31
32 @EXPORT = qw(
33 BAIL_OUT cmp_ok fail diag done_testing is isnt like note ok pass
34 plan unlike use_ok
35 );
36
37 *import = \&Exporter::import;
38 sub _err;
39
40 # Apparently earlier versions of IO::File didn't have flush(),
41 # so we'll just do this instead. And yea, we need both.
42 select STDERR; $|=1;
43 select STDOUT; $|=1;
44
45 our $PLAN_SENT = 0;
46 our $TESTS_PLANNED = 0;
47
48 my $realnode; # defined in note() or diag()
49
50 sub note {
51 my $work = join '', map { defined($_) ? $_ : '<UNDEF>' } @_;
52 chomp $work;
53
54 # done this way so we dont automatically pull in UUID
55 # at compile time. there is one coverage test that uses
56 # this module but relies on it not doing that. it is
57 # careful not to call note() or diag().
58 $realnode ||= do {
59 require UUID;
60 my $rn = UUID::_realnode();
61 substr $rn, 0, 24, '' if $rn;
62 $rn;
63 };
64
65 # hide the real node anywhere it appears.
66 $work =~ s/$realnode/XXXXXXXXXXXX/ig
67 if $realnode;
68
69 print '# ', $work, "\n";
70 undef;
71 }
72
73 sub diag {
74 my $work = join '', map { defined($_) ? $_ : '<UNDEF>' } @_;
75 chomp $work;
76
77 # see note in note().
78 $realnode ||= do {
79 require UUID;
80 my $rn = UUID::_realnode();
81 substr $rn, 0, 24, '' if $rn;
82 $rn;
83 };
84
85 # hide the real node anywhere it appears.
86 $work =~ s/$realnode/XXXXXXXXXXXX/ig
87 if $realnode;
88
89 warn '# ', $work, "\n";
90 undef;
91 }
92
93 sub diag_no_hide {
94 my $work = join '', map { defined($_) ? $_ : '<UNDEF>' } @_;
95 chomp $work;
96
97 warn '# ', $work, "\n";
98 undef;
99 }
100
101 # some of the tests call these from within threads,
102 # so try to be super-safe.
103 sub _TESTS_INC {
104 if (USE_ITHREADS) {
105 sysopen my $fh, $TMPFILE, O_CREAT|O_APPEND|O_WRONLY, 0600
106 or _err "sysopen: ($TMPFILE) $!";
107 flock($fh, LOCK_EX) or _err "flock LOCK_EX: ($TMPFILE) $!";
108 seek($fh, 0, SEEK_END) or _err "seek: ($TMPFILE) $!";
109 syswrite $fh, '+';
110 flock($fh, LOCK_UN) or _err "flock LOCK_UN: ($TMPFILE) $!";
111 close $fh;
112 }
113 else {
114 ++$TESTS_RUN;
115 }
116 }
117
118 sub _TESTS_DEC {
119 if (USE_ITHREADS) {
120 sysopen my $fh, $TMPFILE, O_CREAT|O_APPEND|O_WRONLY, 0600
121 or _err "sysopen: ($TMPFILE) $!";
122 flock($fh, LOCK_EX) or _err "flock LOCK_EX: ($TMPFILE) $!";
123 seek($fh, 0, SEEK_END) or _err "seek: ($TMPFILE) $!";
124 syswrite $fh, '-';
125 flock($fh, LOCK_UN) or _err "flock LOCK_UN: ($TMPFILE) $!";
126 close $fh;
127 }
128 else {
129 --$TESTS_RUN;
130 }
131 }
132
133 sub plan {
134 my (%plan) = @_;
135 $TESTS_PLANNED = $plan{tests};
136 }
137
138 sub pass (;$) {
139 my ($tag) = @_;
140 _TESTS_INC;
141 if (defined($tag) and $tag ne '') { print "ok # $tag\n" }
142 else { print "ok\n" }
143 undef;
144 }
145
146 sub fail (;$) {
147 my ($tag) = @_;
148 _TESTS_INC;
149 if (defined($tag) and $tag ne '') { print "not ok # $tag\n" }
150 else { print "not ok\n" }
151 undef;
152 }
153
154 sub ok ($;$) {
155 my ($val, $tag) = @_;
156 _TESTS_INC;
157 if ($val) {
158 if (defined($tag) and $tag ne '') { print "ok # $tag\n" }
159 else { print "ok\n" }
160 return 1;
161 }
162 else {
163 if (defined($tag) and $tag ne '') { print "not ok # $tag\n" }
164 else { print "not ok\n" }
165 return !!0;
166 }
167 }
168
169 sub is ($$;$) {
170 my ($v1, $v2, $tag) = @_;
171 $tag ||= '';
172 _TESTS_INC;
173 my $d1 = defined $v1;
174 my $d2 = defined $v2;
175 if ($d1 and $d2) {
176 if ($v1 eq $v2) {
177 if (defined($tag) and $tag ne '') { print "ok # $tag\n" }
178 else { print "ok\n" }
179 }
180 else {
181 if (defined($tag) and $tag ne '') { print "not ok # $tag\n" }
182 else { print "not ok\n" }
183 }
184 }
185 elsif (!$d1 and !$d2) {
186 if (defined($tag) and $tag ne '') { print "ok # $tag\n" }
187 else { print "ok\n" }
188 }
189 else {
190 if (defined($tag) and $tag ne '') { print "not ok # $tag\n" }
191 else { print "not ok\n" }
192 }
193 undef;
194 }
195
196 sub isnt ($$;$) {
197 my ($v1, $v2, $tag) = @_;
198 $tag ||= '';
199 _TESTS_INC;
200 unless (defined $v1) { fail $tag; return }
201 unless (defined $v2) { fail $tag; return }
202 if ($v1 ne $v2) {
203 if (defined($tag) and $tag ne '') { print "ok # $tag\n" }
204 else { print "ok\n" }
205 }
206 else {
207 if (defined($tag) and $tag ne '') { print "not ok # $tag\n" }
208 else { print "not ok\n" }
209 }
210 undef;
211 }
212
213 sub like ($$;$) {
214 my ($val, $re, $tag) = @_;
215 $tag ||= '';
216 _TESTS_INC;
217 if ($val =~ $re) {
218 if (defined($tag) and $tag ne '') { print "ok # $tag\n" }
219 else { print "ok\n" }
220 }
221 else {
222 if (defined($tag) and $tag ne '') { print "not ok # $tag\n" }
223 else { print "not ok\n" }
224 }
225 undef;
226 }
227
228 sub unlike ($$;$) {
229 my ($val, $re, $tag) = @_;
230 $tag ||= '';
231 _TESTS_INC;
232 if ($val !~ $re) {
233 if (defined($tag) and $tag ne '') { print "ok # $tag\n" }
234 else { print "ok\n" }
235 }
236 else {
237 if (defined($tag) and $tag ne '') { print "not ok # $tag\n" }
238 else { print "not ok\n" }
239 }
240 undef;
241 }
242
243 sub cmp_ok ($$$;$) {
244 my ($got, $op, $expect, $tag) = @_;
245 $tag ||= '';
246 _TESTS_INC;
247 unless (defined $got) { fail $tag; return }
248 unless (defined $expect) { fail $tag; return }
249 for ($op) {
250 /<=/ and do { _TESTS_DEC; ok($got <= $expect, $tag); last };
251 />/ and do { _TESTS_DEC; ok($got > $expect, $tag); last };
252 _err "OP UNIMPLEMENTED: '$op'\n";
253 }
254 undef;
255 }
256
257 sub done_testing {
258 unless ($PLAN_SENT) {
259 if (USE_ITHREADS) {
260 open my $fh, '<', $TMPFILE or _err "open: ($TMPFILE) $!";
261 my $dat = join '', <$fh>;
262 close $fh;
263
264 my $up = $dat =~ tr/+//;
265 my $dn = $dat =~ tr/-//;
266 $TESTS_RUN = $up - $dn;
267 }
268 print "1..$TESTS_RUN\n";
269 }
270 _cleanup();
271 }
272
273 sub _err {
274 my $msg = shift;
275 _cleanup();
276 my ($pkg, $file, $line) = caller(0);
277 die "$msg at file $file line $line\n";
278 }
279
280 sub _cleanup {
281 if (USE_ITHREADS) {
282 unlink $TMPFILE;
283 rmdir $TMPDIR;
284 }
285 }
286
287 sub BAIL_OUT {
288 my ($tag) = @_;
289 if (defined($tag) and $tag ne '') { print "Bail out! $tag\n" }
290 else { print "Bail out!\n" }
291 }
292
293 # borrowed from Test::More (with slight modification)
294 sub use_ok ($;@) {
295 my( $module, @imports ) = @_;
296
297 my %caller;
298 @caller{qw/pack file line sub args want eval req strict warn/} = caller(0);
299
300 my ($pack, $filename, $line, $warn) = @caller{qw/pack file line warn/};
301 $filename =~ y/\n\r/_/; # so it doesn't run off the "#line $line $f" line
302
303 my $code;
304 if( @imports == 1 and $imports[0] =~ /^\d+(?:\.\d+)?$/ ) {
305 # probably a version check. Perl needs to see the bare number
306 # for it to work with non-Exporter based modules.
307 $code = <<USE;
308 package $pack;
309 BEGIN { \${^WARNING_BITS} = \$args[-1] if defined \$args[-1] }
310 #line $line $filename
311 use $module $imports[0];
312 1;
313 USE
314 }
315 else {
316 $code = <<USE;
317 package $pack;
318 BEGIN { \${^WARNING_BITS} = \$args[-1] if defined \$args[-1] }
319 #line $line $filename
320 use $module \@{\$args[0]};
321 1;
322 USE
323 }
324
325 my ($eval_result, $eval_error) = _eval($code, \@imports, $warn);
326 my $ok = ok( $eval_result, "use $module;" );
327
328 unless($ok) {
329 chomp $eval_error;
330 $@ =~ s{^BEGIN failed--compilation aborted at .*$}
331 {BEGIN failed--compilation aborted at $filename line $line.}m;
332 diag_no_hide(<<DIAGNOSTIC);
333 Tried to use '$module'.
334 Error: $eval_error
335 DIAGNOSTIC
336
337 }
338
339 return $ok;
340 }
341
342 # borrowed from Test::More
343 sub _eval {
344 my( $code, @args ) = @_;
345
346 # Work around oddities surrounding resetting of $@ by immediately
347 # storing it.
348 my( $sigdie, $eval_result, $eval_error );
349 {
350 local( $@, $!, $SIG{__DIE__} ); # isolate eval
351 $eval_result = eval $code; ## no critic (BuiltinFunctions::ProhibitStringyEval)
352 $eval_error = $@;
353 $sigdie = $SIG{__DIE__} || undef;
354 }
355 # make sure that $code got a chance to set $SIG{__DIE__}
356 $SIG{__DIE__} = $sigdie if defined $sigdie;
357
358 return( $eval_result, $eval_error );
359 }
360
361 1;