]>
cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testdir.cpp
2 * SPDX-FileCopyrightText: 2010-2011 Frank Reininghaus <frank78ac@googlemail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include <sys/utime.h>
15 TestDir::TestDir(const QString
&directoryPrefix
)
16 : QTemporaryDir(directoryPrefix
)
24 QUrl
TestDir::url() const
26 return QUrl::fromLocalFile(path());
29 /** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
30 static void setTimeStamp(const QString
&path
, const QDateTime
&mtime
)
34 utbuf
.actime
= mtime
.toSecsSinceEpoch();
35 utbuf
.modtime
= utbuf
.actime
;
36 utime(QFile::encodeName(path
), &utbuf
);
37 #elif defined(Q_OS_WIN)
38 struct _utimbuf utbuf
;
39 utbuf
.actime
= mtime
.toSecsSinceEpoch();
40 utbuf
.modtime
= utbuf
.actime
;
41 _wutime(reinterpret_cast<const wchar_t *>(path
.utf16()), &utbuf
);
45 void TestDir::createFile(const QString
&path
, const QByteArray
&data
, const QDateTime
&time
)
47 QString absolutePath
= path
;
48 makePathAbsoluteAndCreateParents(absolutePath
);
50 QFile
f(absolutePath
);
51 f
.open(QIODevice::WriteOnly
);
56 setTimeStamp(absolutePath
, time
);
59 Q_ASSERT(QFile::exists(absolutePath
));
62 void TestDir::createFiles(const QStringList
&files
)
64 for (const QString
&path
: files
) {
69 void TestDir::createDir(const QString
&path
, const QDateTime
&time
)
71 QString absolutePath
= path
;
72 makePathAbsoluteAndCreateParents(absolutePath
);
73 QDir(TestDir::path()).mkdir(absolutePath
);
76 setTimeStamp(absolutePath
, time
);
79 Q_ASSERT(QFile::exists(absolutePath
));
82 void TestDir::removeFiles(const QStringList
&files
)
84 for (const QString
&path
: files
) {
89 void TestDir::removeFile(const QString
&path
)
91 QString absolutePath
= path
;
92 QFileInfo
fileInfo(absolutePath
);
93 if (!fileInfo
.isAbsolute()) {
94 absolutePath
= TestDir::path() + QLatin1Char('/') + path
;
96 QFile::remove(absolutePath
);
99 void TestDir::removeDir(const QString
&path
)
101 QString absolutePath
= path
;
102 QFileInfo
fileInfo(absolutePath
);
103 if (!fileInfo
.isAbsolute()) {
104 absolutePath
= TestDir::path() + QLatin1Char('/') + path
;
106 QDir dirToRemove
= QDir(absolutePath
);
107 dirToRemove
.removeRecursively();
110 void TestDir::makePathAbsoluteAndCreateParents(QString
&path
)
112 QFileInfo
fileInfo(path
);
113 if (!fileInfo
.isAbsolute()) {
114 path
= TestDir::path() + QLatin1Char('/') + path
;
115 fileInfo
.setFile(path
);
118 const QDir dir
= fileInfo
.dir();
120 createDir(dir
.absolutePath());
123 Q_ASSERT(dir
.exists());