/usr/share/perl5/Test/File.pm is in libtest-file-perl 1.442-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 | package Test::File;
use strict;
use base qw(Exporter);
use vars qw(@EXPORT $VERSION);
use File::Spec;
use Test::Builder;
@EXPORT = qw(
file_exists_ok file_not_exists_ok
file_empty_ok file_not_empty_ok file_size_ok file_max_size_ok
file_min_size_ok file_readable_ok file_not_readable_ok file_writeable_ok
file_not_writeable_ok file_executable_ok file_not_executable_ok
file_mode_is file_mode_isnt
file_mode_has file_mode_hasnt
file_is_symlink_ok
symlink_target_exists_ok symlink_target_is
symlink_target_dangles_ok
dir_exists_ok dir_contains_ok
link_count_is_ok link_count_gt_ok link_count_lt_ok
owner_is owner_isnt
group_is group_isnt
file_line_count_is file_line_count_isnt file_line_count_between
file_contains_like file_contains_unlike
file_contains_utf8_like file_contains_utf8_unlike
file_contains_encoded_like file_contains_encoded_unlike
file_mtime_gt_ok file_mtime_lt_ok file_mtime_age_ok
);
$VERSION = '1.442';
{
use warnings;
}
my $Test = Test::Builder->new();
=encoding utf8
=head1 NAME
Test::File -- test file attributes
=head1 SYNOPSIS
use Test::File;
=head1 DESCRIPTION
This modules provides a collection of test utilities for file
attributes.
Some file attributes depend on the owner of the process testing the
file in the same way the file test operators do. For instance, root
(or super-user or Administrator) may always be able to read files no
matter the permissions.
Some attributes don't make sense outside of Unix, either, so some
tests automatically skip if they think they won't work on the
platform. If you have a way to make these functions work on Windows,
for instance, please send me a patch. :) IF you want to pretend to be
Windows on a non-Windows machine (for instance, to test C<skip()>),
you can set the C<PRETEND_TO_BE_WINDOWS> environment variable.
The optional NAME parameter for every function allows you to specify a
name for the test. If not supplied, a reasonable default will be
generated.
=head2 Functions
=cut
sub _normalize
{
my $file = shift;
return unless defined $file;
return $file =~ m|/|
? File::Spec->catfile( split m|/|, $file )
: $file;
}
sub _win32
{
return 0 if $^O eq 'darwin';
return $ENV{PRETEND_TO_BE_WIN32} if defined $ENV{PRETEND_TO_BE_WIN32};
return $^O =~ m/Win/;
}
# returns true if symlinks can't exist
sub _no_symlinks_here { ! eval { symlink("",""); 1 } }
# owner_is and owner_isn't should skip on OS where the question makes no
# sense. I really don't know a good way to test for that, so I'm going
# to skip on the two OS's that I KNOW aren't multi-user. I'd love to add
# more if anyone knows of any
# Note: I don't have a dos or mac os < 10 machine to test this on
sub _obviously_non_multi_user
{
foreach my $os ( qw(dos MacOS) ) { return 1 if $^O eq $os }
return 0 if $^O eq 'MSWin32';
eval { my $holder = getpwuid(0) };
return 1 if $@;
eval { my $holder = getgrgid(0) };
return 1 if $@;
return 0;
}
=over 4
=item file_exists_ok( FILENAME [, NAME ] )
Ok if the file exists, and not ok otherwise.
=cut
sub file_exists_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename exists";
my $ok = -e $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag("File [$filename] does not exist");
$Test->ok(0, $name);
}
}
=item file_not_exists_ok( FILENAME [, NAME ] )
Ok if the file does not exist, and not okay if it does exist.
=cut
sub file_not_exists_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename does not exist";
my $ok = not -e $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag("File [$filename] exists");
$Test->ok(0, $name);
}
}
=item file_empty_ok( FILENAME [, NAME ] )
Ok if the file exists and has empty size, not ok if the
file does not exist or exists with non-zero size.
=cut
sub file_empty_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename is empty";
unless( -e $filename )
{
$Test->diag( "File [$filename] does not exist!" );
return $Test->ok(0, $name);
}
my $ok = -z $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag( "File [$filename] exists with non-zero size!" );
$Test->ok(0, $name);
}
}
=item file_not_empty_ok( FILENAME [, NAME ] )
Ok if the file exists and has non-zero size, not ok if the
file does not exist or exists with zero size.
=cut
sub file_not_empty_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename is not empty";
unless( -e $filename )
{
$Test->diag( "File [$filename] does not exist!" );
return $Test->ok(0, $name);
}
my $ok = not -z _;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag( "File [$filename] exists with zero size!" );
$Test->ok(0, $name);
}
}
=item file_size_ok( FILENAME, SIZE [, NAME ] )
Ok if the file exists and has SIZE size in bytes (exactly), not ok if
the file does not exist or exists with size other than SIZE.
=cut
sub file_size_ok
{
my $filename = _normalize( shift );
my $expected = int shift;
my $name = shift || "$filename has right size";
unless( -e $filename )
{
$Test->diag( "File [$filename] does not exist!" );
return $Test->ok(0, $name);
}
my $ok = ( -s $filename ) == $expected;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
my $actual = -s $filename;
$Test->diag(
"File [$filename] has actual size [$actual] not [$expected]!" );
$Test->ok(0, $name);
}
}
=item file_max_size_ok( FILENAME, MAX [, NAME ] )
Ok if the file exists and has size less than or equal to MAX bytes, not
ok if the file does not exist or exists with size greater than MAX
bytes.
=cut
sub file_max_size_ok
{
my $filename = _normalize( shift );
my $max = int shift;
my $name = shift || "$filename is under $max bytes";
unless( -e $filename )
{
$Test->diag( "File [$filename] does not exist!" );
return $Test->ok(0, $name);
}
my $ok = ( -s $filename ) <= $max;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
my $actual = -s $filename;
$Test->diag(
"File [$filename] has actual size [$actual] " .
"greater than [$max]!"
);
$Test->ok(0, $name);
}
}
=item file_min_size_ok( FILENAME, MIN [, NAME ] )
Ok if the file exists and has size greater than or equal to MIN bytes,
not ok if the file does not exist or exists with size less than MIN
bytes.
=cut
sub file_min_size_ok
{
my $filename = _normalize( shift );
my $min = int shift;
my $name = shift || "$filename is over $min bytes";
unless( -e $filename )
{
$Test->diag( "File [$filename] does not exist!" );
return $Test->ok(0, $name);
}
my $ok = ( -s $filename ) >= $min;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
my $actual = -s $filename;
$Test->diag(
"File [$filename] has actual size ".
"[$actual] less than [$min]!"
);
$Test->ok(0, $name);
}
}
=item file_line_count_is( FILENAME, COUNT [, NAME ] )
Ok if the file exists and has COUNT lines (exactly), not ok if the
file does not exist or exists with a line count other than COUNT.
This function uses the current value of C<$/> as the line ending and
counts the lines by reading them and counting how many it read.
=cut
sub _ENOFILE () { -1 }
sub _ECANTOPEN () { -2 }
sub _file_line_counter
{
my $filename = shift;
return _ENOFILE unless -e $filename; # does not exist
return _ECANTOPEN unless open my( $fh ), "<", $filename;
my $count = 0;
while( <$fh> ) { $count++ }
return $count;
}
# XXX: lots of cut and pasting here, needs refactoring
# looks like the refactoring might be worse than this though
sub file_line_count_is
{
my $filename = _normalize( shift );
my $expected = shift;
my $name = do {
no warnings 'uninitialized';
shift || "$filename line count is $expected lines";
};
unless( defined $expected && int( $expected ) == $expected )
{
no warnings 'uninitialized';
$Test->diag( "file_line_count_is expects a positive whole number for " .
"the second argument. Got [$expected]!" );
return $Test->ok( 0, $name );
}
my $got = _file_line_counter( $filename );
if( $got eq _ENOFILE )
{
$Test->diag( "File [$filename] does not exist!" );
$Test->ok( 0, $name );
}
elsif( $got == _ECANTOPEN )
{
$Test->diag( "Could not open [$filename]: \$! is [$!]!" );
$Test->ok( 0, $name );
}
elsif( $got == $expected )
{
$Test->ok( 1, $name );
}
else
{
$Test->diag( "Expected [$expected] lines in [$filename], " .
"got [$got] lines!" );
$Test->ok( 0, $name );
}
}
=item file_line_count_isnt( FILENAME, COUNT [, NAME ] )
Ok if the file exists and doesn't have exactly COUNT lines, not ok if the
file does not exist or exists with a line count of COUNT. Read that
carefully: the file must exist for this test to pass!
This function uses the current value of C<$/> as the line ending and
counts the lines by reading them and counting how many it read.
=cut
sub file_line_count_isnt
{
my $filename = _normalize( shift );
my $expected = shift;
my $name = do {
no warnings 'uninitialized';
shift || "$filename line count is not $expected lines";
};
unless( defined $expected && int( $expected ) == $expected )
{
no warnings 'uninitialized';
$Test->diag( "file_line_count_is expects a positive whole number for " .
"the second argument. Got [$expected]!" );
return $Test->ok( 0, $name );
}
my $got = _file_line_counter( $filename );
if( $got eq _ENOFILE )
{
$Test->diag( "File [$filename] does not exist!" );
$Test->ok( 0, $name );
}
elsif( $got == _ECANTOPEN )
{
$Test->diag( "Could not open [$filename]: \$! is [$!]!" );
$Test->ok( 0, $name );
}
elsif( $got != $expected )
{
$Test->ok( 1, $name );
}
else
{
$Test->diag( "Expected something other than [$expected] lines in [$filename], " .
"but got [$got] lines!" );
$Test->ok( 0, $name );
}
}
=item file_line_count_between( FILENAME, MIN, MAX, [, NAME ] )
Ok if the file exists and has a line count between MIN and MAX, inclusively.
This function uses the current value of C<$/> as the line ending and
counts the lines by reading them and counting how many it read.
=cut
sub file_line_count_between
{
my $filename = _normalize( shift );
my $min = shift;
my $max = shift;
my $name = do {
no warnings 'uninitialized';
shift || "$filename line count is between [$min] and [$max] lines";
};
foreach my $ref ( \$min, \$max )
{
unless( defined $$ref && int( $$ref ) == $$ref )
{
no warnings 'uninitialized';
$Test->diag( "file_line_count_between expects positive whole numbers for " .
"the second and third arguments. Got [$min] and [$max]!" );
return $Test->ok( 0, $name );
}
}
my $got = _file_line_counter( $filename );
if( $got eq _ENOFILE )
{
$Test->diag( "File [$filename] does not exist!" );
$Test->ok( 0, $name );
}
elsif( $got == _ECANTOPEN )
{
$Test->diag( "Could not open [$filename]: \$! is [$!]!" );
$Test->ok( 0, $name );
}
elsif( $min <= $got and $got <= $max )
{
$Test->ok( 1, $name );
}
else
{
$Test->diag( "Expected a line count between [$min] and [$max] " .
"in [$filename], but got [$got] lines!"
);
$Test->ok( 0, $name );
}
}
=item file_contains_like ( FILENAME, PATTERN [, NAME ] )
Ok if the file exists and its contents (as one big string) match
PATTERN, not ok if the file does not exist, is not readable, or exists
but doesn't match PATTERN.
Since the file contents are read into memory, you should not use this
for large files. Besides memory consumption, test diagnostics for
failing tests might be difficult to decipher. However, for short
files this works very well.
Because the entire contents are treated as one large string, you can
make a pattern that tests multiple lines. Don't forget that you may
need to use the /s modifier for such patterns:
# make sure file has one or more paragraphs with CSS class X
file_contains_like($html_file, qr{<p class="X">.*?</p>}s);
Contrariwise, if you need to match at the beginning or end of a line
inside the file, use the /m modifier:
# make sure file has a setting for foo
file_contains_like($config_file, qr/^ foo \s* = \s* \w+ $/mx);
If you want to test your file contents against multiple patterns, but
don't want to have the file read in repeatedly, you can pass an
arrayref of patterns instead of a single pattern, like so:
# make sure our template has rendered correctly
file_contains_like($template_out,
[
qr/^ $title_line $/mx,
map { qr/^ $_ $/mx } @chapter_headings,
qr/^ $footer_line $/mx,
]);
Please note that if you do this, and your file does not exist or is
not readable, you'll only get one test failure instead of a failure
for each pattern. This could cause your test plan to be off, although
you may not care at that point because your test failed anyway. If
you do care, either skip the test plan altogether by employing
L<Test::More>'s C<done_testing()> function, or use
L</file_readable_ok> in conjunction with a C<SKIP> block.
Contributed by Buddy Burden C<< <barefoot@cpan.org> >>.
=item file_contains_unlike ( FILENAME, PATTERN [, NAME ] )
Ok if the file exists and its contents (as one big string) do B<not>
match PATTERN, not ok if the file does not exist, is not readable, or
exists but matches PATTERN.
All notes and caveats for L</file_contains_like> apply to this
function as well.
Contributed by Buddy Burden C<< <barefoot@cpan.org> >>.
=item file_contains_utf8_like ( FILENAME, PATTERN [, NAME ] )
The same as C<file_contains_like>, except the file is opened as UTF-8.
=item file_contains_utf8_unlike ( FILENAME, PATTERN [, NAME ] )
The same as C<file_contains_unlike>, except the file is opened as UTF-8.
=item file_contains_encoded_like ( FILENAME, ENCODING, PATTERN [, NAME ] )
The same as C<file_contains_like>, except the file is opened with ENCODING
=item file_contains_encoded_unlike ( FILENAME, ENCODING, PATTERN [, NAME ] )
The same as C<file_contains_unlike>, except the file is opened with ENCODING.
=cut
sub file_contains_like
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
_file_contains(like => "contains", undef, @_);
}
sub file_contains_unlike
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
_file_contains(unlike => "doesn't contain", undef, @_);
}
sub file_contains_utf8_like
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
_file_contains(like => "contains", 'UTF-8', @_);
}
sub file_contains_utf8_unlike
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
_file_contains(unlike => "doesn't contain", 'UTF-8', @_);
}
sub file_contains_encoded_like
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $filename = shift;
my $encoding = shift;
_file_contains(like => "contains", $encoding, $filename, @_);
}
sub file_contains_encoded_unlike
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $filename = shift;
my $encoding = shift;
_file_contains(unlike => "doesn't contain", $encoding, $filename, @_);
}
sub _file_contains
{
my $method = shift;
my $verb = shift;
my $encoding = shift;
my $filename = _normalize( shift );
my $patterns = shift;
my $name = shift;
my (@patterns, %patterns);
if (ref $patterns eq 'ARRAY')
{
@patterns = @$patterns;
%patterns = map { $_ => $name || "$filename $verb $_" } @patterns;
}
else
{
@patterns = ($patterns);
%patterns = ( $patterns => $name || "$filename $verb $patterns" );
}
# for purpose of checking the file's existence, just use the first
# test name as the name
$name = $patterns{$patterns[0]};
unless( -e $filename )
{
$Test->diag( "File [$filename] does not exist!" );
return $Test->ok(0, $name);
}
unless( -r $filename )
{
$Test->diag( "File [$filename] is not readable!" );
return $Test->ok(0, $name);
}
# do the slurp
my $file_contents;
{
unless (open(FH, $filename))
{
$Test->diag( "Could not open [$filename]: \$! is [$!]!" );
return $Test->ok( 0, $name );
}
if (defined $encoding) {
binmode FH, ":encoding($encoding)";
}
local $/ = undef;
$file_contents = <FH>;
close FH;
}
foreach my $p (@patterns)
{
$Test->$method($file_contents, $p, $patterns{$p});
}
}
=item file_readable_ok( FILENAME [, NAME ] )
Ok if the file exists and is readable, not ok
if the file does not exist or is not readable.
=cut
sub file_readable_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename is readable";
my $ok = -r $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag( "File [$filename] is not readable!" );
$Test->ok(0, $name);
}
}
=item file_not_readable_ok( FILENAME [, NAME ] )
Ok if the file exists and is not readable, not ok
if the file does not exist or is readable.
=cut
sub file_not_readable_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename is not readable";
my $ok = not -r $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag( "File [$filename] is readable!" );
$Test->ok(0, $name);
}
}
=item file_writeable_ok( FILENAME [, NAME ] )
Ok if the file exists and is writeable, not ok
if the file does not exist or is not writeable.
=cut
sub file_writeable_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename is writeable";
my $ok = -w $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag( "File [$filename] is not writeable!" );
$Test->ok(0, $name);
}
}
=item file_not_writeable_ok( FILENAME [, NAME ] )
Ok if the file exists and is not writeable, not ok
if the file does not exist or is writeable.
=cut
sub file_not_writeable_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename is not writeable";
my $ok = not -w $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag("File [$filename] is writeable!");
$Test->ok(0, $name);
}
}
=item file_executable_ok( FILENAME [, NAME ] )
Ok if the file exists and is executable, not ok
if the file does not exist or is not executable.
This test automatically skips if it thinks it is on a
Windows platform.
=cut
sub file_executable_ok
{
if( _win32() )
{
$Test->skip( "file_executable_ok doesn't work on Windows!" );
return;
}
my $filename = _normalize( shift );
my $name = shift || "$filename is executable";
my $ok = -x $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag("File [$filename] is not executable!");
$Test->ok(0, $name);
}
}
=item file_not_executable_ok( FILENAME [, NAME ] )
Ok if the file exists and is not executable, not ok
if the file does not exist or is executable.
This test automatically skips if it thinks it is on a
Windows platform.
=cut
sub file_not_executable_ok
{
if( _win32() )
{
$Test->skip( "file_not_executable_ok doesn't work on Windows!" );
return;
}
my $filename = _normalize( shift );
my $name = shift || "$filename is not executable";
my $ok = not -x $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag("File [$filename] is executable!");
$Test->ok(0, $name);
}
}
=item file_mode_is( FILENAME, MODE [, NAME ] )
Ok if the file exists and the mode matches, not ok
if the file does not exist or the mode does not match.
This test automatically skips if it thinks it is on a
Windows platform.
Contributed by Shawn Sorichetti C<< <ssoriche@coloredblocks.net> >>
=cut
sub file_mode_is
{
if( _win32() )
{
$Test->skip( "file_mode_is doesn't work on Windows!" );
return;
}
my $filename = _normalize( shift );
my $mode = shift;
my $name = shift || sprintf("%s mode is %04o", $filename, $mode);
my $ok = -e $filename && ((stat($filename))[2] & 07777) == $mode;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag(sprintf("File [%s] mode is not %04o!", $filename, $mode) );
$Test->ok(0, $name);
}
}
=item file_mode_isnt( FILENAME, MODE [, NAME ] )
Ok if the file exists and mode does not match, not ok
if the file does not exist or mode does match.
This test automatically skips if it thinks it is on a
Windows platform.
Contributed by Shawn Sorichetti C<< <ssoriche@coloredblocks.net> >>
=cut
sub file_mode_isnt
{
if( _win32() )
{
$Test->skip( "file_mode_isnt doesn't work on Windows!" );
return;
}
my $filename = _normalize( shift );
my $mode = shift;
my $name = shift || sprintf("%s mode is not %04o",$filename,$mode);
my $ok = not (-e $filename && ((stat($filename))[2] & 07777) == $mode);
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag(sprintf("File [%s] mode is %04o!",$filename,$mode));
$Test->ok(0, $name);
}
}
=item file_mode_has( FILENAME, MODE [, NAME ] )
Ok if the file exists and has all the bits in mode turned on, not ok
if the file does not exist or the mode does not match. That is,
C<< FILEMODE & MODE == MODE >> must be true.
This test automatically skips if it thinks it is on a
Windows platform.
Contributed by Ricardo Signes C<< <rjbs@cpan.org> >>
=cut
sub file_mode_has
{
if( _win32() )
{
$Test->skip( "file_mode_has doesn't work on Windows!" );
return;
}
my $filename = _normalize( shift );
my $mode = shift;
my $name = shift || sprintf("%s mode has all bits of %04o", $filename, $mode);
my $present = -e $filename;
my $gotmode = $present ? (stat($filename))[2] : undef;
my $ok = $present && ($gotmode & $mode) == $mode;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
my $missing = ($gotmode ^ $mode) & $mode;
$Test->diag(sprintf("File [%s] mode is missing component %04o!", $filename, $missing) );
$Test->ok(0, $name);
}
}
=item file_mode_hasnt( FILENAME, MODE [, NAME ] )
Ok if the file exists and has all the bits in mode turned off, not ok
if the file does not exist or the mode does not match. That is,
C<< FILEMODE & MODE == 0 >> must be true.
This test automatically skips if it thinks it is on a
Windows platform.
Contributed by Ricardo Signes C<< <rjbs@cpan.org> >>
=cut
sub file_mode_hasnt
{
if( _win32() )
{
$Test->skip( "file_mode_hasnt doesn't work on Windows!" );
return;
}
my $filename = _normalize( shift );
my $mode = shift;
my $name = shift || sprintf("%s mode has no bits of %04o", $filename, $mode);
my $present = -e $filename;
my $gotmode = $present ? (stat($filename))[2] : undef;
my $ok = $present && ($gotmode & $mode) == 0;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
my $bad = $gotmode & $mode;
$Test->diag(sprintf("File [%s] mode has forbidden component %04o!", $filename, $bad) );
$Test->ok(0, $name);
}
}
=item file_is_symlink_ok( FILENAME [, NAME ] )
Ok if FILENAME is a symlink, even if it points to a non-existent
file. This test automatically skips if the operating system does
not support symlinks. If the file does not exist, the test fails.
=cut
sub file_is_symlink_ok
{
if( _no_symlinks_here() )
{
$Test->skip(
"file_is_symlink_ok doesn't work on systems without symlinks!" );
return;
}
my $file = shift;
my $name = shift || "$file is a symlink";
if( -l $file )
{
$Test->ok(1, $name)
}
else
{
$Test->diag( "File [$file] is not a symlink!" );
$Test->ok(0, $name);
}
}
=item symlink_target_exists_ok( SYMLINK [, TARGET] [, NAME ] )
Ok if FILENAME is a symlink and it points to a existing file. With the
optional TARGET argument, the test fails if SYMLINK's target is not
TARGET. This test automatically skips if the operating system does not
support symlinks. If the file does not exist, the test fails.
=cut
sub symlink_target_exists_ok
{
if( _no_symlinks_here() )
{
$Test->skip(
"symlink_target_exists_ok doesn't work on systems without symlinks!"
);
return;
}
my $file = shift;
my $dest = shift || readlink( $file );
my $name = shift || "$file is a symlink";
unless( -l $file )
{
$Test->diag( "File [$file] is not a symlink!" );
return $Test->ok( 0, $name );
}
unless( -e $dest )
{
$Test->diag( "Symlink [$file] points to non-existent target [$dest]!" );
return $Test->ok( 0, $name );
}
my $actual = readlink( $file );
unless( $dest eq $actual )
{
$Test->diag(
"Symlink [$file] points to\n" .
" got: $actual\n" .
" expected: $dest\n"
);
return $Test->ok( 0, $name );
}
$Test->ok( 1, $name );
}
=item symlink_target_dangles_ok( SYMLINK [, NAME ] )
Ok if FILENAME is a symlink and if it doesn't point to a existing
file. This test automatically skips if the operating system does not
support symlinks. If the file does not exist, the test fails.
=cut
sub symlink_target_dangles_ok
{
if( _no_symlinks_here() )
{
$Test->skip(
"symlink_target_dangles_ok doesn't work on systems without symlinks!" );
return;
}
my $file = shift;
my $dest = readlink( $file );
my $name = shift || "$file is a symlink";
unless( -l $file )
{
$Test->diag( "File [$file] is not a symlink!" );
return $Test->ok( 0, $name );
}
if( -e $dest )
{
$Test->diag(
"Symlink [$file] points to existing file [$dest] but shouldn't!" );
return $Test->ok( 0, $name );
}
$Test->ok( 1, $name );
}
=item symlink_target_is( SYMLINK, TARGET [, NAME ] )
Ok if FILENAME is a symlink and if points to TARGET. This test
automatically skips if the operating system does not support symlinks.
If the file does not exist, the test fails.
=cut
sub symlink_target_is
{
if( _no_symlinks_here() )
{
$Test->skip(
"symlink_target_is doesn't work on systems without symlinks!" );
return;
}
my $file = shift;
my $dest = shift;
my $name = shift || "symlink $file points to $dest";
unless( -l $file )
{
$Test->diag( "File [$file] is not a symlink!" );
return $Test->ok( 0, $name );
}
my $actual_dest = readlink( $file );
my $link_error = $!;
unless( defined $actual_dest )
{
$Test->diag( "Symlink [$file] does not have a defined target!" );
$Test->diag( "readlink error: $link_error" ) if defined $link_error;
return $Test->ok( 0, $name );
}
if( $dest eq $actual_dest )
{
$Test->ok( 1, $name );
}
else
{
$Test->ok( 0, $name );
$Test->diag(" got: $actual_dest" );
$Test->diag(" expected: $dest" );
}
}
=item symlink_target_is_absolute_ok( SYMLINK [, NAME ] )
Ok if FILENAME is a symlink and if its target is an absolute path.
This test automatically skips if the operating system does not support
symlinks. If the file does not exist, the test fails.
=cut
sub symlink_target_is_absolute_ok
{
if( _no_symlinks_here() )
{
$Test->skip(
"symlink_target_exists_ok doesn't work on systems without symlinks" );
return;
}
my( $from, $from_base, $to, $to_base, $name ) = @_;
my $link = readlink( $from );
my $link_err = defined( $link ) ? '' : $!; # $! doesn't always get reset
my $link_abs = abs_path( rel2abs($link, $from_base) );
my $to_abs = abs_path( rel2abs($to, $to_base) );
if (defined( $link_abs ) && defined( $to_abs ) && $link_abs eq $to_abs) {
$Test->ok( 1, $name );
}
else {
$Test->ok( 0, $name );
$link ||= 'undefined';
$link_abs ||= 'undefined';
$to_abs ||= 'undefined';
$Test->diag(" link: $from");
$Test->diag(" got: $link");
$Test->diag(" (abs): $link_abs");
$Test->diag(" expected: $to");
$Test->diag(" (abs): $to_abs");
$Test->diag(" readlink() error: $link_err") if ($link_err);
}
}
=item dir_exists_ok( DIRECTORYNAME [, NAME ] )
Ok if the file exists and is a directory, not ok if the file doesn't exist, or exists but isn't a
directory.
Contributed by Buddy Burden C<< <barefoot@cpan.org> >>.
=cut
sub dir_exists_ok
{
my $filename = _normalize( shift );
my $name = shift || "$filename is a directory";
unless( -e $filename )
{
$Test->diag( "File [$filename] does not exist!" );
return $Test->ok(0, $name);
}
my $ok = -d $filename;
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag( "File [$filename] exists but is not a directory!" );
$Test->ok(0, $name);
}
}
=item dir_contains_ok( DIRECTORYNAME, FILENAME [, NAME ] )
Ok if the directory exists and contains the file, not ok if the directory doesn't exist, or exists
but doesn't contain the file.
Contributed by Buddy Burden C<< <barefoot@cpan.org> >>.
=cut
sub dir_contains_ok
{
my $dirname = _normalize( shift );
my $filename = _normalize( shift );
my $name = shift || "directory $dirname contains file $filename";
unless( -d $dirname )
{
$Test->diag( "Directory [$dirname] does not exist!" );
return $Test->ok(0, $name);
}
my $ok = -e File::Spec->catfile($dirname, $filename);
if( $ok )
{
$Test->ok(1, $name);
}
else
{
$Test->diag( "File [$filename] does not exist in directory $dirname!" );
$Test->ok(0, $name);
}
}
=item link_count_is_ok( FILE, LINK_COUNT [, NAME ] )
Ok if the link count to FILE is LINK_COUNT. LINK_COUNT is interpreted
as an integer. A LINK_COUNT that evaluates to 0 returns Ok if the file
does not exist.
=cut
sub link_count_is_ok
{
my $file = shift;
my $count = int( 0 + shift );
my $name = shift || "$file has a link count of [$count]";
my $actual = ( stat $file )[3];
unless( $actual == $count )
{
$Test->diag(
"File [$file] points has [$actual] links: expected [$count]!" );
return $Test->ok( 0, $name );
}
$Test->ok( 1, $name );
}
=item link_count_gt_ok( FILE, LINK_COUNT [, NAME ] )
Ok if the link count to FILE is greater than LINK_COUNT. LINK_COUNT is
interpreted as an integer. A LINK_COUNT that evaluates to 0 returns Ok
if the file has at least one link.
=cut
sub link_count_gt_ok
{
my $file = shift;
my $count = int( 0 + shift );
my $name = shift || "$file has a link count of [$count]";
my $actual = (stat $file )[3];
unless( $actual > $count )
{
$Test->diag(
"File [$file] points has [$actual] links: ".
"expected more than [$count]!" );
return $Test->ok( 0, $name );
}
$Test->ok( 1, $name );
}
=item link_count_lt_ok( FILE, LINK_COUNT [, NAME ] )
Ok if the link count to FILE is less than LINK_COUNT. LINK_COUNT is
interpreted as an integer. A LINK_COUNT that evaluates to 0 returns Ok
if the file has at least one link.
=cut
sub link_count_lt_ok
{
my $file = shift;
my $count = int( 0 + shift );
my $name = shift || "$file has a link count of [$count]";
my $actual = (stat $file )[3];
unless( $actual < $count )
{
$Test->diag(
"File [$file] points has [$actual] links: ".
"expected less than [$count]!" );
return $Test->ok( 0, $name );
}
$Test->ok( 1, $name );
}
# owner_is, owner_isnt, group_is and group_isnt are almost
# identical in the beginning, so I'm writing a skeleton they can all use.
# I can't think of a better name...
sub _dm_skeleton
{
no warnings 'uninitialized';
if( _obviously_non_multi_user() )
{
my $calling_sub = (caller(1))[3];
$Test->skip( $calling_sub . " only works on a multi-user OS!" );
return 'skip';
}
my $filename = _normalize( shift );
my $testing_for = shift;
my $name = shift;
unless( defined $filename )
{
$Test->diag( "File name not specified!" );
return $Test->ok( 0, $name );
}
unless( -e $filename )
{
$Test->diag( "File [$filename] does not exist!" );
return $Test->ok( 0, $name );
}
return;
}
=item owner_is( FILE , OWNER [, NAME ] )
Ok if FILE's owner is the same as OWNER. OWNER may be a text user name
or a numeric userid. Test skips on Dos, and Mac OS <= 9.
If the file does not exist, the test fails.
Contributed by Dylan Martin
=cut
sub owner_is
{
my $filename = shift;
my $owner = shift;
my $name = shift || "$filename belongs to $owner";
my $err = _dm_skeleton( $filename, $owner, $name );
return if( defined( $err ) && $err eq 'skip' );
return $err if defined($err);
my $owner_uid = _get_uid( $owner );
unless( defined $owner_uid )
{
$Test->diag("User [$owner] does not exist on this system!");
return $Test->ok( 0, $name );
}
my $file_uid = ( stat $filename )[4];
unless( defined $file_uid )
{
$Test->skip("stat failed to return owner uid for $filename!");
return;
}
return $Test->ok( 1, $name ) if $file_uid == $owner_uid;
my $real_owner = ( getpwuid $file_uid )[0];
unless( defined $real_owner )
{
$Test->diag("File does not belong to $owner!");
return $Test->ok( 0, $name );
}
$Test->diag( "File [$filename] belongs to $real_owner ($file_uid), ".
"not $owner ($owner_uid)!" );
return $Test->ok( 0, $name );
}
=item owner_isnt( FILE, OWNER [, NAME ] )
Ok if FILE's owner is not the same as OWNER. OWNER may be a text user name
or a numeric userid. Test skips on Dos and Mac OS <= 9. If the file
does not exist, the test fails.
Contributed by Dylan Martin
=cut
sub owner_isnt
{
my $filename = shift;
my $owner = shift;
my $name = shift || "$filename doesn't belong to $owner";
my $err = _dm_skeleton( $filename, $owner, $name );
return if( defined( $err ) && $err eq 'skip' );
return $err if defined($err);
my $owner_uid = _get_uid( $owner );
unless( defined $owner_uid )
{
return $Test->ok( 1, $name );
}
my $file_uid = ( stat $filename )[4];
#$Test->diag( "owner_isnt: $owner_uid $file_uid" );
return $Test->ok( 1, $name ) if $file_uid != $owner_uid;
$Test->diag( "File [$filename] belongs to $owner ($owner_uid)!" );
return $Test->ok( 0, $name );
}
=item group_is( FILE , GROUP [, NAME ] )
Ok if FILE's group is the same as GROUP. GROUP may be a text group name or
a numeric group id. Test skips on Dos, Mac OS <= 9 and any other operating
systems that do not support getpwuid() and friends. If the file does not
exist, the test fails.
Contributed by Dylan Martin
=cut
sub group_is
{
my $filename = shift;
my $group = shift;
my $name = ( shift || "$filename belongs to group $group" );
my $err = _dm_skeleton( $filename, $group, $name );
return if( defined( $err ) && $err eq 'skip' );
return $err if defined($err);
my $group_gid = _get_gid( $group );
unless( defined $group_gid )
{
$Test->diag("Group [$group] does not exist on this system!");
return $Test->ok( 0, $name );
}
my $file_gid = ( stat $filename )[5];
unless( defined $file_gid )
{
$Test->skip("stat failed to return group gid for $filename!");
return;
}
return $Test->ok( 1, $name ) if $file_gid == $group_gid;
my $real_group = ( getgrgid $file_gid )[0];
unless( defined $real_group )
{
$Test->diag("File does not belong to $group!");
return $Test->ok( 0, $name );
}
$Test->diag( "File [$filename] belongs to $real_group ($file_gid), ".
"not $group ($group_gid)!" );
return $Test->ok( 0, $name );
}
=item group_isnt( FILE , GROUP [, NAME ] )
Ok if FILE's group is not the same as GROUP. GROUP may be a text group name or
a numeric group id. Test skips on Dos, Mac OS <= 9 and any other operating
systems that do not support getpwuid() and friends. If the file does not
exist, the test fails.
Contributed by Dylan Martin
=cut
sub group_isnt
{
my $filename = shift;
my $group = shift;
my $name = shift || "$filename does not belong to group $group";
my $err = _dm_skeleton( $filename, $group, $name );
return if( defined( $err ) && $err eq 'skip' );
return $err if defined($err);
my $group_gid = _get_gid( $group );
my $file_gid = ( stat $filename )[5];
unless( defined $file_gid )
{
$Test->skip("stat failed to return group gid for $filename!");
return;
}
return $Test->ok( 1, $name ) if $file_gid != $group_gid;
$Test->diag( "File [$filename] belongs to $group ($group_gid)!" );
return $Test->ok( 0, $name );
}
sub _get_uid
{
my $owner = shift;
my $owner_uid;
if ($owner =~ /^\d+/)
{
$owner_uid = $owner;
$owner = ( getpwuid $owner )[0];
}
else
{
$owner_uid = (getpwnam($owner))[2];
}
$owner_uid;
}
sub _get_gid
{
my $group = shift;
my $group_uid;
if ($group =~ /^\d+/)
{
$group_uid = $group;
$group = ( getgrgid $group )[0];
}
else
{
$group_uid = (getgrnam($group))[2];
}
$group_uid;
}
=item file_mtime_age_ok( FILE [, WITHIN_SECONDS ] [, NAME ] )
Ok if FILE's modified time is WITHIN_SECONDS inclusive of the system's current time.
This test uses stat() to obtain the mtime. If the file does not exist the test
returns failure. If stat() fails, the test is skipped.
=cut
sub file_mtime_age_ok
{
my $filename = shift;
my $within_secs = int shift || 0;
my $name = shift || "$filename mtime within $within_secs seconds of current time";
my $time = time();
my $filetime = _stat_file($filename, 9);
return if ( $filetime == -1 ); #skip
return $Test->ok(1, $name) if ( $filetime + $within_secs > $time-1 );
$Test->diag( "Filename [$filename] mtime [$filetime] is not $within_secs seconds within current system time [$time].");
return $Test->ok(0, $name);
}
=item file_mtime_gt_ok( FILE, UNIXTIME [, NAME ] )
Ok if FILE's mtime is > UNIXTIME. This test uses stat() to get the mtime. If stat() fails
this test is skipped. If FILE does not exist, this test fails.
=cut
sub file_mtime_gt_ok
{
my $filename = shift;
my $time = int shift;
my $name = shift || "$filename mtime is less than unix timestamp $time";
my $filetime = _stat_file($filename, 9);
return if ( $filetime == -1 ); #skip
return $Test->ok(1, $name) if ( $filetime > $time );
$Test->diag( "Filename [$filename] mtime [$filetime] not greater than $time" );
$Test->ok(0, $name);
}
=item file_mtime_lt_ok( FILE, UNIXTIME, [, NAME ] )
Ok if FILE's modified time is < UNIXTIME. This test uses stat() to get the mtime. If stat() fails
this test is skipped. If FILE does not exist, this test fails.
=cut
sub file_mtime_lt_ok
{
my $filename = shift;
my $time = int shift;
my $name = shift || "$filename mtime less than unix timestamp $time";
# gets mtime
my $filetime = _stat_file($filename, 9);
return if ( $filetime == -1 ); #skip
return $Test->ok(1, $name) if ( $filetime < $time );
$Test->diag( "Filename [$filename] mtime [$filetime] not less than $time" );
$Test->ok(0, $name);
}
# private function to safely stat a file
#
# Arugments:
# filename file to perform on
# attr_pos pos of the array returned from stat we want to compare. perldoc -f stat
#
# Returns:
# -1 - stat failed
# 0 - failure (file doesn't exist etc)
# filetime - on success, time requested provided by stat
#
sub _stat_file
{
my $filename = _normalize( shift );
my $attr_pos = shift;
unless( defined $filename )
{
$Test->diag( "Filename not specified!" );
return 0;
}
unless( -e $filename )
{
$Test->diag( "Filename [$filename] does not exist!" );
return 0;
}
my $filetime = ( stat($filename) )[$attr_pos];
unless( $filetime )
{
$Test->diag( "stat of $filename failed" );
return -1; #skip on stat failure
}
return $filetime;
}
=back
=head1 TO DO
* check properties for other users (readable_by_root, for instance)
* check times
* check number of links to file
* check path parts (directory, filename, extension)
=head1 SEE ALSO
L<Test::Builder>,
L<Test::More>
=head1 SOURCE AVAILABILITY
This module is in Github:
git://github.com/briandfoy/test-file.git
=head1 AUTHOR
brian d foy, C<< <bdfoy@cpan.org> >>
=head1 CREDITS
Shawn Sorichetti C<< <ssoriche@coloredblocks.net> >> provided
some functions.
Tom Metro helped me figure out some Windows capabilities.
Dylan Martin added C<owner_is> and C<owner_isnt>.
David Wheeler added C<file_line_count_is>.
Buddy Burden C<< <barefoot@cpan.org> >> provided C<dir_exists_ok>,
C<dir_contains_ok>, C<file_contains_like>, and
C<file_contains_unlike>.
xmikew C<< <https://github.com/xmikew> >> provided the C<mtime_age>
stuff.
=head1 COPYRIGHT AND LICENSE
Copyright © 2002-2016, brian d foy <bdfoy@cpan.org>. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
"The quick brown fox jumped over the lazy dog";
|