Add function to expand ~ in .stowrc files (#14)

Add a new expand_tilde() function that performs tilde expansion of
strings, and corresponding unit tests:

    * A ~ at the beginning of a path is expanded to the user's home
      directory.
    * Literal '~' can be provided with '\~'

Combine this with expand_environment() in a new expand_filepath()
function which applies all (both) required expansion functions to a
string, and use that in get_config_file_options() to expand .stowrc
options.

Add more tests to check that tilde expanded in correct places, i.e.:

    * expanded for --target and --dir
    * not expanded for --ignore, --defer, or --override

Update documentation on stowrc files according to this functionality
change.

Fixes #14: https://github.com/aspiers/stow/issues/14
This commit is contained in:
Charles LeDoux 2016-07-14 14:48:40 -05:00 committed by Adam Spiers
parent 9674738792
commit dc42c34107
4 changed files with 134 additions and 7 deletions

View file

@ -7,7 +7,7 @@
use strict;
use warnings;
use Test::More tests => 15;
use Test::More tests => 23;
use testutil;
@ -107,6 +107,16 @@ delete $ENV{'WITH_UNDERSCORE'};
# Expansion with escaped $
is(expand_environment('\$HOME/stow'), '$HOME/stow', 'expand \$HOME');
#
# Test tilde (~) expansion
#
# Basic expansion
is(expand_tilde('~/path'), "$ENV{HOME}/path", 'tilde expansion to $HOME');
# Should not expand if middle of path
is(expand_tilde('/path/~/here'), '/path/~/here', 'middle ~ not expanded');
# Test escaped ~
is(expand_tilde('\~/path'), '~/path', 'escaped tilde');
#
# Test that environment variable expansion is applied.
#
@ -130,6 +140,29 @@ is_deeply($options->{defer}, [qr(\A\$HOME)],
is_deeply($options->{override}, [qr(\A\$HOME)],
"environment expansion not applied on --override");
#
# Test that tilde expansion is applied in correct places.
#
$rc_contents = <<'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}, "$OUT_DIR/stow",
"apply environment expansion on stowrc --dir");
is($options->{target}, "$OUT_DIR/stow",
"apply environment expansion on stowrc --target");
is_deeply($options->{ignore}, [qr(~/stow\z)],
"environment expansion not applied on --ignore");
is_deeply($options->{defer}, [qr(\A~/stow)],
"environment expansion not applied on --defer");
is_deeply($options->{override}, [qr(\A~/stow)],
"environment expansion not applied on --override");
# Clean up files used for testing.
#
unlink $RC_FILE or die "Unable to clean up $RC_FILE.\n";