Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2047,19 +2047,21 @@ def candidate(word)
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
Expand Down
47 changes: 42 additions & 5 deletions test/optparse/test_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ def assert_load(result)
assert_equal({test: result}, into)
end

def assert_load_nothing
assert !new_parser.load
assert_nil @result
end

def setup_options(env, dir, suffix = nil)
env.update({'HOME'=>@tmpdir})
optdir = File.join(@tmpdir, dir)
FileUtils.mkdir_p(optdir)
file = File.join(optdir, [@basename, suffix].join(""))
Expand All @@ -50,23 +56,27 @@ def setup_options(env, dir, suffix = nil)
end

def setup_options_home(&block)
setup_options({'HOME'=>@tmpdir}, ".options", &block)
setup_options({}, ".options", &block)
end

def setup_options_xdg_config_home(&block)
setup_options({'XDG_CONFIG_HOME'=>@tmpdir+"/xdg"}, "xdg", ".options", &block)
end

def setup_options_home_config(&block)
setup_options({'HOME'=>@tmpdir}, ".config", ".options", &block)
setup_options({}, ".config", ".options", &block)
end

def setup_options_xdg_config_dirs(&block)
setup_options({'XDG_CONFIG_DIRS'=>@tmpdir+"/xdgconf"}, "xdgconf", ".options", &block)
end

def setup_options_home_config_settings(&block)
setup_options({'HOME'=>@tmpdir}, "config/settings", ".options", &block)
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
Expand Down Expand Up @@ -135,7 +145,34 @@ def test_load_home_config_settings
end

def test_load_nothing
assert !new_parser.load
assert_nil @result
setup_options({}, "") do
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
Loading