From 847b2b8740717f10023f6bdbbe7d96f71a40427d Mon Sep 17 00:00:00 2001 From: yuuki arisawa Date: Tue, 28 Jun 2011 22:13:24 +0900 Subject: [PATCH] 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