From c2a1bc9cad6f0fcdd996a271ba1f52201974fcc3 Mon Sep 17 00:00:00 2001 From: Artur Trzop Date: Thu, 9 Apr 2020 16:09:46 +0200 Subject: [PATCH 1/2] Add custom formater 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 --- .../formatters/rspec_json_formatter.rb | 20 +++++++++++ .../rspec_test_example_detector.rb | 11 ++++-- .../rspec_test_example_detector_spec.rb | 36 ++++++++++++++----- 3 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 lib/knapsack_pro/formatters/rspec_json_formatter.rb diff --git a/lib/knapsack_pro/formatters/rspec_json_formatter.rb b/lib/knapsack_pro/formatters/rspec_json_formatter.rb new file mode 100644 index 00000000..94a1751e --- /dev/null +++ b/lib/knapsack_pro/formatters/rspec_json_formatter.rb @@ -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 diff --git a/lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb b/lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb index 620ffd50..dcd19964 100644 --- a/lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb +++ b/lib/knapsack_pro/test_case_detectors/rspec_test_example_detector.rb @@ -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') } diff --git a/spec/knapsack_pro/test_case_detectors/rspec_test_example_detector_spec.rb b/spec/knapsack_pro/test_case_detectors/rspec_test_example_detector_spec.rb index 02b75222..0154db00 100644 --- a/spec/knapsack_pro/test_case_detectors/rspec_test_example_detector_spec.rb +++ b/spec/knapsack_pro/test_case_detectors/rspec_test_example_detector_spec.rb @@ -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', @@ -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 From dc67c1d10d2be5603ccd080249ff6abcb80095cd Mon Sep 17 00:00:00 2001 From: Artur Trzop Date: Thu, 9 Apr 2020 16:16:18 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b9d81fb..fd1fbf84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)