Skip to content

Commit

Permalink
Merge pull request #75 from erikzenker/bugfix/erikzenker/#52
Browse files Browse the repository at this point in the history
feat: add test for recursive file watching
  • Loading branch information
erikzenker committed Aug 31, 2020
2 parents 0d949af + 5ca4381 commit 5d0d203
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 48 deletions.
5 changes: 3 additions & 2 deletions src/Inotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ void Inotify::watchDirectoryRecursively(fs::path path)
std::vector<boost::filesystem::path> 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);
Expand All @@ -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());
Expand Down Expand Up @@ -374,4 +375,4 @@ void Inotify::filterEvents(
}
}
}
}
}
96 changes: 50 additions & 46 deletions test/unit/NotifierBuilderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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([&notifier]() { 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([&notifier]() { 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([&notifier]() { 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([&notifier]() { notifier.run(); });

std::this_thread::sleep_for(std::chrono::milliseconds { 1000 });

notifier.stop();
thread.join();
}

BOOST_FIXTURE_TEST_CASE(shouldWatchCreatedFile, NotifierBuilderTests)
{
Expand Down

0 comments on commit 5d0d203

Please sign in to comment.