Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

* TODO

### 0.41.0

* Add after subset queue hook and example how to use JUnit formatter in Queue Mode.

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/41

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v0.40.0...v0.41.0

### 0.40.0

* Replace rake task installer `knapsack_pro:install` with online installation guide. Remove `tty-prompt` gem dependency.
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ The knapsack_pro has also [queue mode](#queue-mode) to get optimal test suite sp
- [B. Use tags to mark set of tests in particular test file](#b-use-tags-to-mark-set-of-tests-in-particular-test-file)
- [How to make knapsack_pro works for forked repositories of my project?](#how-to-make-knapsack_pro-works-for-forked-repositories-of-my-project)
- [How to use junit formatter?](#how-to-use-junit-formatter)
- [How to use junit formatter with knapsack_pro regular mode?](#how-to-use-junit-formatter-with-knapsack_pro-regular-mode)
- [How to use junit formatter with knapsack_pro queue mode?](#how-to-use-junit-formatter-with-knapsack_pro-queue-mode)
- [How many API keys I need?](#how-many-api-keys-i-need)
- [What is optimal order of test commands?](#what-is-optimal-order-of-test-commands)
- [How to set `before(:suite)` and `after(:suite)` RSpec hooks in Queue Mode (Percy.io example)?](#how-to-set-beforesuite-and-aftersuite-rspec-hooks-in-queue-mode-percyio-example)
Expand Down Expand Up @@ -1075,16 +1077,32 @@ Remember to follow other steps required for your CI provider.

#### How to use junit formatter?

##### How to use junit formatter with knapsack_pro regular mode?

You can use junit formatter for rspec thanks to gem [rspec_junit_formatter](https://github.com/sj26/rspec_junit_formatter).
Here you can find example how to generate `rspec.xml` file with junit format and at the same time show normal documentation format output for RSpec.

# Regular Mode
bundle exec rake "knapsack_pro:rspec[--format documentation --format RspecJunitFormatter --out tmp/rspec.xml]"

##### How to use junit formatter with knapsack_pro queue mode?

You can use junit formatter for rspec thanks to gem [rspec_junit_formatter](https://github.com/sj26/rspec_junit_formatter).

# Queue Mode
# The xml report will contain all tests executed across intermediate test subset runs based on queue
bundle exec rake "knapsack_pro:queue:rspec[--format documentation --format RspecJunitFormatter --out tmp/rspec.xml]"

The xml 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.xml` to `rspec_final_results.xml` thanks to that the final results file will contain only single xml tag with all tests executed on the CI node. This is related to the way how queue mode works. Detailed explanation is in the [issue](https://github.com/KnapsackPro/knapsack_pro-ruby/issues/40).

# spec_helper.rb or rails_helper.rb
KnapsackPro::Hooks::Queue.after_subset_queue do |queue_id, subset_queue_id|
# TODO This must be the same path as value for rspec --out argument
old_xml_file = 'tmp/rspec.xml'
# move results to new_xml_file so the results won't accumulate with duplicated xml tags in old_xml_file
new_xml_file = 'tmp/rspec_final_results.xml'
FileUtils.mv(old_xml_file, new_xml_file)
end

#### How many API keys I need?

Basically you need as many API keys as you have steps in your build.
Expand Down
1 change: 1 addition & 0 deletions lib/knapsack_pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'securerandom'
require_relative 'knapsack_pro/version'
require_relative 'knapsack_pro/extensions/time'
require_relative 'knapsack_pro/hooks/queue'
require_relative 'knapsack_pro/utils'
require_relative 'knapsack_pro/logger_wrapper'
require_relative 'knapsack_pro/config/ci/base'
Expand Down
25 changes: 25 additions & 0 deletions lib/knapsack_pro/hooks/queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module KnapsackPro
module Hooks
class Queue
class << self
attr_reader :after_subset_queue

def reset_after_subset_queue
@after_subset_queue = nil
end

def after_subset_queue(&block)
@after_subset_queue ||= block
end

def call_after_subset_queue
return unless after_subset_queue
after_subset_queue.call(
KnapsackPro::Config::Env.queue_id,
KnapsackPro::Config::Env.subset_queue_id
)
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/knapsack_pro/runners/queue/rspec_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def self.run_tests(runner, can_initialize_queue, args, exitstatus, all_test_file
exitstatus = exit_code if exit_code != 0
RSpec.world.example_groups.clear

KnapsackPro::Hooks::Queue.call_after_subset_queue

run_tests(runner, false, args, exitstatus, all_test_file_paths)
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/knapsack_pro/hooks/queue_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe KnapsackPro::Hooks::Queue do
describe '.call_after_subset_queue' do
subject { described_class.call_after_subset_queue }

context 'when callback is not set' do
before do
described_class.reset_after_subset_queue
end

it { should be_nil }
end

context 'when callback is set' do
let(:queue_id) { double }
let(:subset_queue_id) { double }

before do
expect(KnapsackPro::Config::Env).to receive(:queue_id).and_return(queue_id)
expect(KnapsackPro::Config::Env).to receive(:subset_queue_id).and_return(subset_queue_id)

described_class.after_subset_queue do |q_id, subset_q_id|
[:fake_value, q_id, subset_q_id]
end
end

it { should eq [:fake_value, queue_id, subset_queue_id] }
end
end
end
2 changes: 2 additions & 0 deletions spec/knapsack_pro/runners/queue/rspec_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@

expect(RSpec).to receive_message_chain(:world, :example_groups, :clear)

expect(KnapsackPro::Hooks::Queue).to receive(:call_after_subset_queue)

# second call of run_tests because of recursion
expect(runner).to receive(:test_file_paths).with(can_initialize_queue: false).and_return([])
end
Expand Down