<commit_before># frozen_string_literal: true

require 'bacon'
begin
  require 'pry-byebug'
rescue LoadError
  nil
end

class WatchRun
  TMP_DIR = File.join(__dir__, 'tmp')

  attr_reader :filewatcher, :thread, :watched, :processed

  def initialize(
    filename: File.join(TMP_DIR, 'tmp_file.txt'),
    directory: false,
    filewatcher: Filewatcher.new(File.join(TMP_DIR, '**', '*')),
    action: :update,
    interval: 0.1
  )
    @filename = filename
    @directory = directory
    @filewatcher = filewatcher
    @action = action
    @filewatcher.interval = @interval = interval
  end

  def start
    File.write(@filename, 'content1') unless @action == :create

    @thread = thread_initialize
    sleep 1 # thread needs a chance to start
  end

  def run
    start

    make_changes
    # Some OS, filesystems and Ruby interpretators
    # doesn't catch milliseconds of `File.mtime`
    sleep 1

    stop
  end

  def stop
    FileUtils.rm_r(@filename) if File.exist?(@filename)
    @thread.exit
    sleep 1
  end

  private

  def thread_initialize
    @watched ||= 0
    Thread.new(
      @filewatcher, @processed = []
    ) do |filewatcher, processed|
      filewatcher.watch do |changes|
        increment_watched
        processed.concat(changes.keys)
      end
    end
  end

  def increment_watched
    @watched += 1
  end

  def make_changes
    return FileUtils.remove(@filename) if @action == :delete
    return FileUtils.mkdir_p(@filename) if @directory
    File.write(@filename, 'content2')
  end
end

class ShellWatchRun
  EXECUTABLE = "#{'ruby ' if Gem.win_platform?}" \
    "#{File.realpath File.join(__dir__, '..', 'bin', 'filewatcher')}".freeze

  attr_reader :output

  def initialize(
    options: '',
    dumper: :watched,
    output: File.join(WatchRun::TMP_DIR, 'env')
  )
    @options = options
    @dumper = dumper
    @output = output
  end

  def start
    @pid = spawn(
      "#{EXECUTABLE} #{@options} \"#{WatchRun::TMP_DIR}/foo*\"" \
        " \"ruby #{File.join(__dir__, 'dumpers', "#{@dumper}_dumper.rb")}\""
    )
    Process.detach(@pid)
    sleep 10
  end

  def run
    start

    make_changes

    stop
  end

  def stop
    Process.kill('KILL', @pid)
    sleep 4
  end

  private

  def make_changes
    FileUtils.touch "#{WatchRun::TMP_DIR}/foo.txt"
    sleep 10
  end
end

def include_all_files(elements)
  ->(it) { elements.all? { |element| it.include? File.expand_path(element) } }
end

def dump_to_env_file(content)
  File.write File.join(WatchRun::TMP_DIR, 'env'), content
end<commit_msg>Increase sleeping in tests for successful JRuby on macOS builds<commit_after># frozen_string_literal: true

require 'bacon'
begin
  require 'pry-byebug'
rescue LoadError
  nil
end

class WatchRun
  TMP_DIR = File.join(__dir__, 'tmp')

  attr_reader :filewatcher, :thread, :watched, :processed

  def initialize(
    filename: File.join(TMP_DIR, 'tmp_file.txt'),
    directory: false,
    filewatcher: Filewatcher.new(File.join(TMP_DIR, '**', '*')),
    action: :update,
    interval: 0.1
  )
    @filename = filename
    @directory = directory
    @filewatcher = filewatcher
    @action = action
    @filewatcher.interval = @interval = interval
  end

  def start
    File.write(@filename, 'content1') unless @action == :create

    @thread = thread_initialize
    sleep 3 # thread needs a chance to start
  end

  def run
    start

    make_changes
    # Some OS, filesystems and Ruby interpretators
    # doesn't catch milliseconds of `File.mtime`
    sleep 3

    stop
  end

  def stop
    FileUtils.rm_r(@filename) if File.exist?(@filename)
    @thread.exit
    sleep 3
  end

  private

  def thread_initialize
    @watched ||= 0
    Thread.new(
      @filewatcher, @processed = []
    ) do |filewatcher, processed|
      filewatcher.watch do |changes|
        increment_watched
        processed.concat(changes.keys)
      end
    end
  end

  def increment_watched
    @watched += 1
  end

  def make_changes
    return FileUtils.remove(@filename) if @action == :delete
    return FileUtils.mkdir_p(@filename) if @directory
    File.write(@filename, 'content2')
  end
end

class ShellWatchRun
  EXECUTABLE = "#{'ruby ' if Gem.win_platform?}" \
    "#{File.realpath File.join(__dir__, '..', 'bin', 'filewatcher')}".freeze

  attr_reader :output

  def initialize(
    options: '',
    dumper: :watched,
    output: File.join(WatchRun::TMP_DIR, 'env')
  )
    @options = options
    @dumper = dumper
    @output = output
  end

  def start
    @pid = spawn(
      "#{EXECUTABLE} #{@options} \"#{WatchRun::TMP_DIR}/foo*\"" \
        " \"ruby #{File.join(__dir__, 'dumpers', "#{@dumper}_dumper.rb")}\""
    )
    Process.detach(@pid)
    sleep 12
  end

  def run
    start

    make_changes

    stop
  end

  def stop
    Process.kill('KILL', @pid)
    sleep 6
  end

  private

  def make_changes
    FileUtils.touch "#{WatchRun::TMP_DIR}/foo.txt"
    sleep 12
  end
end

def include_all_files(elements)
  ->(it) { elements.all? { |element| it.include? File.expand_path(element) } }
end

def dump_to_env_file(content)
  File.write File.join(WatchRun::TMP_DIR, 'env'), content
end
{'fraction_non_alphanumeric': 0.11982825917252146, 'fraction_numerical': 0.00546448087431694, 'mean_word_length': 3.0164576802507836, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7079927', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1724, 'n_words': 430}