From 77c2210f1ee8771bbf14fbee7ceeecde3c119411 Mon Sep 17 00:00:00 2001 From: Ming Guo Date: Mon, 9 Sep 2024 10:10:11 +1000 Subject: [PATCH] TAI-427: fix missing failed examples if rspec hooks fail --- .../test_collector/library_hooks/rspec.rb | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/buildkite/test_collector/library_hooks/rspec.rb b/lib/buildkite/test_collector/library_hooks/rspec.rb index dd7960b..fa84bcc 100644 --- a/lib/buildkite/test_collector/library_hooks/rspec.rb +++ b/lib/buildkite/test_collector/library_hooks/rspec.rb @@ -23,13 +23,20 @@ # The _buildkite prefix here is added as a safeguard against name collisions # as we are in the main thread Thread.current[:_buildkite_tracer] = tracer - example.run - Thread.current[:_buildkite_tracer] = nil - - tracer.finalize - - trace = Buildkite::TestCollector::RSpecPlugin::Trace.new(example, history: tracer.history) - Buildkite::TestCollector.uploader.traces[example.id] = trace + # It's important to use begin/ensure here, because otherwise if other hooks fail, + # the cleanup code won't run, meaning we will miss some data. + # + # Having said that, this behavior isn't documented by RSpec. + begin + example.run + ensure + Thread.current[:_buildkite_tracer] = nil + + tracer.finalize + + trace = Buildkite::TestCollector::RSpecPlugin::Trace.new(example, history: tracer.history) + Buildkite::TestCollector.uploader.traces[example.id] = trace + end end config.after(:suite) do