File indexing completed on 2025-03-09 05:20:53
0001 /* 0002 This file is part of the Kasten Framework, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2007 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "filesystem.hpp" 0010 0011 // Qt 0012 #include <QDir> 0013 #include <QFile> 0014 #include <QTest> 0015 0016 // TODO QStringLiteral 0017 static constexpr char basePath[] = ".kde-unit-test"; 0018 0019 TestFileSystem::TestFileSystem(const QString& name) 0020 : mBasePath(QDir::homePath() + QLatin1Char('/') 0021 + QLatin1String(basePath) + QLatin1Char('/') + name) 0022 { 0023 // clean up 0024 _removeDir(mBasePath); 0025 0026 _createDir(mBasePath); 0027 } 0028 0029 TestFileSystem::~TestFileSystem() 0030 { 0031 _removeDir(mBasePath); 0032 } 0033 0034 void TestFileSystem::removeDir(const QString& subPath) 0035 { 0036 _removeDir(mBasePath + QLatin1Char('/') + subPath); 0037 } 0038 0039 void TestFileSystem::createDir(const QString& subPath) 0040 { 0041 _createDir(mBasePath + QLatin1Char('/') + subPath); 0042 } 0043 0044 QString TestFileSystem::createFilePath(const QString& fileName, const QString& subPath) 0045 { 0046 if (subPath.isEmpty()) { 0047 return mBasePath + QLatin1Char('/') + fileName; 0048 } 0049 0050 return mBasePath + QLatin1Char('/') + subPath + QLatin1Char('/') + fileName; 0051 } 0052 0053 void TestFileSystem::_removeDir(const QString& path) 0054 { 0055 QDir localDir(path); 0056 const auto filesInDir = localDir.entryList(QDir::Files); 0057 for (const QString& fileName : filesInDir) { 0058 if (!localDir.remove(fileName)) { 0059 qWarning("%s: removing failed", qPrintable(fileName)); 0060 } 0061 } 0062 0063 QCOMPARE((int)localDir.entryList(QDir::Files).size(), 0); 0064 const QString subDirectory = localDir.dirName(); 0065 localDir.cdUp(); 0066 localDir.rmdir(subDirectory); 0067 } 0068 0069 void TestFileSystem::_createDir(const QString& path) 0070 { 0071 QVERIFY(QDir().mkpath(path)); 0072 }