2017-07-09 20:06:36 +08:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 21:39:13 +08:00
|
|
|
|
2016-08-07 00:03:25 +08:00
|
|
|
require "fileutils"
|
2015-11-09 13:01:38 +08:00
|
|
|
|
2015-11-10 23:21:33 +08:00
|
|
|
module FileUpdateCheckerSharedTests
|
2019-08-02 05:18:49 +08:00
|
|
|
def self.included(kls)
|
|
|
|
kls.class_eval do
|
|
|
|
extend ActiveSupport::Testing::Declarative
|
2015-11-09 13:01:38 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
def tmpdir
|
|
|
|
@tmpdir
|
|
|
|
end
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
def tmpfile(name)
|
|
|
|
File.join(tmpdir, name)
|
|
|
|
end
|
2015-11-09 13:01:38 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
def tmpfiles
|
|
|
|
@tmpfiles ||= %w(foo.rb bar.rb baz.rb).map { |f| tmpfile(f) }
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
def run(*args)
|
|
|
|
capture_exceptions do
|
|
|
|
Dir.mktmpdir(nil, __dir__) { |dir| @tmpdir = dir; super }
|
|
|
|
end
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should not execute the block if no paths are given" do
|
|
|
|
silence_warnings { require "listen" }
|
|
|
|
i = 0
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker { i += 1 }
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should not execute the block if no files change" do
|
|
|
|
i = 0
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should execute the block once when files are created" do
|
|
|
|
i = 0
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfiles)
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should execute the block once when files are modified" do
|
|
|
|
i = 0
|
2015-11-09 13:01:38 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should execute the block once when files are deleted" do
|
|
|
|
i = 0
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
rm_f(tmpfiles)
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "updated should become true when watched files are created" do
|
|
|
|
i = 0
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
|
|
|
assert_not_predicate checker, :updated?
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
end
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "updated should become true when watched files are modified" do
|
|
|
|
i = 0
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
|
|
|
assert_not_predicate checker, :updated?
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
end
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "updated should become true when watched files are deleted" do
|
|
|
|
i = 0
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
|
|
|
assert_not_predicate checker, :updated?
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
rm_f(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
end
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should be robust to handle files with wrong modified time" do
|
|
|
|
i = 0
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
FileUtils.touch(tmpfiles)
|
2015-11-10 17:12:51 +08:00
|
|
|
|
2019-08-02 05:18:49 +08: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-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfiles[1..-1])
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should return max_time for files with mtime = Time.at(0)" do
|
|
|
|
i = 0
|
2016-04-13 00:07:56 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
FileUtils.touch(tmpfiles)
|
2016-04-13 00:07:56 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
time = Time.at(0) # wrong mtime from the future
|
|
|
|
File.utime(time, time, tmpfiles[0])
|
2016-04-13 00:07:56 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
2016-04-13 00:07:56 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfiles[1..-1])
|
2016-04-13 00:07:56 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2016-04-13 00:07:56 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should cache updated result until execute" do
|
|
|
|
i = 0
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker(tmpfiles) { i += 1 }
|
|
|
|
assert_not_predicate checker, :updated?
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfiles)
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
checker.execute
|
|
|
|
assert_not_predicate checker, :updated?
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should execute the block if files change in a watched directory one extension" do
|
|
|
|
i = 0
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker([], tmpdir => :rb) { i += 1 }
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfile("foo.rb"))
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2019-03-16 04:19:23 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should execute the block if files change in a watched directory any extensions" do
|
|
|
|
i = 0
|
2019-03-16 04:19:23 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker([], tmpdir => []) { i += 1 }
|
2019-03-16 04:19:23 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfile("foo.rb"))
|
2019-03-16 04:19:23 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should execute the block if files change in a watched directory several extensions" do
|
|
|
|
i = 0
|
2015-11-10 16:59:46 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker([], tmpdir => [:rb, :txt]) { i += 1 }
|
2015-11-10 16:59:46 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfile("foo.rb"))
|
2015-11-10 16:59:46 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
2015-11-10 16:59:46 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfile("foo.txt"))
|
2015-11-10 16:59:46 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 2, i
|
|
|
|
end
|
2015-11-10 16:59:46 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "should not execute the block if the file extension is not watched" do
|
|
|
|
i = 0
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker([], tmpdir => :txt) { i += 1 }
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfile("foo.rb"))
|
2015-10-13 03:44:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
|
|
|
end
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "does not assume files exist on instantiation" do
|
|
|
|
i = 0
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
non_existing = tmpfile("non_existing.rb")
|
|
|
|
checker = new_checker([non_existing]) { i += 1 }
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(non_existing)
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "detects files in new subdirectories" do
|
|
|
|
i = 0
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker([], tmpdir => :rb) { i += 1 }
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
subdir = tmpfile("subdir")
|
|
|
|
mkdir(subdir)
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(File.join(subdir, "nested.rb"))
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
|
|
|
end
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "looked up extensions are inherited in subdirectories not listening to them" do
|
|
|
|
i = 0
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
subdir = tmpfile("subdir")
|
Avoid double wait in EventedFileUpdateCheckerTest
Waiting after touching the file system is a concern of
`EventedFileUpdateCheckerTest`. Therefore, only call `wait` inside
`EventedFileUpdateCheckerTest`. This avoids calling `wait` an extra
time when calling `touch`.
Before:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 43.357019s, 0.9918 runs/s, 2.5371 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
After:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 34.351007s, 1.2518 runs/s, 3.2022 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
2021-07-23 00:48:33 +08:00
|
|
|
FileUtils.mkdir(subdir)
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
checker = new_checker([], tmpdir => :rb, subdir => :txt) { i += 1 }
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(tmpfile("new.txt"))
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert_not checker.execute_if_updated
|
|
|
|
assert_equal 0, i
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
# subdir does not look for Ruby files, but its parent tmpdir does.
|
|
|
|
touch(File.join(subdir, "nested.rb"))
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 1, i
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
touch(File.join(subdir, "nested.txt"))
|
2015-11-10 01:11:13 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
assert checker.execute_if_updated
|
|
|
|
assert_equal 2, i
|
|
|
|
end
|
2017-01-27 23:53:21 +08:00
|
|
|
|
2019-08-02 05:18:49 +08:00
|
|
|
test "initialize raises an ArgumentError if no block given" do
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
new_checker([])
|
|
|
|
end
|
|
|
|
end
|
2017-01-27 23:53:21 +08:00
|
|
|
end
|
|
|
|
end
|
Avoid double wait in EventedFileUpdateCheckerTest
Waiting after touching the file system is a concern of
`EventedFileUpdateCheckerTest`. Therefore, only call `wait` inside
`EventedFileUpdateCheckerTest`. This avoids calling `wait` an extra
time when calling `touch`.
Before:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 43.357019s, 0.9918 runs/s, 2.5371 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
After:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 34.351007s, 1.2518 runs/s, 3.2022 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
2021-07-23 00:48:33 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
def mkdir(dirs)
|
|
|
|
FileUtils.mkdir(dirs)
|
|
|
|
end
|
|
|
|
|
|
|
|
def touch(files)
|
|
|
|
FileUtils.touch(files)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rm_f(files)
|
|
|
|
FileUtils.rm_f(files)
|
|
|
|
end
|
2015-10-13 02:41:14 +08:00
|
|
|
end
|