Class: KnapsackPro::Tracker

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/knapsack_pro/tracker.rb

Constant Summary collapse

DEFAULT_TEST_FILE_TIME =

when test file is pending, empty with no tests or has syntax error then assume time execution to better allocate it in Queue Mode for future CI build runs

0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracker

Returns a new instance of Tracker.



14
15
16
17
18
19
# File 'lib/knapsack_pro/tracker.rb', line 14

def initialize
  @global_time_since_beginning = 0
  KnapsackPro::Config::TempFiles.ensure_temp_directory_exists!
  FileUtils.mkdir_p(tracker_dir_path)
  set_defaults
end

Instance Attribute Details

#current_test_pathObject



50
51
52
53
54
# File 'lib/knapsack_pro/tracker.rb', line 50

def current_test_path
  return unless @current_test_path

  KnapsackPro::TestFileCleaner.clean(@current_test_path)
end

#global_timeObject (readonly)

seconds



11
12
13
# File 'lib/knapsack_pro/tracker.rb', line 11

def global_time
  @global_time
end

#global_time_since_beginningObject (readonly)

seconds



11
12
13
# File 'lib/knapsack_pro/tracker.rb', line 11

def global_time_since_beginning
  @global_time_since_beginning
end

#prerun_tests_loadedObject (readonly)

seconds



11
12
13
# File 'lib/knapsack_pro/tracker.rb', line 11

def prerun_tests_loaded
  @prerun_tests_loaded
end

#test_files_with_timeObject (readonly)

seconds



11
12
13
# File 'lib/knapsack_pro/tracker.rb', line 11

def test_files_with_time
  @test_files_with_time
end

Instance Method Details

#reset!Object



21
22
23
24
25
26
27
28
# File 'lib/knapsack_pro/tracker.rb', line 21

def reset!
  set_defaults

  # Remove report only when the reset! method is called explicitly.
  # The report should be persisted on the disk so that multiple tracker instances can share the report state.
  # Tracker instance can be created by knapsack_pro process and a separate tracker is created by rake task (e.g., RSpec) in Regular Mode.
  File.delete(prerun_tests_report_path) if File.exist?(prerun_tests_report_path)
end

#reset_timerObject



34
35
36
# File 'lib/knapsack_pro/tracker.rb', line 34

def reset_timer
  @start_time = now_without_mock_time.to_f
end

#set_prerun_tests(test_file_paths) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/knapsack_pro/tracker.rb', line 56

def set_prerun_tests(test_file_paths)
  test_file_paths.each do |test_file_path|
    # Set a default time for test file
    # in case when the test file will not be run
    # due syntax error or being pending.
    # The time is required by Knapsack Pro API.
    @test_files_with_time[test_file_path] = {
      time_execution: DEFAULT_TEST_FILE_TIME,
      measured_time: false,
    }
  end

  save_prerun_tests_report(@test_files_with_time)

  @prerun_tests_loaded = true
end

#start_timerObject



30
31
32
# File 'lib/knapsack_pro/tracker.rb', line 30

def start_timer
  @start_time ||= now_without_mock_time.to_f
end

#stop_timerObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/knapsack_pro/tracker.rb', line 38

def stop_timer
  execution_time = @start_time ? now_without_mock_time.to_f - @start_time : 0.0

  if @current_test_path
    update_global_time(execution_time)
    update_test_file_time(execution_time)
    reset_timer
  end

  execution_time
end

#to_aObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/knapsack_pro/tracker.rb', line 79

def to_a
  # When the test files are not loaded in the memory then load them from the disk.
  # Useful for the Regular Mode when the memory is not shared between tracker instances.
  # Tracker instance can be created by knapsack_pro process and a separate tracker is created by rake task (e.g., RSpec)
  load_prerun_tests unless prerun_tests_loaded

  test_files = []
  @test_files_with_time.each do |path, hash|
    test_files << {
      path: path,
      time_execution: hash[:time_execution]
    }
  end
  test_files
end

#unexecuted_test_filesObject



73
74
75
76
77
# File 'lib/knapsack_pro/tracker.rb', line 73

def unexecuted_test_files
  @test_files_with_time.map do |path, hash|
    path unless hash[:measured_time]
  end.compact
end