Skip to content

Commit

Permalink
Use shellwords for .stowrc parsing
Browse files Browse the repository at this point in the history
- Use `shellwords` to parse `.stowrc` files from `Text::ParseWords`
- Add test for `.stowrc` parsing with quotes
  - Add `stow directory` to `tmp-testing-trees` for testing

This change allows `.stowrc` arguments to be parsed similar to shell arguments,
where quotes can be used to group an argument with spaces.

However, this change affects the behavior in --ignore, --defer, --override.

(?^:$HOME\z)
(?^:\A$HOME)
(?^:\A$HOME)
  • Loading branch information
jeremy-code committed Jun 14, 2024
1 parent 2544889 commit fef6dfd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion bin/stow.in
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ require 5.006_001;
use POSIX qw(getcwd);
use Getopt::Long qw(GetOptionsFromArray);
use Scalar::Util qw(reftype);
use Text::ParseWords qw(shellwords);

@USE_LIB_PMDIR@
use Stow;
Expand Down Expand Up @@ -683,7 +684,7 @@ sub get_config_file_options {
or die "Could not open $file for reading\n";
while (my $line = <$FILE>){
chomp $line;
push @defaults, split " ", $line;
push @defaults, shellwords($line);
}
close $FILE or die "Could not close open file: $file\n";
}
Expand Down
22 changes: 17 additions & 5 deletions t/rc_options.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use strict;
use warnings;

use Test::More tests => 34;
use Test::More tests => 35;

use testutil;

Expand Down Expand Up @@ -119,6 +119,19 @@ is($options->{target}, "$ABS_TEST_DIR/target"
is($options->{dir}, "$ABS_TEST_DIR/stow"
=> "-d from \$HOME/.stowrc");

#
# Test ~/.stowrc file with with options with paths containing spaces.
#
local @ARGV = ('dummy');
make_file($HOME_RC_FILE, <<HERE);
-d "$ABS_TEST_DIR/stow directory"
--target "$ABS_TEST_DIR/target"
HERE

($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.
Expand Down Expand Up @@ -229,11 +242,11 @@ 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)],
is($options->{ignore}->[0], '(?^:$HOME\z)',
"environment expansion not applied on --ignore");
is_deeply($options->{defer}, [qr(\A\$HOME)],
is($options->{defer}->[0], '(?^:\A$HOME)',
"environment expansion not applied on --defer");
is_deeply($options->{override}, [qr(\A\$HOME)],
is($options->{override}->[0], '(?^:\A$HOME)',
"environment expansion not applied on --override");

#
Expand Down Expand Up @@ -263,4 +276,3 @@ is_deeply($options->{override}, [qr(\A~/stow)],
#
unlink $HOME_RC_FILE or die "Unable to clean up $HOME_RC_FILE.\n";
remove_dir($ABS_TEST_DIR);

2 changes: 1 addition & 1 deletion t/testutil.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sub init_test_dirs {
# 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") {
for my $dir ("target", "stow", "run_from", "stow directory") {
my $path = "$test_dir/$dir";
-d $path and remove_tree($path);
make_path($path);
Expand Down

0 comments on commit fef6dfd

Please sign in to comment.