| 1 | #!./perl |
| 2 | |
| 3 | BEGIN { |
| 4 | chdir 't' if -d 't'; |
| 5 | @INC = '../lib'; |
| 6 | } |
| 7 | |
| 8 | use Test::More tests => 15; |
| 9 | |
| 10 | # these two should be kept in sync with the pragma itself |
| 11 | # if hint bits are changed there, other things *will* break |
| 12 | my $hint_bits = 0x00400000; |
| 13 | my $error = "filetest: the only implemented subpragma is 'access'.\n"; |
| 14 | |
| 15 | # can't use it yet, because of the import death |
| 16 | ok( require filetest, 'required pragma successfully' ); |
| 17 | |
| 18 | # and here's one culprit, right here |
| 19 | eval { filetest->import('bad subpragma') }; |
| 20 | is( $@, $error, 'filetest dies with bad subpragma on import' ); |
| 21 | |
| 22 | is( $^H & $hint_bits, 0, 'hint bits not set without pragma in place' ); |
| 23 | |
| 24 | # now try the normal usage |
| 25 | # can't check $^H here; it's lexically magic (see perlvar) |
| 26 | # the test harness unintentionally hoards the goodies for itself |
| 27 | use_ok( 'filetest', 'access' ); |
| 28 | |
| 29 | # and import again, to see it here |
| 30 | filetest->import('access'); |
| 31 | ok( $^H & $hint_bits, 'hint bits set with pragma loaded' ); |
| 32 | |
| 33 | # and now get rid of it |
| 34 | filetest->unimport('access'); |
| 35 | is( $^H & $hint_bits, 0, 'hint bits not set with pragma unimported' ); |
| 36 | |
| 37 | eval { filetest->unimport() }; |
| 38 | is( $@, $error, 'filetest dies without subpragma on unimport' ); |
| 39 | |
| 40 | # there'll be a compilation aborted failure here, with the eval string |
| 41 | eval "no filetest 'fake pragma'"; |
| 42 | like( $@, qr/^$error/, 'filetest dies with bad subpragma on unuse' ); |
| 43 | |
| 44 | eval "use filetest 'bad subpragma'"; |
| 45 | like( $@, qr/^$error/, 'filetest dies with bad subpragma on use' ); |
| 46 | |
| 47 | eval "use filetest"; |
| 48 | like( $@, qr/^$error/, 'filetest dies with missing subpragma on use' ); |
| 49 | |
| 50 | eval "no filetest"; |
| 51 | like( $@, qr/^$error/, 'filetest dies with missing subpragma on unuse' ); |
| 52 | |
| 53 | SKIP: { |
| 54 | # A real test for filetest. |
| 55 | # This works for systems with /usr/bin/chflags (i.e. BSD4.4 systems). |
| 56 | my $chflags = "/usr/bin/chflags"; |
| 57 | my $tstfile = "filetest.tst"; |
| 58 | skip("No $chflags available", 4) if !-x $chflags; |
| 59 | |
| 60 | skip("Test does not work on OpenBSD and BSD/OS", 4) |
| 61 | if $^O =~ /^(?:openbsd|bsdos)$/; |
| 62 | |
| 63 | SKIP: { |
| 64 | eval { |
| 65 | if (!-e $tstfile) { |
| 66 | open(T, ">$tstfile") or die "Can't create $tstfile: $!"; |
| 67 | close T; |
| 68 | } |
| 69 | system($chflags, "uchg", $tstfile); |
| 70 | die "Can't exec $chflags uchg" if $? != 0; |
| 71 | }; |
| 72 | skip("Errors in test using chflags: $@", 4) if $@; |
| 73 | |
| 74 | { |
| 75 | use filetest 'access'; |
| 76 | is(-w $tstfile, undef, "$tstfile should not be recognized as writable"); |
| 77 | is(-W $tstfile, undef, "$tstfile should not be recognized as writable"); |
| 78 | } |
| 79 | { |
| 80 | no filetest 'access'; |
| 81 | is(-w $tstfile, 1, "$tstfile should be recognized as writable"); |
| 82 | is(-W $tstfile, 1, "$tstfile should be recognized as writable"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | # cleanup |
| 87 | system($chflags, "nouchg", $tstfile); |
| 88 | unlink $tstfile; |
| 89 | warn "Can't remove $tstfile: $!" if -e $tstfile; |
| 90 | } |