OSSCA Chromium

기여 목록 / 7895660

base: Test file modification in WatchDirectory on Apple

BasetestMerged

Apple에서 디렉터리를 비재귀적으로 감시할 때 내부 파일의 내용을 수정해도 이벤트가 발생하지 않는 동작을 검증하도록 테스트를 보완했습니다.

문제 설명

FilePathWatcherTest.WatchDirectory는 Apple에서 파일 수정 단계를 건너뛰고, 이벤트가 발생하지 않는지 확인하라는 TODO만 남겨 두었습니다.

이 테스트에서 사용하는 Apple의 비재귀 감시(kNonRecursive)는 kqueue로 디렉터리 경로를 감시하며, 내부 파일은 개별 감시하지 않습니다. 파일 생성·삭제는 디렉터리의 항목 목록을 바꾸므로 감지됩니다. 반면 기존 파일의 내용만 수정하면 항목 목록은 그대로이므로, 이 감시 방식에서는 이벤트가 보고되지 않습니다.

해결 내용

WriteFile()을 조건문 밖으로 옮겨 Apple에서도 파일 수정 단계를 실행하도록 했습니다. Apple에서는 ExpectedEventsSinceLastWait::kNone을 전달해 이벤트 루프를 잠시 실행한 뒤, 수신 목록이 기존 기대 목록과 일치하는지 확인합니다.

전체 코드 diff는 다음과 같습니다.

--- a/base/files/file_path_watcher_unittest.cc
+++ b/base/files/file_path_watcher_unittest.cc
@@ -914,13 +914,15 @@
   }
   delegate.RunUntilEventsMatch(event_expecter);

-#if !BUILDFLAG(IS_APPLE)
   ASSERT_TRUE(WriteFile(file1, "content v2"));
-  // Mac implementation does not detect files modified in a directory.
-  // TODO(crbug.com/40263777): Expect that no events are fired on Mac.
+#if BUILDFLAG(IS_APPLE)
+  // Apple implementation does not detect files modified in a directory.
   // TODO(crbug.com/40105284): Consider using FSEvents to support
   // watching a directory and its immediate children, as Type::kNonRecursive
   // does on other platforms.
+  delegate.RunUntilEventsMatch(event_expecter.GetMatcher(),
+                               ExpectedEventsSinceLastWait::kNone);
+#else
   VLOG(1) << "Waiting for file1 modification";
   event_expecter.AddExpectedEventForPath(dir);
 #if BUILDFLAG(IS_WIN)
@@ -929,7 +931,7 @@
   event_expecter.AddExpectedEventForPath(dir);
 #endif
   delegate.RunUntilEventsMatch(event_expecter);
-#endif  // !BUILDFLAG(IS_APPLE)
+#endif  // BUILDFLAG(IS_APPLE)

   ASSERT_TRUE(DeleteFile(file1));
   VLOG(1) << "Waiting for file1 deletion";

테스트 방법

CL에 기록한 테스트는 다음과 같습니다.

base_unittests --gtest_filter=FilePathWatcherTest.WatchDirectory

CQ를 거쳐 2026년 6월 9일 Chromium main에 병합되었습니다.

배운 점

  • 플랫폼에서 알림을 지원하지 않는 동작도, 실행을 건너뛰지 않고 알림이 없음을 검증할 수 있습니다.
  • 이벤트를 누적하는 테스트에서는 이전 이벤트를 포함한 기대 목록과 비교해야 새 이벤트가 추가되지 않았는지 확인할 수 있습니다.

참고 자료