<commit_before>require 'fileutils'

module FileUpdateCheckerSharedTests
  extend ActiveSupport::Testing::Declarative
  include FileUtils

  def tmpdir
    @tmpdir
  end

  def tmpfile(name)
    "#{tmpdir}/#{name}"
  end

  def tmpfiles
    @tmpfiles ||= %w(foo.rb bar.rb baz.rb).map { |f| tmpfile(f) }
  end

  def run(*args)
    Dir.mktmpdir(nil, __dir__) { |dir| @tmpdir = dir; super }
  end

  test 'should not execute the block if no paths are given' do
    silence_warnings { require 'listen' }
    i = 0

    checker = new_checker { i += 1 }

    assert !checker.execute_if_updated
    assert_equal 0, i
  end

  test 'should not execute the block if no files change' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }

    assert !checker.execute_if_updated
    assert_equal 0, i
  end

  test 'should execute the block once when files are created' do
    i = 0

    checker = new_checker(tmpfiles) { i += 1 }

    touch(tmpfiles)

    assert checker.execute_if_updated
    assert_equal 1, i
  end

  test 'should execute the block once when files are modified' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }

    touch(tmpfiles)

    assert checker.execute_if_updated
    assert_equal 1, i
  end

  test 'should execute the block once when files are deleted' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }

    rm_f(tmpfiles)

    assert checker.execute_if_updated
    assert_equal 1, i
  end


  test 'updated should become true when watched files are created' do
    i = 0

    checker = new_checker(tmpfiles) { i += 1 }
    assert !checker.updated?

    touch(tmpfiles)

    assert checker.updated?
  end


  test 'updated should become true when watched files are modified' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }
    assert !checker.updated?

    touch(tmpfiles)

    assert checker.updated?
  end

  test 'updated should become true when watched files are deleted' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }
    assert !checker.updated?

    rm_f(tmpfiles)

    assert checker.updated?
  end

  test 'should be robust to handle files with wrong modified time' do
    i = 0

    FileUtils.touch(tmpfiles)

    now  = Time.now
    time = Time.mktime(now.year + 1, now.month, now.day) # wrong mtime from the future
    File.utime(time, time, tmpfiles[0])

    checker = new_checker(tmpfiles) { i += 1 }

    touch(tmpfiles[1..-1])

    assert checker.execute_if_updated
    assert_equal 1, i
  end

  test 'should return max_time for files with mtime = Time.at(0)' do
    i = 0

    FileUtils.touch(tmpfiles)

    now  = Time.now
    time = Time.at(0) # wrong mtime from the future
    File.utime(time, time, tmpfiles[0])

    checker = new_checker(tmpfiles) { i += 1 }

    touch(tmpfiles[1..-1])

    assert checker.execute_if_updated
    assert_equal 1, i
  end

  test 'should cache updated result until execute' do
    i = 0

    checker = new_checker(tmpfiles) { i += 1 }
    assert !checker.updated?

    touch(tmpfiles)

    assert checker.updated?
    checker.execute<commit_msg>Remove unused variable<commit_after>require 'fileutils'

module FileUpdateCheckerSharedTests
  extend ActiveSupport::Testing::Declarative
  include FileUtils

  def tmpdir
    @tmpdir
  end

  def tmpfile(name)
    "#{tmpdir}/#{name}"
  end

  def tmpfiles
    @tmpfiles ||= %w(foo.rb bar.rb baz.rb).map { |f| tmpfile(f) }
  end

  def run(*args)
    Dir.mktmpdir(nil, __dir__) { |dir| @tmpdir = dir; super }
  end

  test 'should not execute the block if no paths are given' do
    silence_warnings { require 'listen' }
    i = 0

    checker = new_checker { i += 1 }

    assert !checker.execute_if_updated
    assert_equal 0, i
  end

  test 'should not execute the block if no files change' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }

    assert !checker.execute_if_updated
    assert_equal 0, i
  end

  test 'should execute the block once when files are created' do
    i = 0

    checker = new_checker(tmpfiles) { i += 1 }

    touch(tmpfiles)

    assert checker.execute_if_updated
    assert_equal 1, i
  end

  test 'should execute the block once when files are modified' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }

    touch(tmpfiles)

    assert checker.execute_if_updated
    assert_equal 1, i
  end

  test 'should execute the block once when files are deleted' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }

    rm_f(tmpfiles)

    assert checker.execute_if_updated
    assert_equal 1, i
  end


  test 'updated should become true when watched files are created' do
    i = 0

    checker = new_checker(tmpfiles) { i += 1 }
    assert !checker.updated?

    touch(tmpfiles)

    assert checker.updated?
  end


  test 'updated should become true when watched files are modified' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }
    assert !checker.updated?

    touch(tmpfiles)

    assert checker.updated?
  end

  test 'updated should become true when watched files are deleted' do
    i = 0

    FileUtils.touch(tmpfiles)

    checker = new_checker(tmpfiles) { i += 1 }
    assert !checker.updated?

    rm_f(tmpfiles)

    assert checker.updated?
  end

  test 'should be robust to handle files with wrong modified time' do
    i = 0

    FileUtils.touch(tmpfiles)

    now  = Time.now
    time = Time.mktime(now.year + 1, now.month, now.day) # wrong mtime from the future
    File.utime(time, time, tmpfiles[0])

    checker = new_checker(tmpfiles) { i += 1 }

    touch(tmpfiles[1..-1])

    assert checker.execute_if_updated
    assert_equal 1, i
  end

  test 'should return max_time for files with mtime = Time.at(0)' do
    i = 0

    FileUtils.touch(tmpfiles)

    time = Time.at(0) # wrong mtime from the future
    File.utime(time, time, tmpfiles[0])

    checker = new_checker(tmpfiles) { i += 1 }

    touch(tmpfiles[1..-1])

    assert checker.execute_if_updated
    assert_equal 1, i
  end

  test 'should cache updated result until execute' do
    i = 0

    checker = new_checker(tmpfiles) { i += 1 }
    assert !checker.updated?

    touch(tmpfiles)

    assert checker.updated?
    checker.execute
{'fraction_non_alphanumeric': 0.08897845295303054, 'fraction_numerical': 0.011781119206324601, 'mean_word_length': 2.7997644287396937, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5321095', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2122, 'n_words': 653}