Sha256: b07b87bcaf17f3d28463bce5330485a2383649d52c72c3181d873779099fb803

Contents?: true

Size: 1.67 KB

Versions: 22

Compression:

Stored size: 1.67 KB

Contents

require 'tempfile'

describe HammerCLI::ShellHistory do

  before :each do
    # Readline::HISOTRY does not implement #clear in Ruby 1.8
    while not Readline::HISTORY.empty?
      Readline::HISTORY.pop
    end
  end

  let :history_file do
    file = Tempfile.new('history')
    file.puts "line 1"
    file.puts "line 2"
    file.close
    file
  end

  let :new_file do
    Tempfile.new('history')
  end

  describe "loading old history" do

    it "skips loading if the file does not exist" do
      history = HammerCLI::ShellHistory.new(new_file.path)

      Readline::HISTORY.to_a.must_equal []
    end

    it "preseeds readline's history" do
      history = HammerCLI::ShellHistory.new(history_file.path)

      Readline::HISTORY.to_a.must_equal ["line 1", "line 2"]
    end
  end

  describe "saving history" do
    it "creates history file if it does not exist" do
      history = HammerCLI::ShellHistory.new(new_file.path)
      history.push("some command ")

      File.exist?(new_file.path).must_equal true
    end

    it "appends history to the given file" do
      history = HammerCLI::ShellHistory.new(new_file.path)
      history.push("some command ")
      history.push("another command ")

      new_file.read.must_equal "some command\nanother command\n"
    end

    it "appends to readline's history" do
      history = HammerCLI::ShellHistory.new(history_file.path)
      history.push("line 3")

      Readline::HISTORY.to_a.must_equal ["line 1", "line 2", "line 3"]
    end

    it "doesn't save exit command" do
      history = HammerCLI::ShellHistory.new(history_file.path)
      history.push("exit ")

      Readline::HISTORY.to_a.must_equal ["line 1", "line 2"]
    end
  end

end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
hammer_cli-0.14.0 test/unit/history_test.rb
hammer_cli-0.13.1 test/unit/history_test.rb
hammer_cli-0.13.0 test/unit/history_test.rb
hammer_cli-0.12.0 test/unit/history_test.rb
hammer_cli-0.11.0 test/unit/history_test.rb
hammer_cli-0.10.2 test/unit/history_test.rb
hammer_cli-0.10.1 test/unit/history_test.rb
hammer_cli-0.10.0 test/unit/history_test.rb
hammer_cli-0.9.0 test/unit/history_test.rb
hammer_cli-0.8.0 test/unit/history_test.rb
hammer_cli-0.7.0 test/unit/history_test.rb
hammer_cli-0.6.1 test/unit/history_test.rb
hammer_cli-0.6.0 test/unit/history_test.rb
hammer_cli-0.5.1 test/unit/history_test.rb
hammer_cli-0.4.0 test/unit/history_test.rb
hammer_cli-0.3.0 test/unit/history_test.rb
hammer_cli-0.2.0 test/unit/history_test.rb
hammer_cli-0.1.4 test/unit/history_test.rb
hammer_cli-0.1.3 test/unit/history_test.rb
hammer_cli-0.1.2 test/unit/history_test.rb