4 # Utilities shared by test scripts
13 use Stow
::Util
qw(parent canon_path);
15 use base
qw(Exporter);
20 new_Stow new_compat_Stow
21 make_dir make_link make_file
22 remove_dir remove_link
25 our $OUT_DIR = 'tmp-testing-trees';
28 for my $dir ("$OUT_DIR/target", "$OUT_DIR/stow") {
29 -d
$dir and remove_dir
($dir);
33 # Don't let user's ~/.stow-global-ignore affect test results
34 $ENV{HOME
} = '/tmp/fake/home';
39 $opts{dir
} ||= '../stow';
40 $opts{target
} ||= '.';
42 return new Stow
(%opts);
48 return new_Stow
(%opts);
51 #===== SUBROUTINE ===========================================================
53 # Purpose : safely create a link
54 # Parameters: $target => path to the link
55 # : $source => where the new link should point
57 # Throws : fatal error if the link can not be safely created
58 # Comments : checks for existing nodes
59 #============================================================================
61 my ($target, $source) = @_;
64 my $old_source = readlink join('/', parent
($target), $source)
65 or die "could not read link $target/$source";
66 if ($old_source ne $source) {
67 die "$target already exists but points elsewhere\n";
71 die "$target already exists and is not a link\n";
74 symlink $source, $target
75 or die "could not create link $target => $source ($!)\n";
80 #===== SUBROUTINE ===========================================================
82 # Purpose : create a directory and any requisite parents
83 # Parameters: $dir => path to the new directory
85 # Throws : fatal error if the directory or any of its parents cannot be
88 #============================================================================
93 for my $part (split '/', $dir) {
94 my $path = join '/', @parents, $part;
95 if (not -d
$path and not mkdir $path) {
96 die "could not create directory: $path ($!)\n";
103 #===== SUBROUTINE ===========================================================
104 # Name : create_file()
105 # Purpose : create an empty file
106 # Parameters: $path => proposed path to the file
107 # : $contents => (optional) contents to write to file
109 # Throws : fatal error if the file could not be created
110 # Comments : detects clash with an existing non-file
111 #============================================================================
113 my ($path, $contents) =@_;
115 if (-e
$path and ! -f
$path) {
116 die "a non-file already exists at $path\n";
119 open my $FILE ,'>', $path
120 or die "could not create file: $path ($!)\n";
121 print $FILE $contents if defined $contents;
125 #===== SUBROUTINE ===========================================================
126 # Name : remove_link()
127 # Purpose : remove an esiting symbolic link
128 # Parameters: $path => path to the symbolic link
130 # Throws : fatal error if the operation fails or if passed the path to a
133 #============================================================================
137 die qq(remove_link
() called with a non
-link: $path);
139 unlink $path or die "could not remove link: $path ($!)\n";
143 #===== SUBROUTINE ===========================================================
144 # Name : remove_file()
145 # Purpose : remove an existing empty file
146 # Parameters: $path => the path to the empty file
148 # Throws : fatal error if given file is non-empty or the operation fails
150 #============================================================================
154 die "file at $path is non-empty\n";
156 unlink $path or die "could not remove empty file: $path ($!)\n";
160 #===== SUBROUTINE ===========================================================
161 # Name : remove_dir()
162 # Purpose : safely remove a tree of test files
163 # Parameters: $dir => path to the top of the tree
165 # Throws : fatal error if the tree contains a non-link or non-empty file
166 # Comments : recursively removes directories containing softlinks empty files
167 #============================================================================
172 die "$dir is not a directory";
175 opendir my $DIR, $dir or die "cannot read directory: $dir ($!)\n";
176 my @listing = readdir $DIR;
180 for my $node (@listing) {
181 next NODE
if $node eq '.';
182 next NODE
if $node eq '..';
184 my $path = "$dir/$node";
185 if (-l
$path or -z
$path or $node eq $Stow::LOCAL_IGNORE_FILE
) {
186 unlink $path or die "cannot unlink $path ($!)\n";
192 die "$path is not a link, directory, or empty file\n";
195 rmdir $dir or die "cannot rmdir $dir ($!)\n";
200 #===== SUBROUTINE ===========================================================
202 # Purpose : wrapper around chdir
203 # Parameters: $dir => path to chdir to
205 # Throws : fatal error if the chdir fails
207 #============================================================================
210 chdir $dir or die "Failed to chdir($dir): $!\n";
217 # cperl-indent-level: 4