test/cli/commands/test_watch.rb in nanoc-3.4.2 vs test/cli/commands/test_watch.rb in nanoc-3.4.3

- old
+ new

@@ -15,19 +15,16 @@ 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') + self.wait_until_content_equals('content/index.html', 'Hello there!') File.open('content/index.html', 'w') { |io| io.write('Hello there again!') } - sleep 1 - assert_equal 'Hello there again!', File.read('output/index.html') + self.wait_until_content_equals('content/index.html', 'Hello there again!') watch_thread.kill end end @@ -35,17 +32,40 @@ 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 + 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