summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/optparse.rb12
-rw-r--r--test/optparse/test_load.rb30
2 files changed, 37 insertions, 5 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index 332aea2148..23b4844b2c 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -2047,19 +2047,21 @@ XXX
def load(filename = nil, **keywords)
unless filename
basename = File.basename($0, '.*')
- return true if load(File.expand_path(basename, '~/.options'), **keywords) rescue nil
+ return true if load(File.expand_path("~/.options/#{basename}"), **keywords) rescue nil
basename << ".options"
return [
# XDG
ENV['XDG_CONFIG_HOME'],
- '~/.config',
+ ['~/.config', true],
*ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR),
# Haiku
- '~/config/settings',
- ].any? {|dir|
+ ['~/config/settings', true],
+ ].any? {|dir, expand|
next if !dir or dir.empty?
- load(File.expand_path(basename, dir), **keywords) rescue nil
+ filename = File.join(dir, basename)
+ filename = File.expand_path(filename) if expand
+ load(filename, **keywords) rescue nil
}
end
begin
diff --git a/test/optparse/test_load.rb b/test/optparse/test_load.rb
index b8b76a31c5..8c835032ba 100644
--- a/test/optparse/test_load.rb
+++ b/test/optparse/test_load.rb
@@ -75,6 +75,10 @@ class TestOptionParserLoad < Test::Unit::TestCase
setup_options({}, "config/settings", ".options", &block)
end
+ def setup_options_home_options(envname, &block)
+ setup_options({envname => '~/options'}, "options", ".options", &block)
+ end
+
def test_load_home_options
result, = setup_options_home
assert_load(result)
@@ -145,4 +149,30 @@ class TestOptionParserLoad < Test::Unit::TestCase
assert_load_nothing
end
end
+
+ def test_not_expand_path_basename
+ basename = @basename
+ @basename = "~"
+ $test_optparse_basename = "/" + @basename
+ alias $test_optparse_prog $0
+ alias $0 $test_optparse_basename
+ setup_options({'HOME'=>@tmpdir+"/~options"}, "", "options") do
+ assert_load_nothing
+ end
+ ensure
+ alias $0 $test_optparse_prog
+ @basename = basename
+ end
+
+ def test_not_expand_path_xdg_config_home
+ setup_options_home_options('XDG_CONFIG_HOME') do
+ assert_load_nothing
+ end
+ end
+
+ def test_not_expand_path_xdg_config_dirs
+ setup_options_home_options('XDG_CONFIG_DIRS') do
+ assert_load_nothing
+ end
+ end
end