| 1 | # ************************************************************************* | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2014-2020, SUSE LLC | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # All rights reserved. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Redistribution and use in source and binary forms, with or without | ||||||||||||||||||||||||||||
| 7 | # modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||||||||||||||||||||||||||||
| 10 | # this list of conditions and the following disclaimer. | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||||||||||||||||||||||||||||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||||||||||||||||||||||||||||
| 14 | # documentation and/or other materials provided with the distribution. | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # 3. Neither the name of SUSE LLC nor the names of its contributors may be | ||||||||||||||||||||||||||||
| 17 | # used to endorse or promote products derived from this software without | ||||||||||||||||||||||||||||
| 18 | # specific prior written permission. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||||||||||||||||||||||||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||||||||||||||||||||||||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||||||||||||||||||||||||||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||||||||||||||||||||||||||||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||||||||||||||||||||||||||||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||||||||||||||||||||||||||||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||||||||||||||||||||||||||||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||||||||||||||||||||||||||||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||||||||||||||||||||||||||||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||||||||||||||||||||||||||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 31 | # ************************************************************************* | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | package App::CELL::Test; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 17 | ▶ | use 5.012; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 17 | ▶ | use App::CELL::Log qw( $log ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 17 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 NAME | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | App::CELL::Test - functions for unit testing | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | use App::CELL::Test; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | App::CELL::Test::cleartmpdir(); | ||||||||||||||||||||||||||||
| 52 | my $tmpdir = App::CELL::Test::mktmpdir(); | ||||||||||||||||||||||||||||
| 53 | App::CELL::Test::touch_files( $tmpdir, 'foo', 'bar', 'baz' ); | ||||||||||||||||||||||||||||
| 54 | my $booltrue = App::CELL::Test::cmp_arrays( | ||||||||||||||||||||||||||||
| 55 | [ 0, 1, 2 ], [ 0, 1, 2 ] | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | my $boolfalse = App::CELL::Test::cmp_arrays( | ||||||||||||||||||||||||||||
| 58 | [ 0, 1, 2 ], [ 'foo', 'bar', 'baz' ] | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | The C<App::CELL::Test> module provides a number of special-purpose functions for | ||||||||||||||||||||||||||||
| 65 | use in CELL's test suite. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | This module exports the following routines: | ||||||||||||||||||||||||||||
| 72 | cleartmpdir | ||||||||||||||||||||||||||||
| 73 | cmp_arrays | ||||||||||||||||||||||||||||
| 74 | mktmpdir | ||||||||||||||||||||||||||||
| 75 | populate_file | ||||||||||||||||||||||||||||
| 76 | touch_files | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 17 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 17 | our @EXPORT_OK = qw( cleartmpdir cmp_arrays mktmpdir populate_file | |||||||||||||||||||||||||||
| 82 | touch_files _touch ); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 PACKAGE VARIABLES | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 17 | our $tdo; # temporary directory object | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 mktmpdir | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Creates the App::CELL testing directory in a temporary directory | ||||||||||||||||||||||||||||
| 100 | (obtained using L<File::Temp>) and returns the path to this directory in | ||||||||||||||||||||||||||||
| 101 | the payload of a status object. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | ▶ | sub mktmpdir { | ||||||||||||||||||||||||||
|
Subroutine
mktmpdir: called CC=1 SCAR=0.5
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 17 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 4 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 17 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | 4 | $tdo = File::Temp->newdir(); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my $errmsg = $_ || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_ || ''
MC/DC0%0/1
$_ || ''
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||
| 115 | 0 | $errmsg =~ s/\n//g; | |||||||||||||||||||||||||||
| 116 | 0 | $errmsg =~ s/\012/ -- /g; | |||||||||||||||||||||||||||
| 117 | 0 | return App::CELL::Status->new( level => 'ERR', | |||||||||||||||||||||||||||
| 118 | code => 'CELL_CREATE_TMPDIR_FAIL', | ||||||||||||||||||||||||||||
| 119 | args => [ $errmsg ], | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | 4 | }; | |||||||||||||||||||||||||||
| 122 | 4 | $log->debug( "Created temporary directory" . $tdo ); | |||||||||||||||||||||||||||
| 123 | 4 | return App::CELL::Status->ok( $tdo->dirname ); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 cleartmpdir | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | DESTROYs the temporary directory object (see L<File::Temp>). | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2 | ▶ | sub cleartmpdir { | ||||||||||||||||||||||||||
|
Subroutine
cleartmpdir: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | $tdo->DESTROY if defined $tdo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 2 | return App::CELL::Status->ok; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head3 _touch | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Touch a file | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 18 | ▶ | sub _touch { | ||||||||||||||||||||||||||
|
Subroutine
_touch: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 146 | 18 | my ( $file ) = @_; | |||||||||||||||||||||||||||
| 147 | 18 | my $now = time; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 18 | ▶ | utime ($now, $now, $file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$file || warn("Couldn't touch file: $!\n")
MC/DC0%0/1
$file || warn("Couldn't touch file: $!\n")
$file
Truth table50%1/2
A
A= $file
| |||||||||||||||||||||||||||||
| 150 | || open my $fh, ">>", $file | ||||||||||||||||||||||||||||
| 151 | || warn ("Couldn't touch file: $!\n"); | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head2 touch_files | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | "Touch" some files. Takes: directory path and list of files to "touch" in | ||||||||||||||||||||||||||||
| 158 | that directory. Returns number of files successfully touched. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | ▶ | sub touch_files { | ||||||||||||||||||||||||||
|
Subroutine
touch_files: called CC=1 SCAR=0.6
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 2 | my ( $dirspec, @file_list ) = @_; | |||||||||||||||||||||||||||
| 164 | 17 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | my $count = @file_list; | |||||||||||||||||||||||||||
| 167 | 2 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 168 | 2 | foreach my $file ( map { File::Spec->catfile( $dirspec, $_ ); } @file_list ) { | |||||||||||||||||||||||||||
| 169 | 10 | _touch( $file ); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 173 | 0 | my $errmsg = $_; | |||||||||||||||||||||||||||
| 174 | 0 | $errmsg =~ s/\n//g; | |||||||||||||||||||||||||||
| 175 | 0 | $errmsg =~ s/\012/ -- /g; | |||||||||||||||||||||||||||
| 176 | 0 | $errmsg = "Attempting to 'touch' $count files in $dirspec . . . failure: $errmsg"; | |||||||||||||||||||||||||||
| 177 | 0 | $log->debug( $errmsg ); | |||||||||||||||||||||||||||
| 178 | 0 | print STDERR $errmsg, "\n"; | |||||||||||||||||||||||||||
| 179 | 0 | return 0; | |||||||||||||||||||||||||||
| 180 | 2 | }; | |||||||||||||||||||||||||||
| 181 | 2 | $log->debug( "Attempting to 'touch' $count files in $dirspec . . . success" ); | |||||||||||||||||||||||||||
| 182 | 2 | return $count; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 populate_file | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Takes filename (full path) and contents (as a string, potentially | ||||||||||||||||||||||||||||
| 189 | containing newlines) to write to it. If the file exists, it is first | ||||||||||||||||||||||||||||
| 190 | unlinked. Then the routine creates the file and populates it with | ||||||||||||||||||||||||||||
| 191 | the contents. Returns true if something was written, or false if not. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 8 | ▶ | sub populate_file { | ||||||||||||||||||||||||||
|
Subroutine
populate_file: called CC=6 SCAR=19.2
Pod
documented
| |||||||||||||||||||||||||||||
| 196 | 8 | my ( $full_path, $contents ) = @_; | |||||||||||||||||||||||||||
| 197 | 8 | unlink $full_path; | |||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||
| 199 | 8 | ▶ | _touch( $full_path ) or die "Could not touch $full_path"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 8 | ▶ | return 0 unless -f $full_path and -W $full_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $full_path and -W $full_path
MC/DC0%0/2
-f $full_path and -W $full_path
-f $full_path
-W $full_path
Truth table33%1/3
A and B
A= -f $full_pathB= -W $full_path
| |||||||||||||||||||||||||||||
| 202 | 8 | ▶ | return 0 unless $contents; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 8 | ▶ | open(my $fh, '>', $full_path ) or die "Could not open file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 8 | print $fh $contents; | |||||||||||||||||||||||||||
| 205 | 8 | close $fh; | |||||||||||||||||||||||||||
| 206 | 8 | return length $contents; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head2 cmp_arrays | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | Compare two arrays of unique elements, order doesn't matter. | ||||||||||||||||||||||||||||
| 212 | Takes: two array references | ||||||||||||||||||||||||||||
| 213 | Returns: true (they have the same elements) or false (they differ). | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 8 | ▶ | sub cmp_arrays { | ||||||||||||||||||||||||||
|
Subroutine
cmp_arrays: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | 8 | my ( $ref1, $ref2 ) = @_; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 8 | $log->debug( "cmp_arrays: we were asked to compare two arrays:"); | |||||||||||||||||||||||||||
| 221 | 8 | $log->debug( "ARRAY #1: " . join( ',', @$ref1 ) ); | |||||||||||||||||||||||||||
| 222 | 8 | $log->debug( "ARRAY #2: " . join( ',', @$ref2 ) ); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # convert them into hashes | ||||||||||||||||||||||||||||
| 225 | 8 | my ( %ref1, %ref2 ); | |||||||||||||||||||||||||||
| 226 | 8 | map { $ref1{ $_ } = ''; } @$ref1; | |||||||||||||||||||||||||||
| 227 | 8 | map { $ref2{ $_ } = ''; } @$ref2; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # make a copy of ref1 | ||||||||||||||||||||||||||||
| 230 | 8 | my %ref1_copy = %ref1; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # for each element of ref1, if it matches an element in ref2, delete | ||||||||||||||||||||||||||||
| 233 | # the element from _BOTH_ | ||||||||||||||||||||||||||||
| 234 | 8 | foreach ( keys( %ref1_copy ) ) { | |||||||||||||||||||||||||||
| 235 | 17 | ▶ | if ( exists( $ref2{ $_ } ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 14 | delete $ref1{ $_ }; | |||||||||||||||||||||||||||
| 237 | 14 | delete $ref2{ $_ }; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # if the two arrays are the same, the number of keys in both hashes should | ||||||||||||||||||||||||||||
| 242 | # be zero | ||||||||||||||||||||||||||||
| 243 | 8 | $log->debug( "cmp_arrays: after comparison, hash #1 has " . keys( %ref1 ) | |||||||||||||||||||||||||||
| 244 | . " elements and hash #2 has " . keys ( %ref2 ) . " elements" ); | ||||||||||||||||||||||||||||
| 245 | 8 | ▶ | if ( keys( %ref1 ) == 0 and keys( %ref2 ) == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
keys %ref1 == 0 and keys %ref2 == 0
MC/DC100%2/2
keys %ref1 == 0 and keys %ref2 == 0
keys %ref1 == 0
keys %ref2 == 0
Truth table100%3/3
A and B
A= keys %ref1 == 0B= keys %ref2 == 0
| |||||||||||||||||||||||||||||
| 246 | 6 | return 1; | |||||||||||||||||||||||||||
| 247 | } else { | ||||||||||||||||||||||||||||
| 248 | 2 | return 0; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 17 | 1; | |||||||||||||||||||||||||||