2019-05-03 01:33:56 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-10 01:35:36 +02:00
|
|
|
require 'tempfile'
|
|
|
|
|
2012-09-15 14:50:15 -07:00
|
|
|
describe "save-file" do
|
|
|
|
before do
|
|
|
|
@tf = Tempfile.new(["pry", ".py"])
|
|
|
|
@path = @tf.path
|
|
|
|
@t = pry_tester
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@tf.close(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "-f" do
|
|
|
|
it 'should save a file to a file' do
|
2012-12-07 23:08:49 +01:00
|
|
|
temp_file do |f|
|
2012-09-15 14:50:15 -07:00
|
|
|
path = f.path
|
|
|
|
f.puts ":cute_horse"
|
|
|
|
f.flush
|
|
|
|
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval("save-file '#{path}' --to '#{@path}'")
|
2012-09-15 14:50:15 -07:00
|
|
|
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(File.read(@path)).to eq(File.read(path))
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "-i" do
|
|
|
|
it 'should save input expressions to a file (single expression)' do
|
|
|
|
@t.eval ':horse_nostrils'
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file -i 1 --to '#{@path}'"
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(File.read(@path)).to eq(":horse_nostrils\n")
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
|
2012-11-07 14:06:09 +01:00
|
|
|
it "should display a success message on save" do
|
|
|
|
@t.eval ':horse_nostrils'
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(@t.eval("save-file -i 1 --to '#{@path}'")).to match(/successfully saved/)
|
2012-11-07 14:06:09 +01:00
|
|
|
end
|
|
|
|
|
2012-09-15 14:50:15 -07:00
|
|
|
it 'should save input expressions to a file (range)' do
|
|
|
|
@t.eval ':or_nostrils', ':sucking_up_all_the_oxygen', ':or_whatever'
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file -i 1..2 --to '#{@path}'"
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(File.read(@path)).to eq(":or_nostrils\n:sucking_up_all_the_oxygen\n")
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
2013-01-12 22:12:21 +01:00
|
|
|
|
|
|
|
it 'should save multi-ranged input expressions' do
|
|
|
|
@t.eval ':or_nostrils', ':sucking_up_all_the_oxygen', ':or_whatever',
|
2019-02-24 19:39:14 +02:00
|
|
|
':baby_ducks', ':cannot_escape'
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file -i 1..2 -i 4..5 --to '#{@path}'"
|
2019-03-03 17:37:58 +02:00
|
|
|
expect(File.read(@path)).to eq(
|
|
|
|
":or_nostrils\n:sucking_up_all_the_oxygen\n:baby_ducks\n:cannot_escape\n"
|
|
|
|
)
|
2013-01-12 22:12:21 +01:00
|
|
|
end
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
|
2013-01-07 23:12:10 +01:00
|
|
|
describe "saving methods" do
|
2012-09-15 14:50:15 -07:00
|
|
|
before do
|
|
|
|
@o = Object.new
|
|
|
|
def @o.baby
|
|
|
|
:baby
|
|
|
|
end
|
2018-10-14 14:23:34 +08:00
|
|
|
|
2012-09-15 14:50:15 -07:00
|
|
|
def @o.bang
|
|
|
|
:bang
|
|
|
|
end
|
|
|
|
|
|
|
|
@t = pry_tester(@o)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "single method" do
|
|
|
|
it 'should save a method to a file' do
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file --to '#{@path}' baby"
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(File.read(@path)).to eq(Pry::Method.from_obj(@o, :baby).source)
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
|
2012-11-07 14:06:09 +01:00
|
|
|
it "should display a success message on save" do
|
2019-03-03 17:37:58 +02:00
|
|
|
expect(@t.eval("save-file --to '#{@path}' baby"))
|
|
|
|
.to match(/successfully saved/)
|
2012-11-07 14:06:09 +01:00
|
|
|
end
|
|
|
|
|
2012-09-15 14:50:15 -07:00
|
|
|
it 'should save a method to a file truncated by --lines' do
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file --to '#{@path}' baby --lines 2..4"
|
2012-09-15 14:50:15 -07:00
|
|
|
|
|
|
|
# must add 1 as first line of method is 1
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(File.read(@path)).to eq(
|
2012-09-15 14:50:15 -07:00
|
|
|
Pry::Method.from_obj(@o, :baby).source.lines.to_a[1..5].join
|
2015-03-10 22:49:29 +02:00
|
|
|
)
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-07 23:12:10 +01:00
|
|
|
# TODO: do we want to reintroduce this spec??
|
|
|
|
#
|
|
|
|
# describe "multiple method" do
|
|
|
|
# it 'should save multiple methods to a file' do
|
|
|
|
# @t.eval "save-file #{@path} -m baby -m bang"
|
|
|
|
|
|
|
|
# File.read(@path).should == Pry::Method.from_obj(@o, :baby).source +
|
|
|
|
# Pry::Method.from_obj(@o, :bang).source
|
|
|
|
# end
|
|
|
|
|
|
|
|
# it 'should save multiple methods to a file trucated by --lines' do
|
|
|
|
# @t.eval "save-file #{@path} -m baby -m bang --lines 2..-2"
|
|
|
|
|
|
|
|
# # must add 1 as first line of method is 1
|
|
|
|
# File.read(@path).should == (Pry::Method.from_obj(@o, :baby).source +
|
|
|
|
# Pry::Method.from_obj(@o, :bang).source).lines.to_a[1..-2].join
|
|
|
|
# end
|
|
|
|
|
|
|
|
# it 'should save multiple methods to a file trucated by --lines 1 ' \
|
|
|
|
# '(single parameter, not range)' do
|
|
|
|
# @t.eval "save-file #{@path} -m baby -m bang --lines 1"
|
|
|
|
|
|
|
|
# # must add 1 as first line of method is 1
|
|
|
|
# File.read(@path).should == (Pry::Method.from_obj(@o, :baby).source +
|
|
|
|
# Pry::Method.from_obj(@o, :bang).source).lines.to_a[0]
|
|
|
|
# end
|
|
|
|
# end
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "overwrite by default (no --append)" do
|
|
|
|
it 'should overwrite specified file with new input' do
|
|
|
|
@t.eval ':horse_nostrils'
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file -i 1 --to '#{@path}'"
|
2012-09-15 14:50:15 -07:00
|
|
|
|
|
|
|
@t.eval ':sucking_up_all_the_oxygen'
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file -i 2 --to '#{@path}'"
|
2012-09-15 14:50:15 -07:00
|
|
|
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(File.read(@path)).to eq(":sucking_up_all_the_oxygen\n")
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "--append" do
|
|
|
|
it 'should append to end of specified file' do
|
|
|
|
@t.eval ':horse_nostrils'
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file -i 1 --to '#{@path}'"
|
2012-09-15 14:50:15 -07:00
|
|
|
|
|
|
|
@t.eval ':sucking_up_all_the_oxygen'
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file -i 2 --to '#{@path}' -a"
|
2012-09-15 14:50:15 -07:00
|
|
|
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(File.read(@path)).to eq(
|
2012-09-15 14:50:15 -07:00
|
|
|
":horse_nostrils\n:sucking_up_all_the_oxygen\n"
|
2015-03-10 22:49:29 +02:00
|
|
|
)
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-07 23:12:10 +01:00
|
|
|
describe "saving commands" do
|
2018-10-07 00:58:53 +08:00
|
|
|
it 'should save a command to a file' do
|
2013-01-15 12:02:41 +13:00
|
|
|
@t.eval "save-file --to '#{@path}' show-source"
|
2014-04-29 00:03:15 -07:00
|
|
|
cmd_source = Pry.config.commands["show-source"].source
|
2015-03-10 22:49:29 +02:00
|
|
|
expect(File.read(@path)).to eq(cmd_source)
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-07 23:12:10 +01:00
|
|
|
# TODO: reintroduce these specs at some point?
|
|
|
|
#
|
|
|
|
# describe "combined options" do
|
|
|
|
# before do
|
|
|
|
# @o = Object.new
|
|
|
|
# def @o.baby
|
|
|
|
# :baby
|
|
|
|
# end
|
|
|
|
|
|
|
|
# @t = pry_tester(@o)
|
|
|
|
# end
|
|
|
|
|
|
|
|
# it 'should save input cache and a method to a file (in that order)' do
|
|
|
|
# @t.eval ":horse_nostrils"
|
|
|
|
# @t.eval "save-file -i 1 -m baby #{@path}"
|
|
|
|
|
|
|
|
# File.read(@path).should == ":horse_nostrils\n" +
|
|
|
|
# Pry::Method.from_obj(@o, :baby).source
|
|
|
|
# end
|
|
|
|
|
|
|
|
# it 'should select a portion to save using --lines' do
|
|
|
|
# @t.eval ":horse_nostrils"
|
|
|
|
# @t.eval "save-file -i 1 -m baby #{@path} --lines 2..-2"
|
|
|
|
|
|
|
|
# str = ":horse_nostrils\n" + Pry::Method.from_obj(@o, :baby).source
|
|
|
|
# File.read(@path).should == str.lines.to_a[1..-2].join
|
|
|
|
# end
|
|
|
|
# end
|
2012-09-15 14:50:15 -07:00
|
|
|
end
|