From 6e55df5a0f54f1c549c7a3c56b3b04771b6ad1ae Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sun, 9 Jun 2019 17:07:20 +0300 Subject: [PATCH] Use Pry::Env where it makes sense --- lib/pry/commands/shell_command.rb | 2 +- lib/pry/config.rb | 8 ++++---- lib/pry/editor.rb | 14 ++++++++++---- lib/pry/helpers/base_helpers.rb | 3 ++- lib/pry/helpers/platform.rb | 2 +- lib/pry/history.rb | 4 ++-- lib/pry/output.rb | 4 ++-- lib/pry/pager.rb | 2 +- lib/pry/pry_class.rb | 4 ++-- spec/config_spec.rb | 25 +++++++++++++++---------- spec/output_spec.rb | 24 ++++++++++++------------ spec/pry_spec.rb | 14 ++++---------- 12 files changed, 56 insertions(+), 50 deletions(-) diff --git a/lib/pry/commands/shell_command.rb b/lib/pry/commands/shell_command.rb index 7b868607..6069a882 100644 --- a/lib/pry/commands/shell_command.rb +++ b/lib/pry/commands/shell_command.rb @@ -48,7 +48,7 @@ class Pry end def cd_path_env - ENV['CDPATH'] + Pry::Env['CDPATH'] end def cd_path_exists? diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 0c269f76..db19d269 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -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 diff --git a/lib/pry/editor.rb b/lib/pry/editor.rb index df3422a0..c9d92b1a 100644 --- a/lib/pry/editor.rb +++ b/lib/pry/editor.rb @@ -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 diff --git a/lib/pry/helpers/base_helpers.rb b/lib/pry/helpers/base_helpers.rb index 84d97616..b47fbdf3 100644 --- a/lib/pry/helpers/base_helpers.rb +++ b/lib/pry/helpers/base_helpers.rb @@ -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) diff --git a/lib/pry/helpers/platform.rb b/lib/pry/helpers/platform.rb index f4b3dccb..b04fa3e4 100644 --- a/lib/pry/helpers/platform.rb +++ b/lib/pry/helpers/platform.rb @@ -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] diff --git a/lib/pry/history.rb b/lib/pry/history.rb index 5f9a58c3..15da2e2e 100644 --- a/lib/pry/history.rb +++ b/lib/pry/history.rb @@ -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 diff --git a/lib/pry/output.rb b/lib/pry/output.rb index f26ca09a..46fb11dc 100644 --- a/lib/pry/output.rb +++ b/lib/pry/output.rb @@ -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) diff --git a/lib/pry/pager.rb b/lib/pry/pager.rb index 828735dd..e6e40898 100644 --- a/lib/pry/pager.rb +++ b/lib/pry/pager.rb @@ -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 diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 4f3e2326..414c6a5a 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -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 diff --git a/spec/config_spec.rb b/spec/config_spec.rb index d2cda05e..99cce54a 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -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 diff --git a/spec/output_spec.rb b/spec/output_spec.rb index d3f16dc6..f84bf364 100644 --- a/spec/output_spec.rb +++ b/spec/output_spec.rb @@ -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) diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index 2b41c3cc..1ea0210d 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -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