validates file presence in Config#default_rc_file (#2129)

* validates file presence in Config#default_rc_file

Return `nil` if no default rc file is present in the filesystem.
This fixes the behavior if $XDG_CONFIG_HOME is set, but no rc file at
the path `$XDG_CONFIG_HOME/pry/pryrc` (while it may be at `~/.pryrc`).
This commit is contained in:
Armin 2021-07-01 09:21:14 +02:00 committed by GitHub
parent 586e1bd150
commit db856653dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 20 deletions

View File

@ -301,17 +301,14 @@ class Pry
end
def default_rc_file
if (pryrc = Pry::Env['PRYRC'])
pryrc
elsif (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'
end
[Pry::Env['PRYRC'],
# See XDG Base Directory Specification at
# https://specifications.freedesktop.org/basedir-spec/latest/
"#{Pry::Env['XDG_CONFIG_HOME']}/pry/pryrc",
File.expand_path('~/.pryrc'),
File.expand_path('~/.config/pry/pryrc')]
.compact
.find { |file| File.exist?(file) }
end
end
end

View File

@ -85,7 +85,7 @@ class Pry
# Load the local RC file (./.pryrc)
def self.rc_files_to_load
files = []
files << Pry.config.rc_file if Pry.config.should_load_rc
files << Pry.config.rc_file if Pry.config.rc_file && Pry.config.should_load_rc
files << LOCAL_RC_FILE if Pry.config.should_load_local_rc
files.map { |file| real_path_to(file) }.compact.uniq
end

View File

@ -41,11 +41,14 @@ RSpec.describe Pry::Config do
specify { expect(subject.history_load).to eq(true).or be(false) }
specify { expect(subject.history_file).to be_a(String) }
specify { expect(subject.exec_string).to be_a(String) }
specify { expect(subject.rc_file).to be_a(String) }
specify { expect(subject.rc_file).to be_a(String).or be(nil) }
describe "#rc_file" do
context "when $PRYRC env variable is set" do
before do
allow(File).to receive(:exist?)
allow(File).to receive(:exist?)
.with('/foo/pryrc').and_return(true)
allow(Pry::Env).to receive(:[])
allow(Pry::Env).to receive(:[]).with('PRYRC').and_return('/foo/pryrc')
end
@ -67,7 +70,7 @@ RSpec.describe Pry::Config do
end
it "defaults to ~/.pryrc" do
expect(subject.rc_file).to eq('~/.pryrc')
expect(subject.rc_file).to eq(File.expand_path('~/.pryrc'))
end
end
@ -76,29 +79,58 @@ RSpec.describe Pry::Config do
allow(Pry::Env).to receive(:[])
allow(Pry::Env).to receive(:[])
.with('XDG_CONFIG_HOME').and_return('/xdg_home')
end
allow(File).to receive(:exist?)
context "and when '/xdg_home/pry/pryrc' exists" do
before do
allow(File).to receive(:exist?)
allow(File).to receive(:exist?)
.with('/xdg_home/pry/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 exists" do
before do
allow(File).to receive(:exist?)
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')
it "defaults to ~/.pryrc" do
expect(subject.rc_file).to eq(File.expand_path('~/.pryrc'))
end
context "and when ~/.config/pry/pryrc exists" do
before do
allow(File).to receive(:exist?)
allow(File).to receive(:exist?)
.with(File.expand_path('~/.config/pry/pryrc')).and_return(true)
end
it "defaults to ~/.config/pry/pryrc" do
expect(subject.rc_file).to eq(File.expand_path('~/.config/pry/pryrc'))
end
end
end
context "and when ~/.pryrc doesn't exist" do
context "and when no default rc file exists" do
before do
allow(File).to receive(:exist?)
allow(Pry::Env).to receive(:[]).with('PRYRC').and_return(nil)
allow(File).to receive(:exist?)
.with('/xdg_home/pry/pryrc').and_return(false)
allow(File).to receive(:exist?)
.with(File.expand_path('~/.pryrc')).and_return(false)
allow(File).to receive(:exist?)
.with(File.expand_path('~/.config/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')
it "should return nil" do
expect(subject.rc_file).to eq(nil)
end
end
end