From 49d14b13606dcf45e8faed6f4ee28de66c8d7f8b Mon Sep 17 00:00:00 2001 From: yuuki arisawa Date: Sun, 26 Jun 2011 00:13:26 +0900 Subject: [PATCH 1/3] Added dry_run option for Guardfile debug --- lib/guard.rb | 4 ++++ lib/guard/cli.rb | 1 + 2 files changed, 5 insertions(+) diff --git a/lib/guard.rb b/lib/guard.rb index 12b8d47..ee3ee42 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -19,6 +19,10 @@ module Guard @options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off + Kernel.send(:define_method, :system ) do + |program, *args| print program + ' ' + args.join(' ') + "\n" + end if options.dry_run + self end diff --git a/lib/guard/cli.rb b/lib/guard/cli.rb index 9bf323a..fef9254 100644 --- a/lib/guard/cli.rb +++ b/lib/guard/cli.rb @@ -9,6 +9,7 @@ module Guard method_option :notify, :type => :boolean, :default => true, :aliases => '-n', :banner => "Notifications feature (growl/libnotify)" method_option :debug, :type => :boolean, :default => false, :aliases => '-d', :banner => "Print debug messages" method_option :group, :type => :array, :default => [], :aliases => '-g', :banner => "Run only the passed groups" + method_option :dry_run, :type => :boolean, :default => false, :aliases => '-y', :banner => "Do a dry run without executing actions" desc "start", "Starts Guard" def start From 847b2b8740717f10023f6bdbbe7d96f71a40427d Mon Sep 17 00:00:00 2001 From: yuuki arisawa Date: Tue, 28 Jun 2011 22:13:24 +0900 Subject: [PATCH 2/3] Added some specs,and some lines in README/CHANGELOG for dry-run option. And added support for Kernel.#` and %x literal. --- CHANGELOG.md | 1 + README.md | 9 +++++++++ lib/guard.rb | 11 ++++++++--- lib/guard/cli.rb | 2 +- spec/guard_spec.rb | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64e236e..16227eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ ## Improvements +- Pull request [#95](https://github.com/guard/guard/pull/95): Do a dry run without executing actions when the `:clear` option is given. ([@uk-ar][]) - Pull request [#94](https://github.com/guard/guard/pull/94): Show backtrace in terminal when a problem with a watch action occurs. ([@capotej][]) - Pull request [#88](https://github.com/guard/guard/pull/88): Write exception trace in the terminal when a supervised task fail. ([@mcmire][]) - Color in red the "ERROR:" flag when using `UI.error`. ([@rymai][]) diff --git a/README.md b/README.md index 057370d..5d26f51 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,15 @@ $ guard --debug $ guard -d # shortcut ``` +### `--dry-run` option + +Do a dry run without executing actions: + +``` bash +$ guard --dry-run +$ guard -y # shortcut +``` + An exhaustive list of options is available with: ``` bash diff --git a/lib/guard.rb b/lib/guard.rb index ee3ee42..9df6760 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -19,10 +19,15 @@ module Guard @options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off - Kernel.send(:define_method, :system ) do - |program, *args| print program + ' ' + args.join(' ') + "\n" - end if options.dry_run + if @options[:"dry-run"] + Kernel.send(:define_method, :system ) do + |program, *args| Kernel.print program + ' ' + args.join(' ') + "\n" + end + Kernel.send(:define_method, :"`" ) do + |command| Kernel.print command + "\n" + end + end self end diff --git a/lib/guard/cli.rb b/lib/guard/cli.rb index fef9254..e98e627 100644 --- a/lib/guard/cli.rb +++ b/lib/guard/cli.rb @@ -9,7 +9,7 @@ module Guard method_option :notify, :type => :boolean, :default => true, :aliases => '-n', :banner => "Notifications feature (growl/libnotify)" method_option :debug, :type => :boolean, :default => false, :aliases => '-d', :banner => "Print debug messages" method_option :group, :type => :array, :default => [], :aliases => '-g', :banner => "Run only the passed groups" - method_option :dry_run, :type => :boolean, :default => false, :aliases => '-y', :banner => "Do a dry run without executing actions" + method_option :"dry-run", :type => :boolean, :default => false, :aliases => '-y', :banner => "Do a dry run without executing actions" desc "start", "Starts Guard" def start diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index b8c1c43..adbc022 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -39,6 +39,44 @@ describe Guard do ::Guard::Notifier.should_receive(:turn_off) ::Guard.setup(:notify => true) end + + context do + before do + @original_system = Kernel.method(:system) + @original_command = Kernel.method(:"`") + end + + after do + Kernel.send(:define_method, :system, @original_system.to_proc ) + Kernel.send(:define_method, :"`", @original_command.to_proc ) + end + + it "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") + end + + it "overwrites Kernel.#system method if the dry-run option is true" do + ::Guard.setup(:"dry-run" => true) + ::Kernel.should_receive(:print).with("command arg1 arg2\n") + system("command","arg1","arg2") + end + + it "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 + ::Guard.setup(:"dry-run" => true) + ::Kernel.should_receive(:print).twice.with("command\n") + `command` + %x{command} + end + end end describe ".get_guard_class" do From 8d02eec38c74d01deb93ff162ce91d9bd132da98 Mon Sep 17 00:00:00 2001 From: yuuki arisawa Date: Sat, 2 Jul 2011 19:16:06 +0900 Subject: [PATCH 3/3] Add support not to change files when dry run. --- lib/guard.rb | 25 +++++++++++++++++++-- spec/guard_spec.rb | 55 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index 9df6760..ab1b9ce 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -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 diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index adbc022..34c9aa2 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -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