Module: RSpec::SimpleCov::Setup

Defined in:
lib/rspec/simplecov/setup.rb

Class Method Summary collapse

Class Method Details

.build_contexts_and_example(coverage, configuration) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rspec/simplecov/setup.rb', line 35

def build_contexts_and_example( coverage, configuration )
  remove_location_from_reserver_keys

  coverage.example_group = RSpec.describe( configuration.described_thing, location: 'spec_helper_spec.rb')
  coverage.example_context = coverage.example_group.context configuration.context_text
  coverage.example = build_example( coverage.example_context, configuration )

  reinstate_location_in_reserved_keys
end

.build_example(context, configuration) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rspec/simplecov/setup.rb', line 25

def build_example( context, configuration )
  context.it configuration.test_case_text do
    result = configuration.described_thing.result
    minimum_coverage = configuration.described_thing.minimum_coverage
    configuration.described_thing.running = true
    
    expect( result.covered_percent ).to be >= minimum_coverage
  end
end

.do(configuration) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/rspec/simplecov/setup.rb', line 90

def do( configuration )
  RSpec.configure do |config|
    config.after(:suite) do
      Setup.setup_execute_and_analyse_coverage_example( configuration )
    end
  end
end

.evaluate_and_report_result(example) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/rspec/simplecov/setup.rb', line 64

def evaluate_and_report_result( example )
  passed = example.execution_result.status == :passed
  failed = !passed

  RSpec.configuration.reporter.example_failed example if failed
  RSpec.configuration.reporter.example_passed example if passed
end

.fix_example_backtrace(example, backtrace) ⇒ Object



58
59
60
61
62
# File 'lib/rspec/simplecov/setup.rb', line 58

def fix_example_backtrace( example, backtrace )
  return unless example.execution_result.exception
  
  example.execution_result.exception.backtrace.push( *backtrace )
end

.mark_contexts_as_finished(coverage) ⇒ Object



72
73
74
75
# File 'lib/rspec/simplecov/setup.rb', line 72

def mark_contexts_as_finished( coverage )
  RSpec.configuration.reporter.example_group_finished coverage.example_context
  RSpec.configuration.reporter.example_group_finished coverage.example_group
end

.reinstate_location_in_reserved_keysObject



21
22
23
# File 'lib/rspec/simplecov/setup.rb', line 21

def reinstate_location_in_reserved_keys
  RSpec::Core::Metadata::RESERVED_KEYS.push(:location)
end

.remove_location_from_reserver_keysObject



17
18
19
# File 'lib/rspec/simplecov/setup.rb', line 17

def remove_location_from_reserver_keys
  RSpec::Core::Metadata::RESERVED_KEYS.delete(:location)
end

.reset_example_group_paths(example_group, path) ⇒ Object



52
53
54
55
56
# File 'lib/rspec/simplecov/setup.rb', line 52

def reset_example_group_paths( example_group, path )
  example_group.[ :absolute_file_path ] = path
  example_group.[ :rerun_file_path ] = path
  example_group.[ :file_path ] = path
end

.run_example(coverage) ⇒ Object



45
46
47
48
49
50
# File 'lib/rspec/simplecov/setup.rb', line 45

def run_example( coverage )
  RSpec.configuration.reporter.example_group_started coverage.example_group
  RSpec.configuration.reporter.example_group_started coverage.example_context
  RSpec.configuration.reporter.example_started coverage.example
  coverage.example_group.run
end

.setup_execute_and_analyse_coverage_example(configuration) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rspec/simplecov/setup.rb', line 77

def setup_execute_and_analyse_coverage_example( configuration )
  include RSpec::SimpleCov

  coverage = Container.new

  Setup.build_contexts_and_example( coverage, configuration )
  Setup.run_example( coverage )
  Setup.reset_example_group_paths( coverage.example_group, configuration.caller_path )
  Setup.fix_example_backtrace( coverage.example, configuration.backtrace )
  Setup.evaluate_and_report_result( coverage.example )
  Setup.mark_contexts_as_finished( coverage )
end