diff options
author | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2015-08-16 00:03:30 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2015-08-19 05:03:26 +0000 |
commit | b479d5befbf6f6ed1bffef0dbccecf504263e1d6 (patch) | |
tree | 230b13c9b4d0ce4b858ac4b8b29ad057e78d65ba /src/testlib | |
parent | e50629cc0db628efdea6c654a87fe0e7acbad596 (diff) |
Disable the testlib watchdog if a debugger is present
Currently this is only implemented on Linux and Windows. But it's an
improvement nevertheless when debugging Qt unit tests :)
Change-Id: Iea307dad0e12c24f305c16f0a1805e7c246ee372
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Reviewed-by: Oliver Wolff <oliver.wolff@theqtcompany.com>
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src/testlib')
-rw-r--r-- | src/testlib/qtestcase.cpp | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index ae83432eb8..1a74bab538 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -76,6 +76,7 @@ #if defined(Q_OS_LINUX) #include <sys/types.h> #include <unistd.h> +#include <fcntl.h> #endif #ifdef Q_OS_WIN @@ -2213,9 +2214,11 @@ static bool qInvokeTestMethod(const char *slotName, const char *data, WatchDog * QTestDataSetter s(curDataIndex >= dataCount ? static_cast<QTestData *>(0) : table.testData(curDataIndex)); - watchDog->beginTest(); + if (watchDog) + watchDog->beginTest(); qInvokeTestMethodDataEntry(slot); - watchDog->testFinished(); + if (watchDog) + watchDog->testFinished(); if (data) break; @@ -2487,6 +2490,37 @@ char *toPrettyUnicode(const ushort *p, int length) return buffer.take(); } +static bool debuggerPresent() +{ +#if defined(Q_OS_LINUX) + int fd = open("/proc/self/status", O_RDONLY); + if (fd == -1) + return false; + char buffer[2048]; + ssize_t size = read(fd, buffer, sizeof(buffer)); + if (size == -1) { + close(fd); + return false; + } + buffer[size] = 0; + const char tracerPidToken[] = "TracerPid:"; + char *tracerPid = strstr(buffer, tracerPidToken); + if (!tracerPid) { + close(fd); + return false; + } + tracerPid += sizeof(tracerPidToken); + long int pid = strtol(tracerPid, &tracerPid, 10); + close(fd); + return pid != 0; +#elif defined(Q_OS_WIN) + return IsDebuggerPresent(); +#else + // TODO + return false; +#endif +} + static void qInvokeTestMethods(QObject *testObject) { const QMetaObject *metaObject = testObject->metaObject(); @@ -2496,7 +2530,9 @@ static void qInvokeTestMethods(QObject *testObject) QTestTable::globalTestTable(); invokeMethod(testObject, "initTestCase_data()"); - WatchDog watchDog; + QScopedPointer<WatchDog> watchDog; + if (!debuggerPresent()) + watchDog.reset(new WatchDog); if (!QTestResult::skipCurrentTest() && !QTest::currentTestFailed()) { invokeMethod(testObject, "initTestCase()"); @@ -2512,7 +2548,7 @@ static void qInvokeTestMethods(QObject *testObject) if (QTest::testFuncs) { for (int i = 0; i != QTest::testFuncCount; i++) { if (!qInvokeTestMethod(metaObject->method(QTest::testFuncs[i].function()).methodSignature().constData(), - QTest::testFuncs[i].data(), &watchDog)) { + QTest::testFuncs[i].data(), watchDog.data())) { break; } } @@ -2525,7 +2561,7 @@ static void qInvokeTestMethods(QObject *testObject) for (int i = 0; i != methodCount; i++) { if (!isValidSlot(testMethods[i])) continue; - if (!qInvokeTestMethod(testMethods[i].methodSignature().constData(), 0, &watchDog)) + if (!qInvokeTestMethod(testMethods[i].methodSignature().constData(), 0, watchDog.data())) break; } delete[] testMethods; |