From b598d78ce9d64a22569085f018ddce5a3d4714ea Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Sun, 19 Jan 2025 21:23:08 +0000 Subject: [PATCH] t/rc_options.t: split into subtests --- t/rc_options.t | 398 ++++++++++++++++++++++++------------------------- 1 file changed, 198 insertions(+), 200 deletions(-) diff --git a/t/rc_options.t b/t/rc_options.t index ef00ead..80d756d 100755 --- a/t/rc_options.t +++ b/t/rc_options.t @@ -22,7 +22,7 @@ use strict; use warnings; -use Test::More tests => 35; +use Test::More tests => 13; use testutil; @@ -37,8 +37,6 @@ if (-e $HOME_RC_FILE) { die "RC file location $HOME_RC_FILE already exists!\n"; } -my ($options, $pkgs_to_delete, $pkgs_to_stow); - # Init testing directory structure and overwrite ENV{HOME} to prevent # squashing existing .stowrc file. init_test_dirs(); @@ -49,138 +47,137 @@ init_test_dirs(); my $orig_HOME = $ENV{HOME}; -# -# 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"); +subtest('no .stowrc file anywhere', sub { + plan tests => 2; -# -# Test .stowrc file in cwd with relative paths, and $HOME not defined -# -make_file($CWD_RC_FILE, <{target}, "../target" - => "relative --target from \$PWD/.stowrc"); -is($options->{dir}, "../stow" - => "relative -d from \$PWD/.stowrc"); + delete $ENV{HOME}; + local @ARGV = ('dummy'); + cd("$TEST_DIR/run_from"); + my ($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"); +}); -$ENV{HOME} = $orig_HOME; -remove_file($CWD_RC_FILE); +subtest('.stowrc file in cwd with relative paths, and $HOME not defined', sub { + plan tests => 2; -# -# Test .stowrc file in cwd with absolute paths, and $HOME not defined -# -make_file($CWD_RC_FILE, <{target}, "$ABS_TEST_DIR/target" - => "absolute --target from \$PWD/.stowrc"); -is($options->{dir}, "$ABS_TEST_DIR/stow" - => "abs_test_dir -d from \$PWD/.stowrc"); + local @ARGV = ('dummy'); + my ($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); + $ENV{HOME} = $orig_HOME; + remove_file($CWD_RC_FILE); +}); -# -# Test ~/.stowrc file with one relative option per line. -# -local @ARGV = ('dummy'); -make_file($HOME_RC_FILE, < 2; + + make_file($CWD_RC_FILE, <{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); +}); + +subtest('~/.stowrc file with one relative option per line', sub { + plan tests => 2; + + local @ARGV = ('dummy'); + make_file($HOME_RC_FILE, <{target}, "../target", "--target from \$HOME/.stowrc"); -is($options->{dir}, "../stow", "-d ../stow from \$HOME/.stowrc"); + my ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options(); + is($options->{target}, "../target", "--target from \$HOME/.stowrc"); + is($options->{dir}, "../stow", "-d ../stow from \$HOME/.stowrc"); +}); -# -# Test ~/.stowrc file with one absolute option per line. -# -local @ARGV = ('dummy'); -make_file($HOME_RC_FILE, < 2; + + local @ARGV = ('dummy'); + make_file($HOME_RC_FILE, <{target}, "$ABS_TEST_DIR/target" - => "--target from \$HOME/.stowrc"); -is($options->{dir}, "$ABS_TEST_DIR/stow" - => "-d $ABS_TEST_DIR/stow from \$HOME/.stowrc"); + my ($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 $ABS_TEST_DIR/stow from \$HOME/.stowrc"); +}); -# -# Test ~/.stowrc file with with options with paths containing spaces. -# -local @ARGV = ('dummy'); -make_file($HOME_RC_FILE, < 1; + + local @ARGV = ('dummy'); + make_file($HOME_RC_FILE, <{dir}, "$ABS_TEST_DIR/stow directory", - => "-d from \$HOME/.stowrc with spaces"); + my ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options(); + is($options->{dir}, "$ABS_TEST_DIR/stow directory", "-d from \$HOME/.stowrc with spaces"); +}); -# -# Test that some but not all options ~/.stowrc file are overridden by -# .stowrc in cwd. -# -local @ARGV = ('dummy'); -make_file($HOME_RC_FILE, < 3; + + local @ARGV = ('dummy'); + make_file($HOME_RC_FILE, <{target}, "$ABS_TEST_DIR/target" - => "--target overridden by \$PWD/.stowrc"); -is($options->{dir}, "$ABS_TEST_DIR/stow" - => "-d overridden \$PWD/.stowrc"); -is_deeply($options->{defer}, [qr{\A(info)}, qr{\A(man)}], - 'defer man and info'); -unlink($CWD_RC_FILE) or die "Failed to unlink $CWD_RC_FILE"; + my ($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"); + is_deeply($options->{defer}, [qr{\A(info)}, qr{\A(man)}], 'defer man and info'); + 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, <{dir}, "$ABS_TEST_DIR/stow", "cli overwrite scalar rc option."); +subtest('scalar cli option overwrites conflicting ~/.stowrc option', sub { + plan tests => 1; -# -# 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' -); -make_file($HOME_RC_FILE, <{defer}, [qr{\A(info)}, qr{\A(man)}], - 'defer man and info'); + my ($options, $pkgs_to_delete, $pkgs_to_stow) = process_options(); + is($options->{dir}, "$ABS_TEST_DIR/stow", "cli overwrite scalar rc option."); +}); + +subtest('list cli option merges with conflicting .stowrc option', sub { + plan tests => 1; + + # Documentation states that .stowrc options are prepended to cli options. + local @ARGV = ( + '--defer=man', + 'dummy' + ); + make_file($HOME_RC_FILE, <{defer}, [qr{\A(info)}, qr{\A(man)}], 'defer man and info'); +}); # ======== Filepath Expansion Tests ======== # Test proper filepath expansion in rc file. @@ -188,94 +185,95 @@ is_deeply($options->{defer}, [qr{\A(info)}, qr{\A(man)}], # take a filepath, namely target and dir. # ========================================== - -# -# Test environment variable expansion function. -# -# Basic expansion -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}') { - eval { - expand_environment($var, "--foo option"); - }; - is( - $@, - "--foo option references undefined environment variable \$UNDEFINED; " . - "aborting!\n", - "expand $var" - ); -} - -# Expansion with an underscore. -$ENV{'WITH_UNDERSCORE'} = 'test string'; -is(expand_environment('${WITH_UNDERSCORE}'), 'test string', - 'expand ${WITH_UNDERSCORE}'); -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 unless quoted. -# Include examples from the manual -# -make_file($HOME_RC_FILE, <<'HERE'); ---dir=$HOME/stow ---target="$HOME/dir with space in/file with space in" ---ignore=\\$FOO\\$ ---defer="foo\\b.*bar" ---defer="\\.jpg\$" ---override=\\.png\$ ---override=bin|man ---ignore='perllocal\.pod' ---ignore='\.packlist' ---ignore='\.bs' +subtest('basic environment variable expansion', sub { + plan tests => 6; + + 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}') { + eval { + expand_environment($var, "--foo option"); + }; + is( + $@, + "--foo option references undefined environment variable \$UNDEFINED; " . + "aborting!\n", + "expand $var" + ); + } + + # Expansion with an underscore. + $ENV{'WITH_UNDERSCORE'} = 'test string'; + is(expand_environment('${WITH_UNDERSCORE}'), 'test string', 'expand ${WITH_UNDERSCORE}'); + delete $ENV{'WITH_UNDERSCORE'}; + # Expansion with escaped $ + is(expand_environment('\$HOME/stow'), '$HOME/stow', 'expand \$HOME'); +}); + +subtest('tilde expansion', sub { + plan tests => 3; + + is(expand_tilde('~/path'), "$ENV{HOME}/path", 'tilde expansion to $HOME'); + is(expand_tilde('/path/~/here'), '/path/~/here', 'middle ~ not expanded'); + is(expand_tilde('\~/path'), '~/path', 'escaped tilde'); +}); + +subtest('environment variable expansion unless quoted', sub { + plan tests => 5; + + # Include examples from the manual + make_file($HOME_RC_FILE, <<'HERE'); + --dir=$HOME/stow + --target="$HOME/dir with space in/file with space in" + --ignore=\\$FOO\\$ + --defer="foo\\b.*bar" + --defer="\\.jpg\$" + --override=\\.png\$ + --override=bin|man + --ignore='perllocal\.pod' + --ignore='\.packlist' + --ignore='\.bs' HERE -($options, $pkgs_to_delete, $pkgs_to_stow) = get_config_file_options(); -is($options->{dir}, "$ABS_TEST_DIR/stow", - "apply environment expansion on --dir"); -is($options->{target}, "$ABS_TEST_DIR/dir with space in/file with space in", - "apply environment expansion on --target"); -is_deeply($options->{ignore}, [qr{(\$FOO\$)\z}, qr{(perllocal\.pod)\z}, qr{(\.packlist)\z}, qr{(\.bs)\z}], - 'environment expansion not applied on --ignore but backslash removed'); -is_deeply($options->{defer}, [qr{\A(foo\b.*bar)}, qr{\A(\.jpg$)}], - 'environment expansion not applied on --defer but backslash removed'); -is_deeply($options->{override}, [qr{\A(\.png$)}, qr{\A(bin|man)}], - 'environment expansion not applied on --override but backslash removed'); - -# -# Test that tilde expansion is applied in correct places. -# -make_file($HOME_RC_FILE, <<'HERE'); ---dir=~/stow ---target=~/stow ---ignore=~/stow ---defer=~/stow ---override=~/stow + my ($options, $pkgs_to_delete, $pkgs_to_stow) = get_config_file_options(); + is($options->{dir}, "$ABS_TEST_DIR/stow", + "apply environment expansion on --dir"); + is($options->{target}, "$ABS_TEST_DIR/dir with space in/file with space in", + "apply environment expansion on --target"); + is_deeply($options->{ignore}, [qr{(\$FOO\$)\z}, qr{(perllocal\.pod)\z}, qr{(\.packlist)\z}, qr{(\.bs)\z}], + 'environment expansion not applied on --ignore but backslash removed'); + is_deeply($options->{defer}, [qr{\A(foo\b.*bar)}, qr{\A(\.jpg$)}], + 'environment expansion not applied on --defer but backslash removed'); + is_deeply($options->{override}, [qr{\A(\.png$)}, qr{\A(bin|man)}], + 'environment expansion not applied on --override but backslash removed'); +}); + +subtest('tilde expansion in correct places', sub { + plan tests => 5; + + # + # Test that tilde expansion is applied in correct places. + # + make_file($HOME_RC_FILE, <<'HERE'); + --dir=~/stow + --target=~/stow + --ignore=~/stow + --defer=~/stow + --override=~/stow HERE -($options, $pkgs_to_delete, $pkgs_to_stow) = get_config_file_options(); -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}], - "tilde expansion not applied on --ignore"); -is_deeply($options->{defer}, [qr{\A(~/stow)}], - "tilde expansion not applied on --defer"); -is_deeply($options->{override}, [qr{\A(~/stow)}], - "tilde expansion not applied on --override"); + my ($options, $pkgs_to_delete, $pkgs_to_stow) = get_config_file_options(); + 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}], + "tilde expansion not applied on --ignore"); + is_deeply($options->{defer}, [qr{\A(~/stow)}], + "tilde expansion not applied on --defer"); + is_deeply($options->{override}, [qr{\A(~/stow)}], + "tilde expansion not applied on --override"); +}); # # Clean up files used for testing.