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

Conversation

ArturT
Copy link
Member

@ArturT ArturT commented Sep 4, 2020

problem

When you use test runner test-unit https://test-unit.github.io and your test file has shared examples (share_should method) and regular test cases then when tests are executed in random order for this file then it could lead to knapsack_pro detecting wrong test file path for executed test if first test case is shared example for a test file.

Bug also happens when test file contains only shared examples.

Example test file with shared examples https://github.com/KnapsackPro/rails-app-with-knapsack_pro/blob/383cb917755f6d34bd3cacd8261226cc769e49a1/test-unit/models/shared_should_test.rb#L33

Root problem is that detecting test file path based on shared examples method returns wrong test file path.

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

test_path method returned wrong path /Users/artur/.rvm/gems/ruby-2.6.5/gems/shared_should-0.10.0/lib/shared_should/shared_context.rb for shared example instead of test-unit/models/shared_should_test.rb

fix

  • knapsack_pro will try to detect the proper test file path. If the test file contains only shared examples then it's not possible to detect test file properly so the old behavior stays - the wrong path will be used because we don't have any other path.
  • when wrong test file path is detected then show warning in logs

Fix spec, the @@parent_of_test_dir should be path to directory instead of regexp
@ArturT ArturT added the bug label Sep 4, 2020
@ArturT ArturT merged commit 8693a5b into master Sep 4, 2020
@ArturT ArturT deleted the fix-detecting-test-file-path-for-shared-example-in-test-unit branch September 4, 2020 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant