Skip to content

Commit fed17f6

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 fed17f6

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-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: 36 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)
@@ -135,6 +140,37 @@ def test_load_home_config_settings
135140
end
136141

137142
def test_load_nothing
143+
assert_load_nothing
144+
end
145+
146+
def test_not_expand_path_basename
147+
basename = @basename
148+
@basename = "~"
149+
$test_optparse_basename = "/" + @basename
150+
alias $test_optparse_prog $0
151+
alias $0 $test_optparse_basename
152+
setup_options({'HOME'=>@tmpdir+"/~options"}, "", "options") do
153+
assert_load_nothing
154+
end
155+
ensure
156+
alias $0 $test_optparse_prog
157+
@basename = basename
158+
end
159+
160+
def test_not_expand_path_xdg_config_home
161+
setup_options_home_options({'XDG_CONFIG_HOME' => '~/options'}) do
162+
assert_load_nothing
163+
end
164+
end
165+
166+
def test_not_expand_path_xdg_config_dirs
167+
setup_options_home_options({'XDG_CONFIG_DIRS' => '~/options'}) do
168+
assert_load_nothing
169+
end
170+
end
171+
172+
private
173+
def assert_load_nothing
138174
assert !new_parser.load
139175
assert_nil @result
140176
end

0 commit comments

Comments
 (0)