1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Close #2054 - prefer XDG_* paths (if set)

References #2054

Using XDG_* paths (if set) with preference higher than the
home dir, for pry config and history files.

Testing: ran `bundle exec rspec`, observed the tests pass.
This commit is contained in:
A. Nackov 2019-06-04 18:36:51 +03:00
parent 6cc6e55f20
commit 19c51bcbd1
4 changed files with 33 additions and 15 deletions

View file

@ -307,12 +307,12 @@ class Pry
def default_rc_file def default_rc_file
if ENV.key?('PRYRC') if ENV.key?('PRYRC')
ENV['PRYRC'] ENV['PRYRC']
elsif File.exist?(File.expand_path('~/.pryrc'))
'~/.pryrc'
elsif ENV.key?('XDG_CONFIG_HOME') && ENV['XDG_CONFIG_HOME'] != '' elsif ENV.key?('XDG_CONFIG_HOME') && ENV['XDG_CONFIG_HOME'] != ''
# See XDG Base Directory Specification at # See XDG Base Directory Specification at
# https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html # https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
ENV['XDG_CONFIG_HOME'] + '/pry/pryrc' ENV['XDG_CONFIG_HOME'] + '/pry/pryrc'
elsif File.exist?(File.expand_path('~/.pryrc'))
'~/.pryrc'
else else
'~/.config/pry/pryrc' '~/.config/pry/pryrc'
end end

View file

@ -6,12 +6,12 @@ class Pry
class History class History
def self.default_file def self.default_file
history_file = history_file =
if File.exist?(File.expand_path('~/.pry_history')) if ENV.key?('XDG_DATA_HOME') && ENV['XDG_DATA_HOME'] != ''
'~/.pry_history'
elsif ENV.key?('XDG_DATA_HOME') && ENV['XDG_DATA_HOME'] != ''
# See XDG Base Directory Specification at # See XDG Base Directory Specification at
# https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html # https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
ENV['XDG_DATA_HOME'] + '/pry/pry_history' ENV['XDG_DATA_HOME'] + '/pry/pry_history'
elsif File.exist?(File.expand_path('~/.pry_history'))
'~/.pry_history'
else else
'~/.local/share/pry/pry_history' '~/.local/share/pry/pry_history'
end end

View file

@ -67,18 +67,31 @@ RSpec.describe Pry::Config do
end end
context "when $XDG_CONFIG_HOME is defined" do context "when $XDG_CONFIG_HOME is defined" do
before do before { ENV['XDG_CONFIG_HOME'] = '/xdg_home' }
allow(File).to receive(:exist?)
expect(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(false)
ENV['XDG_CONFIG_HOME'] = '/xdg_home'
end
after { ENV.delete('XDG_CONFIG_HOME') } after { ENV.delete('XDG_CONFIG_HOME') }
it "defaults $XDG_CONFIG_HOME/pry/pryrc" do context "when ~/.pryrc exists" do
expect(subject.rc_file).to eq('/xdg_home/pry/pryrc') before do
allow(File).to receive(:exist?)
expect(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(true)
end
it "defaults $XDG_CONFIG_HOME/pry/pryrc" do
expect(subject.rc_file).to eq('/xdg_home/pry/pryrc')
end
end
context "when ~/.pryrc doesn't exist" do
before do
allow(File).to receive(:exist?)
expect(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(false)
end
it "defaults $XDG_CONFIG_HOME/pry/pryrc" do
expect(subject.rc_file).to eq('/xdg_home/pry/pryrc')
end
end end
end end
end end

View file

@ -53,6 +53,11 @@ RSpec.describe Pry::History do
stub_hist has_default: false, xdg_home: '/my/path' stub_hist has_default: false, xdg_home: '/my/path'
expect(described_class.default_file).to eq('/my/path/pry/pry_history') expect(described_class.default_file).to eq('/my/path/pry/pry_history')
end end
it "returns config location relative to $XDG_DATA_HOME when ~/.pryrc exists" do
stub_hist has_default: true, xdg_home: '/my/path'
expect(described_class.default_file).to eq('/my/path/pry/pry_history')
end
end end
end end