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

Improve detection of test file path in test-unit runner for test files with shared examples #123

Merged
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

### 2.2.1

* Improve detection of test file path in test-unit runner for test files with shared examples

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

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v2.2.0...v2.2.1

### 2.2.0

* Allow defining Queue Mode hooks multiple times (`KnapsackPro::Hooks::Queue.before_queue`, `KnapsackPro::Hooks::Queue.after_subset_queue`, `KnapsackPro::Hooks::Queue.after_queue`)
Expand Down
26 changes: 23 additions & 3 deletions lib/knapsack_pro/adapters/test_unit_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,29 @@ class TestUnitAdapter < BaseAdapter
@@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
full_test_path = nil
found_valid_test_file_path = false

obj.tests.each do |test_obj|
method = test_obj.method_name
full_test_path = test_obj.method(method).source_location.first
# if we find a test file path that is a valid test file path within test suite directory
# then break to stop looking further.
# If we won't find a valid test file path then the last found path will be used as full_test_path
# For instance if test file contains only shared examples then it's not possible to properly detect test file path
# so the wrong path can be used like:
# /Users/artur/.rvm/gems/ruby-2.6.5/gems/shared_should-0.10.0/lib/shared_should/shared_context.rb
if full_test_path.include?(@@parent_of_test_dir)
found_valid_test_file_path = true
break
end
end

unless found_valid_test_file_path
KnapsackPro.logger.warn('cannot detect a valid test file path. Probably the test file contains only shared examples. Please add test cases to your test file. Read more at https://github.com/KnapsackPro/knapsack_pro-ruby/pull/123')
KnapsackPro.logger.warn("See test file for #{obj.inspect}")
end

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
Expand Down
3 changes: 1 addition & 2 deletions spec/knapsack_pro/adapters/test_unit_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

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)
described_class.class_variable_set(:@@parent_of_test_dir, parent_of_test_dir)
end

context 'when regular test' do
Expand Down