Sha256: d486bbb02a6121fd94f1bc951d82445cfd02413fc20c4e6d6e67092156bf89c1

Contents?: true

Size: 1.57 KB

Versions: 16

Compression:

Stored size: 1.57 KB

Contents

module KnapsackPro
  class Tracker
    include Singleton

    attr_reader :global_time_since_beginning, :global_time, :test_files_with_time
    attr_writer :current_test_path

    def initialize
      @global_time_since_beginning = 0
      set_defaults
    end

    def reset!
      set_defaults
    end

    def start_timer
      @start_time = now_without_mock_time.to_f
    end

    def stop_timer
      execution_time = @start_time ? now_without_mock_time.to_f - @start_time : 0.0
      update_global_time(execution_time)
      update_test_file_time(execution_time)
      execution_time
    end

    def current_test_path
      raise("current_test_path needs to be set by Knapsack Pro Adapter's bind method") unless @current_test_path
      @current_test_path.sub(/^\.\//, '')
    end

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

    private

    def set_defaults
      @global_time = 0
      @test_files_with_time = {}
      @test_path = nil
    end

    def update_global_time(execution_time)
      @global_time += execution_time
      @global_time_since_beginning += execution_time
    end

    def update_test_file_time(execution_time)
      @test_files_with_time[current_test_path] ||= 0
      @test_files_with_time[current_test_path] += execution_time
    end

    def now_without_mock_time
      if defined?(Timecop)
        Time.now_without_mock_time
      else
        Time.raw_now
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
knapsack_pro-1.0.1 lib/knapsack_pro/tracker.rb
knapsack_pro-1.0.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.57.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.56.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.55.2 lib/knapsack_pro/tracker.rb
knapsack_pro-0.55.1 lib/knapsack_pro/tracker.rb
knapsack_pro-0.55.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.54.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.53.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.52.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.51.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.50.1 lib/knapsack_pro/tracker.rb
knapsack_pro-0.50.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.49.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.48.0 lib/knapsack_pro/tracker.rb
knapsack_pro-0.47.0 lib/knapsack_pro/tracker.rb