Class: Blame

Inherits:
RSpec::Core::Formatters::ProgressFormatter
  • Object
show all
Defined in:
lib/rspec/blame.rb

Overview

Formatter that ouputs the git blame details for the slowest examples. Requirements: Project must be version controlled with git. Usage: ‘rspec –profile –formatter Blame rspec_file.rb` or `rspec -p -f Blame rspec_file.rb` Alternative Usage: RSpec::Core::RakeTask.new(:task) { |t| t.rspec_opts = “-p -f Blame” }

Instance Method Summary collapse

Instance Method Details

#_example_run_timeObject



27
28
29
# File 'lib/rspec/blame.rb', line 27

def _example_run_time
  lambda { |example| example.execution_result[:run_time] }
end

#_print_details_for(example) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rspec/blame.rb', line 31

def _print_details_for(example)
  output.puts "  #{example.full_description}"
  output.print "%s %s" % [
    color("#{format_seconds(_example_run_time.call(example), 4)} secs".rjust(15, ' '), :red),
    color(example.location.ljust(80, ' '), :yellow)
  ]

  file, line_number = example.location.split(":")
  git_blame_output = %x(git blame -c --date=short -L #{line_number},#{line_number} #{file})
  blame = /(?<commit>\S+)\s*\((?<author>\D+)(?<date>\S+)/.match(git_blame_output)

  if blame.nil?
    output.puts
  else
    commit_details = "Author: #{blame[:author].strip} (#{blame[:commit]}), Date: #{blame[:date]}"
    output.puts(color(commit_details.ljust(60, ' '), :cyan))
  end
end

#_print_summary_for(slowest_examples) ⇒ Object



20
21
22
23
24
25
# File 'lib/rspec/blame.rb', line 20

def _print_summary_for(slowest_examples)
  slowest_tests_time, total_time = slowest_examples.map(&_example_run_time).inject { |sum, time| sum + time }, examples.map(&_example_run_time).inject { |sum, time| sum + time }
  formatted_percentage = '%.1f' % (slowest_tests_time / total_time * 100)

  output.puts "\nSlowest #{slowest_examples.size} examples finished in #{format_seconds(slowest_tests_time, 4)} secs (#{formatted_percentage}% of total time: #{format_seconds(total_time, 4)} secs).\n"
end

#dump_profile_slowest_examplesObject

Overrides ProgressFormatter’s output.



9
10
11
12
13
14
15
16
17
18
# File 'lib/rspec/blame.rb', line 9

def dump_profile_slowest_examples
  number_of_examples_to_profile = RSpec.configuration.profile_examples

  slowest_examples = examples.sort_by(&_example_run_time).reverse.first(number_of_examples_to_profile)

  _print_summary_for(slowest_examples)
  slowest_examples.each do |example|
    _print_details_for(example)
  end
end