<commit_before># encoding: utf-8

class Nanoc::CLI::Commands::WatchTest < MiniTest::Unit::TestCase

  include Nanoc::TestHelpers

  def setup
    super
    @@warned ||= begin
      STDERR.puts "\n(fssm deprecation warning can be ignored; master branch uses guard/listen)"
      true
    end
  end

  def test_run
    with_site do |s|
      watch_thread = Thread.new do
        Nanoc::CLI.run %w( watch )
      end
      sleep 1

      File.open('content/index.html', 'w') { |io| io.write('Hello there!') }
      sleep 1
      assert_equal 'Hello there!', File.read('output/index.html')

      File.open('content/index.html', 'w') { |io| io.write('Hello there again!') }
      sleep 1
      assert_equal 'Hello there again!', File.read('output/index.html')

      watch_thread.kill
    end
  end

  def test_notify
    old_path = ENV['PATH']
    with_site do |s|
      watch_thread = Thread.new do
        Nanoc::CLI.run %w( watch )
      end
      sleep 1

      ENV['PATH'] = '.' # so that neither which nor where can be found
      File.open('content/index.html', 'w') { |io| io.write('Hello there!') }
      sleep 1
      assert_equal 'Hello there!', File.read('output/index.html')

      watch_thread.kill
    end
  ensure
    ENV['PATH'] = old_path
  end

end<commit_msg>Backport watch test case from master (because this one sucks)<commit_after># encoding: utf-8

class Nanoc::CLI::Commands::WatchTest < MiniTest::Unit::TestCase

  include Nanoc::TestHelpers

  def test_run
    with_site do |s|
      watch_thread = Thread.new do
        Nanoc::CLI.run %w( watch )
      end

      File.open('content/index.html', 'w') { |io| io.write('Hello there!') }
      self.wait_until_content_equals('content/index.html', 'Hello there!')

      File.open('content/index.html', 'w') { |io| io.write('Hello there again!') }
      self.wait_until_content_equals('content/index.html', 'Hello there again!')

      watch_thread.kill
    end
  end

  def test_notify
    old_path = ENV['PATH']
    with_site do |s|
      watch_thread = Thread.new do
        Nanoc::CLI.run %w( watch )
      end

      ENV['PATH'] = '.' # so that neither which nor where can be found
      File.open('content/index.html', 'w') { |io| io.write('Hello there!') }
      self.wait_until_exists('output/index.html')
      assert_equal 'Hello there!', File.read('output/index.html')

      watch_thread.kill
    end
  ensure
    ENV['PATH'] = old_path
  end

  def wait_until_exists(filename)
    10.times do
      break if File.file?(filename)
      sleep 0.5
    end
    if !File.file?(filename)
      raise RuntimeError, "Expected #{filename} to appear but it didn't :("
    end
  end

  def wait_until_content_equals(filename, content)
    self.wait_until_exists(filename)

    10.times do
      break if File.read(filename) == content
      sleep 0.5
    end
    if File.read(filename) != content
      raise RuntimeError, "Expected #{filename} to have content #{content} but it doesn't :("
    end
  end

end
{'fraction_non_alphanumeric': 0.12743125419181758, 'fraction_numerical': 0.005030181086519115, 'mean_word_length': 2.7664141414141414, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5502774', 'n_tokens_mistral': 1005, 'n_tokens_neox': 963, 'n_words': 254}