From f1084f2d0eaf8405284b2da19e8ac2c2eb7063ad Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 22 May 2019 16:58:01 -0700 Subject: Use existing tmpdir in evented_file_update_test The common include of this test creates a tmpdir, we should use that for consistency. --- .../test/evented_file_update_checker_test.rb | 38 ++++++++++------------ 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/activesupport/test/evented_file_update_checker_test.rb b/activesupport/test/evented_file_update_checker_test.rb index b2d5eb94c2..ce09fa1586 100644 --- a/activesupport/test/evented_file_update_checker_test.rb +++ b/activesupport/test/evented_file_update_checker_test.rb @@ -78,31 +78,29 @@ class EventedFileUpdateCheckerTest < ActiveSupport::TestCase end test "updated should become true when nonexistent directory is added later" do - Dir.mktmpdir do |dir| - watched_dir = File.join(dir, "app") - unwatched_dir = File.join(dir, "node_modules") - not_exist_watched_dir = File.join(dir, "test") + watched_dir = File.join(tmpdir, "app") + unwatched_dir = File.join(tmpdir, "node_modules") + not_exist_watched_dir = File.join(tmpdir, "test") - Dir.mkdir(watched_dir) - Dir.mkdir(unwatched_dir) + Dir.mkdir(watched_dir) + Dir.mkdir(unwatched_dir) - checker = new_checker([], watched_dir => ".rb", not_exist_watched_dir => ".rb") { } + checker = new_checker([], watched_dir => ".rb", not_exist_watched_dir => ".rb") { } - FileUtils.touch(File.join(watched_dir, "a.rb")) - wait - assert_predicate checker, :updated? - assert checker.execute_if_updated + FileUtils.touch(File.join(watched_dir, "a.rb")) + wait + assert_predicate checker, :updated? + assert checker.execute_if_updated - Dir.mkdir(not_exist_watched_dir) - wait - assert_predicate checker, :updated? - assert checker.execute_if_updated + Dir.mkdir(not_exist_watched_dir) + wait + assert_predicate checker, :updated? + assert checker.execute_if_updated - FileUtils.touch(File.join(unwatched_dir, "a.rb")) - wait - assert_not_predicate checker, :updated? - assert_not checker.execute_if_updated - end + FileUtils.touch(File.join(unwatched_dir, "a.rb")) + wait + assert_not_predicate checker, :updated? + assert_not checker.execute_if_updated end end -- cgit v1.2.3 From 2fd6c3799dd04dcae0948acd25c40dded1459756 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 22 May 2019 16:42:34 -0700 Subject: Fix EventedFileUpdateChecker through a symlink On MacOS, Dir.tmpdir gives me a folder inside "/var/folders/". However, /var is a symlink to /private/var. Previously, the nonexistent directory test would fail because it was initialized with /var/folders/... but the filenames from listen would be the realpaths. This commit normalizes the dirs by calling realpath on them if they exist. This is done on boot!, so it will work with newly directories through the symlink. --- .../lib/active_support/evented_file_update_checker.rb | 7 +++++++ activesupport/test/evented_file_update_checker_test.rb | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/activesupport/lib/active_support/evented_file_update_checker.rb b/activesupport/lib/active_support/evented_file_update_checker.rb index 3893b0de0e..15b84537cc 100644 --- a/activesupport/lib/active_support/evented_file_update_checker.rb +++ b/activesupport/lib/active_support/evented_file_update_checker.rb @@ -107,6 +107,7 @@ module ActiveSupport private def boot! + normalize_dirs! Listen.to(*@dtw, &method(:changed)).start end @@ -114,6 +115,12 @@ module ActiveSupport Listen.stop end + def normalize_dirs! + @dirs.transform_keys! do |dir| + dir.exist? ? dir.realpath : dir + end + end + def changed(modified, added, removed) unless updated? @updated.make_true if (modified + added + removed).any? { |f| watching?(f) } diff --git a/activesupport/test/evented_file_update_checker_test.rb b/activesupport/test/evented_file_update_checker_test.rb index ce09fa1586..bec2643b45 100644 --- a/activesupport/test/evented_file_update_checker_test.rb +++ b/activesupport/test/evented_file_update_checker_test.rb @@ -77,6 +77,24 @@ class EventedFileUpdateCheckerTest < ActiveSupport::TestCase Process.wait(pid) end + test "should detect changes through symlink" do + actual_dir = File.join(tmpdir, "actual") + linked_dir = File.join(tmpdir, "linked") + + Dir.mkdir(actual_dir) + FileUtils.ln_s(actual_dir, linked_dir) + + checker = new_checker([], linked_dir => ".rb") { } + + assert_not_predicate checker, :updated? + + FileUtils.touch(File.join(actual_dir, "a.rb")) + wait + + assert_predicate checker, :updated? + assert checker.execute_if_updated + end + test "updated should become true when nonexistent directory is added later" do watched_dir = File.join(tmpdir, "app") unwatched_dir = File.join(tmpdir, "node_modules") -- cgit v1.2.3