Use Pry::Env where it makes sense

This commit is contained in:
Kyrylo Silin 2019-06-09 17:07:20 +03:00
parent 7ce5ca70bb
commit 6e55df5a0f
12 changed files with 56 additions and 50 deletions

View File

@ -48,7 +48,7 @@ class Pry
end
def cd_path_env
ENV['CDPATH']
Pry::Env['CDPATH']
end
def cd_path_exists?

View File

@ -305,12 +305,12 @@ class Pry
end
def default_rc_file
if ENV.key?('PRYRC')
ENV['PRYRC']
elsif ENV.key?('XDG_CONFIG_HOME') && ENV['XDG_CONFIG_HOME'] != ''
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
ENV['XDG_CONFIG_HOME'] + '/pry/pryrc'
xdg_home + '/pry/pryrc'
elsif File.exist?(File.expand_path('~/.pryrc'))
'~/.pryrc'
else

View File

@ -5,12 +5,18 @@ require 'shellwords'
class Pry
class Editor
def self.default
return ENV['VISUAL'] if ENV['VISUAL'] && !ENV['VISUAL'].empty?
return ENV['EDITOR'] if ENV['EDITOR'] && !ENV['EDITOR'].empty?
if (visual = Pry::Env['VISUAL'])
return visual
end
if (editor = Pry::Env['EDITOR'])
return editor
end
return 'notepad' if Helpers::Platform.windows?
%w[editor nano vi].find do |editor|
Kernel.system("which #{editor} > /dev/null 2>&1")
%w[editor nano vi].find do |editor_exe|
Kernel.system("which #{editor_exe} > /dev/null 2>&1")
end
end

View File

@ -36,7 +36,8 @@ class Pry
end
def use_ansi_codes?
Pry::Helpers::Platform.windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
Pry::Helpers::Platform.windows_ansi? ||
((term = Pry::Env['TERM']) && term != "dumb")
end
def colorize_code(code)

View File

@ -28,7 +28,7 @@ class Pry
def self.windows_ansi?
return false unless windows?
!!(defined?(Win32::Console) || ENV['ANSICON'] || mri_2?)
!!(defined?(Win32::Console) || Pry::Env['ANSICON'] || mri_2?)
end
# @return [Boolean]

View File

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

View File

@ -107,7 +107,7 @@ class Pry
end
def env_size
size = [ENV['LINES'] || ENV['ROWS'], ENV['COLUMNS']]
size = [Pry::Env['LINES'] || Pry::Env['ROWS'], Pry::Env['COLUMNS']]
size if nonzero_column?(size)
end
@ -123,7 +123,7 @@ class Pry
end
def ansicon_env_size
return unless ENV['ANSICON'] =~ /\((.*)x(.*)\)/
return unless Pry::Env['ANSICON'] =~ /\((.*)x(.*)\)/
size = [Regexp.last_match(2), Regexp.last_match(1)]
size if nonzero_column?(size)

View File

@ -128,7 +128,7 @@ class Pry
# buffered content.
class SystemPager < NullPager
def self.default_pager
pager = ENV["PAGER"] || ""
pager = Pry::Env['PAGER'] || ''
# Default to less, and make sure less is being passed the correct
# options

View File

@ -150,8 +150,8 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
# @example
# Pry.start(Object.new, :input => MyInput.new)
def self.start(target = nil, options = {})
return if ENV['DISABLE_PRY']
if ENV['FAIL_PRY']
return if Pry::Env['DISABLE_PRY']
if Pry::Env['FAIL_PRY']
raise 'You have FAIL_PRY set to true, which results in Pry calls failing'
end

View File

@ -46,8 +46,10 @@ RSpec.describe Pry::Config do
describe "#rc_file" do
context "when $PRYRC env variable is set" do
before { ENV['PRYRC'] = '/foo/pryrc' }
after { ENV.delete('PRYRC') }
before do
allow(Pry::Env).to receive(:[])
allow(Pry::Env).to receive(:[]).with('PRYRC').and_return('/foo/pryrc')
end
it "defaults to the value of PRYRC env variable" do
expect(subject.rc_file).to eq('/foo/pryrc')
@ -67,29 +69,32 @@ RSpec.describe Pry::Config do
end
context "when $XDG_CONFIG_HOME is defined" do
before { ENV['XDG_CONFIG_HOME'] = '/xdg_home' }
after { ENV.delete('XDG_CONFIG_HOME') }
before do
allow(Pry::Env).to receive(:[])
allow(Pry::Env).to receive(:[])
.with('XDG_CONFIG_HOME').and_return('/xdg_home')
context "when ~/.pryrc exists" do
allow(File).to receive(:exist?)
end
context "and when ~/.pryrc exists" do
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
it "defaults to $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
context "and 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
it "defaults to $XDG_CONFIG_HOME/pry/pryrc" do
expect(subject.rc_file).to eq('/xdg_home/pry/pryrc')
end
end

View File

@ -212,13 +212,13 @@ RSpec.describe Pry::Output do
before do
skip("io/console doesn't support JRuby") if Pry::Helpers::Platform.jruby?
expect(output).to receive(:tty?).and_return(false)
allow(ENV).to receive(:[])
allow(Pry::Env).to receive(:[])
end
context "and ENV has size info in ROWS and COLUMNS" do
before do
expect(ENV).to receive(:[]).with('ROWS').and_return(2)
expect(ENV).to receive(:[]).with('COLUMNS').and_return(2)
expect(Pry::Env).to receive(:[]).with('ROWS').and_return(2)
expect(Pry::Env).to receive(:[]).with('COLUMNS').and_return(2)
end
it "returns the ENV variable winsize" do
@ -228,8 +228,8 @@ RSpec.describe Pry::Output do
context "and ENV has size info in LINES and COLUMNS" do
before do
expect(ENV).to receive(:[]).with('LINES').and_return(3)
expect(ENV).to receive(:[]).with('COLUMNS').and_return(2)
expect(Pry::Env).to receive(:[]).with('LINES').and_return(3)
expect(Pry::Env).to receive(:[]).with('COLUMNS').and_return(2)
end
it "returns ENV variable winsize" do
@ -246,7 +246,7 @@ RSpec.describe Pry::Output do
expect(output).to receive(:tty?).and_return(false)
end
allow(ENV).to receive(:[])
allow(Pry::Env).to receive(:[])
stub_const('Readline', readline)
end
@ -280,7 +280,7 @@ RSpec.describe Pry::Output do
expect(output).to receive(:tty?).and_return(false)
end
allow(ENV).to receive(:[])
allow(Pry::Env).to receive(:[])
stub_const('Readline', readline)
expect(readline).to receive(:respond_to?)
.with(:get_screen_size).and_return(false)
@ -290,7 +290,7 @@ RSpec.describe Pry::Output do
context "and when it can be matched" do
context "and when the size consists of positive integers" do
before do
expect(ENV).to receive(:[]).with('ANSICON').and_return('(5x5)')
expect(Pry::Env).to receive(:[]).with('ANSICON').and_return('(5x5)')
end
it "returns the ansicon winsize" do
@ -300,7 +300,7 @@ RSpec.describe Pry::Output do
context "and when the size has a zero column" do
before do
expect(ENV).to receive(:[]).with('ANSICON').and_return('(0x0)')
expect(Pry::Env).to receive(:[]).with('ANSICON').and_return('(0x0)')
end
it "returns the default winsize" do
@ -311,7 +311,7 @@ RSpec.describe Pry::Output do
context "and when it cannot be matched" do
before do
expect(ENV).to receive(:[]).with('ANSICON').and_return('5x5')
expect(Pry::Env).to receive(:[]).with('ANSICON').and_return('5x5')
end
it "returns the default winsize" do
@ -336,7 +336,7 @@ RSpec.describe Pry::Output do
expect(output).to receive(:tty?).and_return(false)
end
allow(ENV).to receive(:[])
allow(Pry::Env).to receive(:[])
stub_const('Readline', readline)
expect(readline).to receive(:respond_to?)
.with(:get_screen_size).and_return(false)
@ -355,7 +355,7 @@ RSpec.describe Pry::Output do
expect(output).to receive(:tty?).and_return(false)
end
allow(ENV).to receive(:[])
allow(Pry::Env).to receive(:[])
stub_const('Readline', readline)
expect(readline).to receive(:respond_to?)
.with(:get_screen_size).and_return(false)

View File

@ -26,11 +26,8 @@ describe Pry do
describe 'DISABLE_PRY' do
before do
ENV['DISABLE_PRY'] = 'true'
end
after do
ENV.delete 'DISABLE_PRY'
allow(Pry::Env).to receive(:[])
allow(Pry::Env).to receive(:[]).with('DISABLE_PRY').and_return(true)
end
it 'should not binding.pry' do
@ -44,11 +41,8 @@ describe Pry do
describe 'FAIL_PRY' do
before do
ENV['FAIL_PRY'] = 'true'
end
after do
ENV.delete 'FAIL_PRY'
allow(Pry::Env).to receive(:[])
allow(Pry::Env).to receive(:[]).with('FAIL_PRY').and_return(true)
end
it 'should raise an error for binding.pry' do