Skip to content

Commit

Permalink
Use knapsack_pro 7.0.0 and adjust codebase and config to refactored q…
Browse files Browse the repository at this point in the history
…ueue rspec runner (#56)


---------

Co-authored-by: Riccardo <[email protected]>
  • Loading branch information
ArturT and 3v0k4 authored Feb 23, 2024
1 parent 18c2f0c commit 8f95bc3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../knapsack_pro-ruby
specs:
knapsack_pro (6.0.4)
knapsack_pro (7.0.0)
rake

GEM
Expand Down
41 changes: 28 additions & 13 deletions bin/edge_cases/knapsack_pro_queue_rspec_record_first_run
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ BRANCH_NAME=fake-branch
#export KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=true
#export KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN="spec/**{,/*/**}/*_spec.rb"

# In order to mock the Queue API responses, you need to change source code of the knapsack_pro gem:
# https://github.com/KnapsackPro/rails-app-with-knapsack_pro/pull/56/
# uncomment all 3 export lines to test mocked API response
export KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=true
export KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN="spec/features/calculator_spec.rb" # it must match test cases returned from Queue API
export MOCK_QUEUE_API_RESPONSE=true

KNAPSACK_PRO_ENDPOINT=http://api.knapsackpro.test:3000 \
KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=fec3c641a3c4d2e720fe1b6d9dd780bc \
KNAPSACK_PRO_CI_NODE_BUILD_ID=$CI_BUILD_ID \
Expand All @@ -36,17 +43,25 @@ KNAPSACK_PRO_ENDPOINT=http://api.knapsackpro.test:3000 \
KNAPSACK_PRO_CI_NODE_INDEX=${1:-0} \
bundle exec rake "knapsack_pro:queue:rspec[--format d]"

echo
echo
echo
echo
# Example options you can pass to RSpec:
#bundle exec rake "knapsack_pro:queue:rspec[--format d --tag my_tag]"
#bundle exec rake "knapsack_pro:queue:rspec[--format d --tag my_skip_tag]"
#bundle exec rake "knapsack_pro:queue:rspec[--format d --profile]"
#bundle exec rake "knapsack_pro:queue:rspec[--format d --order random]"
#
# --failure-exit-code CODE - Override the exit code used when there are failing specs.
#bundle exec rake "knapsack_pro:queue:rspec[--format d --failure-exit-code 2]"
#
# --error-exit-code CODE - Override the exit code used when there are errors loading or running specs outside of examples.
#bundle exec rake "knapsack_pro:queue:rspec[--format d --error-exit-code 3]"

# Run 2nd CI node to ensure the CI build is finished in the user dashboard
KNAPSACK_PRO_ENDPOINT=http://api.knapsackpro.test:3000 \
KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=fec3c641a3c4d2e720fe1b6d9dd780bc \
KNAPSACK_PRO_CI_NODE_BUILD_ID=$CI_BUILD_ID \
KNAPSACK_PRO_COMMIT_HASH=${3:-$COMMIT_HASH} \
KNAPSACK_PRO_BRANCH=$BRANCH_NAME \
KNAPSACK_PRO_CI_NODE_TOTAL=${2:-2} \
KNAPSACK_PRO_CI_NODE_INDEX=1 \
bundle exec rake "knapsack_pro:queue:rspec[--format d]"

# Uncomment the following to run 2nd CI node to ensure the CI build is finished in the user dashboard
#KNAPSACK_PRO_ENDPOINT=http://api.knapsackpro.test:3000 \
#KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=fec3c641a3c4d2e720fe1b6d9dd780bc \
#KNAPSACK_PRO_CI_NODE_BUILD_ID=$CI_BUILD_ID \
#KNAPSACK_PRO_COMMIT_HASH=${3:-$COMMIT_HASH} \
#KNAPSACK_PRO_BRANCH=$BRANCH_NAME \
#KNAPSACK_PRO_CI_NODE_TOTAL=${2:-2} \
#KNAPSACK_PRO_CI_NODE_INDEX=1 \
#bundle exec rake "knapsack_pro:queue:rspec[--format d]"
63 changes: 0 additions & 63 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,6 @@
SimpleCov.start


before_suite_block = proc {
puts '+'*100
puts 'RSpec before suite hook called only once'
}

unless KnapsackPro::Config::Env.queue_recording_enabled?
RSpec.configure do |config|
config.before(:suite) do
before_suite_block.call
puts 'Run this only when not using Knapsack Pro Queue Mode'
end
end
end

KnapsackPro::Hooks::Queue.before_queue do |queue_id|
before_suite_block.call
puts 'Run this only when using Knapsack Pro Queue Mode'
puts 'This code is executed within the context of RSpec before(:suite) hook'
end


after_suite_block = proc {
puts '+'*100
puts 'after suite hook called only once'
}

unless KnapsackPro::Config::Env.queue_recording_enabled?
RSpec.configure do |config|
config.after(:suite) do
after_suite_block.call
puts 'Run this only when not using Knapsack Pro Queue Mode'
end
end
end

KnapsackPro::Hooks::Queue.after_queue do |queue_id|
after_suite_block.call
puts 'Run this only when using Knapsack Pro Queue Mode'
puts "This code is executed outside of the RSpec after(:suite) hook context because it's impossible to determine which after(:suite) is the last one to execute until it's executed."
end


# CUSTOM_CONFIG_GOES_HERE
KnapsackPro::Hooks::Queue.before_queue do |queue_id|
print '-'*10
Expand All @@ -96,22 +54,7 @@
puts '2nd KnapsackPro::Hooks::Queue.before_subset_queue'
end

# TODO This must be the same path as value for rspec --out argument
TMP_RSPEC_XML_REPORT = "tmp/test-reports/rspec/queue_mode/rspec_#{KnapsackPro::Config::Env.ci_node_index}.xml"
TMP_RSPEC_JSON_REPORT = "tmp/test-reports/rspec/queue_mode/rspec_#{KnapsackPro::Config::Env.ci_node_index}.json"
# move results to FINAL_RSPEC_XML_REPORT so the results won't accumulate with duplicated xml tags in TMP_RSPEC_XML_REPORT
FINAL_RSPEC_XML_REPORT = "tmp/test-reports/rspec/queue_mode/rspec_final_results_#{KnapsackPro::Config::Env.ci_node_index}.xml"
FINAL_RSPEC_JSON_REPORT = "tmp/test-reports/rspec/queue_mode/rspec_final_results_#{KnapsackPro::Config::Env.ci_node_index}.json"

KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
if File.exist?(TMP_RSPEC_XML_REPORT)
FileUtils.mv(TMP_RSPEC_XML_REPORT, FINAL_RSPEC_XML_REPORT)
end

if File.exist?(TMP_RSPEC_JSON_REPORT)
FileUtils.mv(TMP_RSPEC_JSON_REPORT, FINAL_RSPEC_JSON_REPORT)
end

print '-'*10
print 'After Subset Queue Hook - run after the subset of the test suite'
print '-'*10
Expand All @@ -122,12 +65,6 @@
end

KnapsackPro::Hooks::Queue.after_queue do |queue_id|
# Metadata collection
# https://circleci.com/docs/1.0/test-metadata/#metadata-collection-in-custom-test-steps
if File.exist?(FINAL_RSPEC_XML_REPORT) && ENV['CIRCLE_TEST_REPORTS']
FileUtils.cp(FINAL_RSPEC_XML_REPORT, "#{ENV['CIRCLE_TEST_REPORTS']}/rspec.xml")
end

print '-'*10
print 'After Queue Hook - run after test suite'
print '-'*10
Expand Down
6 changes: 3 additions & 3 deletions spec/support/config/rspec_retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
config.verbose_retry = true
# show exception that triggers a retry if verbose_retry is set to true
config.display_try_failure_messages = true

# It's the RSpec option. When too many tests fail then the Knapsack Pro is terminated
# config.fail_fast = 3
# --fail-fast option makes RSpec fail after X failed tests. This can lead to canceled tests in Queue Mode.
# config.fail_fast = 1

# run retry only on features
config.around :each, :js do |ex|
Expand Down

0 comments on commit 8f95bc3

Please sign in to comment.