Skip to content

Commit 5d0d203

Browse files
authored
Merge pull request #75 from erikzenker/bugfix/erikzenker/#52
feat: add test for recursive file watching
2 parents 0d949af + 5ca4381 commit 5d0d203

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

src/Inotify.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ void Inotify::watchDirectoryRecursively(fs::path path)
100100
std::vector<boost::filesystem::path> paths;
101101

102102
if (fs::exists(path)) {
103+
paths.push_back(path);
104+
103105
if (fs::is_directory(path)) {
104106
boost::system::error_code ec;
105107
fs::recursive_directory_iterator it(path, fs::symlink_option::recurse, ec);
@@ -115,7 +117,6 @@ void Inotify::watchDirectoryRecursively(fs::path path)
115117
paths.push_back(currentPath);
116118
}
117119
}
118-
paths.push_back(path);
119120
} else {
120121
throw std::invalid_argument(
121122
"Can´t watch Path! Path does not exist. Path: " + path.string());
@@ -374,4 +375,4 @@ void Inotify::filterEvents(
374375
}
375376
}
376377
}
377-
}
378+
}

test/unit/NotifierBuilderTests.cpp

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -235,52 +235,56 @@ BOOST_FIXTURE_TEST_CASE(shouldIgnoreFile, NotifierBuilderTests)
235235
thread.join();
236236
}
237237

238-
// TODO: Test is flaky, fix by #52
239-
// BOOST_FIXTURE_TEST_CASE(shouldWatchPathRecursively, NotifierBuilderTests)
240-
// {
241-
242-
// auto notifier = BuildNotifier()
243-
// .watchPathRecursively(testDirectory_)
244-
// .onEvent(Event::open, [&](Notification notification) {
245-
// switch (notification.event) {
246-
// default:
247-
// break;
248-
// case Event::open:
249-
// if (notification.path == recursiveTestFile_) {
250-
// promisedOpen_.set_value(notification);
251-
// }
252-
// break;
253-
// }
254-
// });
255-
256-
// std::thread thread([&notifier]() { notifier.runOnce(); });
257-
258-
// openFile(recursiveTestFile_);
259-
260-
// auto futureOpen = promisedOpen_.get_future();
261-
// BOOST_CHECK(futureOpen.wait_for(timeout_) == std::future_status::ready);
262-
263-
// notifier.stop();
264-
// thread.join();
265-
// }
266-
267-
// TODO: Fix by #52
268-
// BOOST_FIXTURE_TEST_CASE(shouldNotTriggerEventsOnWatchRecursively, NotifierBuilderTests)
269-
// {
270-
// auto notifier = BuildNotifier()
271-
// .watchPathRecursively(testDirectory_)
272-
// .onEvent(Event::all, [&](Notification) {
273-
// BOOST_ASSERT_MSG(false, "Events should not be triggered when watching a directory
274-
// recursively.");
275-
// });
276-
277-
// std::thread thread([&notifier]() { notifier.runOnce(); });
278-
279-
// std::this_thread::sleep_for(std::chrono::milliseconds{1000});
280-
281-
// notifier.stop();
282-
// thread.join();
283-
// }
238+
// This test might fail on dev machines when editors trigger events by indexing the build directory
239+
BOOST_FIXTURE_TEST_CASE(shouldWatchPathRecursively, NotifierBuilderTests)
240+
{
241+
242+
auto notifier = BuildNotifier()
243+
.watchPathRecursively(testDirectory_)
244+
.onEvent(Event::open, [&](Notification notification) {
245+
switch (notification.event) {
246+
default:
247+
break;
248+
case Event::open:
249+
if (notification.path == recursiveTestFile_) {
250+
promisedOpen_.set_value(notification);
251+
}
252+
break;
253+
}
254+
});
255+
256+
std::thread thread([&notifier]() { notifier.runOnce(); });
257+
258+
openFile(recursiveTestFile_);
259+
260+
auto futureOpen = promisedOpen_.get_future();
261+
BOOST_CHECK(futureOpen.wait_for(timeout_) == std::future_status::ready);
262+
263+
notifier.stop();
264+
thread.join();
265+
}
266+
267+
// This test might fail on dev machines when editors trigger events by indexing the build directory
268+
BOOST_FIXTURE_TEST_CASE(shouldNotTriggerEventsOnWatchRecursively, NotifierBuilderTests)
269+
{
270+
auto notifier
271+
= BuildNotifier()
272+
.watchPathRecursively(testDirectory_)
273+
.onEvent(Event::all, [&](Notification notification) {
274+
std::cout << "event:" << notification.path << " " << notification.event
275+
<< std::endl;
276+
BOOST_ASSERT_MSG(
277+
false,
278+
"Events should not be triggered when watching a directory recursively.");
279+
});
280+
281+
std::thread thread([&notifier]() { notifier.run(); });
282+
283+
std::this_thread::sleep_for(std::chrono::milliseconds { 1000 });
284+
285+
notifier.stop();
286+
thread.join();
287+
}
284288

285289
BOOST_FIXTURE_TEST_CASE(shouldWatchCreatedFile, NotifierBuilderTests)
286290
{

0 commit comments

Comments
 (0)