Paste P601

Masterwork From Distant Lands
ActivePublic

Authored by sitter on May 19 2020, 1:11 PM.
From 2218ac17ffb8f7d8cd6fb2ca57e5e55bda54b928 Mon Sep 17 00:00:00 2001
From: Harald Sitter <sitter@kde.org>
Date: Wed, 20 Mar 2019 13:22:48 +0100
Subject: [PATCH] add drkonqi version detection in a new helper Private class
---
autotests/CMakeLists.txt | 1 +
autotests/data/option_version.txt | 3 +
autotests/data/option_version_garbage.txt | 3 +
autotests/privatetest.cpp | 74 +++++++++++++++++++++++
src/CMakeLists.txt | 1 +
src/kcrash.cpp | 2 +
src/private.cpp | 59 ++++++++++++++++++
src/private_p.h | 39 ++++++++++++
8 files changed, 182 insertions(+)
create mode 100644 autotests/data/option_version.txt
create mode 100644 autotests/data/option_version_garbage.txt
create mode 100644 autotests/privatetest.cpp
create mode 100644 src/private.cpp
create mode 100644 src/private_p.h
diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt
index b17d546..7eaa985 100644
--- a/autotests/CMakeLists.txt
+++ b/autotests/CMakeLists.txt
@@ -21,6 +21,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/../src)
ecm_add_tests(
coreconfigtest.cpp
kcrashtest.cpp
+ privatetest.cpp
LINK_LIBRARIES Qt5::Core Qt5::Test
)
diff --git a/autotests/data/option_version.txt b/autotests/data/option_version.txt
new file mode 100644
index 0000000..40d3c6d
--- /dev/null
+++ b/autotests/data/option_version.txt
@@ -0,0 +1,3 @@
+2
+
+
diff --git a/autotests/data/option_version_garbage.txt b/autotests/data/option_version_garbage.txt
new file mode 100644
index 0000000..8882504
--- /dev/null
+++ b/autotests/data/option_version_garbage.txt
@@ -0,0 +1,3 @@
+xxxyy_2
+
+
diff --git a/autotests/privatetest.cpp b/autotests/privatetest.cpp
new file mode 100644
index 0000000..fc17350
--- /dev/null
+++ b/autotests/privatetest.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2019 Harald Sitter <sitter@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <QTest>
+
+#include "../src/private.cpp"
+
+class PrivateTest : public QObject
+{
+ Q_OBJECT
+
+ QString drKonqiDataDir;
+ QString drKonqiVersionFile;
+
+private Q_SLOTS:
+ void initTestCase()
+ {
+ QStandardPaths::setTestModeEnabled(true);
+ }
+
+ void init()
+ {
+ drKonqiDataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/drkonqi";
+ QDir().mkpath(drKonqiDataDir);
+ drKonqiVersionFile = drKonqiDataDir + "/option_version.txt";
+ QFile(drKonqiVersionFile).remove(); // make sure there is no lingering data
+ }
+
+ void testDrkonqiVersionUnknown()
+ {
+ KCrash::Private p;
+ // no file -> unknown version
+ QCOMPARE(p.drKonqiOptionVersion, 0);
+ }
+
+ void testDrkonqiVersionValid()
+ {
+ // valid file -> valid version
+ QFile(drKonqiVersionFile).remove();
+ QVERIFY(QFile::copy(QFINDTESTDATA("data/option_version.txt"), drKonqiVersionFile));
+
+ KCrash::Private p;
+ QCOMPARE(p.drKonqiOptionVersion, 2);
+ }
+
+ void testDrkonqiVersionGarbage()
+ {
+ // garbage file -> unknown version
+ QVERIFY(QFile::copy(QFINDTESTDATA("data/option_version_garbage.txt"), drKonqiVersionFile));
+
+ KCrash::Private p;
+ QCOMPARE(p.drKonqiOptionVersion, 0);
+ }
+};
+
+QTEST_MAIN(PrivateTest)
+
+#include "privatetest.moc"
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7a382dd..6ddb4c1 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -2,6 +2,7 @@
set(kcrash_SRCS
kcrash.cpp
coreconfig.cpp
+ private.cpp
${kcrash_QM_LOADER}
)
diff --git a/src/kcrash.cpp b/src/kcrash.cpp
index 0a37c55..ef89f00 100644
--- a/src/kcrash.cpp
+++ b/src/kcrash.cpp
@@ -76,6 +76,7 @@ Q_LOGGING_CATEGORY(LOG_KCRASH, "org.kde.kcrash", QtInfoMsg)
#include <ucontext.h>
#endif
+#include "private_p.h"
#include "coreconfig_p.h"
// Copy from klauncher_cmds
@@ -104,6 +105,7 @@ static char *s_drkonqiPath = nullptr;
static char *s_kdeinit_socket_file = nullptr;
static KCrash::CrashFlags s_flags = KCrash::CrashFlags();
static int s_launchDrKonqi = -1; // -1=initial value 0=disabled 1=enabled
+Q_GLOBAL_STATIC(KCrash::Private, s_private)
Q_GLOBAL_STATIC(KCrash::CoreConfig, s_coreConfig)
static void kcrashInitialize()
diff --git a/src/private.cpp b/src/private.cpp
new file mode 100644
index 0000000..d2731ce
--- /dev/null
+++ b/src/private.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2019 Harald Sitter <sitter@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "private_p.h"
+
+#include <QFile>
+#include <QStandardPaths>
+
+namespace KCrash {
+
+Private::Private()
+{
+ loadDrKonqiOptionVersion();
+}
+
+void Private::loadDrKonqiOptionVersion()
+{
+ if (drKonqiOptionVersion > -1) {
+ return; // already loaded
+ }
+
+ // Set to unknown. Should any of the runtime bits fail we'll assume the
+ // version couldn't be determined.
+ drKonqiOptionVersion = 0;
+
+ const QString &path = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
+ QStringLiteral("drkonqi/option_version.txt"));
+ if (path.isEmpty()) {
+ return;
+ }
+ QFile file(path);
+ if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ return;
+ }
+ bool ok = false;
+ int version = QString::fromLatin1(file.readAll()).trimmed().toInt(&ok);
+ if (!ok) {
+ return;
+ }
+ drKonqiOptionVersion = version;
+}
+
+} // namespace KCrash
diff --git a/src/private_p.h b/src/private_p.h
new file mode 100644
index 0000000..48b7af9
--- /dev/null
+++ b/src/private_p.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2019 Harald Sitter <sitter@kde.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef KCRASH_PRIVATE_H
+#define KCRASH_PRIVATE_H
+
+namespace KCrash {
+
+class Private
+{
+public:
+ Private();
+ ~Private() = default;
+
+ int drKonqiOptionVersion = -1; // -1=initial value; 0=unknown; >0=specified
+
+private:
+ void loadDrKonqiOptionVersion();
+};
+
+} // namespace KCrash
+
+#endif // KCRASH_PRIVATE_H
--
2.17.1
sitter edited the content of this paste. (Show Details)May 19 2020, 1:11 PM
sitter changed the title of this paste from untitled to Masterwork From Distant Lands.