Skip to content

Commit c330ab0

Browse files
authored
Merge pull request #23 from KohaAloha/master
fix spelling typos
2 parents 019a21b + ec7088f commit c330ab0

8 files changed

+160
-80
lines changed

Changes

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Revision history for Overload-FileCheck
22

33
{{$NEXT}}
44

5+
- Fix a PL_statcache bug when checking: -l $f || -e _
6+
- Add some extra test coverage for 'mock_all_from_stat'
7+
58
0.012 2022-01-20 19:00:46-07:00 America/Denver
69

710
- Fix regression from previous release when checking PL_statcache

FileCheck.xs

+11-11
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ int _overload_ft_stat(Stat_t *stat, int *size) {
196196
if (count == 2) sv = POPs; /* RvAV */
197197
check_status = POPi; /* TOOO pop on SV* for true / false & co */
198198

199-
*size = 0; /* by default it fails */
199+
*size = -1; /* by default it fails */
200200

201201
if ( check_status == 1 ) {
202202
AV *stat_array;
@@ -218,8 +218,8 @@ int _overload_ft_stat(Stat_t *stat, int *size) {
218218
if ( av_size > 0 && av_size != ( STAT_T_MAX - 1 ) )
219219
croak( "Overload::FileCheck::_check: Array should contain 13 elements" );
220220

221+
*size = av_size; /* store the av_size */
221222
if ( av_size > 0 ) {
222-
*size = av_size; /* store the av_size */
223223

224224
ary = AvARRAY(stat_array);
225225

@@ -371,23 +371,23 @@ PP(pp_overload_stat) { /* stat & lstat */
371371
/* copy the content of mocked_stat to PL_statcache */
372372
memcpy(&PL_statcache, &mocked_stat, sizeof(PL_statcache));
373373

374+
if ( size >= 0) { /* yes it succeeds */
375+
PL_laststatval = 0;
376+
} else { /* the stat call fails */
377+
PL_laststatval = -1;
378+
}
379+
380+
PL_laststype = PL_op->op_type; /* this was for our OP */
381+
374382
/* Here, we cut early when stat() returned no values
375383
* In such a case, we set the statcache, but do not call
376384
* the real op (CALL_REAL_OP)
377385
*/
378-
if ( !size )
386+
if ( size < 0 )
379387
RETURN;
380388

381389
PUSHs( MUTABLE_SV( PL_defgv ) ); /* add *_ to the stack */
382390

383-
if ( size ) { /* yes it succeeds */
384-
PL_laststatval = 0;
385-
} else { /* the stat call fails */
386-
PL_laststatval = -1;
387-
}
388-
389-
PL_laststype = PL_op->op_type; /* this was for our OP */
390-
391391
/* probably not real necesseary, make warning messages nicer */
392392
if ( previous_stack && SvPOK(previous_stack) )
393393
sv_setpv(PL_statname, SvPV_nolen(previous_stack) );

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ unmock_all_file_checks();
242242

243243
__END__
244244
245-
# The ouput looks similar to
245+
# The output looks similar to
246246
247247
-d '/root' called from at t/perldoc_mock-all-file-check-trace.t line 26.
248248
-l '/root' called from at t/perldoc_mock-all-file-check-trace.t line 27.
@@ -412,7 +412,7 @@ sub my_stat {
412412
413413
return $fake_stat if $f eq 'fake.stat';
414414
415-
# can also retun stats as a hash ref
415+
# can also return stats as a hash ref
416416
return { st_dev => 1, st_atime => 987654321 } if $f eq 'hash.stat';
417417
418418
return {

examples/mock-stat.pl

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ sub my_stat {
4545

4646
return $fake_stat if $f eq 'fake.stat';
4747

48-
# can also retun stats as a hash ref
48+
# can also return stats as a hash ref
4949
return { st_dev => 1, st_atime => 987654321 } if $f eq 'hash.stat';
5050

5151
return {

examples/perldoc_mock-stat.t

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ sub my_stat {
5252

5353
return $fake_stat if $f eq 'fake.stat';
5454

55-
# can also retun stats as a hash ref
55+
# can also return stats as a hash ref
5656
return { st_dev => 1, st_atime => 987654321 } if $f eq 'hash.stat';
5757

5858
return {

examples/trace-code.pl

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sub my_custom_check {
2727

2828
__END__
2929
30-
# The ouput looks similar to
30+
# The output looks similar to
3131
3232
-d '/root' called from at t/perldoc_mock-all-file-check-trace.t line 26.
3333
-l '/root' called from at t/perldoc_mock-all-file-check-trace.t line 27.

t/mock-all-from-stat.t

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/perl -w
2+
3+
# Copyright (c) 2018, cPanel, LLC.
4+
# All rights reserved.
5+
# http://cpanel.net
6+
#
7+
# This is free software; you can redistribute it and/or modify it under the
8+
# same terms as Perl itself. See L<perlartistic>.
9+
10+
use strict;
11+
use warnings;
12+
13+
use Test2::Bundle::Extended;
14+
use Test2::Tools::Explain;
15+
use Test2::Plugin::NoWarnings;
16+
17+
use Overload::FileCheck qw(CHECK_IS_FALSE CHECK_IS_TRUE FALLBACK_TO_REAL_OP);
18+
19+
use File::Temp qw{ tempfile tempdir };
20+
21+
my %STATS;
22+
23+
# non existing but mocked
24+
my $FILENAME;
25+
my $FAKE_DIR;
26+
27+
{
28+
my ( $fh, $filename ) = tempfile();
29+
$STATS{'file'} = [ stat($filename) ];
30+
unlink $filename;
31+
32+
$FILENAME = "$filename";
33+
34+
my $dir = tempdir( CLEANUP => 1 );
35+
36+
$STATS{'dir'} = [ stat("$dir") ];
37+
38+
$STATS{'perl'} = [ stat($^X) ];
39+
40+
$FAKE_DIR = "$dir/not/there";
41+
}
42+
43+
my $current_test = "$0";
44+
45+
our $call_my_stat;
46+
47+
ok !-e $FILENAME, "filename does not exist";
48+
ok !-d $FAKE_DIR, "directory does not exis";
49+
50+
ok !$call_my_stat, 'start - without mock';
51+
52+
# note: we are just mocking stat here...
53+
ok Overload::FileCheck::mock_all_from_stat( \&my_stat ), "mock_all_from_stat succees";
54+
55+
is [ stat( $FILENAME ) ], $STATS{'file'}, "stats for file";
56+
ok $call_my_stat, "stat is now mocked";
57+
ok -e $FILENAME, "-e filename";
58+
ok -f $FILENAME, "-f filename";
59+
60+
ok -e $FILENAME && -f _, "-e filename && -f _";
61+
62+
is -l $FILENAME || -e $FILENAME, 1, q[-l $f || -e $f];
63+
is -l $FILENAME || -e _, 1, q[-l $f || -e _];
64+
65+
is [ stat('/empty') ], [], "stat /empty";
66+
67+
is -l q[/empty] || -e q[/empty], undef, q[-l /empty || -e /empty];
68+
is -l q[/empty] || -e _, undef, q[-l /empty || -e _];
69+
70+
# --- END ---
71+
ok Overload::FileCheck::unmock_all_file_checks(), "unmock all";
72+
done_testing;
73+
74+
exit;
75+
76+
sub my_stat {
77+
my ( $opname, $file_or_handle ) = @_;
78+
79+
note "=== my_stat is called. Type: ", $opname, " File: ", $file_or_handle;
80+
++$call_my_stat;
81+
82+
my $f = $file_or_handle; # alias to use a shorter name later...
83+
84+
return $STATS{'file'} if $f eq $FILENAME;
85+
return $STATS{'dir'} if $f eq $FAKE_DIR;
86+
87+
return [] if $f eq '/empty';
88+
89+
return FALLBACK_TO_REAL_OP();
90+
}
91+
92+
sub stat_for_a_directory {
93+
return $STATS{'dir'};
94+
}
95+
96+
sub stat_for_a_binary {
97+
return $STATS{'perl'};
98+
}
99+
100+
sub stat_for_a_tty {
101+
return $STATS{'tty'};
102+
}
103+
104+
1;

t/mock-stat.t

+37-64
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,43 @@ use Test2::Plugin::NoWarnings;
1616

1717
use Overload::FileCheck qw(CHECK_IS_FALSE CHECK_IS_TRUE FALLBACK_TO_REAL_OP);
1818

19+
use File::Temp qw{ tempfile tempdir };
20+
21+
my %STATS;
22+
23+
our @FAKE_DIR;
24+
25+
{
26+
my ( $fh, $filename ) = tempfile();
27+
$STATS{'file'} = [ stat($filename) ];
28+
29+
my $dir = tempdir( CLEANUP => 1 );
30+
31+
$STATS{'dir'} = [ stat("$dir") ];
32+
33+
$STATS{'$0'} = [ stat($0) ];
34+
35+
$STATS{'perl'} = [ stat($^X) ];
36+
37+
$STATS{'tty'} = [ stat('/dev/tty') ];
38+
39+
@FAKE_DIR = ( "$dir/not/there" );
40+
push @FAKE_DIR, "/not/there" if -e q[/not/there];
41+
}
42+
1943
my $current_test = "$0";
2044

2145
my $call_my_stat = 0;
2246
my $last_called_for;
23-
our @FAKE_DIR = qw{/a/b/c /usr/somewhere /home/fake};
2447

2548
ok 1, 'start';
2649

2750
my $stat_result = [ stat($0) ];
2851
is scalar @$stat_result, 13, "call stat unmocked";
2952

30-
my $unmocked_stat_for_perl = [ stat($^X) ];
53+
my $unmocked_stat_for_perl = $STATS{'perl'};
3154

55+
# note: we are just mocking stat here...
3256
ok Overload::FileCheck::mock_stat( \&my_stat ), "mock_stat succees";
3357

3458
is $call_my_stat, 0, "my_stat was not called at this point";
@@ -86,8 +110,7 @@ foreach my $f (qw{alpha1 alpha2 alpha3}) {
86110

87111
foreach my $d (@FAKE_DIR) {
88112
is [ stat($d) ], stat_for_a_directory(), "stat_for_a_directory - $d";
89-
90-
ok !-e $d, "directory $d does not exist";
113+
ok !-d $d, "!-d $d - we are just mocking the stats";
91114
}
92115

93116
is [ stat('fake.binary') ], stat_for_a_binary(), "stat_for_a_binary - 'fake.binary'";
@@ -114,6 +137,8 @@ like(
114137
is [ stat($^X) ], $unmocked_stat_for_perl, q[stat is mocked but $^X should fallback to the regular stat];
115138
is [ stat(_) ], $unmocked_stat_for_perl, q[stat is mocked - using _ on an unmocked file];
116139

140+
is [ stat('/empty') ], [], "stat /empty";
141+
117142
# --- END ---
118143
ok Overload::FileCheck::unmock_all_file_checks(), "unmock all";
119144
done_testing;
@@ -148,77 +173,25 @@ sub my_stat {
148173

149174
return 666 if $f eq 'evil';
150175

176+
return [] if $f eq '/empty';
177+
151178
return FALLBACK_TO_REAL_OP();
152179
}
153180

154181
sub fake_stat_for_dollar_0 {
155-
return [
156-
0,
157-
0,
158-
4,
159-
3,
160-
2,
161-
1,
162-
42,
163-
10001,
164-
1000,
165-
2000,
166-
3000,
167-
0,
168-
0
169-
];
182+
$STATS{'$0'};
170183
}
171184

172185
sub stat_for_a_directory {
173-
return [
174-
64769,
175-
67149975,
176-
16877,
177-
23,
178-
0,
179-
0,
180-
0,
181-
4096,
182-
1539271725,
183-
1524671853,
184-
1524671853,
185-
4096,
186-
8,
187-
];
186+
return $STATS{'dir'};
188187
}
189188

190189
sub stat_for_a_binary {
191-
return [
192-
64769,
193-
33728572,
194-
33261,
195-
1,
196-
0,
197-
0,
198-
0,
199-
28920,
200-
1539797896,
201-
1523421302,
202-
1526572488,
203-
4096,
204-
64,
205-
];
190+
return $STATS{'perl'};
206191
}
207192

208193
sub stat_for_a_tty {
209-
return [
210-
5,
211-
1043,
212-
8592,
213-
1,
214-
0,
215-
5,
216-
1025,
217-
0,
218-
1538428544,
219-
1538428544,
220-
1538428550,
221-
4096,
222-
0,
223-
];
194+
return $STATS{'tty'};
224195
}
196+
197+
1;

0 commit comments

Comments
 (0)