-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathspec_helper.rb
256 lines (220 loc) · 9.22 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
104
105
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
require 'async/rspec'
require 'knapsack_pro'
#KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
#puts '\\'*100
#puts 'Executed tests for a batch of tests fetched from Queue API'
#Dir.glob(".knapsack_pro/queue/#{queue_id}/*.json").each do |file|
#report = JSON.parse(File.read(file))
#puts report.inspect
#end
#puts '/'*100
#end
#KnapsackPro::Hooks::Queue.after_queue do |queue_id|
#puts '\\'*100
#puts 'Summary of all executed tests'
#Dir.glob(".knapsack_pro/queue/#{queue_id}/*.json").each do |file|
#report = JSON.parse(File.read(file))
#puts report.inspect
#end
#puts '/'*100
#end
# uncomment if you want to test how custom logger works
#require 'logger'
#KnapsackPro.logger = Logger.new(STDOUT)
#KnapsackPro.logger.level = Logger::INFO
require 'simplecov'
if ENV['CI']
require 'simplecov_json_formatter'
SimpleCov.formatter = SimpleCov::Formatter::JSONFormatter
end
SimpleCov.start
# CUSTOM_CONFIG_GOES_HERE
KnapsackPro::Hooks::Queue.before_queue do |queue_id|
print '-'*10
print 'Before Queue Hook - run before the test suite'
print '-'*10
SimpleCov.command_name("rspec_ci_node_#{KnapsackPro::Config::Env.ci_node_index}")
end
KnapsackPro::Hooks::Queue.before_queue do |queue_id|
puts '2nd KnapsackPro::Hooks::Queue.before_queue'
end
KnapsackPro::Hooks::Queue.before_subset_queue do |queue_id, subset_queue_id|
print '-'*10
print 'Before Subset Queue Hook - run before the next subset of tests'
print '-'*10
end
KnapsackPro::Hooks::Queue.before_subset_queue do |queue_id, subset_queue_id|
puts '2nd KnapsackPro::Hooks::Queue.before_subset_queue'
end
KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
print '-'*10
print 'After Subset Queue Hook - run after the previous subset of tests'
print '-'*10
end
KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
puts '2nd KnapsackPro::Hooks::Queue.after_subset_queue'
end
KnapsackPro::Hooks::Queue.after_queue do |queue_id|
print '-'*10
print 'After Queue Hook - run after the test suite'
print '-'*10
end
KnapsackPro::Hooks::Queue.after_queue do |queue_id|
puts '2nd KnapsackPro::Hooks::Queue.after_queue'
end
KnapsackPro::Hooks::Queue.after_queue do |queue_id|
THE_SLOWEST_TEST_FILE_TIME_EXECUTION_LIMIT = 3 # in seconds
# all recorded test files by knapsack_pro gem
test_files = []
Dir.glob(".knapsack_pro/queue/#{queue_id}/*.json").each do |file|
report = JSON.parse(File.read(file))
test_files += report
end
slowest_test_file = test_files.max_by do |test_file|
test_file['time_execution']
end
if slowest_test_file && slowest_test_file['time_execution'].to_f > THE_SLOWEST_TEST_FILE_TIME_EXECUTION_LIMIT
puts '!'*50
puts "The slowest test file took #{slowest_test_file['time_execution']} seconds and exceeded allowed max limit: #{THE_SLOWEST_TEST_FILE_TIME_EXECUTION_LIMIT} seconds. File path: #{slowest_test_file['path']}"
puts '!'*50
File.open('tmp/slowest_test_file_exceeded_allowed_limit.txt', 'w+') do |f|
f.write(slowest_test_file.to_json)
end
end
end
KnapsackPro::Adapters::RSpecAdapter.bind
if ENV['KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES']
puts '+'*100
if ENV['CUSTOM_VARIABLE_FOR_RSPEC_TEST_EXAMPLE_DETECTOR']
# use the following bin script to test this scenario:
# bin/knapsack_pro_queue_rspec_split_by_test_examples_test_example_detector_prefix
# test following env var should be set only when running RSpec in dry run by knapsack_pro:rspec_test_example_detector rake task to generate test examples JSON report
puts "CUSTOM_VARIABLE_FOR_RSPEC_TEST_EXAMPLE_DETECTOR=#{ENV['CUSTOM_VARIABLE_FOR_RSPEC_TEST_EXAMPLE_DETECTOR']}"
else
# use the following bin script to test this scenario:
# bin/knapsack_pro_queue_rspec_split_by_test_examples
puts "CUSTOM_VARIABLE_FOR_RSPEC_TEST_EXAMPLE_DETECTOR is not set"
end
end
RSpec.configure do |config|
config.around(:each) do |example|
#sleep 1 # time tracked
puts 'around each start'
example.run
puts 'around each stop'
end
config.before(:suite) do
#sleep 1 # time not tracked
puts "before suite"
end
config.before(:all) do
#sleep 1 # time not tracked
puts "before all"
end
config.before(:each) do
#sleep 1 # time tracked
puts "before each"
end
config.after(:each) do
puts "after each"
end
config.after(:all) do
puts "after all"
end
config.after(:suite) do
puts "after suite"
end
end
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
# Uncomment for testing purposes
# https://knapsackpro.com/faq/question/rspec-is-not-running-some-tests
# https://knapsackpro.com/faq/question/how-to-split-slow-rspec-test-files-by-test-examples-by-individual-it#warning-dont-use-deprecated-rspec-run_all_when_everything_filtered-option
#config.filter_run_when_matching :focus
#config.filter_run :focus
#config.run_all_when_everything_filtered = true
#config.filter_run_including :focus => true
#config.filter_run_including :focus2 => true
# unless ENV['CI']
# config.filter_run_when_matching :focus
# end
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
# This is deprecated, DO NOT USE IT
# https://knapsackpro.com/faq/question/how-to-split-slow-rspec-test-files-by-test-examples-by-individual-it#warning-dont-use-deprecated-rspec-run_all_when_everything_filtered-option
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
config.disable_monkey_patching!
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end