diff options
author | Xavier Noria <fxn@hashref.com> | 2015-11-08 21:01:38 -0800 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2015-11-08 22:49:51 -0800 |
commit | 60de179c0970d9ca9b7f6f7f2d49d4c428b96afa (patch) | |
tree | 1631379cfae3e52e2f3a0b43578247922de06416 /activesupport | |
parent | e8fbf68aa1c69c73595d32c6a6299fe4baa79108 (diff) | |
download | rails-60de179c0970d9ca9b7f6f7f2d49d4c428b96afa.tar.gz rails-60de179c0970d9ca9b7f6f7f2d49d4c428b96afa.tar.bz2 rails-60de179c0970d9ca9b7f6f7f2d49d4c428b96afa.zip |
refactors the file monitors test suite
In particular files are no longer created in the current working directory,
but in a temporary folder.
Diffstat (limited to 'activesupport')
3 files changed, 47 insertions, 57 deletions
diff --git a/activesupport/test/file_evented_update_checker_test.rb b/activesupport/test/file_evented_update_checker_test.rb index a720b912d0..72cf9bd387 100644 --- a/activesupport/test/file_evented_update_checker_test.rb +++ b/activesupport/test/file_evented_update_checker_test.rb @@ -1,13 +1,11 @@ require 'abstract_unit' -require 'fileutils' -require 'thread' require 'pathname' require 'file_update_checker_with_enumerable_test_cases' class FileEventedUpdateCheckerTest < ActiveSupport::TestCase include FileUpdateCheckerWithEnumerableTestCases - def build_new_watcher(files, dirs={}, &block) + def build_new_watcher(files=[], dirs={}, &block) ActiveSupport::FileEventedUpdateChecker.new(files, dirs, &block) end @@ -15,6 +13,10 @@ class FileEventedUpdateCheckerTest < ActiveSupport::TestCase super Listen.stop end + + def wait + sleep 0.5 + end end class FileEventedUpdateCheckerPathHelperTest < ActiveSupport::TestCase diff --git a/activesupport/test/file_update_checker_test.rb b/activesupport/test/file_update_checker_test.rb index 313c818691..9c7198caa3 100644 --- a/activesupport/test/file_update_checker_test.rb +++ b/activesupport/test/file_update_checker_test.rb @@ -1,12 +1,10 @@ require 'abstract_unit' -require 'fileutils' -require 'thread' require 'file_update_checker_with_enumerable_test_cases' class FileUpdateCheckerTest < ActiveSupport::TestCase include FileUpdateCheckerWithEnumerableTestCases - def build_new_watcher(files, dirs={}, &block) + def build_new_watcher(files=[], dirs={}, &block) ActiveSupport::FileUpdateChecker.new(files, dirs, &block) end end diff --git a/activesupport/test/file_update_checker_with_enumerable_test_cases.rb b/activesupport/test/file_update_checker_with_enumerable_test_cases.rb index c7b5ffb7a5..e9c54880ce 100644 --- a/activesupport/test/file_update_checker_with_enumerable_test_cases.rb +++ b/activesupport/test/file_update_checker_with_enumerable_test_cases.rb @@ -1,29 +1,36 @@ +require 'fileutils' + module FileUpdateCheckerWithEnumerableTestCases - FILES = %w(1.txt 2.txt 3.txt) + include FileUtils + + def wait + # noop + end def setup - FileUtils.mkdir_p('tmp_watcher') - FileUtils.touch(FILES) + @tmpdir = Dir.mktmpdir + + @files = %w(foo.rb bar.rb baz.rb).map {|f| "#{@tmpdir}/#{f}"} + touch(@files) end def teardown - FileUtils.rm_rf('tmp_watcher') - FileUtils.rm_rf(FILES) + rm_rf(@tmpdir) end def test_should_not_execute_the_block_if_no_paths_are_given i = 0 - checker = build_new_watcher([]) { i += 1 } - checker.execute_if_updated + checker = build_new_watcher { i += 1 } + assert !checker.execute_if_updated assert_equal 0, i end def test_should_not_invoke_the_block_if_no_file_has_changed i = 0 - checker = build_new_watcher(FILES) { i += 1 } + checker = build_new_watcher(@files) { i += 1 } assert !checker.execute_if_updated assert_equal 0, i @@ -32,22 +39,24 @@ module FileUpdateCheckerWithEnumerableTestCases def test_should_invoke_the_block_if_a_file_has_changed i = 0 - checker = build_new_watcher(FILES) { i += 1 } - sleep 1 + checker = build_new_watcher(@files) { i += 1 } - FileUtils.touch(FILES) sleep 1 + touch(@files) + wait assert checker.execute_if_updated assert_equal 1, i end def test_updated_should_become_true_when_watched_files_are_deleted - watcher = build_new_watcher(FILES) { i += 1 } + i = 0 + + watcher = build_new_watcher(@files) { i += 1 } assert !watcher.updated? - FileUtils.rm(FILES) - sleep 1 + rm_f(@files) + wait assert watcher.updated? end @@ -55,10 +64,10 @@ module FileUpdateCheckerWithEnumerableTestCases def test_should_be_robust_enough_to_handle_deleted_files i = 0 - checker = build_new_watcher(FILES) { i += 1 } - FileUtils.rm_f(FILES) + checker = build_new_watcher(@files) { i += 1 } - sleep 1 + rm_f(@files) + wait assert checker.execute_if_updated assert_equal 1, i @@ -69,13 +78,13 @@ module FileUpdateCheckerWithEnumerableTestCases now = Time.now time = Time.mktime(now.year + 1, now.month, now.day) # wrong mtime from the future - File.utime(time, time, FILES[2]) + File.utime(time, time, @files[0]) - checker = build_new_watcher(FILES) { i += 1 } - sleep 1 + checker = build_new_watcher(@files) { i += 1 } - FileUtils.touch(FILES[0..1]) sleep 1 + touch(@files[1..-1]) + wait assert checker.execute_if_updated assert_equal 1, i @@ -84,59 +93,40 @@ module FileUpdateCheckerWithEnumerableTestCases def test_should_cache_updated_result_until_execute i = 0 - checker = build_new_watcher(FILES) { i += 1 } + checker = build_new_watcher(@files) { i += 1 } assert !checker.updated? - sleep 1 - FileUtils.touch(FILES) sleep 1 + touch(@files) + wait assert checker.updated? checker.execute assert !checker.updated? end - def test_should_invoke_the_block_if_a_watched_dir_changed_its_glob + def test_should_invoke_the_block_if_a_watched_dir_changes i = 0 - checker = build_new_watcher([], 'tmp_watcher' => [:txt]) { i += 1 } + checker = build_new_watcher([], @tmpdir => :rb) { i += 1 } - FileUtils.cd 'tmp_watcher' do - FileUtils.touch(FILES) - end sleep 1 + touch(@files) + wait assert checker.execute_if_updated assert_equal 1, i end - def test_should_not_invoke_the_block_if_a_watched_dir_changed_its_glob + def test_should_not_invoke_the_block_if_a_watched_dir_does_not_change i = 0 - checker = build_new_watcher([], 'tmp_watcher' => :rb) { i += 1 } + checker = build_new_watcher([], @tmpdir => :txt) { i += 1 } - FileUtils.cd 'tmp_watcher' do - FileUtils.touch(FILES) - end - sleep 1 + touch(@files) + wait assert !checker.execute_if_updated assert_equal 0, i end - - def test_should_not_block_with_unusual_file_names - unusual_dirname = 'tmp_watcher/valid,yetstrange,path,' - FileUtils.mkdir_p(unusual_dirname) - FileUtils.touch(FILES.map { |file_name| "#{unusual_dirname}/#{file_name}" }) - - test = Thread.new do - build_new_watcher([], unusual_dirname => :txt) { i += 1 } - Thread.exit - end - - test.priority = -1 - test.join(5) - - assert !test.alive? - end end |