<commit_before>module SpecTimeLimit
  class Error < StandardError
    def self.message_for(type, timeout)
      case type
      when :target
        "Exceeded the #{timeout} sec target threshold for new/modified specs"
      when :absolute
        "Exceeded the #{timeout} sec absolute threshold for existing specs"
      else
        "Exceeded the #{timeout} sec historical threshold for this particular spec"
      end
    end
  end

  class << self
    def enforce(example)
      type, timeout = timeout_for(example)
      Timeout.timeout(timeout) do
        example.run
      end
    rescue Timeout::Error
      bt = $ERROR_INFO.backtrace
      bt.shift while bt.first =~ /\/(gems|test_setup)\//
      raise Error, Error.message_for(type, timeout), bt
    end

    # find an appropriate timeout for this spec
    def timeout_for(example)
      if (timeout = typical_time_for(example))
        [:status_quo, [timeout, ABSOLUTE_TIMEOUT].min]
      elsif commit_modifies_spec?(example)
        [:target, TARGET_TIMEOUT]<commit_msg>make great expectations work w/ spec time limit<commit_after>module SpecTimeLimit
  class Error < StandardError
    # #initialize and #to_s are overwritten here to prevent Timeout.timeout
    # overwriting the error message to "execution expired"
    def initialize(message)
      @message = message
    end

    def to_s
      @message
    end

    def self.message_for(type, timeout)
      case type
      when :target
        "Exceeded the #{timeout} sec target threshold for new/modified specs"
      when :absolute
        "Exceeded the #{timeout} sec absolute threshold for existing specs"
      else
        "Exceeded the #{timeout} sec historical threshold for this particular spec"
      end
    end
  end

  class << self
    def enforce(example)
      type, timeout = timeout_for(example)
      Timeout.timeout(timeout, Error.new(Error.message_for(type, timeout))) do
        example.run
      end
      # we don't rescue here because example.run rescues the SpecTimeLimit::Error
      # and performs set_exception(spec_time_limit_error) on the example
    end

    # find an appropriate timeout for this spec
    def timeout_for(example)
      if (timeout = typical_time_for(example))
        [:status_quo, [timeout, ABSOLUTE_TIMEOUT].min]
      elsif commit_modifies_spec?(example)
        [:target, TARGET_TIMEOUT]
{'fraction_non_alphanumeric': 0.07415254237288135, 'fraction_numerical': 0.0, 'mean_word_length': 2.876847290640394, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4128674', 'n_tokens_mistral': 663, 'n_tokens_neox': 638, 'n_words': 215}