File indexing completed on 2022-11-29 18:25:17

0001 /* This file is part of the KDE libraries
0002     Copyright (c) 2005 Thomas Braxton <brax108@cox.net>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License version 2 as published by the Free Software Foundation.
0007 
0008     This library is distributed in the hope that it will be useful,
0009     but WITHOUT ANY WARRANTY; without even the implied warranty of
0010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011     Library General Public License for more details.
0012 
0013     You should have received a copy of the GNU Library General Public License
0014     along with this library; see the file COPYING.LIB.  If not, write to
0015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016     Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "qtest_kde.h"
0020 #include <ktempdir.h>
0021 
0022 #include <QDir>
0023 #include <QObject>
0024 #include <QDebug>
0025 
0026 class KTempDirTest : public QObject
0027 {
0028     Q_OBJECT
0029 private Q_SLOTS:
0030     void testDefaultCtor();
0031     void testNoDelete();
0032     void testAutoDelete();
0033     void testCreateSubDir();
0034 };
0035 
0036 void KTempDirTest::testDefaultCtor()
0037 {
0038     QString name;
0039     {
0040         KTempDir dir;
0041         QVERIFY(dir.status() == 0);
0042         QVERIFY(dir.exists());
0043         name = dir.name();
0044         qDebug() << name;
0045         QVERIFY(QDir(name).exists());
0046     }
0047     QVERIFY(!QDir(name).exists());
0048 #ifdef Q_OS_UNIX
0049     QVERIFY2(name.startsWith(QLatin1String("/tmp/")), qPrintable(name));
0050 #endif
0051 }
0052 
0053 void KTempDirTest::testNoDelete()
0054 {
0055     KTempDir dir(QLatin1String("test"));
0056     dir.setAutoRemove(false);
0057     QVERIFY(dir.status() == 0);
0058     QVERIFY(dir.exists());
0059     QVERIFY(QDir(dir.name()).exists());
0060 
0061     dir.unlink();
0062     QVERIFY(dir.status() == 0);
0063     QVERIFY(!dir.exists());
0064     QVERIFY(!QDir(dir.name()).exists());
0065 
0066 }
0067 
0068 void KTempDirTest::testAutoDelete()
0069 {
0070     KTempDir *dir = new KTempDir(QLatin1String("test"));
0071     QVERIFY(dir->status() == 0);
0072     QVERIFY(dir->exists());
0073 
0074     QString dName = dir->name();
0075     delete dir;
0076     QVERIFY(!QDir(dName).exists());
0077 }
0078 
0079 void KTempDirTest::testCreateSubDir()
0080 {
0081     KTempDir *dir = new KTempDir(QLatin1String("test"));
0082     QVERIFY(dir->status() == 0);
0083     QVERIFY(dir->exists());
0084 
0085     QDir d(dir->name());
0086     QVERIFY(d.exists());
0087 
0088     QVERIFY(d.mkdir(QString(QString::fromLatin1("123"))));
0089     QVERIFY(d.mkdir(QString(QString::fromLatin1("456"))));
0090 
0091     QString dName = dir->name();
0092     delete dir;
0093     d.refresh();
0094 
0095     QVERIFY(!QDir(dName).exists());
0096     QVERIFY(!d.exists(QString::fromLatin1(("123"))));
0097     QVERIFY(!d.exists(QString(QString::fromLatin1("456"))));
0098 }
0099 
0100 QTEST_MAIN(KTempDirTest)
0101 
0102 #include "ktempdirtest.moc"