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



102
103
104
105
106
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 102

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
47
48
# 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)
      next [] if failed_id_paths.none?
      next file_path if KnapsackPro::Config::Env.test_files_encrypted?
      # 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



108
109
110
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 108

def duration
  now - @suite_started
end

#example_finished(notification) ⇒ Object



72
73
74
75
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 72

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

#example_group_finished(notification) ⇒ Object



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

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



62
63
64
65
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 62

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



67
68
69
70
# File 'lib/knapsack_pro/formatters/time_tracker.rb', line 67

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

#queueObject



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

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



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

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



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

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