From 8a64824306fd04895f4f6f14e6edb7a82bfe2503 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 10 Nov 2015 01:12:51 -0800 Subject: better tmp file management in the monitors suite --- ...le_update_checker_with_enumerable_test_cases.rb | 124 +++++++++++++++------ 1 file changed, 88 insertions(+), 36 deletions(-) (limited to 'activesupport/test') 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 e00ba893da..23d8ffa5de 100644 --- a/activesupport/test/file_update_checker_with_enumerable_test_cases.rb +++ b/activesupport/test/file_update_checker_with_enumerable_test_cases.rb @@ -3,15 +3,20 @@ require 'fileutils' module FileUpdateCheckerWithEnumerableTestCases include FileUtils - def setup - @tmpdir = Dir.mktmpdir(nil, __dir__) + def tmpdir + @tmpdir ||= Dir.mktmpdir(nil, __dir__) + end + + def tmpfile(name) + "#{tmpdir}/#{name}" + end - @files = %w(foo.rb bar.rb baz.rb).map {|f| "#{@tmpdir}/#{f}"} - FileUtils.touch(@files) + def tmpfiles + @tmpfiles ||= %w(foo.rb bar.rb baz.rb).map {|f| tmpfile(f)} end def teardown - FileUtils.rm_rf(@tmpdir) + FileUtils.rm_rf(@tmpdir) if @tmpdir end def test_should_not_execute_the_block_if_no_paths_are_given @@ -26,55 +31,102 @@ module FileUpdateCheckerWithEnumerableTestCases def test_should_not_execute_the_block_if_no_files_change i = 0 - checker = new_checker(@files) { i += 1 } + FileUtils.touch(tmpfiles) + + checker = new_checker(tmpfiles) { i += 1 } assert !checker.execute_if_updated assert_equal 0, i end - def test_should_execute_the_block_once_when_files_change + def test_should_execute_the_block_once_when_files_are_created i = 0 - checker = new_checker(@files) { i += 1 } + checker = new_checker(tmpfiles) { i += 1 } - touch(@files) + touch(tmpfiles) assert checker.execute_if_updated assert_equal 1, i end - def test_updated_should_become_true_when_watched_files_are_deleted + def test_should_execute_the_block_once_when_files_are_modified i = 0 - checker = new_checker(@files) { i += 1 } - assert !checker.updated? + FileUtils.touch(tmpfiles) - rm_f(@files) + checker = new_checker(tmpfiles) { i += 1 } - assert checker.updated? + touch(tmpfiles) + + assert checker.execute_if_updated + assert_equal 1, i end def test_should_execute_the_block_once_when_files_are_deleted i = 0 - checker = new_checker(@files) { i += 1 } + FileUtils.touch(tmpfiles) + + checker = new_checker(tmpfiles) { i += 1 } - rm_f(@files) + rm_f(tmpfiles) assert checker.execute_if_updated assert_equal 1, i end + + def test_updated_should_become_true_when_watched_files_are_created + i = 0 + + checker = new_checker(tmpfiles) { i += 1 } + assert !checker.updated? + + touch(tmpfiles) + + assert checker.updated? + end + + + def test_updated_should_become_true_when_watched_files_are_modified + i = 0 + + FileUtils.touch(tmpfiles) + + checker = new_checker(tmpfiles) { i += 1 } + assert !checker.updated? + + touch(tmpfiles) + + assert checker.updated? + end + + def test_updated_should_become_true_when_watched_files_are_deleted + i = 0 + + FileUtils.touch(tmpfiles) + + checker = new_checker(tmpfiles) { i += 1 } + assert !checker.updated? + + rm_f(tmpfiles) + + assert checker.updated? + end + def test_should_be_robust_to_handle_files_with_wrong_modified_time i = 0 - now = Time.now + FileUtils.touch(tmpfiles) + + now = Time.now time = Time.mktime(now.year + 1, now.month, now.day) # wrong mtime from the future - File.utime(time, time, @files[0]) + File.utime(time, time, tmpfiles[0]) - checker = new_checker(@files) { i += 1 } + checker = new_checker(tmpfiles) { i += 1 } - touch(@files[1..-1]) + touch(tmpfiles[1..-1]) assert checker.execute_if_updated assert_equal 1, i @@ -83,10 +135,10 @@ module FileUpdateCheckerWithEnumerableTestCases def test_should_cache_updated_result_until_execute i = 0 - checker = new_checker(@files) { i += 1 } + checker = new_checker(tmpfiles) { i += 1 } assert !checker.updated? - touch(@files) + touch(tmpfiles) assert checker.updated? checker.execute @@ -96,9 +148,9 @@ module FileUpdateCheckerWithEnumerableTestCases def test_should_execute_the_block_if_files_change_in_a_watched_directory_one_extension i = 0 - checker = new_checker([], @tmpdir => :rb) { i += 1 } + checker = new_checker([], tmpdir => :rb) { i += 1 } - touch(@files) + touch(tmpfile('foo.rb')) assert checker.execute_if_updated assert_equal 1, i @@ -107,14 +159,14 @@ module FileUpdateCheckerWithEnumerableTestCases def test_should_execute_the_block_if_files_change_in_a_watched_directory_several_extensions i = 0 - checker = new_checker([], @tmpdir => [:rb, :txt]) { i += 1 } + checker = new_checker([], tmpdir => [:rb, :txt]) { i += 1 } - touch("#{@tmpdir}/foo.rb") + touch(tmpfile('foo.rb')) assert checker.execute_if_updated assert_equal 1, i - touch("#{@tmpdir}/foo.rb") + touch(tmpfile('foo.txt')) assert checker.execute_if_updated assert_equal 2, i @@ -123,9 +175,9 @@ module FileUpdateCheckerWithEnumerableTestCases def test_should_not_execute_the_block_if_the_file_extension_is_not_watched i = 0 - checker = new_checker([], @tmpdir => :txt) { i += 1 } + checker = new_checker([], tmpdir => :txt) { i += 1 } - touch(@files) + touch(tmpfile('foo.rb')) assert !checker.execute_if_updated assert_equal 0, i @@ -134,7 +186,7 @@ module FileUpdateCheckerWithEnumerableTestCases def test_does_not_assume_files_exist_on_instantiation i = 0 - non_existing = "#{@tmpdir}/non_existing.rb" + non_existing = tmpfile('non_existing.rb') checker = new_checker([non_existing]) { i += 1 } touch(non_existing) @@ -146,9 +198,9 @@ module FileUpdateCheckerWithEnumerableTestCases def test_detects_files_in_new_subdirectories i = 0 - checker = new_checker([], @tmpdir => :rb) { i += 1 } + checker = new_checker([], tmpdir => :rb) { i += 1 } - subdir = "#{@tmpdir}/subdir" + subdir = tmpfile('subdir') mkdir(subdir) wait @@ -164,17 +216,17 @@ module FileUpdateCheckerWithEnumerableTestCases def test_looked_up_extensions_are_inherited_in_subdirectories_not_listening_to_them i = 0 - subdir = "#{@tmpdir}/subdir" + subdir = tmpfile('subdir') mkdir(subdir) - checker = new_checker([], @tmpdir => :rb, subdir => :txt) { i += 1 } + checker = new_checker([], tmpdir => :rb, subdir => :txt) { i += 1 } - touch("#{@tmpdir}/new.txt") + touch(tmpfile('new.txt')) assert !checker.execute_if_updated assert_equal 0, i - # subdir does not look for Ruby files, but its parent @tmpdir does. + # subdir does not look for Ruby files, but its parent tmpdir does. touch("#{subdir}/nested.rb") assert checker.execute_if_updated -- cgit v1.2.3