From 60a98776071aab9bcd9d9c1e0ee65727ff47dc4d Mon Sep 17 00:00:00 2001 From: ArturT Date: Sun, 1 Oct 2017 22:00:54 +0200 Subject: [PATCH] Fix tests for TestUnitAdapter --- .../adapters/test_unit_adapter.rb | 12 +- .../adapters/test_unit_adapter_spec.rb | 131 +++++------------- 2 files changed, 42 insertions(+), 101 deletions(-) diff --git a/lib/knapsack_pro/adapters/test_unit_adapter.rb b/lib/knapsack_pro/adapters/test_unit_adapter.rb index b0b4ed75..0ad2aac5 100644 --- a/lib/knapsack_pro/adapters/test_unit_adapter.rb +++ b/lib/knapsack_pro/adapters/test_unit_adapter.rb @@ -44,13 +44,13 @@ def run_shutdown(result) def bind_time_tracker Test::Unit::TestSuite.send(:prepend, BindTimeTrackerTestUnitPlugin) - Test::Unit.at_exit do + add_post_run_callback do KnapsackPro.logger.debug(KnapsackPro::Presenter.global_time) end end def bind_save_report - Test::Unit.at_exit do + add_post_run_callback do KnapsackPro::Report.save end end @@ -59,6 +59,14 @@ def set_test_helper_path(file_path) test_dir_path = File.dirname(file_path) @@parent_of_test_dir = File.expand_path('../', test_dir_path) end + + private + + def add_post_run_callback(&block) + Test::Unit.at_exit do + block.call + end + end end end end diff --git a/spec/knapsack_pro/adapters/test_unit_adapter_spec.rb b/spec/knapsack_pro/adapters/test_unit_adapter_spec.rb index c319e0e0..30eac436 100644 --- a/spec/knapsack_pro/adapters/test_unit_adapter_spec.rb +++ b/spec/knapsack_pro/adapters/test_unit_adapter_spec.rb @@ -1,94 +1,50 @@ +require 'test/unit/testcase' + describe KnapsackPro::Adapters::TestUnitAdapter do it do - expect(described_class::TEST_DIR_PATTERN).to eq 'spec/**{,/*/**}/*_spec.rb' - end - - context do - before { expect(::RSpec).to receive(:configure) } - it_behaves_like 'adapter' + expect(described_class::TEST_DIR_PATTERN).to eq 'test/**{,/*/**}/*_test.rb' end describe '.test_path' do - let(:current_example_metadata) do - { - file_path: '1_shared_example.rb', - parent_example_group: { - file_path: '2_shared_example.rb', - parent_example_group: { - file_path: 'a_spec.rb' - } - } - } - end + subject { described_class.test_path(obj) } - subject { described_class.test_path(current_example_metadata) } - - it { should eql 'a_spec.rb' } + before do + parent_of_test_dir = File.expand_path('../../../', File.dirname(__FILE__)) + parent_of_test_dir_regexp = Regexp.new("^#{parent_of_test_dir}") + described_class.class_variable_set(:@@parent_of_test_dir, parent_of_test_dir_regexp) + end - context 'with turnip features' do - describe 'when the turnip version is less than 2' do - let(:current_example_metadata) do - { - file_path: "./spec/features/logging_in.feature", - turnip: true, - parent_example_group: { - file_path: "gems/turnip-1.2.4/lib/turnip/rspec.rb" - } - } + context 'when regular test' do + class FakeTestUnitTest + def method_name + "test_something" end - before { stub_const("Turnip::VERSION", '1.2.4') } - - it { should eql './spec/features/logging_in.feature' } + def test_something + end end - describe 'when turnip is version 2 or greater' do - let(:current_example_metadata) do - { - file_path: "gems/turnip-2.0.0/lib/turnip/rspec.rb", - turnip: true, - parent_example_group: { - file_path: "./spec/features/logging_in.feature", - } - } + class FakeTestUnitTestSuite + def tests + [FakeTestUnitTest.new] end + end - before { stub_const("Turnip::VERSION", '2.0.0') } + let(:obj) { FakeTestUnitTestSuite.new } - it { should eql './spec/features/logging_in.feature' } - end + it { should eq './spec/knapsack_pro/adapters/test_unit_adapter_spec.rb' } end end describe 'bind methods' do - let(:config) { double } - describe '#bind_time_tracker' do - let(:tracker) { instance_double(KnapsackPro::Tracker) } let(:logger) { instance_double(Logger) } - let(:test_path) { 'spec/a_spec.rb' } let(:global_time) { 'Global time: 01m 05s' } - let(:example_group) { double } - let(:current_example) do - OpenStruct.new(metadata: { - example_group: example_group - }) - end it do - expect(config).to receive(:prepend_before).with(:each).and_yield - expect(config).to receive(:append_after).with(:each).and_yield - expect(config).to receive(:after).with(:suite).and_yield - expect(::RSpec).to receive(:configure).and_yield(config) - - expect(::RSpec).to receive(:current_example).twice.and_return(current_example) - expect(described_class).to receive(:test_path).with(example_group).and_return(test_path) - - allow(KnapsackPro).to receive(:tracker).and_return(tracker) - expect(tracker).to receive(:current_test_path=).with(test_path) - expect(tracker).to receive(:start_timer) + expect(Test::Unit::TestSuite).to receive(:send).with(:prepend, KnapsackPro::Adapters::TestUnitAdapter::BindTimeTrackerTestUnitPlugin) - expect(tracker).to receive(:stop_timer) + expect(subject).to receive(:add_post_run_callback).and_yield expect(KnapsackPro::Presenter).to receive(:global_time).and_return(global_time) expect(KnapsackPro).to receive(:logger).and_return(logger) @@ -100,48 +56,25 @@ describe '#bind_save_report' do it do - expect(config).to receive(:after).with(:suite).and_yield - expect(::RSpec).to receive(:configure).and_yield(config) + expect(subject).to receive(:add_post_run_callback).and_yield expect(KnapsackPro::Report).to receive(:save) subject.bind_save_report end end + end - describe '#bind_save_queue_report' do - it do - expect(config).to receive(:after).with(:suite).and_yield - expect(::RSpec).to receive(:configure).and_yield(config) - - expect(KnapsackPro::Report).to receive(:save_subset_queue_to_file) - - subject.bind_save_queue_report - end - end - - describe '#bind_tracker_reset' do - it do - expect(config).to receive(:before).with(:suite).and_yield - expect(::RSpec).to receive(:configure).and_yield(config) + describe '#set_test_helper_path' do + let(:adapter) { described_class.new } + let(:test_helper_path) { '/code/project/test/test_helper.rb' } - tracker = instance_double(KnapsackPro::Tracker) - expect(KnapsackPro).to receive(:tracker).and_return(tracker) - expect(tracker).to receive(:reset!) + subject { adapter.set_test_helper_path(test_helper_path) } - subject.bind_tracker_reset - end + after do + expect(described_class.class_variable_get(:@@parent_of_test_dir)).to eq '/code/project' end - describe '#bind_before_queue_hook' do - it do - expect(config).to receive(:before).with(:suite).and_yield - expect(::RSpec).to receive(:configure).and_yield(config) - - expect(KnapsackPro::Hooks::Queue).to receive(:call_before_queue) - - subject.bind_before_queue_hook - end - end + it { should eql '/code/project' } end end