CQtDeployer/tests/units/testutils.cpp

144 lines
3.3 KiB
C++
Raw Normal View History

2019-09-23 18:37:17 +03:00
/*
2023-01-04 00:48:14 +03:00
* Copyright (C) 2018-2023 QuasarApp.
2019-09-23 18:37:17 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
2019-09-08 14:18:58 +03:00
#include "testutils.h"
#include <QDir>
#include <QFileInfo>
TestUtils::TestUtils()
{
}
2019-09-23 14:26:45 +03:00
QSet<QString> TestUtils::getTree(const QString &path, int limit, int depch) {
2019-09-08 14:18:58 +03:00
QFileInfo info(path);
2019-09-23 14:26:45 +03:00
if (limit > 0 && depch >= limit) {
return {};
}
2019-09-08 14:18:58 +03:00
if (!info.exists()) {
return {};
}
auto result = QSet<QString>{};
if (!info.isDir()) {
result.insert(getFilePath(info.absoluteFilePath()));
2019-09-08 14:18:58 +03:00
return result;
}
QDir dir(info.absoluteFilePath());
auto list = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
2023-11-04 23:58:04 +01:00
for (const auto &i: std::as_const(list)) {
2019-09-23 14:26:45 +03:00
result.unite(getTree(i.absoluteFilePath(), limit, depch + 1));
2019-09-08 14:18:58 +03:00
}
return result;
}
2021-04-21 13:08:18 +03:00
QSet<QString> TestUtils::getFilesSet(const QString &path, int limit, int depch) {
QFileInfo info(path);
if (limit > 0 && depch >= limit) {
return {};
}
if (!info.exists()) {
return {};
}
auto result = QSet<QString>{};
if (!info.isDir()) {
result.insert(getFilePath(info.fileName()));
return result;
}
QDir dir(info.absoluteFilePath());
auto list = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
2023-11-04 23:58:04 +01:00
for (const auto &i: std::as_const(list)) {
2021-04-21 13:08:18 +03:00
result.unite(getFilesSet(i.absoluteFilePath(), limit, depch + 1));
}
return result;
}
QString TestUtils::getFilePath(const QString& i) {
2021-04-21 13:08:18 +03:00
auto file = i;
auto delimiter = file.lastIndexOf("/");
auto shared = file.indexOf(".so.", Qt::CaseInsensitive);
if (shared > delimiter) {
file = file.left(shared);
file.push_back(".so");
}
return file;
}
QSet<QString> TestUtils::createTree(const QStringList &tree) {
QSet<QString> res;
2020-01-12 16:43:03 +03:00
for (const auto &i : tree) {
2021-04-21 13:08:18 +03:00
res.insert(getFilePath(QFileInfo(i).absoluteFilePath()));
}
return res;
}
2022-10-05 17:05:38 +03:00
QSet<QString> TestUtils::deployTree(const QStringList &tree) {
QSet<QString> res;
for (const auto &i : tree) {
QFileInfo fInfo(i);
if (!fInfo.exists() && QDir().mkpath(i)) {
res.insert(getFilePath(QFileInfo(i).absoluteFilePath()));
}
}
return res;
}
2019-09-08 14:18:58 +03:00
QHash<QString, int> TestUtils::compareTree(const QSet<QString> &leftTree, const QSet<QString> &rightTree) {
QHash<QString, int> result;
auto valuel = leftTree - rightTree;
auto valuer = rightTree - leftTree;
2023-11-04 23:58:04 +01:00
for(auto &i :std::as_const(valuel)) {
2019-09-08 14:18:58 +03:00
result.insert(i, 1);
}
2023-11-04 23:58:04 +01:00
for(auto &i :std::as_const(valuer)) {
2019-09-08 14:18:58 +03:00
result.insert(i, -1);
}
return result;
}
2020-03-13 21:49:13 +03:00
bool TestUtils::deployFile(const QString &file, const QString &distanation,
const QHash<QByteArray, QByteArray> &replaceCase) const {
QFile f(file);
2021-11-17 12:04:35 +03:00
2020-03-13 21:49:13 +03:00
if (f.open(QIODevice::ReadOnly)) {
QFile dist(distanation);
if (!dist.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
return false;
}
QByteArray data = f.readAll();
for (auto it = replaceCase.begin(); it != replaceCase.end(); ++it) {
data.replace(it.key(), it.value());
}
return dist.write(data);
}
return false;
}