Skip to content

Commit

Permalink
Fix tests for TestUnitAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturT committed Oct 2, 2017
1 parent 305918a commit 60a9877
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 101 deletions.
12 changes: 10 additions & 2 deletions lib/knapsack_pro/adapters/test_unit_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
131 changes: 32 additions & 99 deletions spec/knapsack_pro/adapters/test_unit_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

0 comments on commit 60a9877

Please sign in to comment.