Skip to content

Commit 14ed691

Browse files
committed
Expand literal home paths only
Paths in environment variables should already be expanded. The base name of the program is also not subject to expansion.
1 parent 2fe7555 commit 14ed691

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

lib/optparse.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,19 +2047,21 @@ def candidate(word)
20472047
def load(filename = nil, **keywords)
20482048
unless filename
20492049
basename = File.basename($0, '.*')
2050-
return true if load(File.expand_path(basename, '~/.options'), **keywords) rescue nil
2050+
return true if load(File.expand_path("~/.options/#{basename}"), **keywords) rescue nil
20512051
basename << ".options"
20522052
return [
20532053
# XDG
20542054
ENV['XDG_CONFIG_HOME'],
2055-
'~/.config',
2055+
['~/.config', true],
20562056
*ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR),
20572057

20582058
# Haiku
2059-
'~/config/settings',
2060-
].any? {|dir|
2059+
['~/config/settings', true],
2060+
].any? {|dir, expand|
20612061
next if !dir or dir.empty?
2062-
load(File.expand_path(basename, dir), **keywords) rescue nil
2062+
filename = File.join(dir, basename)
2063+
filename = File.expand_path(filename) if expand
2064+
load(filename, **keywords) rescue nil
20632065
}
20642066
end
20652067
begin

test/optparse/test_load.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def setup_options_home_config_settings(&block)
6969
setup_options({'HOME'=>@tmpdir}, "config/settings", ".options", &block)
7070
end
7171

72+
def setup_options_home_options(envs, &block)
73+
envs.update({'HOME'=>@tmpdir})
74+
setup_options(envs, "options", ".options", &block)
75+
end
76+
7277
def test_load_home_options
7378
result, = setup_options_home
7479
assert_load(result)
@@ -138,4 +143,30 @@ def test_load_nothing
138143
assert !new_parser.load
139144
assert_nil @result
140145
end
146+
147+
def test_not_expand_path_basename
148+
basename = @basename
149+
@basename = "~"
150+
$test_optparse_basename = "/" + @basename
151+
alias $test_optparse_prog $0
152+
alias $0 $test_optparse_basename
153+
setup_options({'HOME'=>@tmpdir+"/~options"}, "", "options") do
154+
test_load_nothing
155+
end
156+
ensure
157+
alias $0 $test_optparse_prog
158+
@basename = basename
159+
end
160+
161+
def test_not_expand_path_xdg_config_home
162+
setup_options_home_options({'XDG_CONFIG_HOME' => '~/options'}) do
163+
test_load_nothing
164+
end
165+
end
166+
167+
def test_not_expand_path_xdg_config_dirs
168+
setup_options_home_options({'XDG_CONFIG_DIRS' => '~/options'}) do
169+
test_load_nothing
170+
end
171+
end
141172
end

0 commit comments

Comments
 (0)