Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions tests/ut_applicationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,22 @@ TEST_F(TestApplicationManager, identifyService)
}
using namespace std::chrono_literals;
// for service unit
auto workingDir = QDir::cleanPath(QDir::current().absolutePath() + QDir::separator() + ".." + QDir::separator() + "tools");
QFile pidFile{workingDir + QDir::separator() + "pid.txt"};
auto workingDir = QDir::current();
ASSERT_TRUE(workingDir.cdUp());
ASSERT_TRUE(workingDir.cdUp());
ASSERT_TRUE(workingDir.cd("tools"));
QFile pidFile{workingDir.absoluteFilePath("pid.txt")};
if (pidFile.exists()) {
ASSERT_TRUE(pidFile.remove());
}

QProcess fakeServiceProc;
fakeServiceProc.setWorkingDirectory(workingDir);
fakeServiceProc.setWorkingDirectory(workingDir.absolutePath());
auto InstanceId = InstancePath.path().split('/').last();
fakeServiceProc.start("/usr/bin/systemd-run",
{{QString{R"(--unit=app-DDE-test\x2dApplication@%1.service)"}.arg(InstanceId)},
{"--user"},
{QString{"--working-directory=%1"}.arg(workingDir)},
{QString{"--working-directory=%1"}.arg(workingDir.absolutePath())},
{"--slice=app.slice"},
{"./fake-process.sh"}});
fakeServiceProc.waitForFinished();
Expand Down Expand Up @@ -123,11 +126,11 @@ TEST_F(TestApplicationManager, identifyService)
{{"--scope"},
{QString{R"(--unit=app-DDE-test\x2dApplication-%1.scope)"}.arg(InstanceId)},
{"--user"},
{QString{"--working-directory=%1"}.arg(workingDir)},
{QString{"--working-directory=%1"}.arg(workingDir.absolutePath())},
{"--slice=app.slice"},
{"./fake-process.sh"},
{"Scope"}},
workingDir));
workingDir.absolutePath()));

std::this_thread::sleep_for(500ms);

Expand Down
16 changes: 10 additions & 6 deletions tests/ut_desktopentry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class TestDesktopEntry : public testing::Test
static void SetUpTestCase()
{
env = qgetenv("XDG_DATA_DIRS");
auto curDir = QDir::current();
QByteArray fakeXDG = (curDir.absolutePath() + QDir::separator() + "data").toLocal8Bit();
ASSERT_TRUE(qputenv("XDG_DATA_DIRS", fakeXDG));
auto fakeXDG = QDir::current();
ASSERT_TRUE(fakeXDG.cdUp());
ASSERT_TRUE(fakeXDG.cdUp());
ASSERT_TRUE(fakeXDG.cd("tests/data"));
ASSERT_TRUE(qputenv("XDG_DATA_DIRS", fakeXDG.absolutePath().toLocal8Bit()));
Comment on lines +19 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider using a shared helper or QFINDTESTDATA-style lookup for the test data root instead of hardcoding cdUp() hops.

These cdUp() calls still rely on a fixed directory layout (two levels up, then tests/data), which can break under different IDE/CTest/build setups. Centralizing the lookup in a helper (e.g. QDir testDataRoot()) or using QFINDTESTDATA would make the tests more portable and avoid duplicating this logic later in the file.

Suggested implementation:

    static QDir testDataRoot()
    {
        const QString marker = QLatin1String("tests/data");

#ifdef QFINDTESTDATA
        // Try to locate the tests/data directory using QFINDTESTDATA in a
        // build-system independent way. We use "." as a neutral marker and
        // then walk up to find a matching tests/data directory.
        const QString candidate = QFINDTESTDATA(".");
        if (!candidate.isEmpty()) {
            QDir dir(candidate);
            const int idx = candidate.indexOf(marker);
            if (idx != -1) {
                // If the resolved path already contains "tests/data", cut it there.
                return QDir(candidate.left(idx + marker.size()));
            }

            // Otherwise walk upwards until we find a tests/data directory.
            while (!dir.isRoot()) {
                QDir probe(dir);
                if (probe.cd(marker)) {
                    return probe;
                }
                dir.cdUp();
            }
        }
#endif

        // Fallback: walk up from the current directory until we find tests/data.
        QDir dir = QDir::current();
        while (!dir.isRoot()) {
            QDir probe(dir);
            if (probe.cd(marker)) {
                return probe;
            }
            dir.cdUp();
        }

        // As a last resort, return the current directory (tests may fail visibly).
        return QDir::current();
    }

    static void SetUpTestCase()
        env = qgetenv("XDG_DATA_DIRS");
        const auto fakeXDG = testDataRoot();
        ASSERT_TRUE(qputenv("XDG_DATA_DIRS", fakeXDG.absolutePath().toLocal8Bit()));
    auto desktopFileDir = testDataRoot();
  1. Ensure that this file includes the necessary Qt headers if they are not already present:
    • <QDir>
    • <QString>
    • <QTest> (or a header that provides the QFINDTESTDATA macro).
  2. If your project’s QtTest setup doesn’t define QFINDTESTDATA in this compilation unit, either:
    • Add the proper QtTest include, or
    • Remove/adjust the #ifdef QFINDTESTDATA block and rely solely on the fallback directory walk as appropriate for your environment.
  3. If your actual test data root is not literally tests/data or uses a different relative structure, update the marker constant in testDataRoot() accordingly so it resolves the correct directory.

ParserError err;
auto file = DesktopFile::searchDesktopFileById("deepin-editor", err);
if (!file.has_value()) {
Expand All @@ -42,9 +44,11 @@ TEST_F(TestDesktopEntry, desktopFile)
const auto &fileptr = file();
ASSERT_FALSE(fileptr.isNull());
const auto &exampleFile = file();
auto curDir = QDir::current();
QString path{curDir.absolutePath() + QDir::separator() + "data" + QDir::separator() + "applications" + QDir::separator() +
"deepin-editor.desktop"};
auto desktopFileDir = QDir::current();
ASSERT_TRUE(desktopFileDir.cdUp());
ASSERT_TRUE(desktopFileDir.cdUp());
ASSERT_TRUE(desktopFileDir.cd("tests/data/applications"));
const auto& path = desktopFileDir.absoluteFilePath("deepin-editor.desktop");
Comment on lines +47 to +51
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Reuse the same data-root resolution as in SetUpTestCase to keep the desktop file path logic consistent.

This test now resolves the path differently from SetUpTestCase (manual cdUp() twice into tests/data/applications vs. using the shared data-root logic). If the directory layout changes, both places must be updated in lockstep. Consider reusing the same helper or deriving applications from the directory used in SetUpTestCase to avoid divergence between setup and the expected sourcePath() value.

Suggested implementation:

    const auto &fileptr = file();
    ASSERT_FALSE(fileptr.isNull());
    const auto &exampleFile = file();

    // Resolve the desktop file path based on the same data-root used to set XDG_DATA_DIRS.
    QDir applicationsDir{fakeXDG};
    ASSERT_TRUE(applicationsDir.cd("applications"));
    const auto &path = applicationsDir.absoluteFilePath("deepin-editor.desktop");

    EXPECT_EQ(exampleFile->sourcePath(), path);
    EXPECT_EQ(exampleFile->desktopId().toStdString(), "deepin-editor");
}

If fakeXDG is not the same directory used in SetUpTestCase as the data-root, replace fakeXDG with the actual data-root QDir (e.g., a static member initialized in SetUpTestCase) so that both the environment (XDG_DATA_DIRS) and the expected sourcePath() derive from the same root.

EXPECT_EQ(exampleFile->sourcePath(), path);
EXPECT_EQ(exampleFile->desktopId().toStdString(), "deepin-editor");
}
Expand Down
10 changes: 7 additions & 3 deletions tests/ut_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "applicationHooks.h"
#include <gtest/gtest.h>
#include <QDir>
#include <QStringList>
#include <gtest/gtest.h>

TEST(ApplicationHookTest, load)
{
auto file =
QDir::currentPath() + QDir::separator() + "data" + QDir::separator() + "hooks.d" + QDir::separator() + "1-test.json";
auto hooksDir = QDir::current();
ASSERT_TRUE(hooksDir.cdUp());
ASSERT_TRUE(hooksDir.cdUp());
ASSERT_TRUE(hooksDir.cd("tests/data/hooks.d"));

auto file = hooksDir.absoluteFilePath("1-test.json");
Comment on lines +12 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Make hook test data lookup resilient to different working directories (e.g. via a shared helper or QFINDTESTDATA).

This test currently assumes the process starts two levels below the project root and that tests/data/hooks.d exists there, which can be brittle under different runners. Consider resolving 1-test.json via a shared testDataRoot() helper (as used in other tests) or a QFINDTESTDATA-style approach so it no longer depends on the initial working directory.

Suggested implementation:

#include "applicationHooks.h"
#include <gtest/gtest.h>
#include <QDir>
#include <QStringList>
#include <QtTest/QTest>
TEST(ApplicationHookTest, load)
{
    const auto file = QFINDTESTDATA("data/hooks.d/1-test.json");
    ASSERT_FALSE(file.isEmpty());

    auto hook = ApplicationHook::loadFromFile(QString::fromUtf8(file));

To fully support this change, ensure that data/hooks.d/1-test.json is correctly registered as test data in your build system so that QFINDTESTDATA("data/hooks.d/1-test.json") can locate it. For example, with qmake you would add it to TESTDATA += data/hooks.d/1-test.json, and with CMake/Qt6 you would typically use qt_add_test() / qt_add_resources() or set_tests_properties() equivalents to install or expose the test data at runtime.

auto hook = ApplicationHook::loadFromFile(file);
EXPECT_TRUE(hook);
EXPECT_EQ(hook->hookName(), QString{"1-test.json"});
Expand Down