diff --git a/CHANGELOG.md b/CHANGELOG.md index b32d24d4..0e759658 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ * TODO +### 0.51.0 + +* Add support for test-unit test runner. + + https://github.com/KnapsackPro/knapsack_pro-ruby/pull/53 + +https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v0.50.1...v0.51.0 + ### 0.50.1 * Update RSpec timing adapter to be more resilient. diff --git a/README.md b/README.md index c8ae17d6..9fd23755 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ The knapsack_pro gem supports: * [RSpec](http://rspec.info) * [Cucumber](https://cucumber.io) * [Minitest](http://docs.seattlerb.org/minitest/) +* [test-unit](https://github.com/test-unit/test-unit) * [Spinach](https://github.com/codegram/spinach) * [Turnip](https://github.com/jnicklas/turnip) @@ -62,6 +63,7 @@ The knapsack_pro has also [queue mode](#queue-mode) to get an optimal test suite - [Step for RSpec](#step-for-rspec) - [Step for Cucumber](#step-for-cucumber) - [Step for Minitest](#step-for-minitest) + - [Step for test-unit](#step-for-test-unit) - [Step for Spinach](#step-for-spinach) - [Custom configuration](#custom-configuration) - [Setup your CI server (How to set up 2 of 3)](#setup-your-ci-server-how-to-set-up-2-of-3) @@ -86,6 +88,7 @@ The knapsack_pro has also [queue mode](#queue-mode) to get an optimal test suite - [Passing arguments to rspec](#passing-arguments-to-rspec) - [Passing arguments to cucumber](#passing-arguments-to-cucumber) - [Passing arguments to minitest](#passing-arguments-to-minitest) + - [Passing arguments to test-unit](#passing-arguments-to-test-unit) - [Passing arguments to spinach](#passing-arguments-to-spinach) - [Knapsack Pro binary](#knapsack-pro-binary) - [Test file names encryption](#test-file-names-encryption) @@ -260,6 +263,19 @@ knapsack_pro_adapter = KnapsackPro::Adapters::MinitestAdapter.bind knapsack_pro_adapter.set_test_helper_path(__FILE__) ``` +#### Step for test-unit + +Add at the beginning of your `test_helper.rb`: + +```ruby +require 'knapsack_pro' + +# CUSTOM_CONFIG_GOES_HERE + +knapsack_pro_adapter = KnapsackPro::Adapters::TestUnitAdapter.bind +knapsack_pro_adapter.set_test_helper_path(__FILE__) +``` + #### Step for Spinach Create file `features/support/knapsack_pro.rb` and add there: @@ -274,7 +290,7 @@ KnapsackPro::Adapters::SpinachAdapter.bind #### Custom configuration -You can change the default Knapsack Pro configuration for RSpec, Cucumber, Minitest or Spinach tests. Here are examples what you can do. Put the configuration below in place of `CUSTOM_CONFIG_GOES_HERE` (in the configuration samples above). +You can change the default Knapsack Pro configuration for RSpec, Cucumber, Minitest, test-unit or Spinach tests. Here are examples what you can do. Put the configuration below in place of `CUSTOM_CONFIG_GOES_HERE` (in the configuration samples above). ```ruby # you can use your own logger @@ -294,7 +310,8 @@ Set one or more tokens depending on how many test suites you run on CI server. * `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` - as value set token for rspec test suite. Token can be generated when you sign in to [knapsackpro.com](http://www.knapsackpro.com). * `KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER` - token for cucumber test suite. * `KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST` - token for minitest test suite. -* `KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH` - token for minitest test suite. +* `KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT` - token for test-unit test suite. +* `KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH` - token for spinach test suite. __Tip:__ In case you have for instance multiple rspec test suites then prepend each of knapsack_pro command which executes tests with `KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC` variable. @@ -311,6 +328,9 @@ On your CI server run this command for the first CI node. Update `KNAPSACK_PRO_C # Step for Minitest $ KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:minitest + # Step for test-unit + $ KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:test_unit + # Step for Spinach $ KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:spinach @@ -325,6 +345,9 @@ You can add `KNAPSACK_PRO_TEST_FILE_PATTERN` if your tests are not in default di # Step for Minitest $ KNAPSACK_PRO_TEST_FILE_PATTERN="directory_with_tests/**{,/*/**}/*_test.rb" KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:minitest + # Step for test-unit + $ KNAPSACK_PRO_TEST_FILE_PATTERN="directory_with_tests/**{,/*/**}/*_test.rb" KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:test_unit + # Step for Spinach $ KNAPSACK_PRO_TEST_FILE_PATTERN="directory_with_features/**{,/*/**}/*.feature" KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 bundle exec rake knapsack_pro:spinach @@ -533,6 +556,16 @@ For instance to run verbose tests: $ bundle exec rake "knapsack_pro:minitest[--verbose]" +#### Passing arguments to test-unit + +Add arguments to knapsack_pro test-unit task like this: + + $ bundle exec rake "knapsack_pro:test_unit[--arg_name value]" + +For instance to run verbose tests: + + $ bundle exec rake "knapsack_pro:test_unit[--verbose]" + #### Passing arguments to spinach Add arguments to knapsack_pro spinach task like this: @@ -547,6 +580,7 @@ You can install knapsack_pro globally and use binary. For instance: $ knapsack_pro queue:rspec "--tag custom_tag_name --profile" $ knapsack_pro cucumber "--name feature" $ knapsack_pro minitest "--verbose --pride" + $ knapsack_pro test_unit "--verbose" $ knapsack_pro spinach "--arg_name value" This is optional way of using knapsack_pro when you don't want to add it to `Gemfile`. @@ -580,7 +614,7 @@ If you need to check what is the encryption hash for particular test file you ca $ KNAPSACK_PRO_SALT=xxx bundle exec rake knapsack_pro:encrypted_test_file_names[rspec] -You can pass the name of test runner like `rspec`, `minitest`, `cucumber`, `spinach` as argument to rake task. +You can pass the name of test runner like `rspec`, `minitest`, `test_unit`, `cucumber`, `spinach` as argument to rake task. #### How to enable branch names encryption? @@ -621,6 +655,7 @@ machine: # KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: rspec-token # KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER: cucumber-token # KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST: minitest-token + # KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT: test-unit-token # KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH: spinach-token test: override: @@ -636,6 +671,10 @@ test: - bundle exec rake knapsack_pro:minitest: parallel: true # Caution: there are 8 spaces indentation! + # Step for test-unit + - bundle exec rake knapsack_pro:test_unit: + parallel: true # Caution: there are 8 spaces indentation! + # Step for Spinach - bundle exec rake knapsack_pro:spinach: parallel: true # Caution: there are 8 spaces indentation! @@ -707,6 +746,9 @@ script: # Step for Minitest - "bundle exec rake knapsack_pro:minitest" + # Step for test-unit + - "bundle exec rake knapsack_pro:test_unit" + # Step for Spinach - "bundle exec rake knapsack_pro:spinach" @@ -716,6 +758,7 @@ env: - KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token - KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token - KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token + - KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT=test-unit-token - KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token - KNAPSACK_PRO_CI_NODE_TOTAL=2 @@ -726,14 +769,14 @@ env: Such configuration will generate matrix with 2 following ENV rows: - KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token - KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1 KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token + KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=0 KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT=test-unit-token KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token + KNAPSACK_PRO_CI_NODE_TOTAL=2 KNAPSACK_PRO_CI_NODE_INDEX=1 KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=rspec-token KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER=cucumber-token KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST=minitest-token KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT=test-unit-token KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH=spinach-token More info about global and matrix ENV configuration in [travis docs](https://docs.travis-ci.com/user/customizing-the-build/#Build-Matrix). #### Info for semaphoreapp.com users -Knapsack Pro supports semaphoreapp ENVs `SEMAPHORE_THREAD_COUNT` and `SEMAPHORE_CURRENT_THREAD`. The only thing you need to do is set up knapsack_pro rspec/cucumber/minitest command for as many threads as you need. Here is an example: +Knapsack Pro supports semaphoreapp ENVs `SEMAPHORE_THREAD_COUNT` and `SEMAPHORE_CURRENT_THREAD`. The only thing you need to do is set up knapsack_pro rspec/cucumber/minitest/test_unit command for as many threads as you need. Here is an example: # Thread 1 ## Step for RSpec @@ -742,6 +785,8 @@ Knapsack Pro supports semaphoreapp ENVs `SEMAPHORE_THREAD_COUNT` and `SEMAPHORE_ bundle exec rake knapsack_pro:cucumber ## Step for Minitest bundle exec rake knapsack_pro:minitest + ## Step for test-unit + bundle exec rake knapsack_pro:test_unit ## Step for Spinach bundle exec rake knapsack_pro:spinach @@ -752,6 +797,8 @@ Knapsack Pro supports semaphoreapp ENVs `SEMAPHORE_THREAD_COUNT` and `SEMAPHORE_ bundle exec rake knapsack_pro:cucumber ## Step for Minitest bundle exec rake knapsack_pro:minitest + ## Step for test-unit + bundle exec rake knapsack_pro:test_unit ## Step for Spinach bundle exec rake knapsack_pro:spinach @@ -772,6 +819,9 @@ Knapsack Pro supports buildkite ENVs `BUILDKITE_PARALLEL_JOB_COUNT` and `BUILDKI # Step for Minitest bundle exec rake knapsack_pro:minitest + # Step for test-unit + bundle exec rake knapsack_pro:test_unit + # Step for Spinach bundle exec rake knapsack_pro:spinach @@ -863,6 +913,9 @@ Knapsack Pro supports snap-ci.com ENVs `SNAP_WORKER_TOTAL` and `SNAP_WORKER_INDE # Step for Minitest bundle exec rake knapsack_pro:minitest + # Step for test-unit + bundle exec rake knapsack_pro:test_unit + # Step for Spinach bundle exec rake knapsack_pro:spinach @@ -1030,7 +1083,7 @@ For instance to run subset of tests for the first CI node with specified seed yo KNAPSACK_PRO_CI_NODE_INDEX=0 \ bundle exec rake "knapsack_pro:rspec[--seed 123]" -Above example is for RSpec. You can use respectively rake task name and token environment variable when you want to run tests for minitest, cucumber or spinach. +Above example is for RSpec. You can use respectively rake task name and token environment variable when you want to run tests for minitest, test_unit, cucumber or spinach. It should work when all CI nodes finished work and sent time execution data to Knapsack Pro API. You can visit [user dashboard](https://knapsackpro.com/dashboard) to preview particular CI build and ensure time execution data were collected from all CI nodes. If at least one CI node has not sent time execution data to the Knapsack Pro API then you should check below solution. diff --git a/Rakefile b/Rakefile index d4dbb81e..d05fa720 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,2 @@ require 'bundler/gem_tasks' require 'knapsack_pro' - -#KnapsackPro.load_tasks diff --git a/bin/knapsack_pro b/bin/knapsack_pro index 6dc46f89..8b69de1e 100755 --- a/bin/knapsack_pro +++ b/bin/knapsack_pro @@ -10,6 +10,7 @@ MAP = { 'queue:rspec' => KnapsackPro::Runners::Queue::RSpecRunner, 'cucumber' => KnapsackPro::Runners::CucumberRunner, 'minitest' => KnapsackPro::Runners::MinitestRunner, + 'test_unit' => KnapsackPro::Runners::TestUnitRunner, 'spinach' => KnapsackPro::Runners::SpinachRunner, } diff --git a/circle.yml b/circle.yml index 0003f835..a4879f56 100644 --- a/circle.yml +++ b/circle.yml @@ -3,3 +3,10 @@ machine: version: 2.4.0 environment: CODECLIMATE_REPO_TOKEN: b6626e682a8e97e0c5978febc92c3526792a2d018b41b8e1b52689da37fb7d92 +test: + override: + # Specify command explicitly because CircleCI 1.0 adds arguments to RSpec + # $ bundle exec rspec "--color --require spec_helper --format documentation" "" "spec" + # and this cause error because in tests we load test-unit gem that tries to parse + # the unknown arguments. + - bundle exec rspec spec diff --git a/knapsack_pro.gemspec b/knapsack_pro.gemspec index 0ef09e5d..7a3f4078 100644 --- a/knapsack_pro.gemspec +++ b/knapsack_pro.gemspec @@ -26,6 +26,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'cucumber', '>= 0' spec.add_development_dependency 'spinach', '>= 0.8' spec.add_development_dependency 'minitest', '>= 5.0.0' + spec.add_development_dependency 'test-unit', '>= 3.0.0' spec.add_development_dependency 'codeclimate-test-reporter', '~> 0' spec.add_development_dependency 'pry', '~> 0' spec.add_development_dependency 'vcr', '~> 2.9' diff --git a/lib/knapsack_pro.rb b/lib/knapsack_pro.rb index 609871bb..c32db3f0 100644 --- a/lib/knapsack_pro.rb +++ b/lib/knapsack_pro.rb @@ -49,11 +49,13 @@ require_relative 'knapsack_pro/adapters/rspec_adapter' require_relative 'knapsack_pro/adapters/cucumber_adapter' require_relative 'knapsack_pro/adapters/minitest_adapter' +require_relative 'knapsack_pro/adapters/test_unit_adapter' require_relative 'knapsack_pro/adapters/spinach_adapter' require_relative 'knapsack_pro/runners/base_runner' require_relative 'knapsack_pro/runners/rspec_runner' require_relative 'knapsack_pro/runners/cucumber_runner' require_relative 'knapsack_pro/runners/minitest_runner' +require_relative 'knapsack_pro/runners/test_unit_runner' require_relative 'knapsack_pro/runners/spinach_runner' require_relative 'knapsack_pro/runners/queue/base_runner' require_relative 'knapsack_pro/runners/queue/rspec_runner' diff --git a/lib/knapsack_pro/adapters/test_unit_adapter.rb b/lib/knapsack_pro/adapters/test_unit_adapter.rb new file mode 100644 index 00000000..0ad2aac5 --- /dev/null +++ b/lib/knapsack_pro/adapters/test_unit_adapter.rb @@ -0,0 +1,72 @@ +module KnapsackPro + module Adapters + class TestUnitAdapter < BaseAdapter + TEST_DIR_PATTERN = 'test/**{,/*/**}/*_test.rb' + @@parent_of_test_dir = nil + + def self.test_path(obj) + first_test = obj.tests.first + method = first_test.method_name + full_test_path = first_test.method(method).source_location.first + parent_of_test_dir_regexp = Regexp.new("^#{@@parent_of_test_dir}") + test_path = full_test_path.gsub(parent_of_test_dir_regexp, '.') + # test_path will look like ./test/dir/unit_test.rb + test_path + end + + # Overrides the method from unit-test gem + # https://github.com/test-unit/test-unit/blob/master/lib/test/unit/testsuite.rb + module BindTimeTrackerTestUnitPlugin + def run_startup(result) + return if @test_case.nil? + KnapsackPro.tracker.current_test_path = KnapsackPro::Adapters::TestUnitAdapter.test_path(self) + KnapsackPro.tracker.start_timer + return if !@test_case.respond_to?(:startup) + begin + @test_case.startup + rescue Exception + raise unless handle_exception($!, result) + end + end + + def run_shutdown(result) + return if @test_case.nil? + KnapsackPro.tracker.stop_timer + return if !@test_case.respond_to?(:shutdown) + begin + @test_case.shutdown + rescue Exception + raise unless handle_exception($!, result) + end + end + end + + def bind_time_tracker + Test::Unit::TestSuite.send(:prepend, BindTimeTrackerTestUnitPlugin) + + add_post_run_callback do + KnapsackPro.logger.debug(KnapsackPro::Presenter.global_time) + end + end + + def bind_save_report + add_post_run_callback do + KnapsackPro::Report.save + end + end + + 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/lib/knapsack_pro/config/env.rb b/lib/knapsack_pro/config/env.rb index 2d5fae5b..1fe6373c 100644 --- a/lib/knapsack_pro/config/env.rb +++ b/lib/knapsack_pro/config/env.rb @@ -136,6 +136,10 @@ def test_suite_token_minitest ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST'] end + def test_suite_token_test_unit + ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT'] + end + def test_suite_token_cucumber ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER'] end diff --git a/lib/knapsack_pro/runners/test_unit_runner.rb b/lib/knapsack_pro/runners/test_unit_runner.rb new file mode 100644 index 00000000..39c95af0 --- /dev/null +++ b/lib/knapsack_pro/runners/test_unit_runner.rb @@ -0,0 +1,28 @@ +module KnapsackPro + module Runners + class TestUnitRunner < BaseRunner + def self.run(args) + ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN'] = KnapsackPro::Config::Env.test_suite_token_test_unit + ENV['KNAPSACK_PRO_RECORDING_ENABLED'] = 'true' + + runner = new(KnapsackPro::Adapters::TestUnitAdapter) + + if runner.test_files_to_execute_exist? + require 'test/unit' + + cli_args = + (args || '').split + + runner.test_file_paths.map do |f| + File.expand_path(f) + end + + exit Test::Unit::AutoRunner.run( + true, + runner.test_dir, + cli_args + ) + end + end + end + end +end diff --git a/lib/tasks/encrypted_test_file_names.rake b/lib/tasks/encrypted_test_file_names.rake index df5b4320..80fd4328 100644 --- a/lib/tasks/encrypted_test_file_names.rake +++ b/lib/tasks/encrypted_test_file_names.rake @@ -9,12 +9,14 @@ namespace :knapsack_pro do KnapsackPro::Adapters::RSpecAdapter when 'minitest' KnapsackPro::Adapters::MinitestAdapter + when 'test_unit' + KnapsackPro::Adapters::TestUnitAdapter when 'cucumber' KnapsackPro::Adapters::CucumberAdapter when 'spinach' KnapsackPro::Adapters::SpinachAdapter else - raise('Provide adapter name like rspec, minitest, cucumber, spinach') + raise('Provide adapter name like rspec, minitest, test_unit, cucumber, spinach') end test_file_pattern = KnapsackPro::TestFilePattern.call(adapter_class) diff --git a/lib/tasks/test_unit.rake b/lib/tasks/test_unit.rake new file mode 100644 index 00000000..9a5b48da --- /dev/null +++ b/lib/tasks/test_unit.rake @@ -0,0 +1,7 @@ +require 'knapsack_pro' + +namespace :knapsack_pro do + task :test_unit, [:test_unit_args] do |_, args| + KnapsackPro::Runners::TestUnitRunner.run(args[:test_unit_args]) + end +end diff --git a/spec/knapsack_pro/adapters/test_unit_adapter_spec.rb b/spec/knapsack_pro/adapters/test_unit_adapter_spec.rb new file mode 100644 index 00000000..30eac436 --- /dev/null +++ b/spec/knapsack_pro/adapters/test_unit_adapter_spec.rb @@ -0,0 +1,80 @@ +require 'test/unit/testcase' + +describe KnapsackPro::Adapters::TestUnitAdapter do + it do + expect(described_class::TEST_DIR_PATTERN).to eq 'test/**{,/*/**}/*_test.rb' + end + + describe '.test_path' do + subject { described_class.test_path(obj) } + + 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 'when regular test' do + class FakeTestUnitTest + def method_name + "test_something" + end + + def test_something + end + end + + class FakeTestUnitTestSuite + def tests + [FakeTestUnitTest.new] + end + end + + let(:obj) { FakeTestUnitTestSuite.new } + + it { should eq './spec/knapsack_pro/adapters/test_unit_adapter_spec.rb' } + end + end + + describe 'bind methods' do + describe '#bind_time_tracker' do + let(:logger) { instance_double(Logger) } + let(:global_time) { 'Global time: 01m 05s' } + + it do + expect(Test::Unit::TestSuite).to receive(:send).with(:prepend, KnapsackPro::Adapters::TestUnitAdapter::BindTimeTrackerTestUnitPlugin) + + 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) + expect(logger).to receive(:debug).with(global_time) + + subject.bind_time_tracker + end + end + + describe '#bind_save_report' do + it do + expect(subject).to receive(:add_post_run_callback).and_yield + + expect(KnapsackPro::Report).to receive(:save) + + subject.bind_save_report + end + end + end + + describe '#set_test_helper_path' do + let(:adapter) { described_class.new } + let(:test_helper_path) { '/code/project/test/test_helper.rb' } + + subject { adapter.set_test_helper_path(test_helper_path) } + + after do + expect(described_class.class_variable_get(:@@parent_of_test_dir)).to eq '/code/project' + end + + it { should eql '/code/project' } + end +end diff --git a/spec/knapsack_pro/config/env_spec.rb b/spec/knapsack_pro/config/env_spec.rb index e8653ab3..020b51a9 100644 --- a/spec/knapsack_pro/config/env_spec.rb +++ b/spec/knapsack_pro/config/env_spec.rb @@ -513,6 +513,20 @@ end end + describe '.test_suite_token_test_unit' do + subject { described_class.test_suite_token_test_unit } + + context 'when ENV exists' do + let(:test_suite_token_test_unit) { 'test-unit-token' } + before { stub_const("ENV", { 'KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT' => test_suite_token_test_unit }) } + it { should eq test_suite_token_test_unit } + end + + context "when ENV doesn't exist" do + it { should be_nil } + end + end + describe '.test_suite_token_cucumber' do subject { described_class.test_suite_token_cucumber } diff --git a/spec/knapsack_pro/runners/test_unit_runner_spec.rb b/spec/knapsack_pro/runners/test_unit_runner_spec.rb new file mode 100644 index 00000000..653b3a25 --- /dev/null +++ b/spec/knapsack_pro/runners/test_unit_runner_spec.rb @@ -0,0 +1,57 @@ +require 'test/unit' + +describe KnapsackPro::Runners::TestUnitRunner do + subject { described_class.new(KnapsackPro::Adapters::TestUnitAdapter) } + + it { should be_kind_of KnapsackPro::Runners::BaseRunner } + + describe '.run' do + let(:args) { '--verbose --order=random' } + + subject { described_class.run(args) } + + before do + stub_const("ENV", { 'KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT' => 'test-unit-token' }) + end + + context 'when test files were returned by Knapsack Pro API' do + it 'runs tests' do + test_file_paths = ['test-unit_fake/a_test.rb', 'test-unit_fake/b_test.rb'] + runner = instance_double(described_class, + test_dir: 'test-unit_fake', + test_file_paths: test_file_paths, + test_files_to_execute_exist?: true) + expect(described_class).to receive(:new) + .with(KnapsackPro::Adapters::TestUnitAdapter).and_return(runner) + + auto_runner_exit_code = 0 + expect(Test::Unit::AutoRunner).to receive(:run) do |flag, test_dir, cli_args| + expect(flag).to be true + expect(test_dir).to eq 'test-unit_fake' + expect(cli_args.size).to eq 4 + expect(cli_args[0]).to eq '--verbose' + expect(cli_args[1]).to eq '--order=random' + expect(cli_args[2]).to end_with 'test-unit_fake/a_test.rb' + expect(cli_args[3]).to end_with 'test-unit_fake/b_test.rb' + end.and_return(auto_runner_exit_code) + expect(described_class).to receive(:exit).with(auto_runner_exit_code) + + subject + + expect(ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN']).to eq 'test-unit-token' + expect(ENV['KNAPSACK_PRO_RECORDING_ENABLED']).to eq 'true' + end + end + + context 'when test files were not returned by Knapsack Pro API' do + it "doesn't run tests" do + runner = instance_double(described_class, + test_files_to_execute_exist?: false) + expect(described_class).to receive(:new) + .with(KnapsackPro::Adapters::TestUnitAdapter).and_return(runner) + + subject + end + end + end +end