Skip to content

Commit eb2d004

Browse files
committed
Add test id normalizer to configuration
1 parent 9fec553 commit eb2d004

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

ruby/lib/ci/queue/configuration.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,21 @@ def max_duration=(duration)
172172
end
173173

174174
def flaky?(test)
175-
@flaky_tests.include?(test.id)
175+
lazy_normalized_flaky_tests.include?(normalize_test_id(test.id))
176+
end
177+
178+
def normalize_test_id(id)
179+
id
180+
end
181+
182+
def lazy_normalized_flaky_tests
183+
@normalized_flaky_tests ||= @flaky_tests.map { normalize_test_id(_1) }
184+
end
185+
186+
def test_id_normalizer=(normalizer)
187+
normalizer = normalizer.to_proc if Method === normalizer
188+
define_singleton_method(:normalize_test_id, normalizer)
189+
@normalized_flaky_tests = nil
176190
end
177191

178192
def seed

ruby/test/ci/queue/configuration_test.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,58 @@ def test_new_lazy_load_test_helpers_env
200200
assert_equal ["test/test_helper.rb", "test/support/helper.rb"], config.lazy_load_test_helper_paths
201201
end
202202

203+
def test_flaky_without_normalizer
204+
config = Configuration.new(flaky_tests: ["ATest#test_foo", "BTest#test_bar"])
205+
206+
test = Struct.new(:id).new("ATest#test_foo")
207+
assert config.flaky?(test)
208+
209+
test = Struct.new(:id).new("CTest#test_baz")
210+
refute config.flaky?(test)
211+
end
212+
213+
def test_flaky_with_normalizer
214+
config = Configuration.new(flaky_tests: ["./test/my_test.rb:ATest#test_foo", "./test/my_test.rb:BTest#test_bar"])
215+
config.test_id_normalizer = Module.new {
216+
define_method(:normalize) { |id| id.sub(%r{^\./}, "") }
217+
}.instance_method(:normalize)
218+
219+
test = Struct.new(:id).new("test/my_test.rb:ATest#test_foo")
220+
assert config.flaky?(test)
221+
222+
test = Struct.new(:id).new("test/my_test.rb:CTest#test_baz")
223+
refute config.flaky?(test)
224+
end
225+
226+
def test_flaky_with_method_normalizer
227+
config = Configuration.new(flaky_tests: ["./test/my_test.rb:ATest#test_foo", "./test/my_test.rb:BTest#test_bar"])
228+
normalizer_mod = Module.new do
229+
def self.normalize(id)
230+
id.sub(%r{^\./}, "")
231+
end
232+
end
233+
config.test_id_normalizer = normalizer_mod.method(:normalize)
234+
235+
test = Struct.new(:id).new("test/my_test.rb:ATest#test_foo")
236+
assert config.flaky?(test)
237+
238+
test = Struct.new(:id).new("test/my_test.rb:CTest#test_baz")
239+
refute config.flaky?(test)
240+
end
241+
242+
def test_flaky_normalizer_applied_to_both_sides
243+
config = Configuration.new(flaky_tests: [" ATest#test_foo ", "BTest#test_bar"])
244+
config.test_id_normalizer = Module.new {
245+
define_method(:normalize) { |id| id.strip }
246+
}.instance_method(:normalize)
247+
248+
test = Struct.new(:id).new("ATest#test_foo")
249+
assert config.flaky?(test)
250+
251+
test = Struct.new(:id).new(" ATest#test_foo ")
252+
assert config.flaky?(test)
253+
end
254+
203255
def test_heartbeat_max_test_duration_defaults
204256
# defaults to timeout*10 when heartbeat is enabled
205257
config = Configuration.new(timeout: 5, max_missed_heartbeat_seconds: 1)

0 commit comments

Comments
 (0)