<commit_before># Comprehensively test a formula or pull request.
#
# Usage: brew test-bot [options...] <pull-request|formula>
#
# Options:
# --keep-logs:     Write and keep log files under ./brewbot/
# --cleanup:       Clean the Homebrew directory. Very dangerous. Use with care.
# --clean-cache:   Remove all cached downloads. Use with care.
# --skip-setup:    Don't check the local system is setup correctly.
# --skip-homebrew: Don't check Homebrew's files and tests are all valid.
# --junit:         Generate a JUnit XML test results file.
# --email:         Generate an email subject file.
# --no-bottle:     Run brew install without --build-bottle
# --keep-old:      Run brew bottle --keep-old to build new bottles for a single platform.
# --HEAD:          Run brew install with --HEAD
# --local:         Ask Homebrew to write verbose logs under ./logs/ and set HOME to ./home/
# --tap=<tap>:     Use the git repository of the given tap
# --dry-run:       Just print commands, don't run them.
# --fail-fast:     Immediately exit on a failing step.
# --verbose:       Print out all logs in realtime
#
# --ci-master:           Shortcut for Homebrew master branch CI options.
# --ci-pr:               Shortcut for Homebrew pull request CI options.
# --ci-testing:          Shortcut for Homebrew testing CI options.
# --ci-upload:           Homebrew CI bottle upload.
# --ci-reset-and-update: Homebrew CI repository and tap reset and update.

require "formula"
require "utils"
require "date"
require "rexml/document"
require "rexml/xmldecl"
require "rexml/cdata"
require "cmd/tap"

module Homebrew
  EMAIL_SUBJECT_FILE = "brew-test-bot.#{MacOS.cat}.email.txt"
  BYTES_IN_1_MEGABYTE = 1024*1024

  def homebrew_git_repo(tap = nil)
    if tap
      user, repo = tap.split "/"
      HOMEBREW_LIBRARY/"Taps/#{user}/homebrew-#{repo}"
    else
      HOMEBREW_REPOSITORY
    end
  end

  class Step
    attr_reader :command, :name, :status, :output, :time

    def initialize(test, command, options = {})
      @test = test
      @category = test.category
      @command = command
      @puts_output_on_success = options[:puts_output_on_success]
      @name = command[1].delete("-")
      @status = :running
      @repository = options[:repository] || HOMEBREW_REPOSITORY
      @time = 0
    end

    def log_file_path
      file = "#{@category}.#{@name}.txt"
      root = @test.log_root
      root ? root + file : file
    end

    def command_short
      (@command - %w[brew --force --retry --verbose --build-bottle --rb]).join(" ")
    end

    def passed?
      @status == :passed
    end

    def failed?
      @status == :failed
    end

    def puts_command
      if ENV["TRAVIS"]
        @travis_timer_id = rand(2**32).to_s(16)
        puts "travis_fold:start:#{@command.join(".")}"
        puts "travis_time:start:#{@travis_timer_id}"
      end
      puts "#{Tty.blue}==>#{Tty.white} #{@command.join(" ")}#{Tty.reset}"
    end

    def puts_result
      if ENV["TRAVIS"]
        travis_start_time = @start_time.to_i*1000000000
        travis_end_time = @end_time.to_i*1000000000
        travis_duration = travis_end_time - travis_start_time
        puts "travis_fold:end:#{@command.join(".")}"
        puts "travis_time:end:#{@travis_timer_id},start=#{travis_start_time},finish=#{travis_end_time},duration=#{travis_duration}"
      end
      if @status == :failed || true
        puts "#{Tty.red}==> FAILED:#{Tty.white} #{@command.join(" ")}#{Tty.reset}"
      end
    end

    def has_output?
      @output && !@output.empty?
    end

    def time
      @end_time - @start_time
    end

    def run
      @start_time = Time.now

      puts_command
      if ARGV.include? "--dry-run"
        @end_time = Time.now
        @status = :passed
        puts_result
        return
      end

      verbose = ARGV.verbose?
      puts if verbose && !ENV["TRAVIS"]
      @output = ""
      working_dir = Pathname.new(@command.first == "git" ? @repository : Dir.pwd)
      read, write = IO.pipe

      begin<commit_msg>test-bot: don't mark every job as failed.<commit_after># Comprehensively test a formula or pull request.
#
# Usage: brew test-bot [options...] <pull-request|formula>
#
# Options:
# --keep-logs:     Write and keep log files under ./brewbot/
# --cleanup:       Clean the Homebrew directory. Very dangerous. Use with care.
# --clean-cache:   Remove all cached downloads. Use with care.
# --skip-setup:    Don't check the local system is setup correctly.
# --skip-homebrew: Don't check Homebrew's files and tests are all valid.
# --junit:         Generate a JUnit XML test results file.
# --email:         Generate an email subject file.
# --no-bottle:     Run brew install without --build-bottle
# --keep-old:      Run brew bottle --keep-old to build new bottles for a single platform.
# --HEAD:          Run brew install with --HEAD
# --local:         Ask Homebrew to write verbose logs under ./logs/ and set HOME to ./home/
# --tap=<tap>:     Use the git repository of the given tap
# --dry-run:       Just print commands, don't run them.
# --fail-fast:     Immediately exit on a failing step.
# --verbose:       Print out all logs in realtime
#
# --ci-master:           Shortcut for Homebrew master branch CI options.
# --ci-pr:               Shortcut for Homebrew pull request CI options.
# --ci-testing:          Shortcut for Homebrew testing CI options.
# --ci-upload:           Homebrew CI bottle upload.
# --ci-reset-and-update: Homebrew CI repository and tap reset and update.

require "formula"
require "utils"
require "date"
require "rexml/document"
require "rexml/xmldecl"
require "rexml/cdata"
require "cmd/tap"

module Homebrew
  EMAIL_SUBJECT_FILE = "brew-test-bot.#{MacOS.cat}.email.txt"
  BYTES_IN_1_MEGABYTE = 1024*1024

  def homebrew_git_repo(tap = nil)
    if tap
      user, repo = tap.split "/"
      HOMEBREW_LIBRARY/"Taps/#{user}/homebrew-#{repo}"
    else
      HOMEBREW_REPOSITORY
    end
  end

  class Step
    attr_reader :command, :name, :status, :output, :time

    def initialize(test, command, options = {})
      @test = test
      @category = test.category
      @command = command
      @puts_output_on_success = options[:puts_output_on_success]
      @name = command[1].delete("-")
      @status = :running
      @repository = options[:repository] || HOMEBREW_REPOSITORY
      @time = 0
    end

    def log_file_path
      file = "#{@category}.#{@name}.txt"
      root = @test.log_root
      root ? root + file : file
    end

    def command_short
      (@command - %w[brew --force --retry --verbose --build-bottle --rb]).join(" ")
    end

    def passed?
      @status == :passed
    end

    def failed?
      @status == :failed
    end

    def puts_command
      if ENV["TRAVIS"]
        @travis_timer_id = rand(2**32).to_s(16)
        puts "travis_fold:start:#{@command.join(".")}"
        puts "travis_time:start:#{@travis_timer_id}"
      end
      puts "#{Tty.blue}==>#{Tty.white} #{@command.join(" ")}#{Tty.reset}"
    end

    def puts_result
      if ENV["TRAVIS"]
        travis_start_time = @start_time.to_i*1000000000
        travis_end_time = @end_time.to_i*1000000000
        travis_duration = travis_end_time - travis_start_time
        puts "travis_fold:end:#{@command.join(".")}"
        puts "travis_time:end:#{@travis_timer_id},start=#{travis_start_time},finish=#{travis_end_time},duration=#{travis_duration}"
      end
      if @status == :failed
        puts "#{Tty.red}==> FAILED:#{Tty.white} #{@command.join(" ")}#{Tty.reset}"
      end
    end

    def has_output?
      @output && !@output.empty?
    end

    def time
      @end_time - @start_time
    end

    def run
      @start_time = Time.now

      puts_command
      if ARGV.include? "--dry-run"
        @end_time = Time.now
        @status = :passed
        puts_result
        return
      end

      verbose = ARGV.verbose?
      puts if verbose && !ENV["TRAVIS"]
      @output = ""
      working_dir = Pathname.new(@command.first == "git" ? @repository : Dir.pwd)
      read, write = IO.pipe

      begin
{'fraction_non_alphanumeric': 0.12882367570256156, 'fraction_numerical': 0.008952996766973389, 'mean_word_length': 2.501523726599913, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3605897', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2640, 'n_words': 718}