| 1 | # Copyright 1999-2001 Steven Knight. All rights reserved. This program | ||||||||||||||||||||
| 2 | # is free software; you can redistribute it and/or modify it under the | ||||||||||||||||||||
| 3 | # same terms as Perl itself. | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # This package tests an executable program or script, | ||||||||||||||||||||
| 6 | # managing one or more temporary working directories, | ||||||||||||||||||||
| 7 | # keeping track of standard and error output, | ||||||||||||||||||||
| 8 | # and cleaning up after everything is done. | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | package Test::Cmd::Common; | ||||||||||||||||||||
| 11 | 37 | $Test::Cmd::Common::VERSION = '1.09'; | |||||||||||||||||||
| 12 | 37 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 37 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 37 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 37 | ▶ | use Exporter (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 37 | our ($_exe, $_o, $_so, $_a, $_is_win32); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 37 | our @ISA = qw(Test::Cmd Exporter); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 37 | our @EXPORT_OK = qw($_exe $_o $_a $_so $_is_win32); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 37 | ▶ | use Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 37 | ▶ | use Cwd; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 37 | ▶ | use File::Copy (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 37 | ▶ | use Test::Cmd; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | =head1 NAME | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | Test::Cmd::Common - module for common Test::Cmd error handling | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =head1 SYNOPSIS | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | use Test::Cmd::Common; | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | $test = Test::Cmd::Common->new(string => 'functionality being tested', | ||||||||||||||||||||
| 37 | prog => 'program_under_test', | ||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | $test->run(chdir => 'subdir', fail => '$? != 0', | ||||||||||||||||||||
| 41 | flags => '-x', targets => '.', | ||||||||||||||||||||
| 42 | stdout => <<_EOF_, stderr => <<_EOF_); | ||||||||||||||||||||
| 43 | expected standard output | ||||||||||||||||||||
| 44 | _EOF_ | ||||||||||||||||||||
| 45 | expected error output | ||||||||||||||||||||
| 46 | _EOF_ | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | $test->subdir('subdir', ...); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | $test->read(\$contents, 'file'); | ||||||||||||||||||||
| 51 | $test->read(\@lines, 'file'); | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | $test->write('file', <<_EOF_); | ||||||||||||||||||||
| 54 | contents of the file | ||||||||||||||||||||
| 55 | _EOF_ | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | $test->file_matches(); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | $test->must_exist('file', ['subdir', 'file'], ...); | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | $test->must_not_exist('file', ['subdir', 'file'], ...); | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | $test->copy('src_file', 'dst_file'); | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | $test->chmod($mode, 'file', ...); | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | $test->sleep; | ||||||||||||||||||||
| 68 | $test->sleep($seconds); | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | $test->touch('file', ...); | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | $test->unlink('file', ...); | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =head1 DESCRIPTION | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | The C<Test::Cmd::Common> module provides a simple, high-level interface | ||||||||||||||||||||
| 77 | for writing tests of executable commands and scripts, especially | ||||||||||||||||||||
| 78 | commands and scripts that interact with the file system. All methods | ||||||||||||||||||||
| 79 | throw exceptions and exit on failure. This makes it unnecessary to add | ||||||||||||||||||||
| 80 | explicit checks for return values, making the test scripts themselves | ||||||||||||||||||||
| 81 | simpler to write and easier to read. | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | The C<Test::Cmd::Common> class is a subclass of L<Test::Cmd>. In | ||||||||||||||||||||
| 84 | essence, C<Test::Cmd::Common> is a wrapper that treats common | ||||||||||||||||||||
| 85 | L<Test::Cmd> error conditions as exceptions that terminate the test. | ||||||||||||||||||||
| 86 | You can use C<Test::Cmd::Common> directly, or subclass it for your | ||||||||||||||||||||
| 87 | program and add additional (or override) methods to tailor it to your | ||||||||||||||||||||
| 88 | program's specific needs. Alternatively, C<Test::Cmd::Common> serves as | ||||||||||||||||||||
| 89 | a useful example of how to define your own L<Test::Cmd> subclass. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | The C<Test::Cmd::Common> module provides the following importable | ||||||||||||||||||||
| 92 | variables: | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =over 4 | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =item C<$_exe> | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | The executable file suffix. This value is normally available | ||||||||||||||||||||
| 99 | as C<$Config{_exe}> in Perl version 5.005 and later. The | ||||||||||||||||||||
| 100 | C<Test::Cmd::Common> module figures it out via other means in earlier | ||||||||||||||||||||
| 101 | versions. | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =item C<$_o> | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | The object file suffix. This value is normally available | ||||||||||||||||||||
| 106 | from C<$Config{_o}> in Perl version 5.005 and later. The | ||||||||||||||||||||
| 107 | C<Test::Cmd::Common> module figures it out via other means in earlier | ||||||||||||||||||||
| 108 | versions. | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =item C<$_a> | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | The library file suffix. This value is normally available | ||||||||||||||||||||
| 113 | from as C<$Config{_a}> in Perl version 5.005 and later. The | ||||||||||||||||||||
| 114 | C<Test::Cmd::Common> module figures it out via other means in earlier | ||||||||||||||||||||
| 115 | versions. | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | =item C<$_so> | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | The shared library file suffix. This value is normally available | ||||||||||||||||||||
| 120 | as C<$Config{_so}> in Perl version 5.005 and later. The | ||||||||||||||||||||
| 121 | C<Test::Cmd::Common> module figures it out via other means in earlier | ||||||||||||||||||||
| 122 | versions. | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =item C<$_is_win32> | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | A Boolean value that reflects whether the current platform is a Win32 | ||||||||||||||||||||
| 127 | system. | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =back | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 METHODS | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =over 4 | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 37 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=12 SCAR=31.6
| |||||||||||||||||||||
| 138 | 37 | ▶ | if ($] < 5.003) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 0 | eval("require Win32"); | |||||||||||||||||||
| 140 | 0 | $_is_win32 = ! $@; | |||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||
| 142 | 37 | $_is_win32 = $^O eq "MSWin32"; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 37 | $_exe = $Config{_exe}; | |||||||||||||||||||
| 146 | 37 | ▶ | $_exe = $Config{exe_ext} if ! defined $_exe; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | 37 | ▶ | $_exe = $_is_win32 ? '.exe' : '' if ! defined $_exe; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 148 | 37 | $_o = $Config{_o}; | |||||||||||||||||||
| 149 | 37 | ▶ | $_o = $Config{obj_ext} if ! defined $_o; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | 37 | ▶ | $_o = $_is_win32 ? '.obj' : '.o' if ! defined $_o; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 151 | 37 | $_a = $Config{_a}; | |||||||||||||||||||
| 152 | 37 | ▶ | $_a = $Config{lib_ext} if ! defined $_a; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | 37 | ▶ | $_a = $_is_win32 ? '.lib' : '.a'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | 37 | $_so = ".$Config{so}"; | |||||||||||||||||||
| 155 | 37 | ▶ | $_so = $_is_win32 ? '.dll' : '.so' if ! defined $_so; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =item C<new> | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | Creates a new test environment object. Any arguments are keyword-value | ||||||||||||||||||||
| 161 | pairs that are passed through to the construct method for the base | ||||||||||||||||||||
| 162 | class from which we inherit our methods (that is, the L<Test::Cmd> | ||||||||||||||||||||
| 163 | class). In the normal case, this should be the program to be tested and | ||||||||||||||||||||
| 164 | a description of the functionality being tested: | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | $test = Test::Cmd::Common->new(prog => 'my_program', | ||||||||||||||||||||
| 167 | string => 'cool new feature'); | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | By default, methods that match actual versus expected output (the | ||||||||||||||||||||
| 170 | C<run>, and C<file_matches> methods) use an exact match. Tests that | ||||||||||||||||||||
| 171 | require regular expression matches can specify this on initialization of | ||||||||||||||||||||
| 172 | the test environment: | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | $test = Test::Cmd::Common->new(prog => 'my_program', | ||||||||||||||||||||
| 175 | string => 'cool new feature', | ||||||||||||||||||||
| 176 | match_sub => \&Test::Cmd::diff_regex); | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | or by executing the following after initialization of the test | ||||||||||||||||||||
| 179 | environment: | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | $test->match_sub(\&Test::Cmd::diff_regex); | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | Creates a temporary working directory for the test environment and | ||||||||||||||||||||
| 184 | changes directory to it. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Exits NO RESULT if the object can not be created, the temporary working | ||||||||||||||||||||
| 187 | directory can not be created, or the current directory cannot be changed | ||||||||||||||||||||
| 188 | to the temporary working directory. | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 37 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 193 | 37 | my $proto = shift; | |||||||||||||||||||
| 194 | 37 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 195 | 37 | my $test = $class->SUPER::new(@_); | |||||||||||||||||||
| 196 | 36 | $class->SUPER::no_result(! $test, undef, 1); | |||||||||||||||||||
| 197 | # We're going to chdir to the temporary working directory. | ||||||||||||||||||||
| 198 | # So that things work properly relative to the current directory, | ||||||||||||||||||||
| 199 | # turn any relative path names in @INC to absolute paths. | ||||||||||||||||||||
| 200 | 36 | my $cwd = Cwd::cwd(); | |||||||||||||||||||
| 201 | 36 | ▶ | map { $_ = $test->catdir($cwd, $_) if ! $test->file_name_is_absolute($_) } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 202 | @INC; | ||||||||||||||||||||
| 203 | 36 | my $ret = chdir $test->workdir; | |||||||||||||||||||
| 204 | 36 | $test->no_result(! $ret, undef, 1); | |||||||||||||||||||
| 205 | 36 | ▶ | if (! grep {$_ eq 'match_sub'} @_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | 36 | $test->match_sub(\&Test::Cmd::diff_exact); | |||||||||||||||||||
| 207 | } | ||||||||||||||||||||
| 208 | 36 | bless($test, $class); | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 8 | ▶ | sub _fail_match_show { | ||||||||||||||||||
|
Subroutine
_fail_match_show: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 214 | 8 | my($self, $stream, $expected, $actual, $level) = @_; | |||||||||||||||||||
| 215 | 8 | my @diffs; | |||||||||||||||||||
| 216 | $self->fail(! $self->match($actual, $expected, \@diffs) | ||||||||||||||||||||
| 217 | 3 | ▶ | => sub {print STDERR | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 218 | "diff expected vs. actual contents of $stream =====\n", | ||||||||||||||||||||
| 219 | @diffs}, | ||||||||||||||||||||
| 220 | 8 | $level + 1); | |||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =item C<run> | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | Runs the program under test, checking that the test succeeded. | ||||||||||||||||||||
| 228 | Arguments are keyword-value pairs that affect the manner in which the | ||||||||||||||||||||
| 229 | program is executed or the results are evaluated. | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | chdir => 'subdir' | ||||||||||||||||||||
| 232 | fail => 'failure condition' # default is '$? != 0' | ||||||||||||||||||||
| 233 | flags => 'Cons flags' | ||||||||||||||||||||
| 234 | stderr => 'expected error output' | ||||||||||||||||||||
| 235 | stdout => 'expected standard output' | ||||||||||||||||||||
| 236 | targets => 'targets to build' | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | The test fails if: | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | -- The specified failure condition is met. The default failure | ||||||||||||||||||||
| 241 | condition is '$? != 0', i.e. the program exits unsuccesfully. | ||||||||||||||||||||
| 242 | A not-uncommon alternative is: | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | $test->run(fail => '$? == 0'); # expect failure | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | when testing how the program handles errors. | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | -- Actual standard output does not match expected standard output | ||||||||||||||||||||
| 249 | (if any). The expected standard output is an array of lines | ||||||||||||||||||||
| 250 | or a scalar which will be split on newlines. | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | -- Actual error output does not match expected error output (if | ||||||||||||||||||||
| 253 | any). The expected error output is an array of lines or a | ||||||||||||||||||||
| 254 | scalar which will be split on newlines. | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | This method will test for NO error output by default if no | ||||||||||||||||||||
| 257 | expected error output is specified (unlike standard output). | ||||||||||||||||||||
| 258 | The error output test may be explicitly suppressed by | ||||||||||||||||||||
| 259 | specifying undef as the "expected" error output: | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | $test->run(stderr => undef); | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | By default, this method performs an exact match of actual vs. expected | ||||||||||||||||||||
| 264 | standard output or error output: | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | $test->run(stdout => <<_EOF_, stderr => _EOF_); | ||||||||||||||||||||
| 267 | An expected STDOUT line, which must be matched exactly. | ||||||||||||||||||||
| 268 | _EOF_ | ||||||||||||||||||||
| 269 | One or more expected STDERR lines, | ||||||||||||||||||||
| 270 | which must be matched exactly. | ||||||||||||||||||||
| 271 | _EOF_ | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | Tests that require regular expression matches should be executed using a | ||||||||||||||||||||
| 274 | test environment that calls the C<match_sub> method as follows: | ||||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | $test->match_sub(\&Test::Cmd::diff_regex); | ||||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | $test->run(stdout => <<_EOF_, stderr => _EOF_); | ||||||||||||||||||||
| 279 | An expected (STDOUT|standard output) line\. | ||||||||||||||||||||
| 280 | _EOF_ | ||||||||||||||||||||
| 281 | One or more expected (STDERR|error output) lines, | ||||||||||||||||||||
| 282 | which may contain (regexes|regular expressions)\. | ||||||||||||||||||||
| 283 | _EOF_ | ||||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | =cut | ||||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | 10 | ▶ | sub run { | ||||||||||||||||||
|
Subroutine
run: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||
| 288 | 10 | my $self = shift; | |||||||||||||||||||
| 289 | 10 | my %args = @_; | |||||||||||||||||||
| 290 | 10 | my $cmd = $args{'args'}; | |||||||||||||||||||
| 291 | 10 | ▶ | if (! $cmd) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 292 | 7 | $cmd = $args{'targets'}; | |||||||||||||||||||
| 293 | 7 | ▶ | $cmd = "$args{'flags'} $cmd" if $args{'flags'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 294 | } | ||||||||||||||||||||
| 295 | 10 | ▶ | my $lev = $args{'level'} || 0; | ||||||||||||||||||
|
Condition50%1/2
$args{'level'} || 0
MC/DC0%0/1
$args{'level'} || 0
$args{'level'}
Truth table50%1/2
A
A= $args{'level'}
| |||||||||||||||||||||
| 296 | 10 | $self->SUPER::run(@_, args => $cmd); | |||||||||||||||||||
| 297 | 10 | ▶ | my $cond = $args{'fail'} || '$? != 0'; | ||||||||||||||||||
|
Condition100%2/2
$args{'fail'} || '$? != 0'
MC/DC100%1/1
$args{'fail'} || '$? != 0'
$args{'fail'}
Truth table100%2/2
A
A= $args{'fail'}
| |||||||||||||||||||||
| 298 | $self->fail(eval $cond | ||||||||||||||||||||
| 299 | 2 | ▶ | => sub {print STDERR $self->stdout, $self->stderr}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 300 | 10 | $lev + 1); | |||||||||||||||||||
| 301 | 8 | ▶ | if (defined $args{'stdout'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 302 | 2 | my @stdout = $self->stdout; | |||||||||||||||||||
| 303 | 2 | $self->_fail_match_show('STDOUT', $args{'stdout'}, \@stdout, $lev + 1); | |||||||||||||||||||
| 304 | } | ||||||||||||||||||||
| 305 | 7 | ▶ | $args{'stderr'} = '' if ! grep($_ eq 'stderr', keys %args); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 306 | 7 | ▶ | if (defined $args{'stderr'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 307 | 6 | my @stderr = $self->stderr; | |||||||||||||||||||
| 308 | 6 | $self->_fail_match_show('STDERR', $args{'stderr'}, \@stderr, $lev + 1); | |||||||||||||||||||
| 309 | } | ||||||||||||||||||||
| 310 | } | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | =item C<subdir> | ||||||||||||||||||||
| 315 | |||||||||||||||||||||
| 316 | Creates one or more subdirectories in the temporary working directory. | ||||||||||||||||||||
| 317 | Exits NO RESULT if the number of subdirectories actually created does | ||||||||||||||||||||
| 318 | not match the number expected. For compatibility with its superclass | ||||||||||||||||||||
| 319 | method, returns the number of subdirectories actually created. | ||||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | =cut | ||||||||||||||||||||
| 322 | |||||||||||||||||||||
| 323 | 5 | ▶ | sub subdir { | ||||||||||||||||||
|
Subroutine
subdir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 324 | 5 | my $self = shift; | |||||||||||||||||||
| 325 | 5 | my $expected = @_; | |||||||||||||||||||
| 326 | 5 | my $ret = $self->SUPER::subdir(@_); | |||||||||||||||||||
| 327 | $self->no_result($expected != $ret, | ||||||||||||||||||||
| 328 | 2 | ▶ | => sub {print STDERR "could not create subdirectories: $!\n"}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 329 | 5 | 1); | |||||||||||||||||||
| 330 | 3 | return $ret; | |||||||||||||||||||
| 331 | } | ||||||||||||||||||||
| 332 | |||||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | =item C<read> | ||||||||||||||||||||
| 336 | |||||||||||||||||||||
| 337 | Reads the contents of a file, depositing the contents in the destination | ||||||||||||||||||||
| 338 | referred to by the first argument (a scalar or array reference). If the | ||||||||||||||||||||
| 339 | file name is not an absolute path name, it is relative to the temporary | ||||||||||||||||||||
| 340 | working directory. Exits NO RESULT if the file could not be read for | ||||||||||||||||||||
| 341 | any reason. For compatibility with its superclass method, returns TRUE | ||||||||||||||||||||
| 342 | on success. | ||||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | =cut | ||||||||||||||||||||
| 345 | |||||||||||||||||||||
| 346 | 16 | ▶ | sub read { | ||||||||||||||||||
|
Subroutine
read: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 347 | 16 | my $self = shift; | |||||||||||||||||||
| 348 | 16 | my $destref = shift; | |||||||||||||||||||
| 349 | 16 | my $ret = $self->SUPER::read($destref, @_); | |||||||||||||||||||
| 350 | $self->no_result(! $ret | ||||||||||||||||||||
| 351 | 2 | ▶ | => sub {print STDERR "could not read file contents: $!\n"}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 352 | 16 | 1); | |||||||||||||||||||
| 353 | 14 | return 1; | |||||||||||||||||||
| 354 | } | ||||||||||||||||||||
| 355 | |||||||||||||||||||||
| 356 | |||||||||||||||||||||
| 357 | |||||||||||||||||||||
| 358 | =item C<write> | ||||||||||||||||||||
| 359 | |||||||||||||||||||||
| 360 | Writes a file with the specified contents. If the file name is not an | ||||||||||||||||||||
| 361 | absolute path name, it is relative to the temporary working directory. | ||||||||||||||||||||
| 362 | Exits NO RESULT if there were any errors writing the file. | ||||||||||||||||||||
| 363 | For compatibility with its superclass method, returns TRUE on success. | ||||||||||||||||||||
| 364 | |||||||||||||||||||||
| 365 | $test->write('file', <<_EOF_); | ||||||||||||||||||||
| 366 | contents of the file | ||||||||||||||||||||
| 367 | _EOF_ | ||||||||||||||||||||
| 368 | |||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | 17 | ▶ | sub write { | ||||||||||||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 372 | 17 | my $self = shift; | |||||||||||||||||||
| 373 | 17 | my $file = shift; # the file to write to | |||||||||||||||||||
| 374 | 17 | my $ret = $self->SUPER::write($file, @_); | |||||||||||||||||||
| 375 | $self->no_result(! $ret | ||||||||||||||||||||
| 376 | 1 | ▶ | => sub {$file = $self->catfile(@$file) if ref $file; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||
| 377 | 1 | print STDERR "could not write $file: $!\n"}, | |||||||||||||||||||
| 378 | 17 | 1); | |||||||||||||||||||
| 379 | 16 | return 1; | |||||||||||||||||||
| 380 | } | ||||||||||||||||||||
| 381 | |||||||||||||||||||||
| 382 | |||||||||||||||||||||
| 383 | |||||||||||||||||||||
| 384 | =item C<file_matches> | ||||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | Matches the contents of the specified file (first argument) against the | ||||||||||||||||||||
| 387 | expected contents. The expected contents are an array of lines or a | ||||||||||||||||||||
| 388 | scalar which will be split on newlines. By default, each expected line | ||||||||||||||||||||
| 389 | must match exactly its corresponding line in the file: | ||||||||||||||||||||
| 390 | |||||||||||||||||||||
| 391 | $test->file_matches('file', <<_EOF_); | ||||||||||||||||||||
| 392 | Line #1. | ||||||||||||||||||||
| 393 | Line #2. | ||||||||||||||||||||
| 394 | _EOF_ | ||||||||||||||||||||
| 395 | |||||||||||||||||||||
| 396 | Tests that require regular expression matches should be executed using a | ||||||||||||||||||||
| 397 | test environment that calls the C<match_sub> method as follows: | ||||||||||||||||||||
| 398 | |||||||||||||||||||||
| 399 | $test->match_sub(\&Test::Cmd::diff_regex); | ||||||||||||||||||||
| 400 | |||||||||||||||||||||
| 401 | $test->file_matches('file', <<_EOF_); | ||||||||||||||||||||
| 402 | The (1st|first) line\. | ||||||||||||||||||||
| 403 | The (2nd|second) line\. | ||||||||||||||||||||
| 404 | _EOF_ | ||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | =cut | ||||||||||||||||||||
| 407 | |||||||||||||||||||||
| 408 | 6 | ▶ | sub file_matches { | ||||||||||||||||||
|
Subroutine
file_matches: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 409 | 6 | my($self, $file, $regexes) = @_; | |||||||||||||||||||
| 410 | 6 | my @lines; | |||||||||||||||||||
| 411 | 6 | my $ret = $self->SUPER::read(\@lines, $file); | |||||||||||||||||||
| 412 | $self->no_result(! $ret | ||||||||||||||||||||
| 413 | 1 | ▶ | => sub {print STDERR "could not read contents of $file: $!\n"}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 414 | 6 | 1); | |||||||||||||||||||
| 415 | 5 | my @diffs; | |||||||||||||||||||
| 416 | $self->fail(! $self->match(\@lines, $regexes, \@diffs) | ||||||||||||||||||||
| 417 | 1 | ▶ | => sub {$file = $self->catfile(@$file) if ref $file; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||
| 418 | 1 | print STDERR | |||||||||||||||||||
| 419 | "diff expected vs. actual contents of $file =====\n", | ||||||||||||||||||||
| 420 | @diffs}, | ||||||||||||||||||||
| 421 | 5 | 1); | |||||||||||||||||||
| 422 | } | ||||||||||||||||||||
| 423 | |||||||||||||||||||||
| 424 | |||||||||||||||||||||
| 425 | |||||||||||||||||||||
| 426 | =item C<must_exist> | ||||||||||||||||||||
| 427 | |||||||||||||||||||||
| 428 | Ensures that the specified files must exist. Files may be specified as | ||||||||||||||||||||
| 429 | an array reference of directory components, in which case the pathname | ||||||||||||||||||||
| 430 | will be constructed by concatenating them. Exits FAILED if any of the | ||||||||||||||||||||
| 431 | files does not exist. | ||||||||||||||||||||
| 432 | |||||||||||||||||||||
| 433 | =cut | ||||||||||||||||||||
| 434 | |||||||||||||||||||||
| 435 | 2 | ▶ | sub must_exist { | ||||||||||||||||||
|
Subroutine
must_exist: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 436 | 2 | my $self = shift; | |||||||||||||||||||
| 437 | 2 | ▶ | map(ref $_ ? $self->catfile(@$_) : $_, @_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 438 | 2 | my @missing = grep(! -e $_, @_); | |||||||||||||||||||
| 439 | 2 | ▶ | $self->fail(0 + @missing => sub {print STDERR "files are missing: @missing\n"}, 1); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 440 | } | ||||||||||||||||||||
| 441 | |||||||||||||||||||||
| 442 | |||||||||||||||||||||
| 443 | |||||||||||||||||||||
| 444 | =item C<must_not_exist> | ||||||||||||||||||||
| 445 | |||||||||||||||||||||
| 446 | Ensures that the specified files must not exist. Files may be specified | ||||||||||||||||||||
| 447 | as an array reference of directory components, in which case the pathname | ||||||||||||||||||||
| 448 | will be constructed by concatenating them. Exits FAILED if any of the | ||||||||||||||||||||
| 449 | files exists. | ||||||||||||||||||||
| 450 | |||||||||||||||||||||
| 451 | =cut | ||||||||||||||||||||
| 452 | |||||||||||||||||||||
| 453 | 2 | ▶ | sub must_not_exist { | ||||||||||||||||||
|
Subroutine
must_not_exist: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 454 | 2 | my $self = shift; | |||||||||||||||||||
| 455 | 2 | ▶ | map(ref $_ ? $self->catfile(@$_) : $_, @_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 456 | 2 | my @exist = grep(-e $_, @_); | |||||||||||||||||||
| 457 | 2 | ▶ | $self->fail(0 + @exist => sub {print STDERR "unexpected files exist: @exist\n"}, 1); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 458 | } | ||||||||||||||||||||
| 459 | |||||||||||||||||||||
| 460 | |||||||||||||||||||||
| 461 | |||||||||||||||||||||
| 462 | =item C<copy> | ||||||||||||||||||||
| 463 | |||||||||||||||||||||
| 464 | Copies a file from the source (first argument) to the destination | ||||||||||||||||||||
| 465 | (second argument). Exits NO RESULT if the file could not be copied | ||||||||||||||||||||
| 466 | for any reason. | ||||||||||||||||||||
| 467 | |||||||||||||||||||||
| 468 | =cut | ||||||||||||||||||||
| 469 | |||||||||||||||||||||
| 470 | 2 | ▶ | sub copy { | ||||||||||||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 471 | 2 | my($self, $src, $dest) = @_; | |||||||||||||||||||
| 472 | 2 | my $ret = File::Copy::copy($src, $dest); | |||||||||||||||||||
| 473 | $self->no_result(! $ret | ||||||||||||||||||||
| 474 | 1 | ▶ | => sub {print STDERR "could not copy $src to $dest: $!\n"}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 475 | 2 | 1); | |||||||||||||||||||
| 476 | } | ||||||||||||||||||||
| 477 | |||||||||||||||||||||
| 478 | |||||||||||||||||||||
| 479 | |||||||||||||||||||||
| 480 | =item C<chmod> | ||||||||||||||||||||
| 481 | |||||||||||||||||||||
| 482 | Changes the permissions of a list of files to the specified mode (first | ||||||||||||||||||||
| 483 | argument). Exits NO RESULT if any file could not be changed for any | ||||||||||||||||||||
| 484 | reason. | ||||||||||||||||||||
| 485 | |||||||||||||||||||||
| 486 | =cut | ||||||||||||||||||||
| 487 | |||||||||||||||||||||
| 488 | 2 | ▶ | sub chmod { | ||||||||||||||||||
|
Subroutine
chmod: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 489 | 2 | my $self = shift; | |||||||||||||||||||
| 490 | 2 | my $mode = shift; | |||||||||||||||||||
| 491 | 2 | my $expected = @_; | |||||||||||||||||||
| 492 | 2 | my $ret = CORE::chmod($mode, @_); | |||||||||||||||||||
| 493 | $self->no_result($expected != $ret, | ||||||||||||||||||||
| 494 | 1 | ▶ | => sub {print STDERR "could not chmod files: $!\n"}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 495 | 2 | 1); | |||||||||||||||||||
| 496 | } | ||||||||||||||||||||
| 497 | |||||||||||||||||||||
| 498 | |||||||||||||||||||||
| 499 | |||||||||||||||||||||
| 500 | =item C<sleep> | ||||||||||||||||||||
| 501 | |||||||||||||||||||||
| 502 | Sleeps at least the specified number of seconds. If no number is | ||||||||||||||||||||
| 503 | specified, sleeps at least a minimum number of seconds necessary to | ||||||||||||||||||||
| 504 | advance file time stamps on the current system. Sleeping more seconds | ||||||||||||||||||||
| 505 | is all right. Exits NO RESULT if the time slept was less than specified. | ||||||||||||||||||||
| 506 | |||||||||||||||||||||
| 507 | =cut | ||||||||||||||||||||
| 508 | |||||||||||||||||||||
| 509 | 1 | ▶ | sub sleep { | ||||||||||||||||||
|
Subroutine
sleep: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||
| 510 | 1 | my($self, $seconds) = @_; | |||||||||||||||||||
| 511 | # On Windows systems, DOS and FAT file systems have only a | ||||||||||||||||||||
| 512 | # two-second granularity, so we must sleep two seconds to | ||||||||||||||||||||
| 513 | # ensure that file time stamps will be newer. | ||||||||||||||||||||
| 514 | 1 | ▶ | $seconds = $_is_win32 ? 2 : 1 if ! defined $seconds; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 515 | 1 | my $ret = CORE::sleep($seconds); | |||||||||||||||||||
| 516 | $self->no_result($ret < $seconds, | ||||||||||||||||||||
| 517 | 0 | ▶ | => sub {print STDERR "only slept $ret seconds\n"}, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 518 | 1 | 1); | |||||||||||||||||||
| 519 | } | ||||||||||||||||||||
| 520 | |||||||||||||||||||||
| 521 | |||||||||||||||||||||
| 522 | |||||||||||||||||||||
| 523 | =item C<touch> | ||||||||||||||||||||
| 524 | |||||||||||||||||||||
| 525 | Updates the access and modification times of the specified files. | ||||||||||||||||||||
| 526 | Exits NO RESULT if any file could not be modified for any reason. | ||||||||||||||||||||
| 527 | |||||||||||||||||||||
| 528 | =cut | ||||||||||||||||||||
| 529 | |||||||||||||||||||||
| 530 | 2 | ▶ | sub touch { | ||||||||||||||||||
|
Subroutine
touch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 531 | 2 | my $self = shift; | |||||||||||||||||||
| 532 | 2 | my $time = shift; | |||||||||||||||||||
| 533 | 2 | my $expected = @_; | |||||||||||||||||||
| 534 | 2 | my $ret = CORE::utime($time, $time, @_); | |||||||||||||||||||
| 535 | $self->no_result($expected != $ret, | ||||||||||||||||||||
| 536 | 1 | ▶ | => sub {print STDERR "could not touch files: $!\n"}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 537 | 2 | 1); | |||||||||||||||||||
| 538 | } | ||||||||||||||||||||
| 539 | |||||||||||||||||||||
| 540 | |||||||||||||||||||||
| 541 | |||||||||||||||||||||
| 542 | =item C<unlink> | ||||||||||||||||||||
| 543 | |||||||||||||||||||||
| 544 | Removes the specified files. Exits NO RESULT if any file could not be | ||||||||||||||||||||
| 545 | removed for any reason. | ||||||||||||||||||||
| 546 | |||||||||||||||||||||
| 547 | =cut | ||||||||||||||||||||
| 548 | |||||||||||||||||||||
| 549 | 2 | ▶ | sub unlink { | ||||||||||||||||||
|
Subroutine
unlink: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 550 | 2 | my $self = shift; | |||||||||||||||||||
| 551 | 2 | my @not_removed; | |||||||||||||||||||
| 552 | my $file; | ||||||||||||||||||||
| 553 | 2 | foreach $file (@_) { | |||||||||||||||||||
| 554 | 3 | ▶ | $file = $self->catfile(@$file) if ref $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 555 | 3 | ▶ | if (! CORE::unlink($file)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 556 | 2 | push @not_removed, $file; | |||||||||||||||||||
| 557 | } | ||||||||||||||||||||
| 558 | } | ||||||||||||||||||||
| 559 | $self->no_result(@not_removed != 0, | ||||||||||||||||||||
| 560 | 1 | ▶ | => sub {print STDERR "could not unlink files (@not_removed): $!\n"}, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 561 | 2 | 1); | |||||||||||||||||||
| 562 | } | ||||||||||||||||||||
| 563 | |||||||||||||||||||||
| 564 | |||||||||||||||||||||
| 565 | |||||||||||||||||||||
| 566 | 37 | 1; | |||||||||||||||||||
| 567 | __END__ | ||||||||||||||||||||