From 245f9db5c8874681b3523cd5a48995cd9b5dd185 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Fri, 28 Mar 2025 16:34:40 -0400 Subject: [PATCH] refactor: port DotEnvTests to QTests --- src/test/integtests/gui/dotenv_tests.cpp | 48 ---------------------- src/unittests/CMakeLists.txt | 1 + src/unittests/gui/CMakeLists.txt | 9 ++++ src/unittests/gui/DotEnvTests.cpp | 52 ++++++++++++++++++++++++ src/unittests/gui/DotEnvTests.h | 20 +++++++++ 5 files changed, 82 insertions(+), 48 deletions(-) delete mode 100644 src/test/integtests/gui/dotenv_tests.cpp create mode 100644 src/unittests/gui/CMakeLists.txt create mode 100644 src/unittests/gui/DotEnvTests.cpp create mode 100644 src/unittests/gui/DotEnvTests.h diff --git a/src/test/integtests/gui/dotenv_tests.cpp b/src/test/integtests/gui/dotenv_tests.cpp deleted file mode 100644 index d9a1a8ff3..000000000 --- a/src/test/integtests/gui/dotenv_tests.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2024 Symless Ltd. - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -#include "gui/DotEnv.h" - -#include -#include - -#include - -TEST(dotenv_tests, dotenv_fileDoesNotExist_doesNotLoadEnvVar) -{ - const QString envFile = "tmp/test/.env"; - - deskflow::gui::dotenv(envFile); - - const QString actualValue = qEnvironmentVariable("TEST_ENV_VAR"); - EXPECT_TRUE(actualValue.isEmpty()); -} - -TEST(dotenv_tests, dotenv_envFileWithEntry_loadsEnvVar) -{ - const QString envFile = "tmp/test/.env"; - QFile file(envFile); - if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { - FAIL() << "Failed to create: " << envFile.toStdString(); - } - - const QString key = "TEST_ENV_VAR"; - const QString value = R"("test value")"; - const QString entry = key + " = " + value; - - QTextStream out(&file); - out << " # Comment" << Qt::endl; - out << "FOOBAR" << Qt::endl; - out << entry << Qt::endl; - file.close(); - - deskflow::gui::dotenv(envFile); - - const QString actualValue = qEnvironmentVariable(qPrintable(key)); - EXPECT_EQ("test value", actualValue.toStdString()); - - QFile::remove(envFile); -} diff --git a/src/unittests/CMakeLists.txt b/src/unittests/CMakeLists.txt index e3b1555ed..264688c9e 100644 --- a/src/unittests/CMakeLists.txt +++ b/src/unittests/CMakeLists.txt @@ -42,6 +42,7 @@ enable_testing() find_package(Qt6 ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Test) add_subdirectory(common) +add_subdirectory(gui) #make sure to use CI only plugin on Unix if (UNIX AND NOT APPLE) diff --git a/src/unittests/gui/CMakeLists.txt b/src/unittests/gui/CMakeLists.txt new file mode 100644 index 000000000..f5d35097c --- /dev/null +++ b/src/unittests/gui/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-FileCopyrightText: 2025 Deskflow Developers +# SPDX-License-Identifier: MIT + +create_test( + NAME DotEnvTests + DEPENDS gui + SOURCE DotEnvTests.cpp + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/lib/gui" +) diff --git a/src/unittests/gui/DotEnvTests.cpp b/src/unittests/gui/DotEnvTests.cpp new file mode 100644 index 000000000..bd2ef64ef --- /dev/null +++ b/src/unittests/gui/DotEnvTests.cpp @@ -0,0 +1,52 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-FileCopyrightText: (C) 2024 Symless Ltd. + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include "DotEnvTests.h" + +#include "../../lib/gui/DotEnv.h" + +void DotEnvTests::initTestCase() +{ + QDir dir; + QVERIFY(dir.mkpath("tmp/test")); +} + +void DotEnvTests::invalidFile() +{ + deskflow::gui::dotenv(m_envFile); + + const QString actualValue = qEnvironmentVariable("TEST_ENV_VAR"); + + QVERIFY(actualValue.isEmpty()); + QCOMPARE(actualValue, ""); +} + +void DotEnvTests::validFile() +{ + QFile file(m_envFile); + QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text)); + + const QString key = "TEST_ENV_VAR"; + const QString value = R"("test value")"; + const QString entry = key + " = " + value; + + QTextStream out(&file); + out << " # Comment" << Qt::endl; + out << "FOOBAR" << Qt::endl; + out << entry << Qt::endl; + file.close(); + + deskflow::gui::dotenv(m_envFile); + + const QString actualValue = qEnvironmentVariable(qPrintable(key)); + + QCOMPARE(actualValue.toStdString(), "test value"); + + QVERIFY(QFile::remove(m_envFile)); +} + +QTEST_MAIN(DotEnvTests) diff --git a/src/unittests/gui/DotEnvTests.h b/src/unittests/gui/DotEnvTests.h new file mode 100644 index 000000000..b2429a5e7 --- /dev/null +++ b/src/unittests/gui/DotEnvTests.h @@ -0,0 +1,20 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception + */ + +#include + +class DotEnvTests : public QObject +{ + Q_OBJECT +private slots: + // Test are run in order top to bottom + void initTestCase(); + void invalidFile(); + void validFile(); + +private: + inline static const QString m_envFile = QStringLiteral("tmp/test/.env"); +};