Improve test setup
* Improve code for reading environment variables * Make tests work when the temp directory is not `/tmp` because e.g. `TEMP` has been set or tests are executed under Windows
This commit is contained in:
parent
e4bbe852bd
commit
66a8dda244
|
@ -1,3 +1,4 @@
|
||||||
|
#include "../../testhelper/helper.h"
|
||||||
#include "../../testhelper/syncthingtestinstance.h"
|
#include "../../testhelper/syncthingtestinstance.h"
|
||||||
|
|
||||||
#include <c++utilities/io/misc.h>
|
#include <c++utilities/io/misc.h>
|
||||||
|
@ -94,14 +95,21 @@ void ApplicationTests::test()
|
||||||
setenv("ENABLE_ESCAPE_CODES", "0", true);
|
setenv("ENABLE_ESCAPE_CODES", "0", true);
|
||||||
|
|
||||||
// load expected status
|
// load expected status
|
||||||
const auto expectedStatusData(readFile(testFilePath("expected-status.txt"), 4000));
|
const auto expectedStatusData = [] {
|
||||||
const auto expectedStatusLines(splitString<vector<string>>(expectedStatusData, "\n"));
|
auto data = readFile(testFilePath("expected-status.txt"), 4000);
|
||||||
vector<regex> expectedStatusPatterns;
|
findAndReplace(data, "/tmp/", tempDirectory());
|
||||||
expectedStatusPatterns.reserve(expectedStatusLines.size());
|
return data;
|
||||||
for (const auto &line : expectedStatusLines) {
|
}();
|
||||||
expectedStatusPatterns.emplace_back(line);
|
const auto expectedStatusLines = splitString<vector<string>>(expectedStatusData, "\n");
|
||||||
}
|
const auto expectedStatusPatterns = [&] {
|
||||||
CPPUNIT_ASSERT(!expectedStatusPatterns.empty());
|
auto regex = std::vector<std::regex>();
|
||||||
|
regex.reserve(expectedStatusLines.size());
|
||||||
|
for (const auto &line : expectedStatusLines) {
|
||||||
|
regex.emplace_back(line);
|
||||||
|
}
|
||||||
|
CPPUNIT_ASSERT(!regex.empty());
|
||||||
|
return regex;
|
||||||
|
}();
|
||||||
|
|
||||||
// wait till Syncthing GUI becomes available
|
// wait till Syncthing GUI becomes available
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
#include "../../testhelper/helper.h"
|
#include "../../testhelper/helper.h"
|
||||||
#include "../../testhelper/syncthingtestinstance.h"
|
#include "../../testhelper/syncthingtestinstance.h"
|
||||||
|
|
||||||
|
#include <qtutilities/misc/conversion.h>
|
||||||
|
|
||||||
#include <c++utilities/tests/testutils.h>
|
#include <c++utilities/tests/testutils.h>
|
||||||
|
|
||||||
#include <cppunit/TestFixture.h>
|
#include <cppunit/TestFixture.h>
|
||||||
|
@ -472,10 +474,11 @@ void ConnectionTests::checkDirectories() const
|
||||||
const auto &dirInfo = m_connection.dirInfo();
|
const auto &dirInfo = m_connection.dirInfo();
|
||||||
CPPUNIT_ASSERT_EQUAL_MESSAGE("2 dirs present", 2_st, dirInfo.size());
|
CPPUNIT_ASSERT_EQUAL_MESSAGE("2 dirs present", 2_st, dirInfo.size());
|
||||||
const SyncthingDir &dir1 = dirInfo.front();
|
const SyncthingDir &dir1 = dirInfo.front();
|
||||||
|
const auto tempDir = QtUtilities::fromNativeFileName(tempDirectory());
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test1"), dir1.id);
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test1"), dir1.id);
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral(""), dir1.label);
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral(""), dir1.label);
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test1"), dir1.displayName());
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test1"), dir1.displayName());
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("/tmp/some/path/1/"), dir1.path);
|
CPPUNIT_ASSERT_EQUAL(tempDir + QStringLiteral("some/path/1/"), dir1.path);
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("idle"), dir1.statusString());
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("idle"), dir1.statusString());
|
||||||
CPPUNIT_ASSERT_EQUAL(SyncthingDirType::SendReceive, dir1.dirType);
|
CPPUNIT_ASSERT_EQUAL(SyncthingDirType::SendReceive, dir1.dirType);
|
||||||
CPPUNIT_ASSERT(!dir1.paused);
|
CPPUNIT_ASSERT(!dir1.paused);
|
||||||
|
@ -494,8 +497,8 @@ void ConnectionTests::checkDirectories() const
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test2"), dir2.id);
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test2"), dir2.id);
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Test dir 2"), dir2.label);
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Test dir 2"), dir2.label);
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Test dir 2"), dir2.displayName());
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Test dir 2"), dir2.displayName());
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("/tmp/some/path/2/"), dir2.path);
|
CPPUNIT_ASSERT_EQUAL(tempDir + QStringLiteral("some/path/2/"), dir2.path);
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("/tmp/some/path/2"), dir2.pathWithoutTrailingSlash().toString());
|
CPPUNIT_ASSERT_EQUAL(tempDir + QStringLiteral("some/path/2"), dir2.pathWithoutTrailingSlash().toString());
|
||||||
CPPUNIT_ASSERT_EQUAL(QStringLiteral("paused"), dir2.statusString());
|
CPPUNIT_ASSERT_EQUAL(QStringLiteral("paused"), dir2.statusString());
|
||||||
CPPUNIT_ASSERT_EQUAL(SyncthingDirType::SendReceive, dir2.dirType);
|
CPPUNIT_ASSERT_EQUAL(SyncthingDirType::SendReceive, dir2.dirType);
|
||||||
CPPUNIT_ASSERT(dir2.paused);
|
CPPUNIT_ASSERT(dir2.paused);
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
|
@ -54,6 +55,19 @@ namespace CppUtilities {
|
||||||
|
|
||||||
extern SYNCTHINGTESTHELPER_EXPORT double timeoutFactor;
|
extern SYNCTHINGTESTHELPER_EXPORT double timeoutFactor;
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Returns the temp directory using "/" consistently and ensuring a trailing "/" is always present.
|
||||||
|
*/
|
||||||
|
inline std::string tempDirectory()
|
||||||
|
{
|
||||||
|
auto dir = std::filesystem::temp_directory_path().make_preferred().string();
|
||||||
|
findAndReplace(dir, "\\", "/");
|
||||||
|
if (!dir.empty() && (dir.back() != '/' || dir.back() != '\\')) {
|
||||||
|
dir += '/';
|
||||||
|
}
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Waits for the \a duration specified in ms while keeping the event loop running.
|
* \brief Waits for the \a duration specified in ms while keeping the event loop running.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
#include "./syncthingtestinstance.h"
|
#include "./syncthingtestinstance.h"
|
||||||
#include "./helper.h"
|
#include "./helper.h"
|
||||||
|
|
||||||
|
#include <qtutilities/misc/compat.h>
|
||||||
|
#include <qtutilities/misc/conversion.h>
|
||||||
|
|
||||||
#include <c++utilities/conversion/conversionexception.h>
|
#include <c++utilities/conversion/conversionexception.h>
|
||||||
#include <c++utilities/conversion/stringbuilder.h>
|
#include <c++utilities/conversion/stringbuilder.h>
|
||||||
#include <c++utilities/conversion/stringconversion.h>
|
#include <c++utilities/conversion/stringconversion.h>
|
||||||
|
#include <c++utilities/io/misc.h>
|
||||||
#include <c++utilities/tests/testutils.h>
|
#include <c++utilities/tests/testutils.h>
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
@ -40,10 +44,9 @@ void SyncthingTestInstance::start()
|
||||||
cerr << "\n - Setup configuration for Syncthing tests ..." << endl;
|
cerr << "\n - Setup configuration for Syncthing tests ..." << endl;
|
||||||
|
|
||||||
// set timeout factor for helper
|
// set timeout factor for helper
|
||||||
const QByteArray timeoutFactorEnv(qgetenv("SYNCTHING_TEST_TIMEOUT_FACTOR"));
|
if (const auto timeoutFactorEnv = qgetenv("SYNCTHING_TEST_TIMEOUT_FACTOR"); !timeoutFactorEnv.isEmpty()) {
|
||||||
if (!timeoutFactorEnv.isEmpty()) {
|
|
||||||
try {
|
try {
|
||||||
timeoutFactor = stringToNumber<double>(string(timeoutFactorEnv.data()));
|
timeoutFactor = stringToNumber<double>(timeoutFactorEnv.data());
|
||||||
cerr << " - Using timeout factor " << timeoutFactor << endl;
|
cerr << " - Using timeout factor " << timeoutFactor << endl;
|
||||||
} catch (const ConversionException &) {
|
} catch (const ConversionException &) {
|
||||||
cerr << " - Specified SYNCTHING_TEST_TIMEOUT_FACTOR \"" << timeoutFactorEnv.data()
|
cerr << " - Specified SYNCTHING_TEST_TIMEOUT_FACTOR \"" << timeoutFactorEnv.data()
|
||||||
|
@ -55,49 +58,56 @@ void SyncthingTestInstance::start()
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup st config
|
// setup st config
|
||||||
const string configFilePath = workingCopyPath("testconfig/config.xml");
|
const auto tempDir = tempDirectory();
|
||||||
if (configFilePath.empty()) {
|
const auto relativeConfigFilePath = "testconfig/config.xml"s;
|
||||||
|
const auto configFilePathTemplate = testFilePath(relativeConfigFilePath);
|
||||||
|
const auto configFilePath = workingCopyPath(relativeConfigFilePath, WorkingCopyMode::NoCopy);
|
||||||
|
if (configFilePathTemplate.empty() || configFilePath.empty()) {
|
||||||
throw runtime_error("Unable to setup Syncthing config directory.");
|
throw runtime_error("Unable to setup Syncthing config directory.");
|
||||||
}
|
}
|
||||||
const QFileInfo configFile(QString::fromLocal8Bit(configFilePath.data()));
|
auto configFile = readFile(configFilePathTemplate);
|
||||||
|
findAndReplace(configFile, "/tmp/", tempDir);
|
||||||
|
writeFile(configFilePath, configFile);
|
||||||
|
|
||||||
// clean config dir
|
// clean config dir
|
||||||
const QDir configDir(configFile.dir());
|
const auto configFilePathFileInfo = QFileInfo(QtUtilities::fromNativeFileName(configFilePath));
|
||||||
for (QFileInfo &configEntry : configDir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
|
const auto configDir = QDir(configFilePathFileInfo.dir());
|
||||||
|
for (const auto &configEntry : configDir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot)) {
|
||||||
if (configEntry.isDir()) {
|
if (configEntry.isDir()) {
|
||||||
QDir(configEntry.absoluteFilePath()).removeRecursively();
|
QDir(configEntry.absoluteFilePath()).removeRecursively();
|
||||||
} else if (configEntry.fileName() != QStringLiteral("config.xml")) {
|
} else if (configEntry.fileName() != QLatin1String("config.xml")) {
|
||||||
QFile::remove(configEntry.absoluteFilePath());
|
QFile::remove(configEntry.absoluteFilePath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure dirs exist
|
// ensure dirs exist
|
||||||
const QDir parentDir(QStringLiteral("/tmp/some/path"));
|
const auto parentDir = QDir(QtUtilities::fromNativeFileName(tempDir) + QStringLiteral("some/path"));
|
||||||
parentDir.mkpath(QStringLiteral("1"));
|
parentDir.mkpath(QStringLiteral("1"));
|
||||||
parentDir.mkpath(QStringLiteral("2"));
|
parentDir.mkpath(QStringLiteral("2"));
|
||||||
|
|
||||||
// determine st path
|
// determine st path
|
||||||
const QByteArray syncthingPathFromEnv(qgetenv("SYNCTHING_PATH"));
|
auto syncthingPath = qEnvironmentVariable("SYNCTHING_PATH");
|
||||||
const QString syncthingPath(syncthingPathFromEnv.isEmpty() ? QStringLiteral("syncthing") : QString::fromLocal8Bit(syncthingPathFromEnv));
|
if (syncthingPath.isEmpty()) {
|
||||||
|
syncthingPath = QStringLiteral("syncthing");
|
||||||
|
}
|
||||||
|
|
||||||
// determine st port
|
// determine st port
|
||||||
const int syncthingPortFromEnv(qEnvironmentVariableIntValue("SYNCTHING_PORT"));
|
const auto syncthingPortFromEnv = qEnvironmentVariableIntValue("SYNCTHING_PORT");
|
||||||
m_syncthingPort = !syncthingPortFromEnv ? QStringLiteral("4001") : QString::number(syncthingPortFromEnv);
|
m_syncthingPort = !syncthingPortFromEnv ? QStringLiteral("4001") : QString::number(syncthingPortFromEnv);
|
||||||
|
|
||||||
// start st
|
// start st
|
||||||
// clang-format off
|
// clang-format off
|
||||||
const QStringList args{
|
const auto args = QStringList{
|
||||||
QStringLiteral("-gui-address=http://127.0.0.1:") + m_syncthingPort,
|
QStringLiteral("-gui-address=http://127.0.0.1:") + m_syncthingPort,
|
||||||
QStringLiteral("-gui-apikey=") + m_apiKey,
|
QStringLiteral("-gui-apikey=") + m_apiKey,
|
||||||
QStringLiteral("-home=") + configFile.absolutePath(),
|
QStringLiteral("-home=") + configFilePathFileInfo.absolutePath(),
|
||||||
QStringLiteral("-no-browser"),
|
QStringLiteral("-no-browser"),
|
||||||
QStringLiteral("-verbose"),
|
QStringLiteral("-verbose"),
|
||||||
};
|
};
|
||||||
cerr << "\n - Launching Syncthing: "
|
// clang-format on
|
||||||
<< syncthingPath.toStdString()
|
cerr << "\n - Launching Syncthing: " << syncthingPath.toStdString() << ' ' << args.join(QChar(' ')).toStdString() << endl;
|
||||||
<< ' ' << args.join(QChar(' ')).toStdString() << endl;
|
|
||||||
m_processSupposedToRun = true;
|
m_processSupposedToRun = true;
|
||||||
m_syncthingProcess.start(syncthingPath, args);
|
m_syncthingProcess.start(syncthingPath, args);
|
||||||
// clang-format on
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
Loading…
Reference in New Issue