2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "fileutils"
|
2015-11-09 00:01:38 -05:00
|
|
|
|
2015-11-10 10:21:33 -05:00
|
|
|
module FileUpdateCheckerSharedTests
|
2019-08-01 17:18:49 -04:00
|
|
|
def self.included(kls)
|
|
|
|
kls.class_eval do
|
|
|
|
extend ActiveSupport::Testing::Declarative
|
|
|
|
include FileUtils
|
2015-11-09 00:01:38 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
def tmpdir
|
|
|
|
@tmpdir
|
|
|
|
end
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
def tmpfile(name)
|
|
|
|
File.join(tmpdir, name)
|
|
|
|
end
|
2015-11-09 00:01:38 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
def tmpfiles
|
|
|
|
@tmpfiles ||= %w(foo.rb bar.rb baz.rb).map { |f| tmpfile(f) }
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
def run(*args)
|
|
|
|
capture_exceptions do
|
|
|
|
Dir.mktmpdir(nil, __dir__) { |dir| @tmpdir = dir; super }
|
|
|
|
end
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should not execute the block if no paths are given" do
|
|
|
|
silence_warnings { require "listen" }
|
|
|
|
i = 0
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker { i += 1 }
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should not execute the block if no files change" do
|
|
|
|
i = 0
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should execute the block once when files are created" do
|
|
|
|
i = 0
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfiles)
|
|
|
|
wait
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should execute the block once when files are modified" do
|
|
|
|
i = 0
|
2015-11-09 00:01:38 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfiles)
|
|
|
|
wait
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should execute the block once when files are deleted" do
|
|
|
|
i = 0
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
rm_f(tmpfiles)
|
|
|
|
wait
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "updated should become true when watched files are created" do
|
|
|
|
i = 0
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
|
|
|
assert_not_predicate checker, :updated?
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfiles)
|
|
|
|
wait
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
end
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "updated should become true when watched files are modified" do
|
|
|
|
i = 0
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
|
|
|
assert_not_predicate checker, :updated?
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfiles)
|
|
|
|
wait
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
end
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "updated should become true when watched files are deleted" do
|
|
|
|
i = 0
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
|
|
|
assert_not_predicate checker, :updated?
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
rm_f(tmpfiles)
|
|
|
|
wait
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
end
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should be robust to handle files with wrong modified time" do
|
|
|
|
i = 0
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 04:12:51 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
now = Time.now
|
|
|
|
time = Time.mktime(now.year + 1, now.month, now.day) # wrong mtime from the future
|
|
|
|
File.utime(time, time, tmpfiles[0])
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfiles[1..-1])
|
|
|
|
wait
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should return max_time for files with mtime = Time.at(0)" do
|
|
|
|
i = 0
|
2016-04-12 12:07:56 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
FileUtils.touch(tmpfiles)
|
2016-04-12 12:07:56 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
time = Time.at(0) # wrong mtime from the future
|
|
|
|
File.utime(time, time, tmpfiles[0])
|
2016-04-12 12:07:56 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2016-04-12 12:07:56 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfiles[1..-1])
|
|
|
|
wait
|
2016-04-12 12:07:56 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2016-04-12 12:07:56 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should cache updated result until execute" do
|
|
|
|
i = 0
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
|
|
|
assert_not_predicate checker, :updated?
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfiles)
|
|
|
|
wait
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
checker.execute
|
|
|
|
assert_not_predicate checker, :updated?
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should execute the block if files change in a watched directory one extension" do
|
|
|
|
i = 0
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker([], tmpdir => :rb) { i += 1 }
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfile("foo.rb"))
|
|
|
|
wait
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2019-03-15 16:19:23 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should execute the block if files change in a watched directory any extensions" do
|
|
|
|
i = 0
|
2019-03-15 16:19:23 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker([], tmpdir => []) { i += 1 }
|
2019-03-15 16:19:23 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfile("foo.rb"))
|
|
|
|
wait
|
2019-03-15 16:19:23 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should execute the block if files change in a watched directory several extensions" do
|
|
|
|
i = 0
|
2015-11-10 03:59:46 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker([], tmpdir => [:rb, :txt]) { i += 1 }
|
2015-11-10 03:59:46 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfile("foo.rb"))
|
|
|
|
wait
|
2015-11-10 03:59:46 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
2015-11-10 03:59:46 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfile("foo.txt"))
|
|
|
|
wait
|
2015-11-10 03:59:46 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 2, i
|
|
|
|
end
|
2015-11-10 03:59:46 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "should not execute the block if the file extension is not watched" do
|
|
|
|
i = 0
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker([], tmpdir => :txt) { i += 1 }
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfile("foo.rb"))
|
|
|
|
wait
|
2015-10-12 15:44:21 -04:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
|
|
|
end
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "does not assume files exist on instantiation" do
|
|
|
|
i = 0
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
non_existing = tmpfile("non_existing.rb")
|
|
|
|
checker = new_checker([non_existing]) { i += 1 }
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(non_existing)
|
|
|
|
wait
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "detects files in new subdirectories" do
|
|
|
|
i = 0
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker([], tmpdir => :rb) { i += 1 }
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
subdir = tmpfile("subdir")
|
|
|
|
mkdir(subdir)
|
|
|
|
wait
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(File.join(subdir, "nested.rb"))
|
|
|
|
wait
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "looked up extensions are inherited in subdirectories not listening to them" do
|
|
|
|
i = 0
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
subdir = tmpfile("subdir")
|
|
|
|
mkdir(subdir)
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
checker = new_checker([], tmpdir => :rb, subdir => :txt) { i += 1 }
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(tmpfile("new.txt"))
|
|
|
|
wait
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
# subdir does not look for Ruby files, but its parent tmpdir does.
|
|
|
|
touch(File.join(subdir, "nested.rb"))
|
|
|
|
wait
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
touch(File.join(subdir, "nested.txt"))
|
|
|
|
wait
|
2015-11-09 12:11:13 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 2, i
|
|
|
|
end
|
2017-01-27 10:53:21 -05:00
|
|
|
|
2019-08-01 17:18:49 -04:00
|
|
|
test "initialize raises an ArgumentError if no block given" do
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
new_checker([])
|
|
|
|
end
|
|
|
|
end
|
2017-01-27 10:53:21 -05:00
|
|
|
end
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
end
|