|
4 | 4 | #include <fstream> |
5 | 5 | #include <gtest/gtest.h> |
6 | 6 | #include <ios> |
| 7 | +#include <stdexcept> |
7 | 8 | #include <thread> |
8 | 9 |
|
9 | 10 | // Platform-specific headers for directory operations |
@@ -123,22 +124,16 @@ TEST_F(FileWatcherTest, FileReplacedMultipleTimesCallbackCalled) { |
123 | 124 | EXPECT_GE(callbackCount, 10); // Ensure callback was called at least once |
124 | 125 | } |
125 | 126 |
|
126 | | -// Test creating a file after starting to watch it |
127 | | -TEST_F(FileWatcherTest, FileCreatedAfterWatchingCallbackCalled) { |
| 127 | +// Test watching a non-existent file fails fast |
| 128 | +TEST_F(FileWatcherTest, WatchingMissingFileThrows) { |
128 | 129 | bool callbackCalled = false; |
129 | 130 | auto callback = [&callbackCalled]() { callbackCalled = true; }; |
130 | 131 |
|
131 | | - // Start watching before file exists |
132 | | - auto watcher = filewatcher_factory::createFileWatcher(); |
133 | | - watcher->startWatching(testFilePath, callback); |
134 | | - std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_WAIT_TIME_MS)); |
135 | | - |
136 | | - // Now create the file |
137 | | - createFile(testFilePath, "Initial content"); |
138 | | - std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_WAIT_TIME_MS)); |
139 | | - watcher->stopWatching(); |
| 132 | + std::remove(testFilePath.c_str()); |
140 | 133 |
|
141 | | - EXPECT_TRUE(callbackCalled); |
| 134 | + auto watcher = filewatcher_factory::createFileWatcher(); |
| 135 | + EXPECT_THROW(watcher->startWatching(testFilePath, callback), |
| 136 | + std::runtime_error); |
142 | 137 | } |
143 | 138 |
|
144 | 139 | // Test stopping watcher multiple times is safe (idempotent) |
@@ -219,25 +214,7 @@ TEST_F(FileWatcherTest, FileInSubdirectoryWatched) { |
219 | 214 | EXPECT_TRUE(callbackCalled); |
220 | 215 | } |
221 | 216 |
|
222 | | -// Test file creation specifically (not modification or replacement) |
223 | | -TEST_F(FileWatcherTest, NewFileCreationCallbackCalled) { |
224 | | - bool callbackCalled = false; |
225 | | - auto callback = [&callbackCalled]() { callbackCalled = true; }; |
226 | | - |
227 | | - // Ensure file doesn't exist |
228 | | - std::remove(testFilePath.c_str()); |
229 | | - |
230 | | - auto watcher = filewatcher_factory::createFileWatcher(); |
231 | | - watcher->startWatching(testFilePath, callback); |
232 | | - std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_WAIT_TIME_MS)); |
233 | | - |
234 | | - // Create new file (not modifying existing) |
235 | | - createFile(testFilePath, "Brand new file"); |
236 | | - std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_WAIT_TIME_MS)); |
237 | | - watcher->stopWatching(); |
238 | | - |
239 | | - EXPECT_TRUE(callbackCalled); |
240 | | -} |
| 217 | + |
241 | 218 |
|
242 | 219 | // Test multiple separate modifications with proper spacing |
243 | 220 | TEST_F(FileWatcherTest, MultipleModificationsWithSpacing) { |
|
0 commit comments