2019-03-12 05:51:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
return unless Rails.env.test?
|
|
|
|
|
2017-03-07 07:07:13 -05:00
|
|
|
module RspecProfilingExt
|
2019-03-12 05:51:37 -04: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 07:07:13 -05:00
|
|
|
end
|
2017-01-26 16:31:43 -05:00
|
|
|
end
|
|
|
|
|
2017-03-07 07:07:13 -05:00
|
|
|
module Git
|
|
|
|
def branch
|
2017-03-24 14:57:27 -04:00
|
|
|
if ENV['CI_COMMIT_REF_NAME']
|
|
|
|
"#{defined?(Gitlab::License) ? 'ee' : 'ce'}:#{ENV['CI_COMMIT_REF_NAME']}"
|
|
|
|
else
|
2019-03-12 05:51:37 -04:00
|
|
|
super&.chomp
|
2017-03-24 14:57:27 -04:00
|
|
|
end
|
2017-03-07 07:07:13 -05:00
|
|
|
end
|
2019-03-12 05:51:37 -04:00
|
|
|
|
|
|
|
def sha
|
|
|
|
super&.chomp
|
|
|
|
end
|
2017-03-07 07:07:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module Run
|
|
|
|
def example_finished(*args)
|
|
|
|
super
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => err
|
2017-11-22 02:50:36 -05:00
|
|
|
return if @already_logged_example_finished_error # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-03-07 07:07:13 -05:00
|
|
|
|
2020-01-27 16:08:47 -05:00
|
|
|
warn "rspec_profiling couldn't collect an example: #{err}. Further warnings suppressed."
|
2017-11-22 02:50:36 -05:00
|
|
|
@already_logged_example_finished_error = true # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2017-03-07 07:07:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :example_passed, :example_finished
|
|
|
|
alias_method :example_failed, :example_finished
|
2017-02-10 08:31:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-12 05:51:37 -04: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 10:07:48 -05:00
|
|
|
config.csv_path = -> do
|
2021-07-14 14:08:31 -04:00
|
|
|
prefix = "#{ENV['CI_JOB_NAME']}-".gsub(%r{[ /]}, '-') if ENV['CI_JOB_NAME']
|
2022-01-25 13:11:55 -05:00
|
|
|
"#{ENV['RSPEC_PROFILING_FOLDER_PATH']}/#{prefix}#{Time.now.to_i}-#{SecureRandom.hex(8)}-rspec-data.csv"
|
2019-12-23 10:07:48 -05:00
|
|
|
end
|
2017-03-07 07:07:13 -05:00
|
|
|
end
|
2017-01-26 16:31:43 -05:00
|
|
|
end
|