From de09f3dbc2e3ec5f6c94211dfa4ccb3f67dd5770 Mon Sep 17 00:00:00 2001 From: ComixHe Date: Mon, 14 Aug 2023 16:30:16 +0800 Subject: [PATCH] refact: add test-coverage.sh and some docs Signed-off-by: ComixHe --- .gitignore | 2 +- CMakeLists.txt | 6 ++++- docs/customVariables.md | 6 +++++ examples/launchApp/README.md | 9 +++++++ src/CMakeLists.txt | 2 +- src/constant.h | 24 ++++++++++++++++--- src/dbus/applicationservice.h | 2 +- src/desktopentry.cpp | 11 +++++---- tests/CMakeLists.txt | 13 ++-------- .../deepin-editor.desktop} | 0 tests/ut_desktopentry.cpp | 18 +++++++++----- tools/test-coverage.sh | 24 +++++++++++++++++++ 12 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 docs/customVariables.md rename tests/data/{desktopExample.desktop => applications/deepin-editor.desktop} (100%) create mode 100755 tools/test-coverage.sh diff --git a/.gitignore b/.gitignore index 5acb669..002089c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -build +build* .vscode diff --git a/CMakeLists.txt b/CMakeLists.txt index 8739711..a94ae70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,12 +14,17 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(THREADS_PREFER_PTHREAD_FLAG ON) set(BUILD_EXAMPLES ON CACHE BOOL "Whether to build examples or not.") +set(DEBUG_MODE ON CACHE BOOL "start a dde-applicatiom-manager service for debug") find_package(Qt6 REQUIRED COMPONENTS Core DBus Concurrent) find_package(Threads REQUIRED) set(APP_LAUNCH_HELPER_BIN app-launch-helper) +if(DEBUG_MODE) + add_compile_definitions(-DDEBUG_MODE) +endif() + add_subdirectory(src) add_subdirectory(plugins) add_subdirectory(apps) @@ -27,7 +32,6 @@ add_subdirectory(apps) include(CTest) if(BUILD_TESTING) - enable_testing() add_subdirectory(tests) endif() diff --git a/docs/customVariables.md b/docs/customVariables.md new file mode 100644 index 0000000..bb138c4 --- /dev/null +++ b/docs/customVariables.md @@ -0,0 +1,6 @@ +# Environment Variable + +本文档描述了 dde-application-manager 所用到的环境变量。 + +- `DEEPIN_APPLICATION_MANAGER_APP_LAUNCH_HELPER_BIN` + 指定运行时app_launcher_helper_bin的绝对路径,便于调试. diff --git a/examples/launchApp/README.md b/examples/launchApp/README.md index e69de29..3fef2c7 100644 --- a/examples/launchApp/README.md +++ b/examples/launchApp/README.md @@ -0,0 +1,9 @@ +# LaunchApp Example + +## Introduction + +这是 dde-application-manager 的一个拉起应用的例子。 + +本示例演示了如何使用 D-Bus 调用通过 dde-application-manager 启动 Google Chrome Stable 应用。 + +如需更改启动行为,请修改对应的 AppId 和启动参数。 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ae68130..c3f2531 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,7 +8,7 @@ set(LIB_NAME dde_am_static) file(GLOB SRCS ${CMAKE_CURRENT_LIST_DIR}/*.cpp ${CMAKE_CURRENT_LIST_DIR}/*.h) -add_library(${LIB_NAME} ${SRCS}) +add_library(${LIB_NAME} STATIC ${SRCS}) target_include_directories(${LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/src/constant.h b/src/constant.h index bf71405..bffef65 100644 --- a/src/constant.h +++ b/src/constant.h @@ -8,7 +8,13 @@ constexpr auto SystemdService = u8"org.freedesktop.systemd1"; constexpr auto SystemdObjectPath = u8"/org/freedesktop/systemd1"; constexpr auto SystemdInterfaceName = u8"org.freedesktop.systemd1.Manager"; -constexpr auto DDEApplicationManager1ServiceName = u8"org.deepin.dde.ApplicationManager1"; +constexpr auto DDEApplicationManager1ServiceName = +#ifdef DEBUG_MODE + u8"org.deepin.dde.debug.ApplicationManager1"; +#else + u8"org.deepin.dde.ApplicationManager1"; +#endif + constexpr auto DDEApplicationManager1ObjectPath = u8"/org/deepin/dde/ApplicationManager1"; constexpr auto DDEApplicationManager1ApplicationObjectPath = u8"/org/deepin/dde/ApplicationManager1/Application/"; constexpr auto DDEApplicationManager1InstanceObjectPath = u8"/org/deepin/dde/ApplicationManager1/Instance/"; @@ -16,7 +22,19 @@ constexpr auto DDEApplicationManager1JobManagerObjectPath = u8"/org/deepin/dde/A constexpr auto DDEApplicationManager1JobObjectPath = u8"/org/deepin/dde/ApplicationManager1/JobManager1/Job/"; constexpr auto DesktopFileEntryKey = u8"Desktop Entry"; constexpr auto DesktopFileActionKey = u8"Desktop Action "; -constexpr auto ApplicationManagerServerDBusName = u8"deepin_application_manager_server_bus"; -constexpr auto ApplicationManagerDestDBusName = "deepin_application_manager_dest_bus"; + +constexpr auto ApplicationManagerServerDBusName = +#ifdef DEBUG_MODE + u8"deepin_application_manager_debug_server_bus"; +#else + u8"deepin_application_manager_server_bus"; +#endif + +constexpr auto ApplicationManagerDestDBusName = +#ifdef DEBUG_MODE + u8"deepin_application_manager_debug_dest_bus"; +#else + u8"deepin_application_manager_dest_bus"; +#endif #endif diff --git a/src/dbus/applicationservice.h b/src/dbus/applicationservice.h index ddeb44a..4cd8bf0 100644 --- a/src/dbus/applicationservice.h +++ b/src/dbus/applicationservice.h @@ -76,7 +76,7 @@ private: if constexpr (std::is_same_v) { m_applicationPath = -#ifdef QT_DEBUG +#ifdef DEBUG_MODE QDBusObjectPath{objectPath + escapeToObjectPath(dbusAppid)}; #else QDBusObjectPath{objectPath + QUuid::createUuid().toString(QUuid::Id128)}; diff --git a/src/desktopentry.cpp b/src/desktopentry.cpp index c747af8..881d135 100644 --- a/src/desktopentry.cpp +++ b/src/desktopentry.cpp @@ -87,9 +87,9 @@ DesktopErrorCode DesktopEntry::parseEntry(const QString &str, decltype(m_entryMa std::optional DesktopFile::searchDesktopFileByPath(const QString &desktopFile, DesktopErrorCode &err) noexcept { - constexpr decltype(auto) desktopPostfix = ".desktop"; + constexpr decltype(auto) desktopSuffix = ".desktop"; - if (!desktopFile.endsWith(desktopPostfix)) { + if (!desktopFile.endsWith(desktopSuffix)) { qWarning() << "file isn't a desktop file:" << desktopFile; err = DesktopErrorCode::MismatchedFile; return std::nullopt; @@ -111,7 +111,7 @@ std::optional DesktopFile::searchDesktopFileByPath(const QString &d }); if (idGen) { - auto tmp = path.chopped(sizeof(desktopPostfix) - 1); + auto tmp = path.chopped(sizeof(desktopSuffix) - 1); auto components = tmp.split(QDir::separator()).toList(); auto it = std::find(components.cbegin(), components.cend(), "applications"); QString FileId; @@ -138,9 +138,10 @@ std::optional DesktopFile::searchDesktopFileByPath(const QString &d std::optional DesktopFile::searchDesktopFileById(const QString &appId, DesktopErrorCode &err) noexcept { auto XDGDataDirs = getXDGDataDirs(); + constexpr auto desktopSuffix = u8".desktop"; for (const auto &dir : XDGDataDirs) { - auto app = QFileInfo{dir + QDir::separator() + appId}; + auto app = QFileInfo{dir + QDir::separator() + appId + desktopSuffix}; while (!app.exists()) { auto filePath = app.absoluteFilePath(); auto hyphenIndex = filePath.indexOf('-'); @@ -155,6 +156,8 @@ std::optional DesktopFile::searchDesktopFileById(const QString &app return searchDesktopFileByPath(app.absoluteFilePath(), err); } } + + err = DesktopErrorCode::NotFound; return std::nullopt; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ec116b9..356dce1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,19 +18,10 @@ target_link_libraries(${BIN_NAME} PRIVATE dde_am_static ) -target_compile_options(${BIN_NAME} PRIVATE - -fno-access-control - -fsanitize=undefined - -fsanitize=address -) - -target_link_options(${BIN_NAME} PRIVATE - -fsanitize=undefined - -fsanitize=address -) +target_compile_options(${BIN_NAME} PRIVATE "-fno-access-control") add_test( NAME UnitTest COMMAND ${BIN_NAME} - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests ) diff --git a/tests/data/desktopExample.desktop b/tests/data/applications/deepin-editor.desktop similarity index 100% rename from tests/data/desktopExample.desktop rename to tests/data/applications/deepin-editor.desktop diff --git a/tests/ut_desktopentry.cpp b/tests/ut_desktopentry.cpp index 583c8bd..f3b1d04 100644 --- a/tests/ut_desktopentry.cpp +++ b/tests/ut_desktopentry.cpp @@ -15,22 +15,28 @@ class TestDesktopEntry : public testing::Test public: static void SetUpTestCase() { + env = qgetenv("XDG_DATA_DIRS"); auto curDir = QDir::current(); - QString path{curDir.absolutePath() + "/data/desktopExample.desktop"}; + QByteArray fakeXDG = (curDir.absolutePath() + QDir::separator() + "data").toLocal8Bit(); + qputenv("XDG_DATA_DIRS", fakeXDG); DesktopErrorCode err; - auto file = DesktopFile::searchDesktopFileByPath(path, err); + auto file = DesktopFile::searchDesktopFileById("deepin-editor", err); if (!file.has_value()) { - qWarning() << "search " << path << "failed:" << err; + qWarning() << "search failed:" << err; return; } m_file.reset(new DesktopFile{std::move(file).value()}); } + + static void TearDownTestCase() { qputenv("XDG_DATA_DIRS", env); } + void SetUp() override {} void TearDown() override {} QSharedPointer file() { return m_file; } private: static inline QSharedPointer m_file; + static inline QByteArray env; }; TEST_F(TestDesktopEntry, desktopFile) @@ -39,16 +45,16 @@ TEST_F(TestDesktopEntry, desktopFile) ASSERT_FALSE(fileptr.isNull()); const auto &exampleFile = file(); auto curDir = QDir::current(); - QString path{curDir.absolutePath() + "/data/desktopExample.desktop"}; + QString path{curDir.absolutePath() + QDir::separator() + "data" + QDir::separator() + "applications" + QDir::separator() + + "deepin-editor.desktop"}; EXPECT_EQ(exampleFile->filePath().toStdString(), path.toStdString()); - EXPECT_EQ(exampleFile->desktopId().toStdString(), ""); + EXPECT_EQ(exampleFile->desktopId().toStdString(), "deepin-editor"); } TEST_F(TestDesktopEntry, prase) { const auto &exampleFile = file(); ASSERT_FALSE(exampleFile.isNull()); - ; DesktopEntry entry; QFile in{exampleFile->filePath()}; ASSERT_TRUE(in.open(QFile::ExistingOnly | QFile::ReadOnly | QFile::Text)); diff --git a/tools/test-coverage.sh b/tools/test-coverage.sh new file mode 100755 index 0000000..df7b6be --- /dev/null +++ b/tools/test-coverage.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. +# +# SPDX-License-Identifier: LGPL-3.0-or-later + +cd "$(git rev-parse --show-toplevel)" || exit 255 + +BUILD_DIR=${BUILD_DIR:="build-cov"} + +HTML_DIR=${BUILD_DIR}/html + +export ASAN_OPTIONS="halt_on_error=0" + +cmake -B "$BUILD_DIR" \ + -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_CXX_FLAGS="--coverage -fsanitize=address -fsanitize-recover=address " \ + -DCMAKE_CXX_LINK_FLAGS="-lasan" + +cmake --build "$BUILD_DIR" -j$(nproc) + +cmake --build "$BUILD_DIR" -j$(nproc) -t test + +gcovr -f "src/*" --html-details "$BUILD_DIR"/coverage.html