Class: Knapsack::Adapters::MinitestAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/knapsack/adapters/minitest_adapter.rb

Defined Under Namespace

Modules: BindTimeTrackerMinitestPlugin

Constant Summary collapse

TEST_DIR_PATTERN =
'test/**{,/*/**}/*_test.rb'
REPORT_PATH =
'knapsack_minitest_report.json'
@@parent_of_test_dir =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

bind, #bind

Class Method Details

.test_path(obj) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/knapsack/adapters/minitest_adapter.rb', line 52

def self.test_path(obj)
  # Pick the first public method in the class itself, that starts with "test_"
  test_method_name = obj.public_methods(false).select{|m| m =~ /^test_/ }.first
  if test_method_name.nil?
    # case for shared examples
    method_object = obj.method(obj.location.sub(/.*?test_/, 'test_'))
  else
    method_object = obj.method(test_method_name)
  end
  full_test_path = method_object.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

Instance Method Details

#bind_report_generatorObject



31
32
33
34
35
36
# File 'lib/knapsack/adapters/minitest_adapter.rb', line 31

def bind_report_generator
  add_post_run_callback do
    Knapsack.report.save
    Knapsack.logger.info(Presenter.report_details)
  end
end

#bind_time_offset_warningObject



38
39
40
41
42
43
44
45
# File 'lib/knapsack/adapters/minitest_adapter.rb', line 38

def bind_time_offset_warning
  add_post_run_callback do
    Knapsack.logger.log(
      Presenter.time_offset_log_level,
      Presenter.time_offset_warning
    )
  end
end

#bind_time_trackerObject



23
24
25
26
27
28
29
# File 'lib/knapsack/adapters/minitest_adapter.rb', line 23

def bind_time_tracker
  ::Minitest::Test.send(:include, BindTimeTrackerMinitestPlugin)

  add_post_run_callback do
    Knapsack.logger.info(Presenter.global_time)
  end
end

#set_test_helper_path(file_path) ⇒ Object



47
48
49
50
# File 'lib/knapsack/adapters/minitest_adapter.rb', line 47

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