1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
# NAME
**Test::Files** - A [Test::Builder](https://metacpan.org/pod/Test::Builder)
based module to ease testing with files and dirs.
In general, the following can be tested:
- If the contents of the file being tested match the expected pattern.
- If the file being tested is identical to the expected file in regard to contents, or size, or existence.
If necessary, some parts of the contents can be excluded from the comparison.
- If the directory being tested contains all expected files.
- If the files in the directory being tested are identical to the files in the reference directory
in regard to contents, or size, or existence.
If necessary, some files as well as some parts of contents can be excluded from the comparison.
- If all files in the directory being tested fulfill certain requirements.
- If the archive (container) being tested is logically identical to the the reference archive (container).
If necessary, some members of archives, as well as some parts of their contents, as well as some metadata
can be excluded from the comparison.
# SYNOPSIS
All examples listed below can be found and executed using **xt/synopsis.t**
located on [GitHub](https://github.com/jsf116/Test-Files).
```perl
use Path::Tiny qw( path );
use Test::Files;
my $got_file = path( 'path' )->child( qw( got file ) );
my $reference_file = path( 'path' )->child( qw( reference file ) );
my $got_dir = path( 'path' )->child( qw( got dir ) );
my $reference_dir = path( 'path' )->child( qw( reference dir with some stuff ) );
my @file_list = qw( expected file );
my ( $content_check, $expected, $filter, $options );
plan( 24 );
# Simply compares file contents to a string:
$expected = "contents\nof file";
file_ok( $got_file, $expected, 'got file has expected contents' );
# Two identical variants comparing file contents
# to a string ignoring differences in time stamps:
$expected = "filtered contents\nof file\ncreated at 00:00:00";
$filter = sub {
shift =~ s{ \b (?: [01] \d | 2 [0-3] ) : (?: [0-5] \d ) : (?: [0-5] \d ) \b }
{00:00:00}grx
};
$options = { FILTER => $filter };
file_ok (
$got_file, $expected, $options,
"'$got_file' has contents expected after filtering"
);
file_filter_ok(
$got_file, $expected, $filter,
"'$got_file' has contents expected after filtering"
);
# Simply compares two file contents:
compare_ok( $got_file, $reference_file, 'files are the same' );
# Two identical variants comparing contents of two files
# ignoring differences in time stamps:
$filter = sub {
shift =~ s{ \b (?: [01] \d | 2 [0-3] ) : (?: [0-5] \d ) : (?: [0-5] \d ) \b }
{00:00:00}grx
};
$options = { FILTER => $filter };
compare_ok (
$got_file, $reference_file, $options, 'files are almost the same'
);
compare_filter_ok(
$got_file, $reference_file, $filter, 'files are almost the same'
);
# Verifies if both got file and reference file exist:
$options = { EXISTENCE_ONLY => 1 };
compare_ok( $got_file, $reference_file, $options, 'both files exist' );
# Verifies if got file and reference file have identical size:
$options = { SIZE_ONLY => 1 };
compare_ok(
$got_file, $reference_file, $options, 'both files have identical size'
);
# Verifies if the directory has all expected files (not recursively!):
$expected = [ qw( files got_dir must contain ) ];
dir_contains_ok( $got_dir, $expected, 'directory has all files in list' );
# Two identical variants doing the same verification as before,
# but additionally verifying if the directory has nothing
# but the expected files (not recursively!):
$options = { SYMMETRIC => 1 };
dir_contains_ok (
$got_dir, $expected, $options, 'directory has exactly the files in the list'
);
dir_only_contains_ok(
$got_dir, $expected, 'directory has exactly the files in the list'
);
# The same as before, but recursive:
$options = { RECURSIVE => 1, SYMMETRIC => 1 };
dir_contains_ok(
$got_dir, $expected, $options,
'directory and its subdirectories have exactly the files in the list'
);
# The same as before, but ignoring files,
# which names do not match the required pattern (file "must" will be skipped):
$options = { NAME_PATTERN => '^[cfg]', RECURSIVE => 1, SYMMETRIC => 1 };
dir_contains_ok(
$got_dir, $expected, $options,
'directory and its subdirectories ' .
"have exactly the files in the list except of file 'must'"
);
# Compares two directories by comparing file contents (not recursively!):
compare_dirs_ok(
$got_dir, $reference_dir,
"all files from '$got_dir' are the same in '$reference_dir' " .
'(same names, same contents), subdirs are skipped'
);
# The same as before, but subdirectories are considered, too:
$options = { RECURSIVE => 1 };
compare_dirs_ok(
$got_dir, $reference_dir, $options,
"all files from '$got_dir' and its subdirs are the same in '$reference_dir'"
);
# The same as before, but only file sizes are compared:
$options = { RECURSIVE => 1, SIZE_ONLY => 1 };
compare_dirs_ok(
$got_dir, $reference_dir, $options,
"all files from '$got_dir' and its subdirs have same sizes in '$reference_dir'"
);
# The same as before, but only file existence is verified:
$options = { EXISTENCE_ONLY => 1, RECURSIVE => 1 };
compare_dirs_ok(
$got_dir, $reference_dir, $options,
"all files from '$got_dir' and its subdirs exist in '$reference_dir'"
);
# The same as before, but only files with base names starting with 'A' are considered:
$options = { EXISTENCE_ONLY => 1, NAME_PATTERN => '^A', RECURSIVE => 1 };
compare_dirs_ok(
$got_dir, $reference_dir, $options,
"all files from '$got_dir' and its subdirs " .
"with base names starting with 'A' exist in '$reference_dir'"
);
# The same as before, but the symmetric verification is requested:
$options = {
EXISTENCE_ONLY => 1,
NAME_PATTERN => '^A',
RECURSIVE => 1,
SYMMETRIC => 1,
};
compare_dirs_ok(
$got_dir, $reference_dir, $options,
"all files from '$got_dir' and its subdirs with base names " .
"starting with 'A' exist in '$reference_dir' and vice versa"
);
# Two identical variants of comparison of two directories by file contents,
# whereas these contents are first filtered
# so that time stamps in form of 'HH:MM:SS' are replaced by '00:00:00'
# like in examples for file_filter_ok and compare_filter_ok:
$filter = sub {
shift =~ s{ \b (?: [01] \d | 2 [0-3] ) : (?: [0-5] \d ) : (?: [0-5] \d ) \b }
{00:00:00}grx
};
$options = { FILTER => $filter };
compare_dirs_ok(
$got_dir, $reference_dir, $options,
"all files from '$got_dir' are the same in '$reference_dir', " .
'subdirs are skipped, differences of time stamps ignored'
);
compare_dirs_filter_ok(
$got_dir, $reference_dir, $filter,
"all files from '$got_dir' are the same in '$reference_dir', " .
'subdirs are skipped, differences of time stamps ignored'
);
# Verifies if all plain files in directory and its subdirectories
# contain the word 'good' (take into consideration the -f test below
# excluding special files from comparison!):
$content_check = sub {
my ( $file ) = @_;
! -f $file or path( $file )->slurp =~ / \b good \b /x;
};
$options = { RECURSIVE => 1 };
find_ok(
$got_dir, $content_check, $options,
"all files from '$got_dir' and subdirectories contain the word 'good'"
);
# Compares PKZIP archives considering both global and file comments.
# Both archives contain the same members in different order:
my $extract = sub {
my ( $file ) = @_;
my $zip = Archive::Zip->new();
die( "Cannot read '$file'" ) if $zip->read( $file ) != AZ_OK;
die( "Cannot extract from '$file'" ) if $zip->extractTree != AZ_OK;
};
my $meta_data = sub {
my ( $file ) = @_;
my $zip = Archive::Zip->new();
die( "Cannot read '$file'" ) if $zip->read( $file ) != AZ_OK;
my %meta_data = ( '' => $zip->zipfileComment );
$meta_data{ $_->fileName } = $_->fileComment foreach $zip->members;
return \%meta_data;
};
my $got_compressed_content = path( "$got_file.zip" )->slurp;
my $reference_compressed_content = path( "$reference_file.zip" )->slurp;
ok(
$got_compressed_content ne $reference_compressed_content,
"'$got_file.zip' and '$reference_file.zip' are physically different, but"
);
compare_archives_ok(
"$got_file.zip", "$reference_file.zip", { EXTRACT => $extract, META_DATA => $meta_data },
"'$got_file.zip' and '$reference_file.zip' are logically identical"
);
```
# DESCRIPTION
This module is like [Test2::V0](https://metacpan.org/pod/Test2::V0) or
[Test::Expander](https://metacpan.org/pod/Test::Expander),
in fact you should use that first as shown above.
It supports comparison of files and directories in different ways.
Any file or directory passed to functions of this module can be both a string or an object of
[Path::Tiny](https://metacpan.org/pod/Path::Tiny).
Though the test names i.e. the last parameter of every function is optional,
you should provide a name of each test for a better maintainability.
You should follow the lead of the ["SYNOPSIS"](#synopsis) examples and use [Path::Tiny](https://metacpan.org/pod/Path::Tiny) or,
if you prefer, [File::Spec](https://metacpan.org/pod/File::Spec).
This makes it much more likely that your tests will pass on a different operating system.
All of the contents comparison routines provide diff diagnostic output when they report failure.
The diff output style can be changed using the option **STYLE** (see below).
The filter function receives each line of each file.
It may perform any necessary transformations (like excising dates),
then it must return the line in (possibly) transformed state.
For example, the first filter of [Phil Crow](https://metacpan.org/author/PHILCROW), the creator of this module was
```perl
sub chop_dates {
my $line = shift;
$line =~ s/\d{4}(.\d\d){5}//g;
return $line;
}
```
This removes all strings like **2003.10.14.14.17.37**.
Everything else is unchanged and failing tests started passing when they should.
If you want to exclude the line from consideration, return empty string or **undef**.
## FUNCTIONS
### file\_ok
There are two forms of calls:
- The generic form.
```perl
file_ok( $got_file, $expected_string, \%options, $test_name )
```
- The short form, which is also backward compatible.
```perl
file_ok( $got_file, $expected_string, $test_name )
```
Compares the contents of a file **$got\_file** to a string **$expected\_string**.
In the generic form, if the parameter **\\%options** is passed and contains the key **FILTER**,
**file\_ok** provides the same functionality as **file\_filter\_ok**.
Supported options:
- **FILTER**
Code reference providing filtering of file contents before comparison.
The only expected parameter is the current line from the file contents, the return value replaces this line.
In addition, the special variable **$.** representing the number of the current line in the file can be used.
If the return value is undefined, empty string is returned instead.
Line breaks are neither removed nor added after the execution.
Defaults to **undef** i.e. no filtering is provided.
- All options supported by [Text::Diff](https://metacpan.org/pod/Text::Diff)
except of **FILENAME\_A** and **FILENAME\_B**.
The most useful of them seems to be **STYLE** defining the style of output for content differences.
Defaults to **Unified**.
### file\_filter\_ok
There is only one form of call namely
```perl
file_filter_ok( $got_file, $expected_string, \&filter_func, $test_name )
```
Works like **file\_ok** with the option **FILTER** i.e. compares the contents of a file to a string,
but filters the file first using **&filter\_func** for that. The string contents must be filtered before if necessary.
This function is deprecated and stays for backward compatibility reasons only.
### compare\_ok
There are two forms of calls:
- The generic form.
```perl
compare_ok( $got_file, $reference_file, \%options, $test_name )
```
- The short form, which is also backward compatible.
```perl
compare_ok( $got_file, $reference_file, $test_name )
```
Compares two files.
In the generic form, if the parameter **\\%options** is passed and contains the key **FILTER**,
**compare\_ok** provides the same functionality as **compare\_filter\_ok**.
Supported options:
- **EXISTENCE\_ONLY**
Boolean. If set to **true**, only existence of both **$got\_file** and **$reference\_file** is compared.
Defaults to **false**.
- **FILTER**
Code reference providing filtering of file contents before comparison and
being applied to both **$got\_file** and **$reference\_file**.
The only expected parameter is the current line from the file contents, the return value replaces this line.
In addition, the special variable **$.** representing the number of the current line in the file can be used.
If the return value is undefined, empty string is returned instead.
Line breaks are neither removed nor added after the execution.
Ignored if either **EXISTENCE\_ONLY** or **SIZE\_ONLY** is set to **true**.
Defaults to **undef** i.e. no filtering is provided.
- **SIZE\_ONLY**
Boolean. If set to **true** and the options **EXISTENCE\_ONLY** is not set to **true**,
**$got\_file** and **$reference\_file** are compared by size only.
Defaults to **false**.
- All options supported by [Text::Diff](https://metacpan.org/pod/Text::Diff)
except of **FILENAME\_A** and **FILENAME\_B**.
The most useful of them seems to be **STYLE** defining the style of output for content differences.
Defaults to **Unified**.
### compare\_filter\_ok
There is only one form of call namely
```perl
compare_filter_ok( $got_file, $reference_file, \&filter_func, $test_name )
```
Works like **compare\_ok** with option **FILTER** i.e. compares the contents of two files,
but sends each line through the filter **&filter\_func** so things that shouldn't count against success can be stripped.
This function is deprecated and stays for backward compatibility reasons only.
### dir\_contains\_ok
There are two forms of calls:
- The generic form.
```perl
dir_contains_ok( $got_dir, \@file_list, \%options, $test_name )
```
- The short form, which is also backward compatible.
```perl
dir_contains_ok( $got_dir, \@file_list, $test_name )
```
Verifies the directory **$got\_dir** for the presence of a list files in **@file\_list**.
If **$got\_dir** is a symlink, this will be accepted, but symlinks therein are not followed.
Subdirectories are not involved in the verification, but files located therein are considered
if recursive appraoch is required (see the option **RECURSIVE** below).
Special files like named pipes are involved in the verification only if the sole file existence is required
(see the option **EXISTENCE\_ONLY** below), otherwise they are skipped and reported as error.
In the generic form, if the parameter **\\%options** is passed and
contains the key **SYMMETRIC** set to **true**, **dirs\_contains\_ok** provides the same functionality
as **dir\_only\_contains\_ok**.
Supported options:
- **NAME\_PATTERN**
String containing RegEx. Files with base names not matching this RegEx will be skipped.
Defaults to the dot sign (**.**) i.e. no file will be skipped.
- **RECURSIVE**
Boolean. If set to **true**, subdirectories of **$got\_dir** will be checked, too.
Defaults to **false**.
- **SYMMETRIC**
Boolean. If set to **true**, additionally verifies if all files from **$got\_dir** are listed in **@file\_list**.
Defaults to **false**.
### dir\_only\_contains\_ok
There is only one form of call namely
```perl
dir_only_contains_ok( $got_dir, \@file_list, $test_name )
```
Works like **dir\_contains\_ok** with option **SYMMETRIC** set to **true** i.e.
checks directory without following symlinks therein to ensure
that the listed files are present and that they are the only ones present.
This function is deprecated and stays for backward compatibility reasons only.
### compare\_dirs\_ok
There are two forms of calls:
- The generic form.
```perl
compare_dirs_ok( $got_dir, $reference_dir, \%options, $test_name )
```
- The short form, which is also backward compatible.
```perl
compare_dirs_ok( $got_dir, $reference_dir, $test_name )
```
Compares all files in the directories **$got\_dir** and **$reference\_dir** reporting differences.
If **$got\_dir** or **$reference\_dir** is a symlink, this will be accepted, but symlinks therein are not followed.
In the generic form, if the parameter **\\%options** is passed and contains the key **FILTER**,
**compare\_dirs\_ok** provides the same functionality as **compare\_dirs\_filter\_ok**.
Supported options:
- **EXISTENCE\_ONLY**
Boolean. If set to **true**, only checks if every file from **$reference\_dir** is found in **$got\_dir**.
Defaults to **false**.
- **FILTER**
Code reference providing filtering of file contents before comparison and
applied to files from both **$got\_dir** and **$reference\_dir**.
The only expected parameter is the current line from the file contents, the return value replaces this line.
In addition, the special variable **$.** representing the number of the current line in the file can be used.
If the return value is undefined, empty string is returned instead.
Line breaks are neither removed nor added after the execution.
Ignored if either **EXISTENCE\_ONLY** or **SIZE\_ONLY** is set to **true**.
Defaults to **undef** i.e. no filtering is provided.
- **NAME\_PATTERN**
String containing RegEx.
Files with base names not matching this RegEx will be skipped both in **$got\_dir** and **$reference\_dir**.
Defaults to the dot sign (**.**) i.e. no file will be skipped.
- **RECURSIVE**
Boolean. If set to **true**, subdirectories of both **$got\_dir** and **$reference\_dir** will be checked, too.
Defaults to **false**.
- **SIZE\_ONLY**
Boolean. If set to **true** and the options **EXISTENCE\_ONLY** is not set to **true**,
files from **$got\_dir** and **$reference\_dir** are compared by size only.
Defaults to **false**.
- **SYMMETRIC**
Boolean. If set to **true**, additionally verifies if all files from **$got\_dir** exist in **$reference\_dir**, too.
Defaults to **false**.
- All options supported by [Text::Diff](https://metacpan.org/pod/Text::Diff)
except of **FILENAME\_A** and **FILENAME\_B**.
The most useful of them seems to be **STYLE** defining the style of output for content differences.
Defaults to **Unified**.
### compare\_dirs\_filter\_ok
There is only one form of call namely
```perl
compare_dirs_filter_ok( $got_dir, $reference_dir, \&filter_func, $test_name )
```
Works like **compare\_dirs\_ok** with option **FILTER** i.e. calls the filter function **&filter\_func** on each line
of every file allowing you to exclude or alter some text to avoid spurious failures (like timestamp disagreements).
This function is deprecated and stays for backward compatibility reasons only.
### find\_ok
The signature is
```perl
find_ok( $got_dir, \&content_check_func, \%options, $test_name )
```
Verifies if the condition **&content\_check\_func** is true for all files in directory **$got\_dir**.
The code reference **&content\_check\_func** returning boolean is called for any type of file except of directory
i.e. for symlinks, devices, etc and the only parameter is the full-qualified file name.
If you want to consider plain files only, you must apply the test operator **-f** to the parameter
like shown in ["SYNOPSIS"](#synopsis).
Supported options:
- **RECURSIVE**
Boolean. If set to **true**, subdirectories of **$got\_dir** will be checked, too.
Defaults to **false**.
### compare\_archives\_ok
The signature is
```perl
compare_archives_ok( $got_archive, $reference_archive, \%options, $test_name )
```
Verifies if the archives (containers) **$got\_archive** and **$reference\_archive** are logically identical.
The term "logically identical" means that these files might be physically different e.g. because their members are
stored in different order, or because some members are marked as deleted, but the metadata relevant for the current
test case and the members are identical.
Which metadata and which members must be compared can be controlled using **\\%options**.
The comparison itself begins with the extraction and comparison of metadata;
if they are not identical, no further comparison is provided and the test fails.
If the metadata comparison succeeds, members of **$got\_archive** and **$reference\_archive** are extracted in
temporary directories and compared in the same manner like **compare\_dirs\_ok** this does.
Supported options:
- All options supported by **compare\_dirs\_ok**.
- **EXTRACT**
Code reference. Extracts members from the archive in the current directory.
The only expected parameter is the archive file name.
The current directory at the time point of extraction is a temporary directory that is removed after the test.
The return value is ignored.
Defaults to empty function **sub {}**.
- **META\_DATA**
Code reference. Returns metadata e.g. comments from a PKZIP archive.
The only expected parameter is the archive file name.
Defaults to empty function **sub {}**.
# SEE ALSO
Consult [Test::Simple](https://metacpan.org/pod/Test::Simple), [Test2::V0](https://metacpan.org/pod/Test2::V0),
and [Test::Builder](https://metacpan.org/pod/Test::Builder) for more testing help.
This module really just adds functions to what [Test2::V0](https://metacpan.org/pod/Test2::V0) does.
As recommended by the author of [Test::More](https://metacpan.org/pod/Test::More)
and [Test2::V0](https://metacpan.org/pod/Test2::V0), the latter module should be preferred,
that's why [Test::More](https://metacpan.org/pod/Test::More) is not listed in ["SYNOPSIS"](#synopsis).
# BUGS
Please report any bugs or feature requests through the web interface at
[https://github.com/jsf116/Test-Files/issues](https://github.com/jsf116/Test-Files/issues).
# CAVEATS
Although this module can cope with binary files, too, confirming their equality,
but in case of differences a proper representation of comparison results is not guaranteed.
# AUTHOR
Phil Crow, <philcrow2000@yahoo.com>
Jurij Fajnberg, <fajnbergj@gmail.com>
# COPYRIGHT AND LICENSE
Copyright 2003-2007 by Phil Crow
Copyright 2020-2024 by Jurij Fajnberg
This module is free software; you can redistribute it and/or modify
it under the same terms as the Perl 5 programming language system itself.
|