How to use JSON formatter for RSpec?
How to use RSpec JSON formatter with knapsack_pro Queue Mode?
You need to specify format
and out
argument (it's important to provide both).
# Queue Mode
bundle exec rake "knapsack_pro:queue:rspec[--format documentation --format json --out tmp/rspec.json]"
The JSON report will contain all tests executed across intermediate test subset runs based on work queue. You need to add after subset queue hook to rename rspec.json
to rspec_final_results.json
thanks to that the final results file will contain valid json with all tests executed on the CI node. This is related to the way how Queue Mode works. Detailed explanation is in the issue.
# spec_helper.rb or rails_helper.rb
# TODO This must be the same path as value for rspec --out argument
# Note the path should not contain ~ char, for instance path ~/project/tmp/rspec.json may not work. Please use full path instead.
TMP_RSPEC_JSON_REPORT = 'tmp/rspec.json'
# move results to FINAL_RSPEC_JSON_REPORT so the results won't accumulate with duplicated JSON in TMP_RSPEC_JSON_REPORT
FINAL_RSPEC_JSON_REPORT = 'tmp/rspec_final_results.json'
KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
if File.exist?(TMP_RSPEC_JSON_REPORT)
FileUtils.mv(TMP_RSPEC_JSON_REPORT, FINAL_RSPEC_JSON_REPORT)
end
end
How to use RSpec JSON formatter with knapsack_pro Queue Mode when CI nodes use common local drive?
Note if you use a CI provider or your own CI solution that uses common local drive for all parallel CI nodes then above solution needs to be adjusted to produce report file with CI node index number in the file name to avoid file conflicts. Example file name with CI node index number: tmp/rspec_final_results_N.json
.
# Queue Mode
# must be exported to read correctly the value in below knapsack_pro command
export KNAPSACK_PRO_CI_NODE_INDEX=0
# if your CI provider exposes CI node index under other environment variable name then you could use it instead
bundle exec rake "knapsack_pro:queue:rspec[--format documentation --format json --out tmp/rspec_$KNAPSACK_PRO_CI_NODE_INDEX.json]"
In below code we use CI node index number in TMP_RSPEC_JSON_REPORT
and FINAL_RSPEC_JSON_REPORT
:
# spec_helper.rb or rails_helper.rb
# TODO This must be the same path as value for rspec --out argument
# Note the path should not contain ~ char, for instance path ~/project/tmp/rspec.json may not work. Please use full path instead.
TMP_RSPEC_JSON_REPORT = "tmp/rspec_#{ENV['KNAPSACK_PRO_CI_NODE_INDEX']}.json"
# move results to FINAL_RSPEC_JSON_REPORT so the results won't accumulate with duplicated JSON in TMP_RSPEC_JSON_REPORT
FINAL_RSPEC_JSON_REPORT = "tmp/rspec_final_results_#{ENV['KNAPSACK_PRO_CI_NODE_INDEX']}.json"
KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
if File.exist?(TMP_RSPEC_JSON_REPORT)
FileUtils.mv(TMP_RSPEC_JSON_REPORT, FINAL_RSPEC_JSON_REPORT)
end
end