<commit_before># -*- encoding : utf-8 -*-

require 'rubygems'
require 'minitest/autorun'
$LOAD_PATH.unshift File.expand_path('../../lib')
require 'gitdocs'
require 'aruba'
require 'aruba/api'
require 'timeout'

module MiniTest::Aruba
  class ArubaApiWrapper
    include Aruba::Api
  end

  def aruba
    @aruba ||= ArubaApiWrapper.new
  end

  def run(*args)
    if args.length == 0
      super
    else
      aruba.run(*args)
    end
  end

  def method_missing(method, *args, &block)
    aruba.send(method, *args, &block)
  end

  def before_setup
    super
    original = (ENV['PATH'] || '').split(File::PATH_SEPARATOR)
    set_env('PATH', ([File.expand_path('bin')] + original).join(File::PATH_SEPARATOR))
    FileUtils.rm_rf(current_dir)
  end

  def after_teardown
    super
    restore_env
    processes.clear
  end
end

module Helper
  include MiniTest::Aruba

  def before_setup
    super
    set_env('HOME', abs_current_dir)
    ENV['TEST'] = nil
  end

  def after_teardown
    super

    terminate_processes!
    prep_for_fs_check do
      next unless File.exists?('gitdocs.pid')

      pid = IO.read('gitdocs.pid').to_i
      Process.kill('KILL', pid)
      begin
        Process.wait(pid)
      rescue SystemCallError
        # This means that the process is already gone.
        # Nothing to do.
      end
    end
  end

  # @overload abs_current_dir
  #   @return [String] absolute path for current_dir
  # @overload abs_current_dir(relative_path)
  #   @param [String] relative_path to the current directory
  #   @return [String] the absolute path
  def abs_current_dir(relative_path = nil)
    return File.absolute_path(File.join(current_dir)) unless relative_path
    File.absolute_path(File.join(current_dir, relative_path))
  end

  # @return [String] the absolute path for the repository
  def git_init_local(path = 'local')
    abs_path = abs_current_dir(path)
    Rugged::Repository.init_at(abs_path)
    abs_path
  end

  # @return [String] the absolute path for the repository
  def git_init_remote(path = 'remote')
    abs_path = abs_current_dir(path)
    Rugged::Repository.init_at(abs_path, :bare)
    abs_path
  end

  def wait_for_clean_workdir(path)
    dirty = true
    Timeout.timeout(10) do
      while dirty
        begin
          sleep(0.1)
          rugged = Rugged::Repository.new(abs_current_dir(path))
          dirty = !rugged.diff_workdir(rugged.head.target, include_untracked: true).deltas.empty?
        rescue Rugged::ReferenceError
          nil
        rescue Rugged::InvalidError
          nil
        rescue Rugged::RepositoryError
          nil
        end
      end
    end
  rescue Timeout::Error
    assert(false, "#{path} workdir is still dirty")
  end

  def wait_for_exact_file_content(file, exact_content)
    in_current_dir do
      begin
        Timeout.timeout(10) do
          sleep(0.1) until File.exist?(file) && IO.read(file) == exact_content
        end
      rescue Timeout::Error
        nil
      end

      assert(File.exist?(file), "Missing #{file}")
      actual_content = IO.read(file)
      assert(
        actual_content == exact_content,
        "Expected #{file} content: #{exact_content}\nActual content #{actual_content}"
      )
    end
  end

  def wait_for_directory(path)
    in_current_dir do
      begin
        Timeout.timeout(10) { sleep(0.1) until Dir.exist?(path) }
      rescue Timeout::Error
        nil
      end

      assert(Dir.exist?(path), "Missing #{path}")
    end
  end

  def wait_for_conflict_markers(path)
    in_current_dir do
      begin
        Timeout.timeout(10) { sleep(0.1) if File.exist?(path) }
      rescue Timeout::Error
        nil
      ensure
        assert(!File.exist?(path), "#{path} should have been removed")
      end

      begin
        Timeout.timeout(10) { sleep(0.1) if Dir.glob("#{path} (*)").empty? }
      rescue Timeout::Error
        nil
      ensure
        assert(!Dir.glob("#{path} (*)").empty?, "#{path} conflict marks should have been created")
      end
    end
  end

  def gitdocs_add(path = 'local')
    add_cmd = "gitdocs add #{path} --pid=gitdocs.pid"
    run_simple(add_cmd, true, 15)
    assert_success(true)
    assert_partial_output("Added path #{path} to doc list", output_from(add_cmd))
  end

  def git_clone_and_gitdocs_add(remote_path, *clone_paths)
    clone_paths.each do |clone_path|
      abs_clone_path = abs_current_dir(clone_path)
      FileUtils.rm_rf(abs_clone_path)
      repo = Rugged::Repository.clone_at(
        "file://#{remote_path}",
        abs_clone_path
      )
      repo.config['user.email'] = '<EMAIL>'
      repo.config['user.name']  = '<NAME>'
      gitdocs_add(clone_path)
    end
  end
<commit_msg>Increase all timeouts to 20 seconds<commit_after># -*- encoding : utf-8 -*-

require 'rubygems'
require 'minitest/autorun'
$LOAD_PATH.unshift File.expand_path('../../lib')
require 'gitdocs'
require 'aruba'
require 'aruba/api'
require 'timeout'

module MiniTest::Aruba
  class ArubaApiWrapper
    include Aruba::Api
  end

  def aruba
    @aruba ||= ArubaApiWrapper.new
  end

  def run(*args)
    if args.length == 0
      super
    else
      aruba.run(*args)
    end
  end

  def method_missing(method, *args, &block)
    aruba.send(method, *args, &block)
  end

  def before_setup
    super
    original = (ENV['PATH'] || '').split(File::PATH_SEPARATOR)
    set_env('PATH', ([File.expand_path('bin')] + original).join(File::PATH_SEPARATOR))
    FileUtils.rm_rf(current_dir)
  end

  def after_teardown
    super
    restore_env
    processes.clear
  end
end

module Helper
  include MiniTest::Aruba

  def before_setup
    super
    set_env('HOME', abs_current_dir)
    ENV['TEST'] = nil
  end

  def after_teardown
    super

    terminate_processes!
    prep_for_fs_check do
      next unless File.exists?('gitdocs.pid')

      pid = IO.read('gitdocs.pid').to_i
      Process.kill('KILL', pid)
      begin
        Process.wait(pid)
      rescue SystemCallError
        # This means that the process is already gone.
        # Nothing to do.
      end
    end
  end

  # @overload abs_current_dir
  #   @return [String] absolute path for current_dir
  # @overload abs_current_dir(relative_path)
  #   @param [String] relative_path to the current directory
  #   @return [String] the absolute path
  def abs_current_dir(relative_path = nil)
    return File.absolute_path(File.join(current_dir)) unless relative_path
    File.absolute_path(File.join(current_dir, relative_path))
  end

  # @return [String] the absolute path for the repository
  def git_init_local(path = 'local')
    abs_path = abs_current_dir(path)
    Rugged::Repository.init_at(abs_path)
    abs_path
  end

  # @return [String] the absolute path for the repository
  def git_init_remote(path = 'remote')
    abs_path = abs_current_dir(path)
    Rugged::Repository.init_at(abs_path, :bare)
    abs_path
  end

  def wait_for_clean_workdir(path)
    dirty = true
    Timeout.timeout(20) do
      while dirty
        begin
          sleep(0.1)
          rugged = Rugged::Repository.new(abs_current_dir(path))
          dirty = !rugged.diff_workdir(rugged.head.target, include_untracked: true).deltas.empty?
        rescue Rugged::ReferenceError
          nil
        rescue Rugged::InvalidError
          nil
        rescue Rugged::RepositoryError
          nil
        end
      end
    end
  rescue Timeout::Error
    assert(false, "#{path} workdir is still dirty")
  end

  def wait_for_exact_file_content(file, exact_content)
    in_current_dir do
      begin
        Timeout.timeout(20) do
          sleep(0.1) until File.exist?(file) && IO.read(file) == exact_content
        end
      rescue Timeout::Error
        nil
      end

      assert(File.exist?(file), "Missing #{file}")
      actual_content = IO.read(file)
      assert(
        actual_content == exact_content,
        "Expected #{file} content: #{exact_content}\nActual content #{actual_content}"
      )
    end
  end

  def wait_for_directory(path)
    in_current_dir do
      begin
        Timeout.timeout(20) { sleep(0.1) until Dir.exist?(path) }
      rescue Timeout::Error
        nil
      end

      assert(Dir.exist?(path), "Missing #{path}")
    end
  end

  def wait_for_conflict_markers(path)
    in_current_dir do
      begin
        Timeout.timeout(20) { sleep(0.1) if File.exist?(path) }
      rescue Timeout::Error
        nil
      ensure
        assert(!File.exist?(path), "#{path} should have been removed")
      end

      begin
        Timeout.timeout(20) { sleep(0.1) if Dir.glob("#{path} (*)").empty? }
      rescue Timeout::Error
        nil
      ensure
        assert(!Dir.glob("#{path} (*)").empty?, "#{path} conflict marks should have been created")
      end
    end
  end

  def gitdocs_add(path = 'local')
    add_cmd = "gitdocs add #{path} --pid=gitdocs.pid"
    run_simple(add_cmd, true, 15)
    assert_success(true)
    assert_partial_output("Added path #{path} to doc list", output_from(add_cmd))
  end

  def git_clone_and_gitdocs_add(remote_path, *clone_paths)
    clone_paths.each do |clone_path|
      abs_clone_path = abs_current_dir(clone_path)
      FileUtils.rm_rf(abs_clone_path)
      repo = Rugged::Repository.clone_at(
        "file://#{remote_path}",
        abs_clone_path
      )
      repo.config['user.email'] = '<EMAIL>'
      repo.config['user.name']  = '<NAME>'
      gitdocs_add(clone_path)
    end
  end

{'fraction_non_alphanumeric': 0.1082693947144075, 'fraction_numerical': 0.005328218243819267, 'mean_word_length': 2.799595141700405, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4417421', 'n_tokens_mistral': 3121, 'n_tokens_neox': 3077, 'n_words': 693}