File indexing completed on 2022-09-20 12:43:22

0001 /*  This file is part of the KI18N Framework
0002     SPDX-FileCopyrightText: 2014 Kevin Krammer <krammer@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "testhelpers.h"
0008 
0009 #include <QDebug>
0010 #include <QDir>
0011 #include <QFile>
0012 #include <QFileInfo>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 
0016 static const QString targetFileName = QStringLiteral("ktranscript.ini");
0017 
0018 bool deployTestConfig()
0019 {
0020     QStandardPaths::setTestModeEnabled(true);
0021 
0022     QDir configDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
0023     const QFileInfo targetFile = QFileInfo(configDir, targetFileName);
0024 
0025     if (!configDir.exists()) {
0026         if (!QDir::current().mkpath(configDir.absolutePath())) {
0027             qWarning() << "Failed to create config dir" << configDir.absolutePath();
0028             return false;
0029         }
0030     } else if (targetFile.exists()) {
0031         if (!configDir.remove(targetFileName)) {
0032             qWarning() << "Failed to remove existing test config file" << targetFile.absoluteFilePath();
0033             return false;
0034         }
0035     }
0036 
0037     QFile sourceFile(QFINDTESTDATA("ktranscript-test.ini"));
0038     if (!sourceFile.exists()) {
0039         qWarning() << "Could not locate test data file" << sourceFile.fileName();
0040         return false;
0041     }
0042 
0043     if (!sourceFile.copy(targetFile.absoluteFilePath())) {
0044         qWarning() << "Failed to copy test config file" << sourceFile.fileName() << "to target location" << targetFile.absoluteFilePath();
0045         return false;
0046     }
0047 
0048     return true;
0049 }
0050 
0051 bool removeTestConfig()
0052 {
0053     Q_ASSERT(QStandardPaths::isTestModeEnabled());
0054 
0055     QDir configDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
0056     if (!configDir.exists()) {
0057         return true;
0058     }
0059 
0060     return configDir.remove(targetFileName);
0061 }