aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/file_update_checker_with_enumerable_test_cases.rb
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2015-11-08 21:01:38 -0800
committerXavier Noria <fxn@hashref.com>2015-11-08 22:49:51 -0800
commit60de179c0970d9ca9b7f6f7f2d49d4c428b96afa (patch)
tree1631379cfae3e52e2f3a0b43578247922de06416 /activesupport/test/file_update_checker_with_enumerable_test_cases.rb
parente8fbf68aa1c69c73595d32c6a6299fe4baa79108 (diff)
downloadrails-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/test/file_update_checker_with_enumerable_test_cases.rb')
-rw-r--r--activesupport/test/file_update_checker_with_enumerable_test_cases.rb92
1 files changed, 41 insertions, 51 deletions
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