File: links.t

package info (click to toggle)
libtest-file-perl 1.41-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 244 kB
  • ctags: 54
  • sloc: perl: 986; makefile: 2
file content (192 lines) | stat: -rw-r--r-- 4,885 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
use strict;

use Test::Builder::Tester;
use Test::More 0.95;
use Test::File;

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

plan skip_all => "This system does't do symlinks" unless $can_symlink;

require "t/setup_common";

subtest dont_work_with_symlinks => sub {
	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();
		}
	};


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";

subtest should_work => sub {
	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\n    ok 2 - $test_name" );
	symlink_target_exists_ok( $readable_sym, $readable );
	symlink_target_is( $readable_sym, $readable, $test_name );
	test_test();
	};

subtest should_work => sub {
	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();
	};

subtest bad_target_does_not_exist => sub {
	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();
	};

subtest bad_target_does_exists => sub {
	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();
	};

subtest dangling_exists => sub {
	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();
	};


done_testing();