Skip to content

Commit

Permalink
Ignore non-absolute XDG_CONFIG_HOME
Browse files Browse the repository at this point in the history
https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
> All paths set in these environment variables must be absolute.
> If an implementation encounters a relative path in any of these
> variables it should consider the path invalid and ignore it.
  • Loading branch information
nobu committed Apr 24, 2020
1 parent a67ca3d commit 45af6ee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/reline/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ def inputrc_path
home_rc_path = File.expand_path('~/.inputrc')
return home_rc_path if File.exist?(home_rc_path)

case ENV['XDG_CONFIG_HOME']
case path = ENV['XDG_CONFIG_HOME']
when nil, ''
path = File.expand_path('~/.config/readline/inputrc')
return path if File.exist?(path)
else
path = File.expand_path("#{ENV['XDG_CONFIG_HOME']}/readline/inputrc")
return path if File.exist?(path)
path = File.join(path, 'readline/inputrc')
return path if File.exist?(path) and path == File.expand_path(path)
end

path = File.expand_path('~/.config/readline/inputrc')
return path if File.exist?(path)

return home_rc_path
end

Expand Down
25 changes: 25 additions & 0 deletions test/reline/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def test_empty_inputrc_env
assert_nothing_raised do
@config.read
end
ensure
ENV['INPUTRC'] = inputrc_backup
end

Expand All @@ -221,6 +222,7 @@ def test_inputrc
expected = "#{@tmpdir}/abcde"
ENV['INPUTRC'] = expected
assert_equal expected, @config.inputrc_path
ensure
ENV['INPUTRC'] = inputrc_backup
end

Expand All @@ -234,6 +236,7 @@ def test_xdg_config_home
ENV['HOME'] = @tmpdir
ENV['XDG_CONFIG_HOME'] = xdg_config_home
assert_equal expected, @config.inputrc_path
ensure
FileUtils.rm(expected)
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
ENV['HOME'] = home_backup
Expand All @@ -248,6 +251,28 @@ def test_empty_xdg_config_home
FileUtils.mkdir_p(File.dirname(expected))
FileUtils.touch(expected)
assert_equal expected, @config.inputrc_path
ensure
FileUtils.rm(expected)
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
ENV['HOME'] = home_backup
end

def test_relative_xdg_config_home
home_backup = ENV['HOME']
xdg_config_home_backup = ENV['XDG_CONFIG_HOME']
ENV['HOME'] = @tmpdir
expected = File.expand_path('~/.config/readline/inputrc')
FileUtils.mkdir_p(File.dirname(expected))
FileUtils.touch(expected)
result = Dir.chdir(@tmpdir) do
xdg_config_home = ".config/example_dir"
ENV['XDG_CONFIG_HOME'] = xdg_config_home
inputrc = "#{xdg_config_home}/readline/inputrc"
FileUtils.mkdir_p(File.dirname(inputrc))
FileUtils.touch(inputrc)
@config.inputrc_path
end
assert_equal expected, result
FileUtils.rm(expected)
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
ENV['HOME'] = home_backup
Expand Down

0 comments on commit 45af6ee

Please sign in to comment.