diff options
Diffstat (limited to 'activesupport/test/file_update_checker_shared_tests.rb')
-rw-r--r-- | activesupport/test/file_update_checker_shared_tests.rb | 104 |
1 files changed, 69 insertions, 35 deletions
diff --git a/activesupport/test/file_update_checker_shared_tests.rb b/activesupport/test/file_update_checker_shared_tests.rb index 9c07e38fe5..48cd387196 100644 --- a/activesupport/test/file_update_checker_shared_tests.rb +++ b/activesupport/test/file_update_checker_shared_tests.rb @@ -1,27 +1,29 @@ -require 'fileutils' +require "fileutils" module FileUpdateCheckerSharedTests extend ActiveSupport::Testing::Declarative include FileUtils def tmpdir - @tmpdir ||= Dir.mktmpdir(nil, __dir__) + @tmpdir end def tmpfile(name) - "#{tmpdir}/#{name}" + File.join(tmpdir, name) end def tmpfiles @tmpfiles ||= %w(foo.rb bar.rb baz.rb).map { |f| tmpfile(f) } end - def teardown - FileUtils.rm_rf(@tmpdir) if defined? @tmpdir + def run(*args) + capture_exceptions do + Dir.mktmpdir(nil, __dir__) { |dir| @tmpdir = dir; super } + end end - test 'should not execute the block if no paths are given' do - silence_warnings { require 'listen' } + test "should not execute the block if no paths are given" do + silence_warnings { require "listen" } i = 0 checker = new_checker { i += 1 } @@ -30,7 +32,7 @@ module FileUpdateCheckerSharedTests assert_equal 0, i end - test 'should not execute the block if no files change' do + test "should not execute the block if no files change" do i = 0 FileUtils.touch(tmpfiles) @@ -41,18 +43,19 @@ module FileUpdateCheckerSharedTests assert_equal 0, i end - test 'should execute the block once when files are created' do + test "should execute the block once when files are created" do i = 0 checker = new_checker(tmpfiles) { i += 1 } touch(tmpfiles) + wait assert checker.execute_if_updated assert_equal 1, i end - test 'should execute the block once when files are modified' do + test "should execute the block once when files are modified" do i = 0 FileUtils.touch(tmpfiles) @@ -60,12 +63,13 @@ module FileUpdateCheckerSharedTests checker = new_checker(tmpfiles) { i += 1 } touch(tmpfiles) + wait assert checker.execute_if_updated assert_equal 1, i end - test 'should execute the block once when files are deleted' do + test "should execute the block once when files are deleted" do i = 0 FileUtils.touch(tmpfiles) @@ -73,25 +77,25 @@ module FileUpdateCheckerSharedTests checker = new_checker(tmpfiles) { i += 1 } rm_f(tmpfiles) + wait assert checker.execute_if_updated assert_equal 1, i end - - test 'updated should become true when watched files are created' do + test "updated should become true when watched files are created" do i = 0 checker = new_checker(tmpfiles) { i += 1 } assert !checker.updated? touch(tmpfiles) + wait assert checker.updated? end - - test 'updated should become true when watched files are modified' do + test "updated should become true when watched files are modified" do i = 0 FileUtils.touch(tmpfiles) @@ -100,11 +104,12 @@ module FileUpdateCheckerSharedTests assert !checker.updated? touch(tmpfiles) + wait assert checker.updated? end - test 'updated should become true when watched files are deleted' do + test "updated should become true when watched files are deleted" do i = 0 FileUtils.touch(tmpfiles) @@ -113,11 +118,12 @@ module FileUpdateCheckerSharedTests assert !checker.updated? rm_f(tmpfiles) + wait assert checker.updated? end - test 'should be robust to handle files with wrong modified time' do + test "should be robust to handle files with wrong modified time" do i = 0 FileUtils.touch(tmpfiles) @@ -129,112 +135,140 @@ module FileUpdateCheckerSharedTests checker = new_checker(tmpfiles) { i += 1 } touch(tmpfiles[1..-1]) + wait + + assert checker.execute_if_updated + assert_equal 1, i + end + + test "should return max_time for files with mtime = Time.at(0)" do + 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]) + wait assert checker.execute_if_updated assert_equal 1, i end - test 'should cache updated result until execute' do + test "should cache updated result until execute" do i = 0 checker = new_checker(tmpfiles) { i += 1 } assert !checker.updated? touch(tmpfiles) + wait assert checker.updated? checker.execute assert !checker.updated? end - test 'should execute the block if files change in a watched directory one extension' do + test "should execute the block if files change in a watched directory one extension" do i = 0 checker = new_checker([], tmpdir => :rb) { i += 1 } - touch(tmpfile('foo.rb')) + touch(tmpfile("foo.rb")) + wait assert checker.execute_if_updated assert_equal 1, i end - test 'should execute the block if files change in a watched directory several extensions' do + test "should execute the block if files change in a watched directory several extensions" do i = 0 checker = new_checker([], tmpdir => [:rb, :txt]) { i += 1 } - touch(tmpfile('foo.rb')) + touch(tmpfile("foo.rb")) + wait assert checker.execute_if_updated assert_equal 1, i - touch(tmpfile('foo.txt')) + touch(tmpfile("foo.txt")) + wait assert checker.execute_if_updated assert_equal 2, i end - test 'should not execute the block if the file extension is not watched' do + test "should not execute the block if the file extension is not watched" do i = 0 checker = new_checker([], tmpdir => :txt) { i += 1 } - touch(tmpfile('foo.rb')) + touch(tmpfile("foo.rb")) + wait assert !checker.execute_if_updated assert_equal 0, i end - test 'does not assume files exist on instantiation' do + test "does not assume files exist on instantiation" do i = 0 - non_existing = tmpfile('non_existing.rb') + non_existing = tmpfile("non_existing.rb") checker = new_checker([non_existing]) { i += 1 } touch(non_existing) + wait assert checker.execute_if_updated assert_equal 1, i end - test 'detects files in new subdirectories' do + test "detects files in new subdirectories" do i = 0 checker = new_checker([], tmpdir => :rb) { i += 1 } - subdir = tmpfile('subdir') + subdir = tmpfile("subdir") mkdir(subdir) wait assert !checker.execute_if_updated assert_equal 0, i - touch("#{subdir}/nested.rb") + touch(File.join(subdir, "nested.rb")) + wait assert checker.execute_if_updated assert_equal 1, i end - test 'looked up extensions are inherited in subdirectories not listening to them' do + test "looked up extensions are inherited in subdirectories not listening to them" do i = 0 - subdir = tmpfile('subdir') + subdir = tmpfile("subdir") mkdir(subdir) checker = new_checker([], tmpdir => :rb, subdir => :txt) { i += 1 } - touch(tmpfile('new.txt')) + touch(tmpfile("new.txt")) + wait assert !checker.execute_if_updated assert_equal 0, i # subdir does not look for Ruby files, but its parent tmpdir does. - touch("#{subdir}/nested.rb") + touch(File.join(subdir, "nested.rb")) + wait assert checker.execute_if_updated assert_equal 1, i - touch("#{subdir}/nested.txt") + touch(File.join(subdir, "nested.txt")) + wait assert checker.execute_if_updated assert_equal 2, i |