refactor: port DotEnvTests to QTests

This commit is contained in:
sithlord48
2025-03-28 16:34:40 -04:00
committed by Nick Bolton
parent 807f32975a
commit 245f9db5c8
5 changed files with 82 additions and 48 deletions

View File

@ -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 <QFile>
#include <QTextStream>
#include <gtest/gtest.h>
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);
}

View File

@ -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)

View File

@ -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"
)

View File

@ -0,0 +1,52 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* 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)

View File

@ -0,0 +1,20 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#include <QTest>
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");
};