3 # This file is part of GNU Stow.
5 # GNU Stow is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # GNU Stow is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see https://www.gnu.org/licenses/.
19 # Utilities shared by test scripts
27 use Carp
qw(confess croak);
29 use File
::Path
qw(make_path remove_tree);
34 use Stow
::Util
qw(parent canon_path);
36 use base
qw(Exporter);
42 new_Stow new_compat_Stow
43 make_path make_link make_invalid_link make_file
44 remove_dir remove_file remove_link
46 is_link is_dir_not_symlink is_nonexistent_path
49 our $TEST_DIR = 'tmp-testing-trees';
50 our $ABS_TEST_DIR = File
::Spec
->rel2abs('tmp-testing-trees');
53 my $test_dir = shift || $TEST_DIR;
54 my $abs_test_dir = File
::Spec
->rel2abs($test_dir);
56 # Create a run_from/ subdirectory for tests which want to run
57 # from a separate directory outside the Stow directory or
59 for my $dir ("target", "stow", "run_from", "stow directory") {
60 my $path = "$test_dir/$dir";
61 -d
$path and remove_tree
($path);
65 # Don't let user's ~/.stow-global-ignore affect test results
66 $ENV{HOME
} = $abs_test_dir;
72 # These default paths assume that execution will be triggered from
73 # within the target directory.
74 $opts{dir
} ||= '../stow';
75 $opts{target
} ||= '.';
77 my $stow = eval { new Stow
(%opts) };
79 confess
"Error while trying to instantiate new Stow(%opts): $@";
87 return new_Stow
(%opts);
90 #===== SUBROUTINE ===========================================================
92 # Purpose : safely create a link
93 # Parameters: $link_src => path to the link
94 # : $link_dest => where the new link should point
95 # : $invalid => true iff $link_dest refers to non-existent file
97 # Throws : fatal error if the link can not be safely created
98 # Comments : checks for existing nodes
99 #============================================================================
101 my ($link_src, $link_dest, $invalid) = @_;
104 my $old_source = readlink join('/', parent
($link_src), $link_dest)
105 or croak
"$link_src is already a link but could not read link $link_src/$link_dest";
106 if ($old_source ne $link_dest) {
107 croak
"$link_src already exists but points elsewhere\n";
110 croak
"$link_src already exists and is not a link\n" if -e
$link_src;
111 my $abs_target = File
::Spec
->rel2abs($link_src);
112 my $link_src_container = dirname
($abs_target);
113 my $abs_source = File
::Spec
->rel2abs($link_dest, $link_src_container);
114 #warn "t $link_src c $link_src_container as $abs_source";
115 if (-e
$abs_source) {
116 croak
"Won't make invalid link pointing to existing $abs_target"
120 croak
"Won't make link pointing to non-existent $abs_target"
123 symlink $link_dest, $link_src
124 or croak
"could not create link $link_src => $link_dest ($!)\n";
127 #===== SUBROUTINE ===========================================================
128 # Name : make_invalid_link()
129 # Purpose : safely create an invalid link
130 # Parameters: $target => path to the link
131 # : $source => the non-existent source where the new link should point
133 # Throws : fatal error if the link can not be safely created
134 # Comments : checks for existing nodes
135 #============================================================================
136 sub make_invalid_link
{
137 my ($target, $source, $allow_invalid) = @_;
138 make_link
($target, $source, 1);
141 #===== SUBROUTINE ===========================================================
142 # Name : create_file()
143 # Purpose : create an empty file
144 # Parameters: $path => proposed path to the file
145 # : $contents => (optional) contents to write to file
147 # Throws : fatal error if the file could not be created
148 # Comments : detects clash with an existing non-file
149 #============================================================================
151 my ($path, $contents) = @_;
153 if (-e
$path and ! -f
$path) {
154 croak
"a non-file already exists at $path\n";
157 open my $FILE ,'>', $path
158 or croak
"could not create file: $path ($!)\n";
159 print $FILE $contents if defined $contents;
163 #===== SUBROUTINE ===========================================================
164 # Name : remove_link()
165 # Purpose : remove an esiting symbolic link
166 # Parameters: $path => path to the symbolic link
168 # Throws : fatal error if the operation fails or if passed the path to a
171 #============================================================================
175 croak
qq(remove_link
() called with a non
-link: $path);
177 unlink $path or croak
"could not remove link: $path ($!)\n";
181 #===== SUBROUTINE ===========================================================
182 # Name : remove_file()
183 # Purpose : remove an existing empty file
184 # Parameters: $path => the path to the empty file
186 # Throws : fatal error if given file is non-empty or the operation fails
188 #============================================================================
192 croak
"file at $path is non-empty\n";
194 unlink $path or croak
"could not remove empty file: $path ($!)\n";
198 #===== SUBROUTINE ===========================================================
199 # Name : remove_dir()
200 # Purpose : safely remove a tree of test files
201 # Parameters: $dir => path to the top of the tree
203 # Throws : fatal error if the tree contains a non-link or non-empty file
204 # Comments : recursively removes directories containing softlinks empty files
205 #============================================================================
210 croak
"$dir is not a directory";
213 opendir my $DIR, $dir or croak
"cannot read directory: $dir ($!)\n";
214 my @listing = readdir $DIR;
218 for my $node (@listing) {
219 next NODE
if $node eq '.';
220 next NODE
if $node eq '..';
222 my $path = "$dir/$node";
223 if (-l
$path or (-f
$path and -z
$path) or $node eq $Stow::LOCAL_IGNORE_FILE
) {
224 unlink $path or croak
"cannot unlink $path ($!)\n";
230 croak
"$path is not a link, directory, or empty file\n";
233 rmdir $dir or croak
"cannot rmdir $dir ($!)\n";
238 #===== SUBROUTINE ===========================================================
240 # Purpose : wrapper around chdir
241 # Parameters: $dir => path to chdir to
243 # Throws : fatal error if the chdir fails
245 #============================================================================
248 chdir $dir or croak
"Failed to chdir($dir): $!\n";
251 #===== SUBROUTINE ===========================================================
253 # Purpose : return file contents
254 # Parameters: $file => file to read
256 # Throws : fatal error if the open fails
258 #============================================================================
261 open F
, $file or croak
"Failed to open($file): $!\n";
262 my $contents = join '', <F
>;
267 #===== SUBROUTINE ===========================================================
269 # Purpose : assert path is a symlink
270 # Parameters: $path => path to check
271 # : $dest => target symlink should point to
272 #============================================================================
274 my ($path, $dest) = @_;
275 ok
(-l
$path => "$path should be symlink");
276 is
(readlink $path, $dest => "$path symlinks to $dest");
279 #===== SUBROUTINE ===========================================================
280 # Name : is_dir_not_symlink()
281 # Purpose : assert path is a directory not a symlink
282 # Parameters: $path => path to check
283 #============================================================================
284 sub is_dir_not_symlink
{
286 ok
(! -l
$path => "$path should not be symlink");
287 ok
(-d _
=> "$path should be a directory");
290 #===== SUBROUTINE ===========================================================
291 # Name : is_nonexistent_path()
292 # Purpose : assert path does not exist
293 # Parameters: $path => path to check
294 #============================================================================
295 sub is_nonexistent_path
{
297 ok
(! -l
$path => "$path should not be symlink");
298 ok
(! -e _
=> "$path should not exist");