[ruby/optparse] Use ~/.config only if $XDG_CONFIG_HOME is unset or empty

2f9c7500a3
This commit is contained in:
Nobuyoshi Nakada 2025-08-08 19:33:00 +09:00 committed by git
parent 3ad26d0501
commit f76ce9fd28
2 changed files with 21 additions and 5 deletions

View file

@ -2049,10 +2049,16 @@ XXX
basename = File.basename($0, '.*') basename = File.basename($0, '.*')
return true if load(File.expand_path("~/.options/#{basename}"), **keywords) rescue nil return true if load(File.expand_path("~/.options/#{basename}"), **keywords) rescue nil
basename << ".options" 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 [ return [
# XDG xdg,
ENV['XDG_CONFIG_HOME'],
['~/.config', true],
*ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR), *ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR),
# Haiku # Haiku

View file

@ -47,7 +47,7 @@ class TestOptionParserLoad < Test::Unit::TestCase
begin begin
yield dir, optdir yield dir, optdir
ensure ensure
File.unlink(file) File.unlink(file) rescue nil
Dir.rmdir(optdir) rescue nil Dir.rmdir(optdir) rescue nil
end end
else else
@ -101,7 +101,7 @@ class TestOptionParserLoad < Test::Unit::TestCase
end end
def test_load_xdg_config_home def test_load_xdg_config_home
result, = setup_options_xdg_config_home result, dir = setup_options_xdg_config_home
assert_load(result) assert_load(result)
setup_options_home_config do setup_options_home_config do
@ -115,6 +115,11 @@ class TestOptionParserLoad < Test::Unit::TestCase
setup_options_home_config_settings do setup_options_home_config_settings do
assert_load(result) assert_load(result)
end end
File.unlink("#{dir}/#{@basename}.options")
setup_options_home_config do
assert_load_nothing
end
end end
def test_load_home_config def test_load_home_config
@ -128,6 +133,11 @@ class TestOptionParserLoad < Test::Unit::TestCase
setup_options_home_config_settings do setup_options_home_config_settings do
assert_load(result) assert_load(result)
end end
setup_options_xdg_config_home do |_, dir|
File.unlink("#{dir}/#{@basename}.options")
assert_load_nothing
end
end end
def test_load_xdg_config_dirs def test_load_xdg_config_dirs