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