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: 9 additions & 3 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2049,10 +2049,16 @@ def load(filename = nil, **keywords)
basename = File.basename($0, '.*')
return true if load(File.expand_path("~/.options/#{basename}"), **keywords) rescue nil
basename << ".options"
if !(xdg = ENV['XDG_CONFIG_HOME']) or xdg.empty?
# https://specifications.freedesktop.org/basedir-spec/latest/#variables
#
# If $XDG_CONFIG_HOME is either not set or empty, a default
# equal to $HOME/.config should be used.
xdg = ['~/.config', true]
end
return [
# XDG
ENV['XDG_CONFIG_HOME'],
['~/.config', true],
xdg,

*ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR),

# Haiku
Expand Down
14 changes: 12 additions & 2 deletions test/optparse/test_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def setup_options(env, dir, suffix = nil)
begin
yield dir, optdir
ensure
File.unlink(file)
File.unlink(file) rescue nil
Dir.rmdir(optdir) rescue nil
end
else
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_load_home_options
end

def test_load_xdg_config_home
result, = setup_options_xdg_config_home
result, dir = setup_options_xdg_config_home
assert_load(result)

setup_options_home_config do
Expand All @@ -115,6 +115,11 @@ def test_load_xdg_config_home
setup_options_home_config_settings do
assert_load(result)
end

File.unlink("#{dir}/#{@basename}.options")
setup_options_home_config do
assert_load_nothing
end
end

def test_load_home_config
Expand All @@ -128,6 +133,11 @@ def test_load_home_config
setup_options_home_config_settings do
assert_load(result)
end

setup_options_xdg_config_home do |_, dir|
File.unlink("#{dir}/#{@basename}.options")
assert_load_nothing
end
end

def test_load_xdg_config_dirs
Expand Down
Loading