2019-03-12 09:51:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
return unless Rails.env.test?
|
|
|
|
|
2017-03-07 12:07:13 +00:00
|
|
|
module RspecProfilingExt
|
2019-03-12 09:51:37 +00:00
|
|
|
module Collectors
|
|
|
|
class CSVWithTimestamps < ::RspecProfiling::Collectors::CSV
|
|
|
|
TIMESTAMP_FIELDS = %w(created_at updated_at).freeze
|
|
|
|
HEADERS = (::RspecProfiling::Collectors::CSV::HEADERS + TIMESTAMP_FIELDS).freeze
|
|
|
|
|
|
|
|
def insert(attributes)
|
|
|
|
output << HEADERS.map do |field|
|
|
|
|
if TIMESTAMP_FIELDS.include?(field)
|
|
|
|
Time.now
|
|
|
|
else
|
|
|
|
attributes.fetch(field.to_sym)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def output
|
|
|
|
@output ||= ::CSV.open(path, "w").tap { |csv| csv << HEADERS }
|
|
|
|
end
|
2017-03-07 12:07:13 +00:00
|
|
|
end
|
2017-01-26 21:31:43 +00:00
|
|
|
end
|
|
|
|
|
2017-03-07 12:07:13 +00:00
|
|
|
module Git
|
|
|
|
def branch
|
2017-03-24 18:57:27 +00:00
|
|
|
if ENV['CI_COMMIT_REF_NAME']
|
|
|
|
"#{defined?(Gitlab::License) ? 'ee' : 'ce'}:#{ENV['CI_COMMIT_REF_NAME']}"
|
|
|
|
else
|
2019-03-12 09:51:37 +00:00
|
|
|
super&.chomp
|
2017-03-24 18:57:27 +00:00
|
|
|
end
|
2017-03-07 12:07:13 +00:00
|
|
|
end
|
2019-03-12 09:51:37 +00:00
|
|
|
|
|
|
|
def sha
|
|
|
|
super&.chomp
|
|
|
|
end
|
2017-03-07 12:07:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module Run
|
|
|
|
def example_finished(*args)
|
|
|
|
super
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError => err
|
2017-11-22 07:50:36 +00:00
|
|
|
return if @already_logged_example_finished_error # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-03-07 12:07:13 +00:00
|
|
|
|
2020-01-27 21:08:47 +00:00
|
|
|
warn "rspec_profiling couldn't collect an example: #{err}. Further warnings suppressed."
|
2017-11-22 07:50:36 +00:00
|
|
|
@already_logged_example_finished_error = true # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-03-07 12:07:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :example_passed, :example_finished
|
|
|
|
alias_method :example_failed, :example_finished
|
2017-02-10 13:31:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-12 09:51:37 +00:00
|
|
|
RspecProfiling.configure do |config|
|
|
|
|
if ENV.key?('CI') || ENV.key?('RSPEC_PROFILING')
|
|
|
|
RspecProfiling::VCS::Git.prepend(RspecProfilingExt::Git)
|
|
|
|
RspecProfiling::Run.prepend(RspecProfilingExt::Run)
|
|
|
|
config.collector = RspecProfilingExt::Collectors::CSVWithTimestamps
|
2019-12-23 15:07:48 +00:00
|
|
|
config.csv_path = -> do
|
2021-07-14 18:08:31 +00:00
|
|
|
prefix = "#{ENV['CI_JOB_NAME']}-".gsub(%r{[ /]}, '-') if ENV['CI_JOB_NAME']
|
2019-12-23 15:07:48 +00:00
|
|
|
"rspec_profiling/#{prefix}#{Time.now.to_i}-#{SecureRandom.hex(8)}-rspec-data.csv"
|
|
|
|
end
|
2017-03-07 12:07:13 +00:00
|
|
|
end
|
2017-01-26 21:31:43 +00:00
|
|
|
end
|