Created
May 22, 2012 02:44
-
-
Save kyamaguchi/2766217 to your computer and use it in GitHub Desktop.
RSpec rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Could be placed in spec_helper.rb | |
# Requiring foo.rake in this example. | |
def run_foo_bar(arg) | |
require 'rake' | |
@rake = Rake::Application.new | |
Rake.application = @rake | |
Rake.application.rake_require "lib/tasks/foo" | |
Rake::Task.define_task(:environment) | |
silence do | |
@task = Rake::Task["foo:bar"].invoke(arg) | |
end | |
end | |
def silence | |
begin | |
orig_stderr = $stderr.clone | |
orig_stdout = $stdout.clone | |
$stderr.reopen File.new('/dev/null', 'w') | |
$stdout.reopen File.new('/dev/null', 'w') | |
return_value = yield | |
rescue Exception => e | |
$stdout.reopen orig_stdout | |
$stderr.reopen orig_stderr | |
raise e | |
ensure | |
$stdout.reopen orig_stdout | |
$stderr.reopen orig_stderr | |
end | |
return_value | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment