stow/t/cli_options.t
Charles LeDoux 9674738792 Apply environment expansion to options in .stowrc files
Expand environment variables used in stowrc, as requested in

    https://savannah.gnu.org/bugs/?41826

This is achieved by creating a new function expand_environment() that
replaces any substring of the form '$VAR' or '${VAR}' with contents of
environment variable $VAR.  Literal '$' can be given by '\$'.

N.B. The function is only applied to the --target and --dir options,
and only for options specified in .stowrc; cli options are left
untouched.

Undefined variables are expanded to the empty string, as they would be
in normal shell parameter expansion.

Unit tests added accordingly:

  - Test expand_environment():
    * Expand $HOME
    * Expand ${HOME}
    * Expand ${WITH SPACE}
    * Expand '\$HOME'. Expected is '$HOME'
    * Expand ${UNDEFINED}. Expected is ''.

  - Test that it's applied to the correct options.

  - Test that CLI options are not expanded.
2019-06-25 19:38:26 +01:00

97 lines
2 KiB
Perl
Executable file

#!/usr/local/bin/perl
#
# Test processing of CLI options.
#
use strict;
use warnings;
use Test::More tests => 10;
use testutil;
require 'stow';
init_test_dirs();
local @ARGV = (
'-v',
'-d', "$OUT_DIR/stow",
'-t', "$OUT_DIR/target",
'dummy'
);
my ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{verbose}, 1, 'verbose option');
is($options->{dir}, "$OUT_DIR/stow", 'stow dir option');
my $stow = new_Stow(%$options);
is($stow->{stow_path}, "../stow" => 'stow dir');
is_deeply($pkgs_to_stow, [ 'dummy' ] => 'default to stow');
#
# Check mixed up package options
#
local @ARGV = (
'-v',
'-D', 'd1', 'd2',
'-S', 's1',
'-R', 'r1',
'-D', 'd3',
'-S', 's2', 's3',
'-R', 'r2',
);
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is_deeply($pkgs_to_delete, [ 'd1', 'd2', 'r1', 'd3', 'r2' ] => 'mixed deletes');
is_deeply($pkgs_to_stow, [ 's1', 'r1', 's2', 's3', 'r2' ] => 'mixed stows');
#
# Check setting deferred paths
#
local @ARGV = (
'--defer=man',
'--defer=info',
'dummy'
);
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is_deeply($options->{defer}, [ qr(\Aman), qr(\Ainfo) ] => 'defer man and info');
#
# Check setting override paths
#
local @ARGV = (
'--override=man',
'--override=info',
'dummy'
);
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is_deeply($options->{override}, [qr(\Aman), qr(\Ainfo)] => 'override man and info');
#
# Check setting ignored paths
#
local @ARGV = (
'--ignore=~',
'--ignore=\.#.*',
'dummy'
);
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is_deeply($options->{ignore}, [ qr(~\z), qr(\.#.*\z) ] => 'ignore temp files');
#
# Check that expansion not applied.
#
local @ARGV = (
"--target=$OUT_DIR/".'$HOME',
'dummy'
);
make_dir("$OUT_DIR/".'$HOME');
($options, $pkgs_to_delete, $pkgs_to_stow) = process_options();
is($options->{target}, "$OUT_DIR/".'$HOME', 'no expansion');
remove_dir("$OUT_DIR/".'$HOME');
# vim:ft=perl