]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/testdir.cpp
GIT_SILENT Sync po/docbooks with svn
[dolphin.git] / src / tests / testdir.cpp
1 /*
2 * SPDX-FileCopyrightText: 2010-2011 Frank Reininghaus <frank78ac@googlemail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "testdir.h"
8
9 #ifdef Q_OS_UNIX
10 #include <utime.h>
11 #else
12 #include <sys/utime.h>
13 #endif
14
15 TestDir::TestDir(const QString &directoryPrefix)
16 : QTemporaryDir(directoryPrefix)
17 {
18 }
19
20 TestDir::~TestDir()
21 {
22 }
23
24 QUrl TestDir::url() const
25 {
26 return QUrl::fromLocalFile(path());
27 }
28
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)
31 {
32 #ifdef Q_OS_UNIX
33 struct utimbuf utbuf;
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);
42 #endif
43 }
44
45 void TestDir::createFile(const QString &path, const QByteArray &data, const QDateTime &time)
46 {
47 QString absolutePath = path;
48 makePathAbsoluteAndCreateParents(absolutePath);
49
50 QFile f(absolutePath);
51 f.open(QIODevice::WriteOnly);
52 f.write(data);
53 f.close();
54
55 if (time.isValid()) {
56 setTimeStamp(absolutePath, time);
57 }
58
59 Q_ASSERT(QFile::exists(absolutePath));
60 }
61
62 void TestDir::createFiles(const QStringList &files)
63 {
64 for (const QString &path : files) {
65 createFile(path);
66 }
67 }
68
69 void TestDir::createDir(const QString &path, const QDateTime &time)
70 {
71 QString absolutePath = path;
72 makePathAbsoluteAndCreateParents(absolutePath);
73 QDir(TestDir::path()).mkdir(absolutePath);
74
75 if (time.isValid()) {
76 setTimeStamp(absolutePath, time);
77 }
78
79 Q_ASSERT(QFile::exists(absolutePath));
80 }
81
82 void TestDir::removeFiles(const QStringList &files)
83 {
84 for (const QString &path : files) {
85 removeFile(path);
86 }
87 }
88
89 void TestDir::removeFile(const QString &path)
90 {
91 QString absolutePath = path;
92 QFileInfo fileInfo(absolutePath);
93 if (!fileInfo.isAbsolute()) {
94 absolutePath = TestDir::path() + QLatin1Char('/') + path;
95 }
96 QFile::remove(absolutePath);
97 }
98
99 void TestDir::removeDir(const QString &path)
100 {
101 QString absolutePath = path;
102 QFileInfo fileInfo(absolutePath);
103 if (!fileInfo.isAbsolute()) {
104 absolutePath = TestDir::path() + QLatin1Char('/') + path;
105 }
106 QDir dirToRemove = QDir(absolutePath);
107 dirToRemove.removeRecursively();
108 }
109
110 void TestDir::makePathAbsoluteAndCreateParents(QString &path)
111 {
112 QFileInfo fileInfo(path);
113 if (!fileInfo.isAbsolute()) {
114 path = TestDir::path() + QLatin1Char('/') + path;
115 fileInfo.setFile(path);
116 }
117
118 const QDir dir = fileInfo.dir();
119 if (!dir.exists()) {
120 createDir(dir.absolutePath());
121 }
122
123 Q_ASSERT(dir.exists());
124 }