class KnapsackPro::Formatters::TimeTracker

Attributes

output[R]

Public Class Methods

new(_output) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 17
def initialize(_output)
  @output = StringIO.new
  @time_each = nil
  @time_all = nil
  @before_all = 0.0
  @group = {}
  @paths = {}
  @suite_started = now
end

Public Instance Methods

batch() click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 66
def batch
  @paths.values.map do |example|
    example.transform_keys(&:to_s)
  end
end
duration() click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 72
def duration
  now - @suite_started
end
example_finished(notification) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 37
def example_finished(notification)
  record_example(@group, notification.example, @time_each)
  @time_all = now
end
example_group_finished(notification) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 42
def example_group_finished(notification)
  return unless top_level_group?(notification.group)

  after_all = @time_all.nil? ? 0.0 : now - @time_all
  add_hooks_time(@group, @before_all, after_all)
  @before_all = 0.0
  @paths = merge(@paths, @group)
  @group = {}
end
example_group_started(notification) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 27
def example_group_started(notification)
  return unless top_level_group?(notification.group)
  @time_all = now
end
example_started(_notification) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 32
def example_started(_notification)
  @before_all = now - @time_all if @before_all == 0.0
  @time_each = now
end
queue(scheduled_paths) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 52
def queue(scheduled_paths)
  recorded_paths = @paths.values.map do |example|
    KnapsackPro::Adapters::RSpecAdapter.parse_file_path(example[:path])
  end

  missing = (scheduled_paths - recorded_paths).each_with_object({}) do |path, object|
    object[path] = { path: path, time_execution: 0.0 }
  end

  merge(@paths, missing).values.map do |example|
    example.transform_keys(&:to_s)
  end
end
unexecuted_test_files(scheduled_paths) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 76
def unexecuted_test_files(scheduled_paths)
  pending_paths = @paths.values
    .filter { |example| example[:time_execution] == 0.0 }
    .map { |example| example[:path] }

  not_run_paths = scheduled_paths -
    @paths.values
    .map { |example| example[:path] }

  pending_paths + not_run_paths
end

Private Instance Methods

add_hooks_time(group, before_all, after_all) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 94
def add_hooks_time(group, before_all, after_all)
  group.each do |_, example|
    next if example[:time_execution] == 0.0
    example[:time_execution] += before_all + after_all
  end
end
file_path_for(example) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 127
def file_path_for(example)
  KnapsackPro::Adapters::RSpecAdapter.file_path_for(example)
end
merge(h1, h2) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 139
def merge(h1, h2)
  h1.merge(h2) do |key, v1, v2|
    {
      path: key,
      time_execution: v1[:time_execution] + v2[:time_execution]
    }
  end
end
now() click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 148
def now
  KnapsackPro::Utils.time_now
end
path_for(example) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 113
def path_for(example)
  file = file_path_for(example)
  return nil if file == ""

  path = rspec_split_by_test_example?(file) ? example.id : file
  KnapsackPro::TestFileCleaner.clean(path)
end
record_example(accumulator, example, started_at) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 101
def record_example(accumulator, example, started_at)
  path = path_for(example)
  return if path.nil?

  time_execution = time_execution_for(example, started_at)
  if accumulator.key?(path)
    accumulator[path][:time_execution] += time_execution
  else
    accumulator[path] = { path: path, time_execution: time_execution }
  end
end
rspec_split_by_test_example?(file) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 121
def rspec_split_by_test_example?(file)
  return false unless KnapsackPro::Config::Env.rspec_split_by_test_examples?
  return false unless KnapsackPro::Adapters::RSpecAdapter.slow_test_file?(KnapsackPro::Adapters::RSpecAdapter, file)
  true
end
time_execution_for(example, started_at) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 131
def time_execution_for(example, started_at)
  if example.execution_result.status.to_s == "pending"
    0.0
  else
    (now - started_at).to_f
  end
end
top_level_group?(group) click to toggle source
# File lib/knapsack_pro/formatters/time_tracker.rb, line 90
def top_level_group?(group)
  group.metadata[:parent_example_group].nil?
end