File: links.t

package info (click to toggle)
libtest-file-perl 1.29-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 180 kB
  • ctags: 40
  • sloc: perl: 771; makefile: 2
file content (214 lines) | stat: -rw-r--r-- 5,532 bytes parent folder | download
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
use strict;

use Test::Builder::Tester;
use Test::More tests => 37;
use Test::File;

my $can_symlink = eval { symlink("",""); 1 };

my $test_directory = 'test_files';
SKIP: {
    skip "This system does't do symlinks", 5 unless $can_symlink;
    require "t/setup_common";
};

chdir $test_directory or print "bail out! Could not change directories: $!";


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Things that don't work with symlinks. Fake that we don't understand
# symlinks
{
no warnings 'redefine';
local *Test::File::_no_symlinks_here = sub { 1 };

my @subs = qw(
	file_is_symlink_ok
	symlink_target_exists_ok
	symlink_target_dangles_ok
	symlink_target_is
	);

foreach my $sub ( @subs )
	{
	no strict 'refs';
	ok( defined &{$sub}, "$sub is defined" );
	}

foreach my $sub ( @subs )
	{
	no strict 'refs';
	
	test_out("ok 1 # skip $sub doesn't work on systems without symlinks!");
	&{$sub}();	
	test_test();
	}

}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Stuff that should work
{
my $test_name     = "This is my test name";
my $readable      = 'readable';
my $readable_sym  = 'readable_sym';
my $not_there     = 'not_there';
my $dangle_sym    = 'dangle_sym';

my $s = ! $can_symlink
	? "# skip file_is_symlink_ok doesn't work on systems without symlinks"
	: "- $readable_sym is a symlink";

file_exists_ok( $readable );
file_not_exists_ok( $readable_sym );

if( $can_symlink )
	{
	symlink( $readable, $readable_sym );
	
	open my($fh), ">", $not_there;
	close $fh;
	file_exists_ok( $not_there );
	
	symlink( $not_there, $dangle_sym );
	file_exists_ok( $readable_sym );
	file_exists_ok( $dangle_sym );
	
	unlink $not_there;
	ok( ! -e $not_there );
	file_is_symlink_ok( $dangle_sym );
	}
else
	{
	pass();
	}

test_out( "ok 1 $s" );
file_is_symlink_ok( $readable_sym );
test_test();

test_out( "ok 1 - $test_name" );
file_is_symlink_ok( $readable_sym, $test_name );
test_test();

test_out( "ok 1 - $test_name" );
symlink_target_dangles_ok( $dangle_sym, $test_name );
test_test();

test_out( "ok 1 - $test_name" );
symlink_target_exists_ok( $readable_sym, $readable, $test_name );
test_test();

test_out( "ok 1 $s\nok 2 - $test_name" );
symlink_target_exists_ok( $readable_sym, $readable );
symlink_target_is( $readable_sym, $readable, $test_name );
test_test();


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Stuff that shouldn't work (not a symlink)
{
ok( ! -l $readable,  "$readable is not a symlink" );
ok( ! -l $not_there, "$not_there is not a symlink" );

test_out( "not ok 1 - $test_name" );
test_diag( 
	"File [$readable] is not a symlink!\n" .
	"#   Failed test '$test_name'\n" . 
	"#   at $0 line " . line_num(+5) . "." 
	);
file_is_symlink_ok( $readable, $test_name );
test_test();

test_out( "not ok 1 - $test_name" );
test_diag( 
	"File [$not_there] is not a symlink!\n" .
	"#   Failed test '$test_name'\n" . 
	"#   at $0 line " . line_num(+5) . "." 
	);
file_is_symlink_ok( $not_there, $test_name );
test_test();

test_out( "not ok 1 - $test_name" );
test_diag( 
	"File [$not_there] is not a symlink!\n" .
	"#   Failed test '$test_name'\n" . 
	"#   at $0 line " . line_num(+5) . "." 
	);
symlink_target_dangles_ok( $not_there, $test_name );
test_test();

test_out( "not ok 1 - $test_name" );
test_diag( 
	"File [$readable] is not a symlink!\n" .
	"#   Failed test '$test_name'\n" . 
	"#   at $0 line " . line_num(+5) . "." 
	);
symlink_target_is( $readable, $readable_sym, $test_name );
test_test();

test_out( "not ok 1 - $readable is a symlink" );
test_diag( 
	"File [$readable] is not a symlink!\n" . 
	"#   Failed test '$readable is a symlink'\n" . 
	"#   at $0 line " . line_num(+5) . "." 
	);
symlink_target_exists_ok( $readable );
test_test();
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Test using bad target that doesn't exist
test_out( "not ok 1 $s" );
test_diag( 
	"Symlink [$readable_sym] points to non-existent target [$not_there]!\n" .
	"#   Failed test '$readable_sym is a symlink'\n" . 
	"#   at $0 line " . line_num(+5) . "." 
	);
symlink_target_exists_ok( $readable_sym, $not_there );
test_test();

test_out( "not ok 1 - symlink $readable_sym points to $not_there" );
test_diag( 
	"  Failed test 'symlink $readable_sym points to $not_there'\n" .
	"#   at $0 line " . line_num(+6) . ".\n" .
	"#        got: $readable\n" .
	"#   expected: $not_there" 
	);
symlink_target_is( $readable_sym, $not_there );
test_test();


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Test using bad target that exists
test_out( "not ok 1 $s" );
test_diag( 
 	"Symlink [readable_sym] points to\n" .
 	"#          got: readable\n" .
	"#     expected: writeable\n" .
	"#   Failed test 'readable_sym is a symlink'\n" .
	"#   at $0 line " . line_num(+7) . "."
	);
symlink_target_exists_ok( $readable_sym, "writeable" );
test_test();

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Test dangling with existing targets
test_out( "not ok 1 - $test_name" );
test_diag( 
	"Symlink [$readable_sym] points to existing file [$readable] but shouldn't!\n" .
	"#   Failed test '$test_name'\n" . 
	"#   at $0 line " . line_num(+5) . "." 
	);
symlink_target_dangles_ok( $readable_sym, $test_name );
test_test();
}


# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

END {
unlink glob( "test_files/*" );
rmdir "test_files";
}