Add separate tests for .stowrc from $HOME and $PWD

.stowrc can be obtained from $HOME and/or the current working
directory; however only the $HOME case was tested before, because
during tests Stow was being run from $HOME.

So switch $TEST_DIR to an absolute path, create a new run_from/
subdirectory, and chdir to that before invoking any Stow code.  This
allows us to test the behaviour of .stowrc in $HOME and run_from/
separately.
This commit is contained in:
Adam Spiers 2019-06-28 09:56:46 +01:00
parent ac74d75a98
commit 74f0182834
4 changed files with 156 additions and 67 deletions

View file

@ -22,67 +22,144 @@
use strict;
use warnings;
use Test::More tests => 23;
use Test::More tests => 33;
use testutil;
require 'stow';
# stowrc file used for testing.
my $RC_FILE = "$TEST_DIR/.stowrc";
# .stowrc files used for testing, relative to run_from/
my $CWD_RC_FILE = ".stowrc";
my $HOME_RC_FILE = "../.stowrc";
# Take the safe route and cowardly refuse to continue if there's
# already a file at $RC_FILE.
if (-e $RC_FILE) {
die "RC file location $RC_FILE already exists!\n";
# already a file at $HOME_RC_FILE.
if (-e $HOME_RC_FILE) {
die "RC file location $HOME_RC_FILE already exists!\n";
}
# Define the variable that will be used to write stowrc.
my $rc_contents;
my ($options, $pkgs_to_delete, $pkgs_to_stow);
# Init testing directory structure and overwrite ENV{HOME} to prevent
# squashing existing stowrc file.
# squashing existing .stowrc file.
init_test_dirs();
# =========== RC Loading Tests ===========
# Basic parsing and loading rc file tests.
# ========================================
my $orig_HOME = $ENV{HOME};
#
# Test stowrc file with one options per line.
# Test no .stowrc file anywhere
#
delete $ENV{HOME};
local @ARGV = ('dummy');
cd("$TEST_DIR/run_from");
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{target}, "$ABS_TEST_DIR", "default --target with no .stowrc");
is($options->{dir}, "$ABS_TEST_DIR/run_from", "default -d with no .stowrc");
#
# Test .stowrc file in cwd with relative paths, and $HOME not defined
#
make_file($CWD_RC_FILE, <<HERE);
-d ../stow
--target ../target
HERE
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{target}, "../target"
=> "relative --target from \$PWD/.stowrc");
is($options->{dir}, "../stow"
=> "relative -d from \$PWD/.stowrc");
$ENV{HOME} = $orig_HOME;
remove_file($CWD_RC_FILE);
#
# Test .stowrc file in cwd with absolute paths, and $HOME not defined
#
make_file($CWD_RC_FILE, <<HERE);
-d $ABS_TEST_DIR/stow
--target $ABS_TEST_DIR/target
HERE
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{target}, "$ABS_TEST_DIR/target"
=> "absolute --target from \$PWD/.stowrc");
is($options->{dir}, "$ABS_TEST_DIR/stow"
=> "abs_test_dir -d from \$PWD/.stowrc");
$ENV{HOME} = $orig_HOME;
remove_file($CWD_RC_FILE);
#
# Test ~/.stowrc file with one relative option per line.
#
local @ARGV = ('dummy');
$rc_contents = <<HERE;
-d $TEST_DIR/stow
--target $TEST_DIR/target
make_file($HOME_RC_FILE, <<HERE);
-d ../stow
--target ../target
HERE
make_file($RC_FILE, $rc_contents);
my ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{target}, "$TEST_DIR/target", "rc options different lines");
is($options->{dir}, "$TEST_DIR/stow", "rc options different lines");
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{target}, "../target", "--target from \$HOME/.stowrc");
is($options->{dir}, "../stow", "-d from \$HOME/.stowrc");
#
# Test that scalar cli option overwrites conflicting stowrc option.
# Test ~/.stowrc file with one absolute option per line.
#
local @ARGV = ('-d', "$TEST_DIR/stow",'dummy');
$rc_contents = <<HERE;
local @ARGV = ('dummy');
make_file($HOME_RC_FILE, <<HERE);
-d $ABS_TEST_DIR/stow
--target $ABS_TEST_DIR/target
HERE
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{target}, "$ABS_TEST_DIR/target"
=> "--target from \$HOME/.stowrc");
is($options->{dir}, "$ABS_TEST_DIR/stow"
=> "-d from \$HOME/.stowrc");
#
# Test ~/.stowrc file is overridden by .stowrc in cwd.
#
local @ARGV = ('dummy');
make_file($HOME_RC_FILE, <<HERE);
-d $ABS_TEST_DIR/stow-will-be-overridden
--target $ABS_TEST_DIR/target-will-be-overridden
HERE
make_file($CWD_RC_FILE, <<HERE);
-d $ABS_TEST_DIR/stow
--target $ABS_TEST_DIR/target
HERE
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{target}, "$ABS_TEST_DIR/target"
=> "--target overridden by \$PWD/.stowrc");
is($options->{dir}, "$ABS_TEST_DIR/stow"
=> "-d overridden \$PWD/.stowrc");
unlink($CWD_RC_FILE) or die "Failed to unlink $CWD_RC_FILE";
#
# Test that scalar cli option overwrites conflicting ~/.stowrc option.
#
local @ARGV = ('-d', "$ABS_TEST_DIR/stow", 'dummy');
make_file($HOME_RC_FILE, <<HERE);
-d bad/path
HERE
make_file($RC_FILE, $rc_contents);
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{dir}, "$TEST_DIR/stow", "cli overwrite scalar rc option.");
is($options->{dir}, "$ABS_TEST_DIR/stow", "cli overwrite scalar rc option.");
#
# Test that list cli option merges with conflicting stowrc option.
# Documentation states that stowrc options are prepended to cli options.
# Test that list cli option merges with conflicting .stowrc option.
# Documentation states that .stowrc options are prepended to cli options.
#
local @ARGV = (
'--defer=man',
'dummy'
);
$rc_contents = <<HERE;
make_file($HOME_RC_FILE, <<HERE);
--defer=info
HERE
make_file($RC_FILE, $rc_contents);
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is_deeply($options->{defer}, [qr(\Ainfo), qr(\Aman)],
'defer man and info');
@ -98,8 +175,8 @@ is_deeply($options->{defer}, [qr(\Ainfo), qr(\Aman)],
# Test environment variable expansion function.
#
# Basic expansion
is(expand_environment('$HOME/stow'), "$TEST_DIR/stow", 'expand $HOME');
is(expand_environment('${HOME}/stow'), "$TEST_DIR/stow", 'expand ${HOME}');
is(expand_environment('$HOME/stow'), "$ABS_TEST_DIR/stow", 'expand $HOME');
is(expand_environment('${HOME}/stow'), "$ABS_TEST_DIR/stow", 'expand ${HOME}');
delete $ENV{UNDEFINED}; # just in case
foreach my $var ('$UNDEFINED', '${UNDEFINED}') {
@ -135,19 +212,18 @@ is(expand_tilde('\~/path'), '~/path', 'escaped tilde');
#
# Test that environment variable expansion is applied.
#
$rc_contents = <<'HERE';
make_file($HOME_RC_FILE, <<'HERE');
--dir=$HOME/stow
--target=$HOME/stow
--ignore=\$HOME
--defer=\$HOME
--override=\$HOME
HERE
make_file($RC_FILE, $rc_contents);
($options, $pkgs_to_delete, $pkgs_to_stow) = get_config_file_options();
is($options->{dir}, "$TEST_DIR/stow",
"apply environment expansion on stowrc --dir");
is($options->{target}, "$TEST_DIR/stow",
"apply environment expansion on stowrc --target");
is($options->{dir}, "$ABS_TEST_DIR/stow",
"apply environment expansion on \$HOME/.stowrc --dir");
is($options->{target}, "$ABS_TEST_DIR/stow",
"apply environment expansion on \$HOME/.stowrc --target");
is_deeply($options->{ignore}, [qr(\$HOME\z)],
"environment expansion not applied on --ignore");
is_deeply($options->{defer}, [qr(\A\$HOME)],
@ -158,28 +234,28 @@ is_deeply($options->{override}, [qr(\A\$HOME)],
#
# Test that tilde expansion is applied in correct places.
#
$rc_contents = <<'HERE';
make_file($HOME_RC_FILE, <<'HERE');
--dir=~/stow
--target=~/stow
--ignore=~/stow
--defer=~/stow
--override=~/stow
HERE
make_file($RC_FILE, $rc_contents);
($options, $pkgs_to_delete, $pkgs_to_stow) = get_config_file_options();
is($options->{dir}, "$TEST_DIR/stow",
"apply environment expansion on stowrc --dir");
is($options->{target}, "$TEST_DIR/stow",
"apply environment expansion on stowrc --target");
is($options->{dir}, "$ABS_TEST_DIR/stow",
"apply tilde expansion on \$HOME/.stowrc --dir");
is($options->{target}, "$ABS_TEST_DIR/stow",
"apply tilde expansion on \$HOME/.stowrc --target");
is_deeply($options->{ignore}, [qr(~/stow\z)],
"environment expansion not applied on --ignore");
"tilde expansion not applied on --ignore");
is_deeply($options->{defer}, [qr(\A~/stow)],
"environment expansion not applied on --defer");
"tilde expansion not applied on --defer");
is_deeply($options->{override}, [qr(\A~/stow)],
"environment expansion not applied on --override");
"tilde expansion not applied on --override");
#
# Clean up files used for testing.
#
unlink $RC_FILE or die "Unable to clean up $RC_FILE.\n";
remove_dir($TEST_DIR);
unlink $HOME_RC_FILE or die "Unable to clean up $HOME_RC_FILE.\n";
remove_dir($ABS_TEST_DIR);

View file

@ -36,19 +36,21 @@ use Stow::Util qw(parent canon_path);
use base qw(Exporter);
our @EXPORT = qw(
$ABS_TEST_DIR
$TEST_DIR
$stderr
init_test_dirs
cd
new_Stow new_compat_Stow
make_path make_link make_invalid_link make_file
remove_dir remove_link
remove_dir remove_file remove_link
cat_file
is_link is_dir_not_symlink is_nonexistent_path
capture_stderr uncapture_stderr
);
our $TEST_DIR = 'tmp-testing-trees';
our $ABS_TEST_DIR = File::Spec->rel2abs('tmp-testing-trees');
our $stderr;
my $tied_err;
@ -64,13 +66,17 @@ sub uncapture_stderr {
}
sub init_test_dirs {
for my $dir ("$TEST_DIR/target", "$TEST_DIR/stow") {
-d $dir and remove_tree($dir);
make_path($dir);
# Create a run_from/ subdirectory for tests which want to run
# from a separate directory outside the Stow directory or
# target directory.
for my $dir ("target", "stow", "run_from") {
my $path = "$TEST_DIR/$dir";
-d $path and remove_tree($path);
make_path($path);
}
# Don't let user's ~/.stow-global-ignore affect test results
$ENV{HOME} = $TEST_DIR;
$ENV{HOME} = $ABS_TEST_DIR;
}
sub new_Stow {