Skip to content

Commit bd9ec3e

Browse files
test(debug-log): cover external delete of a watched directory
Assert the Windows FindNextChangeNotification warning stays off the watcher thread and only lands in the log after the GUI processes events.
1 parent 0e4a0ae commit bd9ec3e

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

‎tests/test_debug_log_dock.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
#include <QtTest>
1919

20+
#include <QDir>
21+
#include <QFile>
22+
#include <QFileSystemWatcher>
2023
#include <QPlainTextEdit>
24+
#include <QTemporaryDir>
2125
#include <QThread>
2226

2327
#include <atomic>
@@ -33,6 +37,7 @@ private slots:
3337
void init();
3438
void warningFromOtherThread_doesNotAppendUntilGuiProcessesEvents();
3539
void warningOnGuiThread_doesNotAppendUntilEventsProcessed();
40+
void externalDeleteOfWatchedDirectory_doesNotCrash();
3641

3742
private:
3843
QString logText(DebugLogDock &dock) const;
@@ -94,5 +99,40 @@ void TestDebugLogDock::warningOnGuiThread_doesNotAppendUntilEventsProcessed()
9499
QVERIFY(logText(dock).contains(token));
95100
}
96101

102+
void TestDebugLogDock::externalDeleteOfWatchedDirectory_doesNotCrash()
103+
{
104+
#ifndef Q_OS_WIN
105+
QSKIP("FindNextChangeNotification is a Windows QFileSystemWatcher path");
106+
#else
107+
DebugLogDock dock;
108+
QVERIFY(dock.findChild<QPlainTextEdit *>(QStringLiteral("txtDebugOutput")));
109+
110+
QTemporaryDir tmp;
111+
QVERIFY(tmp.isValid());
112+
const QString watched = QDir(tmp.path()).filePath(QStringLiteral("watched"));
113+
QVERIFY(QDir().mkpath(watched));
114+
QFile file(QDir(watched).filePath(QStringLiteral("a.txt")));
115+
QVERIFY(file.open(QIODevice::WriteOnly));
116+
QCOMPARE(file.write("x"), qint64(1));
117+
file.close();
118+
119+
QFileSystemWatcher watcher;
120+
QVERIFY(watcher.addPath(watched));
121+
122+
// Another process (agent, Explorer, rm) unlinks the folder while this
123+
// app still has the watch. Same warning as Delete Permanently, without
124+
// going through the menu unwatch path.
125+
QVERIFY(QDir(watched).removeRecursively());
126+
127+
QThread::msleep(1500);
128+
QVERIFY2(!logText(dock).contains(QLatin1String("FindNextChangeNotification")),
129+
"log widget was mutated on the watcher thread");
130+
131+
QCoreApplication::processEvents();
132+
QVERIFY2(logText(dock).contains(QLatin1String("FindNextChangeNotification")),
133+
"expected the Windows watcher warning after GUI events");
134+
#endif
135+
}
136+
97137
QTEST_MAIN(TestDebugLogDock)
98138
#include "test_debug_log_dock.moc"

0 commit comments

Comments
 (0)