Add support not to change files when dry run.
This commit is contained in:
parent
847b2b8740
commit
8d02eec38c
25
lib/guard.rb
25
lib/guard.rb
@ -20,13 +20,34 @@ module Guard
|
|||||||
@options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off
|
@options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off
|
||||||
|
|
||||||
if @options[:"dry-run"]
|
if @options[:"dry-run"]
|
||||||
Kernel.send(:define_method, :system ) do
|
Kernel.send(:define_method, :system) do
|
||||||
|program, *args| Kernel.print program + ' ' + args.join(' ') + "\n"
|
|program, *args| Kernel.print program + ' ' + args.join(' ') + "\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
Kernel.send(:define_method, :"`" ) do
|
Kernel.send(:define_method, :"`") do
|
||||||
|command| Kernel.print command + "\n"
|
|command| Kernel.print command + "\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class << File
|
||||||
|
unless method_defined?(:guard_original_open)
|
||||||
|
alias guard_original_open open
|
||||||
|
end
|
||||||
|
def open(name, *rest, &block)
|
||||||
|
rest.shift
|
||||||
|
rest.unshift "r"
|
||||||
|
guard_original_open(name, *rest, &block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
#because IO.write effect print method
|
||||||
|
File.send(:define_method, :"write") do
|
||||||
|
|str| return
|
||||||
|
end
|
||||||
|
|
||||||
|
File.send(:define_method, :"write_nonblock") do
|
||||||
|
|str| return
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
@ -49,9 +49,18 @@ describe Guard do
|
|||||||
after do
|
after do
|
||||||
Kernel.send(:define_method, :system, @original_system.to_proc )
|
Kernel.send(:define_method, :system, @original_system.to_proc )
|
||||||
Kernel.send(:define_method, :"`", @original_command.to_proc )
|
Kernel.send(:define_method, :"`", @original_command.to_proc )
|
||||||
|
class << File
|
||||||
|
if method_defined?(:guard_original_open)
|
||||||
|
alias open guard_original_open
|
||||||
|
end
|
||||||
|
end
|
||||||
|
File.send(:remove_method, :write) if
|
||||||
|
File.instance_methods(false).include?(:write)
|
||||||
|
File.send(:remove_method, :write_nonblock) if
|
||||||
|
File.instance_methods(false).include?(:write_nonblock)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "overwrites Kernel.#system method if the dry-run option is false" do
|
it "don't overwrites Kernel.#system method if the dry-run option is false" do
|
||||||
::Guard.setup(:"dry-run" => false)
|
::Guard.setup(:"dry-run" => false)
|
||||||
::Kernel.should_not_receive(:print).with("command arg1 arg2\n")
|
::Kernel.should_not_receive(:print).with("command arg1 arg2\n")
|
||||||
system("command","arg1","arg2")
|
system("command","arg1","arg2")
|
||||||
@ -63,19 +72,59 @@ describe Guard do
|
|||||||
system("command","arg1","arg2")
|
system("command","arg1","arg2")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "overwrites Kernel.#` method if the dry-run option is false" do
|
it "don't overwrites Kernel.#` method if the dry-run option is false" do
|
||||||
::Guard.setup(:"dry-run" => false)
|
::Guard.setup(:"dry-run" => false)
|
||||||
::Kernel.should_not_receive(:print).with("command\n")
|
::Kernel.should_not_receive(:print).with("command\n")
|
||||||
`command`
|
`command`
|
||||||
%x{command}
|
%x{command}
|
||||||
end
|
end
|
||||||
|
|
||||||
it "overwrites Kernel.#` method if the dry-run option is false" do
|
it "overwrites Kernel.#` method if the dry-run option is true" do
|
||||||
::Guard.setup(:"dry-run" => true)
|
::Guard.setup(:"dry-run" => true)
|
||||||
::Kernel.should_receive(:print).twice.with("command\n")
|
::Kernel.should_receive(:print).twice.with("command\n")
|
||||||
`command`
|
`command`
|
||||||
%x{command}
|
%x{command}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context do
|
||||||
|
before do
|
||||||
|
@file = @fixture_path.join("newfile1.rb")
|
||||||
|
File.open(@file, 'w') { |f| f.write('original') }
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
if File.exists?(@file)
|
||||||
|
begin
|
||||||
|
File.delete @file
|
||||||
|
rescue
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "overwrites File.write method if the dry-run option is true" do
|
||||||
|
::Guard.setup(:"dry-run" => true)
|
||||||
|
File.open(@file, 'w') { |f| f.write('write') }
|
||||||
|
File.open(@file, 'r') { |f| f.read.should == 'original' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "don't overwrites File.write method if the dry-run option is false" do
|
||||||
|
::Guard.setup(:"dry-run" => false)
|
||||||
|
File.open(@file, 'w') { |f| f.write('write') }
|
||||||
|
File.open(@file, 'r') { |f| f.read.should == 'write' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "overwrites File.write_nonblock method if the dry-run option is true" do
|
||||||
|
::Guard.setup(:"dry-run" => true)
|
||||||
|
File.open(@file, 'w') { |f| f.write_nonblock('write') }
|
||||||
|
File.open(@file, 'r') { |f| f.read.should == 'original' }
|
||||||
|
end
|
||||||
|
|
||||||
|
it "don't overwrites File.write_nonblock method if the dry-run option is false" do
|
||||||
|
::Guard.setup(:"dry-run" => false)
|
||||||
|
File.open(@file, 'w') { |f| f.write_nonblock('write') }
|
||||||
|
File.open(@file, 'r') { |f| f.read.should == 'write' }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user