Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom formatter KnapsackPro::Formatters::RSpecJsonFormatter to add support for RSpec example.id in JSON report to be able to split test files by test examples in RSpec older than 3.6.0 #104

Merged
merged 2 commits into from
Apr 9, 2020
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
@@ -1,5 +1,13 @@
# Change Log

### 1.20.0

* Add support for tests split by test examples to RSpec older than 3.6.0

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

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v1.19.0...v1.20.0

### 1.19.0

* RSpec split test files by test examples (by individual `it`s)
Expand Down
20 changes: 20 additions & 0 deletions lib/knapsack_pro/formatters/rspec_json_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
RSpec::Support.require_rspec_core('formatters/json_formatter')

# based on https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/formatters/json_formatter.rb
module KnapsackPro
module Formatters
class RSpecJsonFormatter < RSpec::Core::Formatters::JsonFormatter
RSpec::Core::Formatters.register self, :message, :dump_summary, :dump_profile, :stop, :seed, :close

private

# add example.id to json report to support < RSpec 3.6.0
# based on https://github.com/rspec/rspec-core/pull/2369/files
def format_example(example)
super.merge({
:id => example.id,
})
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ class RSpecTestExampleDetector
def generate_json_report
require 'rspec/core'

cli_format =
if Gem::Version.new(RSpec::Core::Version::STRING) < Gem::Version.new('3.6.0')
require_relative '../formatters/rspec_json_formatter'
['--format', KnapsackPro::Formatters::RSpecJsonFormatter.to_s]
else
['--format', 'json']
end

ensure_report_dir_exists
remove_old_json_report

test_file_paths = KnapsackPro::TestFileFinder.call(test_file_pattern)

cli_args = [
cli_args = cli_format + [
'--dry-run',
'--format', 'json',
'--out', REPORT_PATH,
'--default-path', test_dir,
] + test_file_paths.map { |t| t.fetch('path') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

options = double
expect(RSpec::Core::ConfigurationOptions).to receive(:new).with([
'--format', expected_format,
'--dry-run',
'--format', 'json',
'--out', report_path,
'--default-path', test_dir,
'spec/a_spec.rb', 'spec/b_spec.rb',
Expand All @@ -39,20 +39,38 @@
expect(rspec_core_runner).to receive(:run).with($stderr, $stdout).and_return(exit_code)
end

context 'when exit code from RSpec::Core::Runner is 0' do
let(:exit_code) { 0 }
shared_examples 'generate_json_report runs RSpec::Core::Runner' do
context 'when exit code from RSpec::Core::Runner is 0' do
let(:exit_code) { 0 }

it do
expect(subject).to be_nil
it do
expect(subject).to be_nil
end
end

context 'when exit code from RSpec::Core::Runner is 1' do
let(:exit_code) { 1 }

it do
expect { subject }.to raise_error(RuntimeError, 'There was problem to generate test examples for test suite')
end
end
end

context 'when exit code from RSpec::Core::Runner is 1' do
let(:exit_code) { 1 }
context 'when RSpec >= 3.6.0' do
let(:expected_format) { 'json' }

it do
expect { subject }.to raise_error(RuntimeError, 'There was problem to generate test examples for test suite')
it_behaves_like 'generate_json_report runs RSpec::Core::Runner'
end

context 'when RSpec < 3.6.0' do
let(:expected_format) { 'KnapsackPro::Formatters::RSpecJsonFormatter' }

before do
stub_const('RSpec::Core::Version::STRING', '3.5.0')
end

it_behaves_like 'generate_json_report runs RSpec::Core::Runner'
end
end

Expand Down