Use `shellwords` for `.stowrc` parsing
[gnu-stow.git] / t / testutil.pm
blob9fb9862d112cd38e57d14abd9fe5192ee18c5644
1 #!/usr/bin/perl
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
22 package testutil;
24 use strict;
25 use warnings;
27 use Carp qw(confess croak);
28 use File::Basename;
29 use File::Path qw(make_path remove_tree);
30 use File::Spec;
31 use Test::More;
33 use Stow;
34 use Stow::Util qw(parent canon_path);
36 use base qw(Exporter);
37 our @EXPORT = qw(
38 $ABS_TEST_DIR
39 $TEST_DIR
40 init_test_dirs
42 new_Stow new_compat_Stow
43 make_path make_link make_invalid_link make_file
44 remove_dir remove_file remove_link
45 cat_file
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');
52 sub init_test_dirs {
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
58 # target directory.
59 for my $dir ("target", "stow", "run_from", "stow directory") {
60 my $path = "$test_dir/$dir";
61 -d $path and remove_tree($path);
62 make_path($path);
65 # Don't let user's ~/.stow-global-ignore affect test results
66 $ENV{HOME} = $abs_test_dir;
67 return $abs_test_dir;
70 sub new_Stow {
71 my %opts = @_;
72 # These default paths assume that execution will be triggered from
73 # within the target directory.
74 $opts{dir} ||= '../stow';
75 $opts{target} ||= '.';
76 $opts{test_mode} = 1;
77 my $stow = eval { new Stow(%opts) };
78 if ($@) {
79 confess "Error while trying to instantiate new Stow(%opts): $@";
81 return $stow;
84 sub new_compat_Stow {
85 my %opts = @_;
86 $opts{compat} = 1;
87 return new_Stow(%opts);
90 #===== SUBROUTINE ===========================================================
91 # Name : make_link()
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
96 # Returns : n/a
97 # Throws : fatal error if the link can not be safely created
98 # Comments : checks for existing nodes
99 #============================================================================
100 sub make_link {
101 my ($link_src, $link_dest, $invalid) = @_;
103 if (-l $link_src) {
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"
117 if $invalid;
119 else {
120 croak "Won't make link pointing to non-existent $abs_target"
121 unless $invalid;
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
132 # Returns : n/a
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
146 # Returns : n/a
147 # Throws : fatal error if the file could not be created
148 # Comments : detects clash with an existing non-file
149 #============================================================================
150 sub make_file {
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;
160 close $FILE;
163 #===== SUBROUTINE ===========================================================
164 # Name : remove_link()
165 # Purpose : remove an esiting symbolic link
166 # Parameters: $path => path to the symbolic link
167 # Returns : n/a
168 # Throws : fatal error if the operation fails or if passed the path to a
169 # : non-link
170 # Comments : none
171 #============================================================================
172 sub remove_link {
173 my ($path) = @_;
174 if (not -l $path) {
175 croak qq(remove_link() called with a non-link: $path);
177 unlink $path or croak "could not remove link: $path ($!)\n";
178 return;
181 #===== SUBROUTINE ===========================================================
182 # Name : remove_file()
183 # Purpose : remove an existing empty file
184 # Parameters: $path => the path to the empty file
185 # Returns : n/a
186 # Throws : fatal error if given file is non-empty or the operation fails
187 # Comments : none
188 #============================================================================
189 sub remove_file {
190 my ($path) = @_;
191 if (-z $path) {
192 croak "file at $path is non-empty\n";
194 unlink $path or croak "could not remove empty file: $path ($!)\n";
195 return;
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
202 # Returns : n/a
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 #============================================================================
206 sub remove_dir {
207 my ($dir) = @_;
209 if (not -d $dir) {
210 croak "$dir is not a directory";
213 opendir my $DIR, $dir or croak "cannot read directory: $dir ($!)\n";
214 my @listing = readdir $DIR;
215 closedir $DIR;
217 NODE:
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";
226 elsif (-d "$path") {
227 remove_dir($path);
229 else {
230 croak "$path is not a link, directory, or empty file\n";
233 rmdir $dir or croak "cannot rmdir $dir ($!)\n";
235 return;
238 #===== SUBROUTINE ===========================================================
239 # Name : cd()
240 # Purpose : wrapper around chdir
241 # Parameters: $dir => path to chdir to
242 # Returns : n/a
243 # Throws : fatal error if the chdir fails
244 # Comments : none
245 #============================================================================
246 sub cd {
247 my ($dir) = @_;
248 chdir $dir or croak "Failed to chdir($dir): $!\n";
251 #===== SUBROUTINE ===========================================================
252 # Name : cat_file()
253 # Purpose : return file contents
254 # Parameters: $file => file to read
255 # Returns : n/a
256 # Throws : fatal error if the open fails
257 # Comments : none
258 #============================================================================
259 sub cat_file {
260 my ($file) = @_;
261 open F, $file or croak "Failed to open($file): $!\n";
262 my $contents = join '', <F>;
263 close(F);
264 return $contents;
267 #===== SUBROUTINE ===========================================================
268 # Name : is_link()
269 # Purpose : assert path is a symlink
270 # Parameters: $path => path to check
271 # : $dest => target symlink should point to
272 #============================================================================
273 sub is_link {
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 {
285 my ($path) = @_;
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 {
296 my ($path) = @_;
297 ok(! -l $path => "$path should not be symlink");
298 ok(! -e _ => "$path should not exist");
304 # Local variables:
305 # mode: perl
306 # end:
307 # vim: ft=perl