Skip to content

Instantly share code, notes, and snippets.

@DNNX DNNX/my_formatter.rb
Created Aug 12, 2011

Embed
What would you like to do?
require 'rspec/core/formatters/progress_formatter'
require 'awesome_print'
class MyFormatter < RSpec::Core::Formatters::ProgressFormatter
def start_dump
super
current_hash = examples_to_hash(examples)
if File.exist? 'result.txt'
lines = read_log 'result.txt'
previous_hash = lines_to_hash lines
all_tests = (previous_hash.keys + current_hash.keys).uniq
affected_tests = all_tests.select do |description|
current_result = current_hash[description]
previous_result = previous_hash[description]
current_result != previous_result
end
if affected_tests.empty?
output.puts "No changes since last run"
else
output.puts "Affected tests (#{affected_tests.size}). "
output.puts "PS CS Description"
affected_tests.each do |description|
current_result = current_hash[description]
previous_result = previous_hash[description]
output.puts([previous_result, current_result, description] * ' ')
end
output.puts "PS - Previous Status"
output.puts "CS - Current Status"
end
end
write_log('result.txt', current_hash)
# ap examples.first.execution_result
end
def write_log(file_name, hash)
File.open(file_name, 'w') do |f|
f.puts(hash.map{|descr,status| "#{status} #{descr}"})
end
end
def read_log(file_name)
lines = []
File.open(file_name, 'r') do |f|
s = nil
lines << s while s = f.gets
end
lines
end
def examples_to_hash(examples)
h = Hash.new('?')
examples.each do |example|
status = example.execution_result[:status]
h[example.full_description] = result_to_char(status)
end
h
end
def result_to_char(result)
case result
when 'passed' then '.'
when 'failed' then 'F'
when 'pending' then 'P'
else '?'
end
end
def lines_to_hash(lines)
h = Hash.new('?')
lines.each do |line|
next if line.size <= 3
result = line[0..0]
description = line[2..-1].chomp
h[description] = result
end
h
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.