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.3'.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

#log(message) ⇒ Object



165
166
167
# File 'lib/quarantine.rb', line 165

def log(message)
  @options[:log].call(message) if @options[:logging]
end

#on_completeObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/quarantine.rb', line 106

def on_complete
  quarantined_tests = @tests.values.select { |test| test.status == :quarantined }.sort_by(&:id)

  if !@options[:record_tests]
    log('Recording tests disabled; skipping')
  elsif @tests.empty?
    log('No tests found; skipping recording')
  elsif quarantined_tests.count { |test| old_tests[test.id]&.status != :quarantined } >= @options[:failsafe_limit]
    log('Number of quarantined tests above failsafe limit; skipping recording')
  else
    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

  log("    \\n[quarantine] Quarantined tests:\n      \#{quarantined_tests.map { |test| \"\#{test.id} \#{test.full_description}\" }.join(\"\\n  \")}\n\n    [quarantine] Database errors:\n      \#{@database_failures.join(\"\\n  \")}\n  MESSAGE\nend\n")

#on_startObject



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 on_start
  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(
      "        Failed to pull the quarantine list from \#{@options[:test_statuses_table_name]}\n        because of \#{e.cause&.class}: \#{e.cause&.message}\n      ERROR_MSG\n    )\n  end\n\n  pairs =\n    test_statuses\n    .group_by { |t| t['id'] }\n    .map { |_id, tests| tests.max_by { |t| t['created_at'] } }\n    .compact\n    .filter { |t| t['last_status'] == 'quarantined' }\n    .map do |t|\n      [\n        t['id'],\n        Quarantine::Test.new(\n          id: t['id'],\n          status: t['last_status'].to_sym,\n          consecutive_passes: t['consecutive_passes'].to_i,\n          full_description: t['full_description'],\n          location: t['location'],\n          extra_attributes: t['extra_attributes']\n        )\n      ]\n    end\n\n  @old_tests = Hash[pairs]\nend\n"

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/quarantine.rb', line 139

def on_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

#test_quarantined?(example) ⇒ Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/quarantine.rb', line 160

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