Skip to content

Commit deb0240

Browse files
nobumatzbot
authored andcommitted
[ruby/optparse] 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. ruby/optparse@181752391c
1 parent 1619afb commit deb0240

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ def setup_options_home_config_settings(&block)
7575
setup_options({}, "config/settings", ".options", &block)
7676
end
7777

78+
def setup_options_home_options(envname, &block)
79+
setup_options({envname => '~/options'}, "options", ".options", &block)
80+
end
81+
7882
def test_load_home_options
7983
result, = setup_options_home
8084
assert_load(result)
@@ -145,4 +149,30 @@ def test_load_nothing
145149
assert_load_nothing
146150
end
147151
end
152+
153+
def test_not_expand_path_basename
154+
basename = @basename
155+
@basename = "~"
156+
$test_optparse_basename = "/" + @basename
157+
alias $test_optparse_prog $0
158+
alias $0 $test_optparse_basename
159+
setup_options({'HOME'=>@tmpdir+"/~options"}, "", "options") do
160+
assert_load_nothing
161+
end
162+
ensure
163+
alias $0 $test_optparse_prog
164+
@basename = basename
165+
end
166+
167+
def test_not_expand_path_xdg_config_home
168+
setup_options_home_options('XDG_CONFIG_HOME') do
169+
assert_load_nothing
170+
end
171+
end
172+
173+
def test_not_expand_path_xdg_config_dirs
174+
setup_options_home_options('XDG_CONFIG_DIRS') do
175+
assert_load_nothing
176+
end
177+
end
148178
end

0 commit comments

Comments
 (0)