| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
|
| 3 |
#include "test_utils.h" |
| 4 |
#include "tests/tests_common.h" |
| 5 |
|
| 6 |
#include <QRegularExpression> |
| 7 |
#include <QTemporaryFile> |
| 8 |
|
| 9 |
TemporaryFile::TemporaryFile() |
| 10 |
{ |
| 11 |
QTemporaryFile tmp; |
| 12 |
tmp.setAutoRemove(false); |
| 13 |
QVERIFY(tmp.open()); |
| 14 |
m_fileName = tmp.fileName(); |
| 15 |
tmp.close(); |
| 16 |
} |
| 17 |
|
| 18 |
TemporaryFile::~TemporaryFile() |
| 19 |
{ |
| 20 |
QFile::remove(m_fileName); |
| 21 |
} |
| 22 |
|
| 23 |
bool testStderr( |
| 24 |
const QByteArray &stderrData, |
| 25 |
const QRegularExpression &ignoreRe) |
| 26 |
{ |
| 27 |
static const QRegularExpression reFailure( |
| 28 |
"(?:^|\n).*(?:Warning:|Warning <.*>:|ERROR:|ERROR <.*>:|ASSERT).*", |
| 29 |
QRegularExpression::CaseInsensitiveOption); |
| 30 |
|
| 31 |
const auto plain = [](const char *str){ |
| 32 |
return QRegularExpression(QRegularExpression::escape(QLatin1String(str))); |
| 33 |
}; |
| 34 |
const auto regex = [](const char *str){ |
| 35 |
return QRegularExpression(QLatin1String(str)); |
| 36 |
}; |
| 37 |
// Ignore exceptions and errors from clients in application log |
| 38 |
// (these are expected in some tests). |
| 39 |
static const std::array ignoreList{ |
| 40 |
plain("CopyQ server is already running"), |
| 41 |
plain("Cannot connect to server! Start CopyQ server first."), |
| 42 |
plain("Aborting clipboard cloning"), |
| 43 |
plain("Failed to provide clipboard"), |
| 44 |
plain("Failed to provide selection"), |
| 45 |
regex("Warning <.*>: ELAPSED .* ms accessing"), |
| 46 |
regex("ERROR <.*>: Connection lost!"), |
| 47 |
|
| 48 |
// Always ignore errors and exceptions from scripts. |
| 49 |
// These are expected in many test cases. |
| 50 |
plain("ScriptError:"), |
| 51 |
|
| 52 |
// X11 (Linux) |
| 53 |
plain("QXcbXSettings::QXcbXSettings(QXcbScreen*) Failed to get selection owner for XSETTINGS_S atom"), |
| 54 |
plain("QXcbConnection: XCB error:"), |
| 55 |
plain("QXcbClipboard: SelectionRequest too old"), |
| 56 |
plain("libpng warning: iCCP: known incorrect sRGB profile"), |
| 57 |
plain("QMime::convertToMime: unhandled mimetype: text/plain"), |
| 58 |
plain("Failed to register with host portal"), |
| 59 |
plain("Deleting keychain failed"), |
| 60 |
|
| 61 |
// Wayland (Linux) |
| 62 |
plain("Wayland does not support QWindow::requestActivate()"), |
| 63 |
plain("Unexpected wl_keyboard.enter event"), |
| 64 |
plain("The compositor sent a wl_pointer.enter"), |
| 65 |
plain("QObject::connect: No such signal QPlatformNativeInterface::systemTrayWindowChanged(QScreen*)"), |
| 66 |
plain("Could not init WaylandClipboard, falling back to QtClipboard."), |
| 67 |
plain("QDBusTrayIcon encountered a D-Bus error"), |
| 68 |
|
| 69 |
// KDE Frameworks (Linux) |
| 70 |
plain("[kf.notifications]"), |
| 71 |
plain("[kf.statusnotifieritem]"), |
| 72 |
|
| 73 |
// Windows |
| 74 |
plain("QWindowsPipeWriter: write failed"), |
| 75 |
plain("QWindowsWindow::setGeometry: Unable to set geometry"), |
| 76 |
plain("QWinEventNotifier: no event dispatcher, application shutting down? Cannot deliver event."), |
| 77 |
plain("setGeometry: Unable to set geometry"), |
| 78 |
plain("Failed to raise: "), |
| 79 |
plain("[qt.qpa.mime] Retrying to obtain clipboard."), |
| 80 |
plain("[qt.qpa.mime] Unable to obtain clipboard."), |
| 81 |
plain("[default] QSystemTrayIcon::setVisible: No Icon set"), |
| 82 |
|
| 83 |
plain("[kf.notifications] Received a response for an unknown notification."), |
| 84 |
// KStatusNotifierItem |
| 85 |
plain("[kf.windowsystem] Could not find any platform plugin"), |
| 86 |
|
| 87 |
regex("QTemporaryDir: Unable to remove .* most likely due to the presence of read-only files."), |
| 88 |
|
| 89 |
// macOS |
| 90 |
plain("Failed to get QCocoaScreen for NSObject(0x0)"), |
| 91 |
plain("ERROR: Failed to open session mutex: QSystemSemaphore::handle:: ftok failed"), |
| 92 |
plain("Warning: [qt.tlsbackend.ossl] Failed to load libssl/libcrypto."), |
| 93 |
regex(R"(Window position.* outside any known screen.*)"), |
| 94 |
regex(R"(Populating font family aliases took .* ms. Replace uses of missing font family "Font Awesome.*" with one that exists to avoid this cost.)"), |
| 95 |
|
| 96 |
// New in Qt 5.15.0 |
| 97 |
regex(R"(Populating font family aliases took .* ms. Replace uses of missing font family "Monospace" with one that exists to avoid this cost.)"), |
| 98 |
|
| 99 |
// New in Qt 6.5 |
| 100 |
regex("Error in contacting registry"), |
| 101 |
|
| 102 |
// KNotification bug |
| 103 |
plain(R"(QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout)"), |
| 104 |
|
| 105 |
// Warnings from itemsync plugin, not sure what it causes |
| 106 |
regex(R"(Could not remove our own lock file .* maybe permissions changed meanwhile)"), |
| 107 |
}; |
| 108 |
|
| 109 |
const QString output = QString::fromUtf8(stderrData); |
| 110 |
QRegularExpressionMatchIterator it = reFailure.globalMatch(output); |
| 111 |
bool result = true; |
| 112 |
const bool isIgnoreReValid = !ignoreRe.pattern().isEmpty(); |
| 113 |
while ( it.hasNext() ) { |
| 114 |
const auto match = it.next(); |
| 115 |
const QString log = match.captured(); |
| 116 |
|
| 117 |
const bool ignore = (isIgnoreReValid && log.contains(ignoreRe)) |
| 118 |
|| std::any_of( |
| 119 |
std::begin(ignoreList), std::end(ignoreList), |
| 120 |
[&log](const QRegularExpression &reIgnore){ |
| 121 |
return log.contains(reIgnore); |
| 122 |
}); |
| 123 |
|
| 124 |
if (!ignore) { |
| 125 |
qWarning() << "🛑 Failure in logs:" << log.trimmed(); |
| 126 |
result = false; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (!result && isIgnoreReValid) |
| 131 |
qWarning().noquote() << "🟡 Ignored logs matching:" << ignoreRe.pattern(); |
| 132 |
|
| 133 |
return result; |
| 134 |
} |
| 135 |
|
| 136 |
int count(const QStringList &items, const QString &pattern) |
| 137 |
{ |
| 138 |
int from = -1; |
| 139 |
int count = 0; |
| 140 |
const QRegularExpression re(pattern); |
| 141 |
while ( (from = items.indexOf(re, from + 1)) != -1 ) |
| 142 |
++count; |
| 143 |
return count; |
| 144 |
} |
| 145 |
|
| 146 |
QStringList splitLines(const QByteArray &nativeText) |
| 147 |
{ |
| 148 |
return QString::fromUtf8(nativeText).split(QRegularExpression("\r\n|\n|\r")); |
| 149 |
} |
| 150 |
|
| 151 |
QByteArray generateData() |
| 152 |
{ |
| 153 |
static int i = 0; |
| 154 |
const QByteArray id = "tests_" |
| 155 |
+ QByteArray::number(QDateTime::currentMSecsSinceEpoch() % 1000); |
| 156 |
return id + '_' + QByteArray::number(++i); |
| 157 |
} |
| 158 |
|
| 159 |
QString appWindowTitle(const QString &text) |
| 160 |
{ |
| 161 |
#ifdef Q_OS_MAC |
| 162 |
return QStringLiteral("CopyQ - %1\n").arg(text); |
| 163 |
#elif defined(Q_OS_WIN) |
| 164 |
return QStringLiteral("%1 - CopyQ-%2\n").arg(text, sessionName); |
| 165 |
#else |
| 166 |
return QStringLiteral("%1 — CopyQ-%2\n").arg(text, sessionName); |
| 167 |
#endif |
| 168 |
} |