2011-11-16 09:46:31 -05:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
2011-11-16 09:04:03 -05:00
|
|
|
#
|
|
|
|
# Utilities shared by test scripts
|
|
|
|
#
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2011-11-24 11:28:09 -05:00
|
|
|
use Stow;
|
|
|
|
use Stow::Util qw(parent);
|
2011-11-17 13:46:13 -05:00
|
|
|
|
2011-11-23 18:45:48 -05:00
|
|
|
sub init_test_dirs {
|
2011-11-24 11:28:09 -05:00
|
|
|
die "t/ didn't exist; are you running the tests from the root of the tree?\n"
|
|
|
|
unless -d 't';
|
|
|
|
|
|
|
|
for my $dir ('t/target', 't/stow') {
|
2011-11-23 18:45:48 -05:00
|
|
|
-d $dir and remove_dir($dir);
|
2011-11-24 11:28:09 -05:00
|
|
|
make_dir($dir);
|
|
|
|
}
|
2011-11-23 18:45:48 -05:00
|
|
|
|
|
|
|
# Don't let user's ~/.stow-global-ignore affect test results
|
|
|
|
$ENV{HOME} = '/tmp/fake/home';
|
2011-11-24 11:28:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sub new_Stow {
|
|
|
|
my %opts = @_;
|
|
|
|
$opts{dir} ||= '../stow';
|
|
|
|
$opts{target} ||= '.';
|
|
|
|
$opts{test_mode} = 1;
|
|
|
|
return new Stow(%opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub new_compat_Stow {
|
|
|
|
my %opts = @_;
|
|
|
|
$opts{compat} = 1;
|
|
|
|
return new_Stow(%opts);
|
2011-11-17 13:46:13 -05:00
|
|
|
}
|
|
|
|
|
2011-11-16 09:04:03 -05:00
|
|
|
#===== SUBROUTINE ===========================================================
|
|
|
|
# Name : make_link()
|
|
|
|
# Purpose : safely create a link
|
|
|
|
# Parameters: $target => path to the link
|
|
|
|
# : $source => where the new link should point
|
|
|
|
# Returns : n/a
|
|
|
|
# Throws : fatal error if the link can not be safely created
|
|
|
|
# Comments : checks for existing nodes
|
|
|
|
#============================================================================
|
|
|
|
sub make_link {
|
|
|
|
my ($target, $source) = @_;
|
|
|
|
|
|
|
|
if (-l $target) {
|
2011-11-24 11:28:09 -05:00
|
|
|
my $old_source = readlink join('/', parent($target), $source)
|
2011-11-16 09:04:03 -05:00
|
|
|
or die "could not read link $target/$source";
|
|
|
|
if ($old_source ne $source) {
|
|
|
|
die "$target already exists but points elsewhere\n";
|
|
|
|
}
|
|
|
|
}
|
2011-11-24 11:28:09 -05:00
|
|
|
elsif (-e $target) {
|
2011-11-16 09:04:03 -05:00
|
|
|
die "$target already exists and is not a link\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
symlink $source, $target
|
|
|
|
or die "could not create link $target => $source ($!)\n";
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#===== SUBROUTINE ===========================================================
|
|
|
|
# Name : make_dir()
|
2011-11-24 11:28:09 -05:00
|
|
|
# Purpose : create a directory and any requisite parents
|
2011-11-16 09:04:03 -05:00
|
|
|
# Parameters: $dir => path to the new directory
|
|
|
|
# Returns : n/a
|
|
|
|
# Throws : fatal error if the directory or any of its parents cannot be
|
|
|
|
# : created
|
|
|
|
# Comments : none
|
|
|
|
#============================================================================
|
|
|
|
sub make_dir {
|
|
|
|
my ($dir) = @_;
|
|
|
|
|
|
|
|
my @parents = ();
|
|
|
|
for my $part (split '/', $dir) {
|
|
|
|
my $path = join '/', @parents, $part;
|
|
|
|
if (not -d $path and not mkdir $path) {
|
|
|
|
die "could not create directory: $path ($!)\n";
|
|
|
|
}
|
|
|
|
push @parents, $part;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#===== SUBROUTINE ===========================================================
|
|
|
|
# Name : create_file()
|
|
|
|
# Purpose : create an empty file
|
|
|
|
# Parameters: $path => proposed path to the file
|
2011-11-23 18:45:48 -05:00
|
|
|
# : $contents => (optional) contents to write to file
|
2011-11-16 09:04:03 -05:00
|
|
|
# Returns : n/a
|
|
|
|
# Throws : fatal error if the file could not be created
|
|
|
|
# Comments : detects clash with an existing non-file
|
|
|
|
#============================================================================
|
|
|
|
sub make_file {
|
2011-11-23 18:45:48 -05:00
|
|
|
my ($path, $contents) =@_;
|
2011-11-16 09:04:03 -05:00
|
|
|
|
2011-11-23 18:45:48 -05:00
|
|
|
if (-e $path and ! -f $path) {
|
2011-11-16 09:04:03 -05:00
|
|
|
die "a non-file already exists at $path\n";
|
|
|
|
}
|
2011-11-23 18:45:48 -05:00
|
|
|
|
|
|
|
open my $FILE ,'>', $path
|
|
|
|
or die "could not create file: $path ($!)\n";
|
|
|
|
print $FILE $contents if defined $contents;
|
|
|
|
close $FILE;
|
2011-11-16 09:04:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#===== SUBROUTINE ===========================================================
|
|
|
|
# Name : remove_link()
|
|
|
|
# Purpose : remove an esiting symbolic link
|
|
|
|
# Parameters: $path => path to the symbolic link
|
|
|
|
# Returns : n/a
|
|
|
|
# Throws : fatal error if the operation fails or if passed the path to a
|
|
|
|
# : non-link
|
|
|
|
# Comments : none
|
|
|
|
#============================================================================
|
|
|
|
sub remove_link {
|
|
|
|
my ($path) = @_;
|
|
|
|
if (not -l $path) {
|
|
|
|
die qq(remove_link() called with a non-link: $path);
|
|
|
|
}
|
|
|
|
unlink $path or die "could not remove link: $path ($!)\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#===== SUBROUTINE ===========================================================
|
|
|
|
# Name : remove_file()
|
|
|
|
# Purpose : remove an existing empty file
|
|
|
|
# Parameters: $path => the path to the empty file
|
|
|
|
# Returns : n/a
|
|
|
|
# Throws : fatal error if given file is non-empty or the operation fails
|
|
|
|
# Comments : none
|
|
|
|
#============================================================================
|
|
|
|
sub remove_file {
|
|
|
|
my ($path) = @_;
|
|
|
|
if (-z $path) {
|
|
|
|
die "file at $path is non-empty\n";
|
|
|
|
}
|
|
|
|
unlink $path or die "could not remove empty file: $path ($!)\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#===== SUBROUTINE ===========================================================
|
|
|
|
# Name : remove_dir()
|
|
|
|
# Purpose : safely remove a tree of test files
|
|
|
|
# Parameters: $dir => path to the top of the tree
|
|
|
|
# Returns : n/a
|
|
|
|
# Throws : fatal error if the tree contains a non-link or non-empty file
|
|
|
|
# Comments : recursively removes directories containing softlinks empty files
|
|
|
|
#============================================================================
|
|
|
|
sub remove_dir {
|
|
|
|
my ($dir) = @_;
|
|
|
|
|
|
|
|
if (not -d $dir) {
|
|
|
|
die "$dir is not a directory";
|
|
|
|
}
|
|
|
|
|
|
|
|
opendir my $DIR, $dir or die "cannot read directory: $dir ($!)\n";
|
|
|
|
my @listing = readdir $DIR;
|
|
|
|
closedir $DIR;
|
|
|
|
|
|
|
|
NODE:
|
|
|
|
for my $node (@listing) {
|
|
|
|
next NODE if $node eq '.';
|
|
|
|
next NODE if $node eq '..';
|
|
|
|
|
|
|
|
my $path = "$dir/$node";
|
2011-11-23 18:45:48 -05:00
|
|
|
if (-l $path or -z $path or $node eq $Stow::LOCAL_IGNORE_FILE) {
|
2011-11-16 09:04:03 -05:00
|
|
|
unlink $path or die "cannot unlink $path ($!)\n";
|
|
|
|
}
|
|
|
|
elsif (-d "$path") {
|
|
|
|
remove_dir($path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
die "$path is not a link, directory, or empty file\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir $dir or die "cannot rmdir $dir ($!)\n";
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-24 11:28:09 -05:00
|
|
|
#===== SUBROUTINE ===========================================================
|
|
|
|
# Name : cd()
|
|
|
|
# Purpose : wrapper around chdir
|
|
|
|
# Parameters: $dir => path to chdir to
|
|
|
|
# Returns : n/a
|
|
|
|
# Throws : fatal error if the chdir fails
|
|
|
|
# Comments : none
|
|
|
|
#============================================================================
|
|
|
|
sub cd {
|
|
|
|
my ($dir) = @_;
|
|
|
|
chdir $dir or die "Failed to chdir($dir): $!\n";
|
|
|
|
}
|
|
|
|
|
2011-11-16 09:04:03 -05:00
|
|
|
1;
|
2011-11-24 11:28:09 -05:00
|
|
|
|
|
|
|
# Local variables:
|
|
|
|
# mode: perl
|
|
|
|
# cperl-indent-level: 4
|
|
|
|
# end:
|
|
|
|
# vim: ft=perl
|