Skip to content

Instantly share code, notes, and snippets.

@foysavas
Created July 11, 2012 18:16
Show Gist options
  • Save foysavas/3092126 to your computer and use it in GitHub Desktop.
Save foysavas/3092126 to your computer and use it in GitHub Desktop.
LastFailed for RSpec
require 'rspec/core/formatters/progress_formatter'
class LastFailedLogger < RSpec::Core::Formatters::BaseFormatter
def example_failed(example)
super
@all_examples_passed = false
File.open("log/last_failed_spec.log", "w") do |f|
f.write(example.location)
end
end
def stop
if @failure_count > 0
FileUtils.rm("log/last_failed_spec.log")
end
super
end
end
namespace :spec do
require './last_failed_logger.rb'
default_opts = ["--format progress", "--color", "--fail-fast", "-f LastFailedLogger"]
RSpec::Core::RakeTask.new(:auto) do |t|
if File.exists? "./log/last_failed_spec.log"
$ran_last_failed_test = true
t.rspec_opts = [*default_opts, "--tag ~wip"]
t.pattern = File.open("./log/last_failed_spec.log").read
FileUtils.rm "./log/last_failed_spec.log"
else
$ran_last_failed_test = false
t.rspec_opts = [*default_opts, "--tag ~wip"]
end
end
desc "Run all specs"
RSpec::Core::RakeTask.new(:all) do |t|
t.rspec_opts = [*default_opts]
end
desc "Run all specs that should pass"
RSpec::Core::RakeTask.new(:ok) do |t|
t.rspec_opts = [*default_opts, "--tag ~wip"]
end
end
Rake::Task["spec:auto"].enhance do
if $ran_last_failed_test
STDOUT.puts
STDOUT.puts "\e[32mLast failed example passed! Running all examples now...\e[0m"
STDOUT.puts
Rake::Task["spec:ok"].invoke
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment