Add support not to change files when dry run.

This commit is contained in:
yuuki arisawa 2011-07-02 19:16:06 +09:00
parent 847b2b8740
commit 8d02eec38c
2 changed files with 75 additions and 5 deletions

View File

@ -20,13 +20,34 @@ module Guard
@options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off
if @options[:"dry-run"]
Kernel.send(:define_method, :system ) do
Kernel.send(:define_method, :system) do
|program, *args| Kernel.print program + ' ' + args.join(' ') + "\n"
end
Kernel.send(:define_method, :"`" ) do
Kernel.send(:define_method, :"`") do
|command| Kernel.print command + "\n"
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
self
end

View File

@ -49,9 +49,18 @@ describe Guard do
after do
Kernel.send(:define_method, :system, @original_system.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
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)
::Kernel.should_not_receive(:print).with("command arg1 arg2\n")
system("command","arg1","arg2")
@ -63,19 +72,59 @@ describe Guard do
system("command","arg1","arg2")
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)
::Kernel.should_not_receive(:print).with("command\n")
`command`
%x{command}
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)
::Kernel.should_receive(:print).twice.with("command\n")
`command`
%x{command}
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