Skip to content

Commit

Permalink
Allow loading pryrc in other location even if XDG env is set
Browse files Browse the repository at this point in the history
  • Loading branch information
ttych committed Apr 16, 2021
1 parent 0aae8c9 commit 9560d1f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
17 changes: 9 additions & 8 deletions lib/pry/config.rb
Expand Up @@ -301,17 +301,18 @@ def lazy_readline
end

def default_rc_file
if (pryrc = Pry::Env['PRYRC'])
pryrc
elsif (xdg_home = Pry::Env['XDG_CONFIG_HOME'])
return Pry::Env['PRYRC'] if Pry::Env['PRYRC']

if (xdg_home = Pry::Env['XDG_CONFIG_HOME'])
# See XDG Base Directory Specification at
# https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
xdg_home + '/pry/pryrc'
elsif File.exist?(File.expand_path('~/.pryrc'))
'~/.pryrc'
else
'~/.config/pry/pryrc'
xdg_home_pryrc = xdg_home + '/pry/pryrc'
return xdg_home_pryrc if File.exist?(xdg_home_pryrc)
end

return '~/.pryrc' if File.exist?(File.expand_path('~/.pryrc'))

'~/.config/pry/pryrc'
end
end
end
54 changes: 45 additions & 9 deletions spec/config_spec.rb
Expand Up @@ -80,25 +80,61 @@
allow(File).to receive(:exist?)
end

context "and when ~/.pryrc exists" do
context "and when $XDG_CONFIG_HOME/pry/pryrc exists" do
before do
allow(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(true)
allow(File).to receive(:exist?).and_return(true)
end

it "defaults to $XDG_CONFIG_HOME/pry/pryrc" do
expect(subject.rc_file).to eq('/xdg_home/pry/pryrc')
context "and when ~/.pryrc exists" do
before do
allow(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(true)
end

it "defaults to $XDG_CONFIG_HOME/pry/pryrc" do
expect(subject.rc_file).to eq('/xdg_home/pry/pryrc')
end
end

context "and when ~/.pryrc doesn't exist" do
before do
allow(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(false)
end

it "defaults to $XDG_CONFIG_HOME/pry/pryrc" do
expect(subject.rc_file).to eq('/xdg_home/pry/pryrc')
end
end
end

context "and when ~/.pryrc doesn't exist" do
context "and when $XDG_CONFIG_HOME/pry/pryrc doesn't exist" do
before do
allow(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(false)
.with(File.expand_path('/xdg_home/pry/pryrc'))
.and_return(false)
end

it "defaults to $XDG_CONFIG_HOME/pry/pryrc" do
expect(subject.rc_file).to eq('/xdg_home/pry/pryrc')
context "and when ~/.pryrc exists" do
before do
allow(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(true)
end

it "defaults to ~/.pryrc" do
expect(subject.rc_file).to eq('~/.pryrc')
end
end

context "and when ~/.pryrc doesn't exist" do
before do
allow(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(false)
end

it "defaults to $XDG_CONFIG_HOME/pry/pryrc" do
expect(subject.rc_file).to eq('~/.config/pry/pryrc')
end
end
end
end
Expand Down

0 comments on commit 9560d1f

Please sign in to comment.