From 5ca438170213fc1e8c3209b1bc0a9474216361e1 Mon Sep 17 00:00:00 2001 From: Erik Zenker Date: Mon, 31 Aug 2020 23:57:19 +0200 Subject: [PATCH] feat: add test for recursive file watching --- src/Inotify.cpp | 5 +- test/unit/NotifierBuilderTests.cpp | 96 ++++++++++++++++-------------- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/Inotify.cpp b/src/Inotify.cpp index 103d469..bbce3b4 100644 --- a/src/Inotify.cpp +++ b/src/Inotify.cpp @@ -100,6 +100,8 @@ void Inotify::watchDirectoryRecursively(fs::path path) std::vector paths; if (fs::exists(path)) { + paths.push_back(path); + if (fs::is_directory(path)) { boost::system::error_code ec; fs::recursive_directory_iterator it(path, fs::symlink_option::recurse, ec); @@ -115,7 +117,6 @@ void Inotify::watchDirectoryRecursively(fs::path path) paths.push_back(currentPath); } } - paths.push_back(path); } else { throw std::invalid_argument( "CanĀ“t watch Path! Path does not exist. Path: " + path.string()); @@ -374,4 +375,4 @@ void Inotify::filterEvents( } } } -} +} \ No newline at end of file diff --git a/test/unit/NotifierBuilderTests.cpp b/test/unit/NotifierBuilderTests.cpp index 8da7442..154108d 100644 --- a/test/unit/NotifierBuilderTests.cpp +++ b/test/unit/NotifierBuilderTests.cpp @@ -235,52 +235,56 @@ BOOST_FIXTURE_TEST_CASE(shouldIgnoreFile, NotifierBuilderTests) thread.join(); } -// TODO: Test is flaky, fix by #52 -// BOOST_FIXTURE_TEST_CASE(shouldWatchPathRecursively, NotifierBuilderTests) -// { - -// auto notifier = BuildNotifier() -// .watchPathRecursively(testDirectory_) -// .onEvent(Event::open, [&](Notification notification) { -// switch (notification.event) { -// default: -// break; -// case Event::open: -// if (notification.path == recursiveTestFile_) { -// promisedOpen_.set_value(notification); -// } -// break; -// } -// }); - -// std::thread thread([¬ifier]() { notifier.runOnce(); }); - -// openFile(recursiveTestFile_); - -// auto futureOpen = promisedOpen_.get_future(); -// BOOST_CHECK(futureOpen.wait_for(timeout_) == std::future_status::ready); - -// notifier.stop(); -// thread.join(); -// } - -// TODO: Fix by #52 -// BOOST_FIXTURE_TEST_CASE(shouldNotTriggerEventsOnWatchRecursively, NotifierBuilderTests) -// { -// auto notifier = BuildNotifier() -// .watchPathRecursively(testDirectory_) -// .onEvent(Event::all, [&](Notification) { -// BOOST_ASSERT_MSG(false, "Events should not be triggered when watching a directory -// recursively."); -// }); - -// std::thread thread([¬ifier]() { notifier.runOnce(); }); - -// std::this_thread::sleep_for(std::chrono::milliseconds{1000}); - -// notifier.stop(); -// thread.join(); -// } +// This test might fail on dev machines when editors trigger events by indexing the build directory +BOOST_FIXTURE_TEST_CASE(shouldWatchPathRecursively, NotifierBuilderTests) +{ + + auto notifier = BuildNotifier() + .watchPathRecursively(testDirectory_) + .onEvent(Event::open, [&](Notification notification) { + switch (notification.event) { + default: + break; + case Event::open: + if (notification.path == recursiveTestFile_) { + promisedOpen_.set_value(notification); + } + break; + } + }); + + std::thread thread([¬ifier]() { notifier.runOnce(); }); + + openFile(recursiveTestFile_); + + auto futureOpen = promisedOpen_.get_future(); + BOOST_CHECK(futureOpen.wait_for(timeout_) == std::future_status::ready); + + notifier.stop(); + thread.join(); +} + +// This test might fail on dev machines when editors trigger events by indexing the build directory +BOOST_FIXTURE_TEST_CASE(shouldNotTriggerEventsOnWatchRecursively, NotifierBuilderTests) +{ + auto notifier + = BuildNotifier() + .watchPathRecursively(testDirectory_) + .onEvent(Event::all, [&](Notification notification) { + std::cout << "event:" << notification.path << " " << notification.event + << std::endl; + BOOST_ASSERT_MSG( + false, + "Events should not be triggered when watching a directory recursively."); + }); + + std::thread thread([¬ifier]() { notifier.run(); }); + + std::this_thread::sleep_for(std::chrono::milliseconds { 1000 }); + + notifier.stop(); + thread.join(); +} BOOST_FIXTURE_TEST_CASE(shouldWatchCreatedFile, NotifierBuilderTests) {