Sha256: 192cc6a8bb004c23604c591de8d6173f053fe14e563720f0465780c6be2d20a1

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

# 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

      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)
    20.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)

    20.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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nanoc-3.4.3 test/cli/commands/test_watch.rb