Sha256: b7f6f4c1411dee6ffb91421e21a4d08085ab05aab514762f4255c7e30e76757c

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

# 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)
    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

4 entries across 4 versions & 1 rubygems

Version Path
nanoc-3.6.0 test/cli/commands/test_watch.rb
nanoc-3.5.0 test/cli/commands/test_watch.rb
nanoc-3.5.0b2 test/cli/commands/test_watch.rb
nanoc-3.5.0b1 test/cli/commands/test_watch.rb