diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2019-04-10 03:15:49 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-10 03:15:49 +0300 |
commit | acf213773d6d6c589e3f97b78a0b04fa07eb249a (patch) | |
tree | 61f9037f774514caa491caac989f0568923e13fb | |
parent | 8f98b66a1311f34385dca9a565deb0ff884f7587 (diff) | |
parent | e1c51d7662f91e37ff5bea76d85a8d4f3c7ce3bd (diff) | |
download | pry-acf213773d6d6c589e3f97b78a0b04fa07eb249a.tar.gz |
Merge pull request #2009 from pry/history-file-refactoring
Move history file detection to Pry::History
-rw-r--r-- | lib/pry.rb | 2 | ||||
-rw-r--r-- | lib/pry/config.rb | 20 | ||||
-rw-r--r-- | lib/pry/history.rb | 14 | ||||
-rw-r--r-- | spec/history_spec.rb | 28 |
4 files changed, 47 insertions, 17 deletions
@@ -19,6 +19,7 @@ require 'pry/block_command' require 'pry/command_set' require 'pry/syntax_highlighter' require 'pry/editor' +require 'pry/history' Pry::Commands = Pry::CommandSet.new unless defined?(Pry::Commands) @@ -64,7 +65,6 @@ require 'pry/wrapped_module/candidate' require 'pry/slop' require 'pry/cli' -require 'pry/history' require 'pry/core_extensions' require 'pry/repl_file_loader' diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 9f7afbbb..694d01b1 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -119,21 +119,11 @@ class Pry file_completions: proc { Dir["."] }, ls: Pry::Config.from_hash(Pry::Command::Ls::DEFAULT_OPTIONS), completer: Pry::InputCompleter, - history: Pry::Config.from_hash( - should_save: true, should_load: true - ).tap do |history| - history_file = - if File.exist?(File.expand_path('~/.pry_history')) - '~/.pry_history' - elsif ENV.key?('XDG_DATA_HOME') && 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' - else - '~/.local/share/pry/pry_history' - end - history.file = File.expand_path(history_file) - end, + history: { + should_save: true, + should_load: true, + file: Pry::History.default_file + }, exec_string: "" ) end diff --git a/lib/pry/history.rb b/lib/pry/history.rb index 663d6183..ad1c590d 100644 --- a/lib/pry/history.rb +++ b/lib/pry/history.rb @@ -2,6 +2,20 @@ class Pry # The History class is responsible for maintaining the user's input history, # both internally and within Readline. class History + def self.default_file + history_file = + if File.exist?(File.expand_path('~/.pry_history')) + '~/.pry_history' + elsif ENV.key?('XDG_DATA_HOME') && 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' + else + '~/.local/share/pry/pry_history' + end + File.expand_path(history_file) + end + attr_accessor :loader, :saver # @return [Fixnum] Number of lines in history when Pry first loaded. diff --git a/spec/history_spec.rb b/spec/history_spec.rb index f98fae85..f69fea0c 100644 --- a/spec/history_spec.rb +++ b/spec/history_spec.rb @@ -1,7 +1,7 @@ require 'tempfile' require 'rbconfig' -describe Pry do +RSpec.describe Pry::History do before do Pry.history.clear @@ -19,6 +19,32 @@ describe Pry do Pry.history.instance_variable_set(:@original_lines, 0) end + describe ".default_file" do + it "returns ~/.local/share/pry/pry_history" do + expect(described_class.default_file).to match('/.local/share/pry/pry_history') + end + + context "when ~/.pry_history exists" do + before do + allow(File).to receive(:exist?) + .with(File.expand_path('~/.pry_history')).and_return(true) + end + + it "returns ~/.pry_history" do + expect(described_class.default_file).to match('/.pry_history') + end + end + + context "when $XDG_DATA_HOME is defined" do + before { ENV['XDG_DATA_HOME'] = '/my/path' } + after { ENV['XDG_DATA_HOME'] = nil } + + it "returns config location relative to $XDG_DATA_HOME" do + expect(described_class.default_file).to eq('/my/path/pry/pry_history') + end + end + end + describe '#push' do it "does not record duplicated lines" do Pry.history << '3' |