Module: Quarantine::RSpecAdapter

Extended by:
T::Sig
Defined in:
lib/quarantine/rspec_adapter.rb

Class Method Summary collapse

Class Method Details

.bindObject



11
12
13
14
15
16
17
# File 'lib/quarantine/rspec_adapter.rb', line 11

def self.bind
  bind_rspec_configurations
  bind_fetch_test_statuses
  bind_record_tests
  bind_upload_tests
  bind_logger
end

.bind_fetch_test_statusesObject



48
49
50
51
52
53
54
# File 'lib/quarantine/rspec_adapter.rb', line 48

def self.bind_fetch_test_statuses
  ::RSpec.configure do |config|
    config.before(:suite) do
      Quarantine::RSpecAdapter.quarantine.fetch_test_statuses
    end
  end
end

.bind_loggerObject



99
100
101
102
103
104
105
106
107
# File 'lib/quarantine/rspec_adapter.rb', line 99

def self.bind_logger
  ::RSpec.configure do |config|
    config.after(:suite) do
      if RSpec.configuration.quarantine_logging
        RSpec.configuration.reporter.message(Quarantine::RSpecAdapter.quarantine.summary)
      end
    end
  end
end

.bind_record_testsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/quarantine/rspec_adapter.rb', line 58

def self.bind_record_tests
  ::RSpec.configure do |config|
    config.after(:each) do |example|
       = example.

      # optionally, the upstream RSpec configuration could define an after hook that marks an example as flaky in
      # the example's metadata
      quarantined = Quarantine::RSpecAdapter.quarantine.test_quarantined?(example) || [:flaky]
      if example.exception
        if [:retry_attempts] + 1 == [:retry]
          # will record the failed test if it's final retry from the rspec-retry gem
          if RSpec.configuration.skip_quarantined_tests && quarantined
            example.clear_exception!
            Quarantine::RSpecAdapter.quarantine.record_test(example, :quarantined, passed: false)
          else
            Quarantine::RSpecAdapter.quarantine.record_test(example, :failing, passed: false)
          end
        end
      elsif [:retry_attempts] > 0
        # will record the flaky test if it failed the first run but passed a subsequent run
        Quarantine::RSpecAdapter.quarantine.record_test(example, :quarantined, passed: false)
      elsif quarantined
        Quarantine::RSpecAdapter.quarantine.record_test(example, :quarantined, passed: true)
      else
        Quarantine::RSpecAdapter.quarantine.record_test(example, :passing, passed: true)
      end
    end
  end
end

.bind_rspec_configurationsObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/quarantine/rspec_adapter.rb', line 33

def self.bind_rspec_configurations
  ::RSpec.configure do |config|
    config.add_setting(:quarantine_database, default: { type: :dynamodb, region: 'us-west-1' })
    config.add_setting(:quarantine_test_statuses, { default: 'test_statuses' })
    config.add_setting(:skip_quarantined_tests, { default: true })
    config.add_setting(:quarantine_record_tests, { default: true })
    config.add_setting(:quarantine_logging, { default: true })
    config.add_setting(:quarantine_extra_attributes)
    config.add_setting(:quarantine_failsafe_limit, default: 10)
    config.add_setting(:quarantine_release_at_consecutive_passes)
  end
end

.bind_upload_testsObject



89
90
91
92
93
94
95
# File 'lib/quarantine/rspec_adapter.rb', line 89

def self.bind_upload_tests
  ::RSpec.configure do |config|
    config.after(:suite) do
      Quarantine::RSpecAdapter.quarantine.upload_tests if RSpec.configuration.quarantine_record_tests
    end
  end
end

.quarantineObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/quarantine/rspec_adapter.rb', line 20

def self.quarantine
  @quarantine = T.let(@quarantine, T.nilable(Quarantine))
  @quarantine ||= Quarantine.new(
    database: RSpec.configuration.quarantine_database,
    test_statuses_table_name: RSpec.configuration.quarantine_test_statuses,
    extra_attributes: RSpec.configuration.quarantine_extra_attributes,
    failsafe_limit: RSpec.configuration.quarantine_failsafe_limit,
    release_at_consecutive_passes: RSpec.configuration.quarantine_release_at_consecutive_passes,
  )
end