Class: KnapsackPro::Formatters::TimeTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/knapsack_pro/formatters/time_tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_output) ⇒ TimeTracker

Returns a new instance of TimeTracker.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 18

def initialize(_output)
  @output = StringIO.new
  @time_each = nil
  @time_all = nil
  @time_all_by_group_id_path = Hash.new(0)
  @group = {}
  @paths = {}
  @suite_started = now
  @batched_scheduled_paths = []
  @split_by_test_example_file_paths = Set.new
  @current_batch_examples = []
end

Instance Attribute Details

#outputObject (readonly)

RSpec < v3.10.2



16
17
18
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 16

def output
  @output
end

Instance Method Details

#batchObject



100
101
102
103
104
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 100

def batch
  @paths.values.map do |example|
    example.transform_keys(&:to_s)
  end
end

#current_batch_failed_pathsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 31

def current_batch_failed_paths
  examples_by_file_path = @current_batch_examples
    .group_by { |example| file_path_for(example) }

  paths =
    examples_by_file_path.flat_map do |file_path, examples|
      failed_id_paths = examples.filter { |example| example.execution_result.status.to_s == "failed" }.map(&:id)

      # Other nodes may have run some examples from this file, it's not safe to compact.
      next failed_id_paths if rspec_split_by_test_example?(file_path)
      next file_path if failed_id_paths.size == examples.size
      failed_id_paths
    end

  paths.map { |path| KnapsackPro::TestFileCleaner.clean(path) }
end

#durationObject



106
107
108
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 106

def duration
  now - @suite_started
end

#example_finished(notification) ⇒ Object



70
71
72
73
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 70

def example_finished(notification)
  record_example(@group, notification.example, @time_each)
  @time_all = now
end

#example_group_finished(notification) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 75

def example_group_finished(notification)
  record_time_all(notification.group, @time_all_by_group_id_path, @time_all)
  @time_all = now
  return unless top_level_group?(notification.group)

  add_hooks_time(@group, @time_all_by_group_id_path)
  @time_all_by_group_id_path = Hash.new(0)
  @paths = merge(@paths, @group)
  @group = {}
end

#example_group_started(notification) ⇒ Object



60
61
62
63
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 60

def example_group_started(notification)
  record_time_all(notification.group.parent_groups[1], @time_all_by_group_id_path, @time_all)
  @time_all = now
end

#example_started(notification) ⇒ Object



65
66
67
68
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 65

def example_started(notification)
  record_time_all(notification.example.example_group, @time_all_by_group_id_path, @time_all)
  @time_each = now
end

#queueObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 86

def queue
  recorded_paths = @paths.values.map do |example|
    KnapsackPro::Adapters::RSpecAdapter.parse_file_path(example[:path])
  end

  missing = (@batched_scheduled_paths.flatten - 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

#schedule(paths) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 48

def schedule(paths)
  @current_batch_examples = []
  @batched_scheduled_paths << paths

  paths.each do |path|
    next unless KnapsackPro::Adapters::RSpecAdapter.id_path?(path)

    file_path = KnapsackPro::Adapters::RSpecAdapter.parse_file_path(path)
    @split_by_test_example_file_paths << KnapsackPro::TestFileCleaner.clean(file_path)
  end
end

#unexecuted_test_pathsObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 110

def unexecuted_test_paths
  pending_paths = @paths.values
    .filter { |example| example[:time_execution] == 0.0 }
    .map { |example| example[:path] }

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

  pending_paths + not_run_paths
end