Skip to content

Commit 29b643f

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 ee23f37 commit 29b643f

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
@@ -75,6 +75,11 @@ def setup_options_home_config_settings(&block)
7575
setup_options({'HOME'=>@tmpdir}, "config/settings", ".options", &block)
7676
end
7777

78+
def setup_options_home_options(envs, &block)
79+
envs.update({'HOME'=>@tmpdir})
80+
setup_options(envs, "options", ".options", &block)
81+
end
82+
7883
def test_load_home_options
7984
result, = setup_options_home
8085
assert_load(result)
@@ -145,4 +150,30 @@ def test_load_nothing
145150
assert_load_nothing
146151
end
147152
end
153+
154+
def test_not_expand_path_basename
155+
basename = @basename
156+
@basename = "~"
157+
$test_optparse_basename = "/" + @basename
158+
alias $test_optparse_prog $0
159+
alias $0 $test_optparse_basename
160+
setup_options({'HOME'=>@tmpdir+"/~options"}, "", "options") do
161+
assert_load_nothing
162+
end
163+
ensure
164+
alias $0 $test_optparse_prog
165+
@basename = basename
166+
end
167+
168+
def test_not_expand_path_xdg_config_home
169+
setup_options_home_options({'XDG_CONFIG_HOME' => '~/options'}) do
170+
assert_load_nothing
171+
end
172+
end
173+
174+
def test_not_expand_path_xdg_config_dirs
175+
setup_options_home_options({'XDG_CONFIG_DIRS' => '~/options'}) do
176+
assert_load_nothing
177+
end
178+
end
148179
end

0 commit comments

Comments
 (0)