File Coverage

blib/lib/Test/Symlink.pm
Criterion Covered Total %
statement 42 42 100.0
branch 14 14 100.0
condition 8 9 88.8
subroutine 5 5 100.0
pod 1 1 100.0
total 70 71 98.5


line stmt bran cond sub pod time code
1             package Test::Symlink;
2              
3             # Copyright (c) 2005 Nik Clayton
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
8             # are met:
9             # 1. Redistributions of source code must retain the above copyright
10             # notice, this list of conditions and the following disclaimer.
11             # 2. Redistributions in binary form must reproduce the above copyright
12             # notice, this list of conditions and the following disclaimer in the
13             # documentation and/or other materials provided with the distribution.
14             #
15             # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16             # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18             # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19             # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20             # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21             # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22             # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23             # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24             # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25             # SUCH DAMAGE.
26              
27 3     3   47458 use warnings;
  3         7  
  3         101  
28 3     3   16 use strict;
  3         6  
  3         146  
29              
30 3     3   16 use Test::Builder;
  3         10  
  3         1956  
31              
32             require Exporter;
33             our @ISA = qw(Exporter);
34             our @EXPORT = qw(symlink_ok);
35              
36             my $Test = Test::Builder->new;
37             my $Symlinks = eval { symlink("",""); 1 }; # Do we have symlink support?
38              
39             sub import {
40 3     3   25 my($self) = shift;
41 3         6 my $pack = caller;
42              
43 3         28 $Test->exported_to($pack);
44 3         82 $Test->plan(@_);
45              
46 3         442 $self->export_to_level(1, $self, 'symlink_ok');
47             }
48              
49             =head1 NAME
50              
51             Test::Symlink - Test::Builder based test for symlink correctness
52              
53             =head1 VERSION
54              
55             Version 0.02
56              
57             =cut
58              
59             our $VERSION = '0.02';
60              
61             =head1 SYNOPSIS
62              
63             use Test::Symlink tests => 3;
64              
65             symlink_ok('foo', 'bar', 'foo links to bar');
66             symlink_ok('foo' => 'bar', 'Use fat comma for visual clarity');
67              
68             # The test name is optional
69             symlink_ok('foo' => 'bar') # ok 3 - Symlink: foo -> bar
70              
71             Test::Symlink B exports C for testing
72             the correctness of symlinks. Test::Symlink uses Test::Builder, so
73             plays nicely with Test::Simple, Test::More, and other Test::Builder
74             based modules.
75              
76             =head1 FUNCTIONS
77              
78             =head2 symlink_ok($src, $dst, [ $test_name ]);
79              
80             Verifies that $src exists, and is a symlink to $dst.
81              
82             Does B verify that $dst exists, as this is legal, and there is at
83             least one valid usage of this that I'm aware of (F on
84             FreeBSD). If you want to ensure that the destination exists then write
85             this as two tests. For example:
86              
87             ok(-e $dst, "$dst exists");
88             symlink_ok($src, $dst, " ... and $src links to it");
89              
90             The test name (C<$test_name>) is optional. If it is omitted then a test
91             name of the form "Symlink: $src -> $dst" is used.
92              
93             Perl's fat comma operator can be usefully used as an visual aid.
94              
95             The test will be skipped on systems that do not support symlinks.
96             However, the arguments to symlink_ok() will still be checked to ensure
97             that they are defined and non-empty.
98              
99             =cut
100              
101             sub symlink_ok {
102 11     11 1 30082 my($src, $dst, $test_name) = @_;
103              
104 11 100 100     74 if(! defined $src or $src eq '') {
105 2         11 my $ok = $Test->ok(0, 'symlink_ok()');
106 2         192 $Test->diag(' You must provide a $src argument to symlink_ok()');
107 2         54 return $ok;
108             }
109              
110 9 100 100     42 if(! defined $dst or $dst eq '') {
111 2         13 my $ok = $Test->ok(0, "symlink_ok($src)");
112 2         247 $Test->diag(' You must provide a $dst argument to symlink_ok()');
113 2         50 return $ok;
114             }
115              
116 7 100       24 $test_name = "Symlink: $src -> $dst" unless defined $test_name;
117              
118 7 100       21 if(! $Symlinks) {
119 1         8 return $Test->skip('symlinks are not supported on this platform');
120             }
121              
122             # '-e' will follow symlinks. So, to verify that $src really doesn't
123             # exist you have to do the -e check, and you have to readlink() to make
124             # sure it really doesn't exist.
125 6 100 66     173 if(! -e $src and ! defined readlink($src)) {
126 1         11 my $ok = $Test->ok(0, $test_name);
127 1         127 $Test->diag(" $src does not exist");
128 1         32 return $ok;
129             }
130              
131 5 100       57 if(! -l $src) {
132 1         7 my $ok = $Test->ok(0, $test_name);
133 1         97 $Test->diag(" $src exists, but is not a symlink");
134 1         26 return $ok;
135             }
136              
137 4         6 my $act_dst;
138 4 100       33 if(($act_dst = readlink($src)) ne $dst) {
139 1         7 my $ok = $Test->ok(0, $test_name);
140 1         108 $Test->diag(" $src is not a symlink to $dst");
141 1         31 $Test->diag(" got: $src -> $act_dst");
142 1         27 $Test->diag(" expected: $src -> $dst");
143 1         23 return $ok;
144             }
145              
146 3         19 return $Test->ok(1, $test_name);
147             }
148              
149             =head1 AUTHOR
150              
151             Nik Clayton,
152              
153             =head1 BUGS
154              
155             Please report any bugs or feature requests to
156             C, or through the web interface at
157             L.
158             I will be notified, and then you'll automatically be notified of progress on
159             your bug as I make changes.
160              
161             =head1 COPYRIGHT & LICENSE
162              
163             Copyright (c) 2003 Nik Clayton
164             All rights reserved.
165              
166             Redistribution and use in source and binary forms, with or without
167             modification, are permitted provided that the following conditions
168             are met:
169              
170             1. Redistributions of source code must retain the above copyright
171             notice, this list of conditions and the following disclaimer.
172             2. Redistributions in binary form must reproduce the above copyright
173             notice, this list of conditions and the following disclaimer in the
174             documentation and/or other materials provided with the distribution.
175              
176             THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
177             ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
178             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
179             ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180             FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
181             DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
182             OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
183             HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
184             LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
185             OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
186             SUCH DAMAGE.
187              
188             =cut
189              
190             1; # End of Test::Symlink