Skip to content

Instantly share code, notes, and snippets.

@kyamaguchi
Created May 22, 2012 02:44
Show Gist options
  • Save kyamaguchi/2766217 to your computer and use it in GitHub Desktop.
Save kyamaguchi/2766217 to your computer and use it in GitHub Desktop.
RSpec rake
# 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