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
# File 'lib/knapsack_pro/tracker.rb', line 14

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

Instance Attribute Details

#current_test_pathObject



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

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

#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



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

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



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

def reset_timer
  @start_time = now_without_mock_time.to_f
end

#set_prerun_tests(test_file_paths) ⇒ Object



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

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



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

def start_timer
  @start_time ||= now_without_mock_time.to_f
end

#stop_timerObject



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

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/knapsack_pro/tracker.rb', line 72

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