Class: Quarantine

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/quarantine/test.rb,
lib/quarantine.rb,
lib/quarantine/cli.rb,
lib/quarantine/error.rb,
lib/quarantine/version.rb,
lib/quarantine/rspec_adapter.rb,
lib/quarantine/databases/base.rb,
lib/quarantine/databases/dynamo_db.rb,
lib/quarantine/databases/google_sheets.rb

Overview

typed: strict

Defined Under Namespace

Modules: Databases, RSpecAdapter Classes: CLI, DatabaseError, Error, Test, UnknownUploadError, UnsupportedDatabaseError

Constant Summary collapse

VERSION =
'2.1.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Quarantine

Returns a new instance of Quarantine.



39
40
41
42
43
44
45
# File 'lib/quarantine.rb', line 39

def initialize(options)
  @options = options
  @old_tests = T.let({}, T::Hash[String, Quarantine::Test])
  @tests = T.let({}, T::Hash[String, Quarantine::Test])
  @database_failures = T.let([], T::Array[String])
  @database = T.let(nil, T.nilable(Quarantine::Databases::Base))
end

Instance Attribute Details

#old_testsObject (readonly)

Returns the value of attribute old_tests.



36
37
38
# File 'lib/quarantine.rb', line 36

def old_tests
  @old_tests
end

#testsObject (readonly)

Returns the value of attribute tests.



33
34
35
# File 'lib/quarantine.rb', line 33

def tests
  @tests
end

Instance Method Details

#databaseObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/quarantine.rb', line 48

def database
  @database ||=
    case @options[:database]
    when Quarantine::Databases::Base
      @options[:database]
    else
      database_options = @options[:database].dup
      type = database_options.delete(:type)
      case type
      when :dynamodb
        Quarantine::Databases::DynamoDB.new(database_options)
      when :google_sheets
        Quarantine::Databases::GoogleSheets.new(database_options)
      else
        raise Quarantine::UnsupportedDatabaseError.new("Quarantine does not support database type: #{type.inspect}")
      end
    end
end

#fetch_test_statusesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/quarantine.rb', line 69

def fetch_test_statuses
  begin
    test_statuses = database.fetch_items(@options[:test_statuses_table_name])
  rescue Quarantine::DatabaseError => e
    @database_failures << "#{e.cause&.class}: #{e.cause&.message}"
    raise Quarantine::DatabaseError.new(
      <<~ERROR_MSG
        Failed to pull the quarantine list from #{@options[:test_statuses_table_name]}
        because of #{e.cause&.class}: #{e.cause&.message}
      ERROR_MSG
    )
  end

  pairs =
    test_statuses
    .group_by { |t| t['id'] }
    .map { |_id, tests| tests.max_by { |t| t['created_at'] } }
    .compact
    .filter { |t| t['last_status'] == 'quarantined' }
    .map do |t|
      [
        t['id'],
        Quarantine::Test.new(
          id: t['id'],
          status: t['last_status'].to_sym,
          consecutive_passes: t['consecutive_passes'].to_i,
          full_description: t['full_description'],
          location: t['location'],
          extra_attributes: t['extra_attributes']
        )
      ]
    end

  @old_tests = Hash[pairs]
end

#record_test(example, status, passed:) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/quarantine.rb', line 123

def record_test(example, status, passed:)
  extra_attributes = @options[:extra_attributes] ? @options[:extra_attributes].call(example) : {}

  new_consecutive_passes = passed ? (@old_tests[example.id]&.consecutive_passes || 0) + 1 : 0
  release_at = @options[:release_at_consecutive_passes]
  new_status = !release_at.nil? && new_consecutive_passes >= release_at ? :passing : status
  test = Quarantine::Test.new(
    id: example.id,
    status: new_status,
    consecutive_passes: new_consecutive_passes,
    full_description: example.full_description,
    location: example.location,
    extra_attributes: extra_attributes
  )

  @tests[test.id] = test
end

#summaryObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/quarantine.rb', line 149

def summary
  quarantined_tests = @tests.values.select { |test| test.status == :quarantined }.sort_by(&:id)
  <<~MESSAGE
    \n[quarantine] Quarantined tests:
      #{quarantined_tests.map { |test| "#{test.id} #{test.full_description}" }.join("\n  ")}

    [quarantine] Database errors:
      #{@database_failures.join("\n  ")}
  MESSAGE
end

#test_quarantined?(example) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/quarantine.rb', line 144

def test_quarantined?(example)
  @old_tests[example.id]&.status == :quarantined
end

#upload_testsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/quarantine.rb', line 106

def upload_tests
  return if @tests.empty? || @tests.values.count { |test| test.status == :quarantined } >= @options[:failsafe_limit]

  begin
    timestamp = Time.now.to_i / 1000 # Truncated millisecond from timestamp for reasons specific to Flexport
    database.write_items(
      @options[:test_statuses_table_name],
      @tests.values.map { |item| item.to_hash.merge('updated_at' => timestamp) }
    )
  rescue Quarantine::DatabaseError => e
    @database_failures << "#{e.cause&.class}: #{e.cause&.message}"
  end
end