2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2012-01-19 15:26:54 +01:00
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2012-01-19 15:26:54 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2012-01-19 15:26:54 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:55:33 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-01-19 15:26:54 +01:00
|
|
|
**
|
2016-01-15 14:55:33 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2012-01-19 15:26:54 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2012-01-19 15:26:54 +01:00
|
|
|
|
|
|
|
#include <QtTest>
|
2012-08-06 13:42:46 +02:00
|
|
|
#include <QDebug>
|
2012-01-19 15:26:54 +01:00
|
|
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
|
|
//TESTED_COMPONENT=src/libs/utils
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
class tst_fileutils : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void parentDir_data();
|
|
|
|
void parentDir();
|
2012-10-04 06:06:09 +02:00
|
|
|
void isChildOf_data();
|
|
|
|
void isChildOf();
|
2015-01-10 23:23:15 +02:00
|
|
|
void fileName_data();
|
|
|
|
void fileName();
|
2012-01-19 15:26:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void tst_fileutils::parentDir_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("path");
|
|
|
|
QTest::addColumn<QString>("parentPath");
|
|
|
|
QTest::addColumn<QString>("expectFailMessage");
|
|
|
|
|
2015-01-09 10:53:46 +02:00
|
|
|
QTest::newRow("empty path") << "" << "" << "";
|
|
|
|
QTest::newRow("root only") << "/" << "" << "";
|
|
|
|
QTest::newRow("//") << "//" << "" << "";
|
|
|
|
QTest::newRow("/tmp/dir") << "/tmp/dir" << "/tmp" << "";
|
|
|
|
QTest::newRow("relative/path") << "relative/path" << "relative" << "";
|
2015-10-18 02:43:59 +02:00
|
|
|
QTest::newRow("relativepath") << "relativepath" << "." << "";
|
2012-01-19 15:26:54 +01:00
|
|
|
|
|
|
|
// Windows stuff:
|
|
|
|
#ifdef Q_OS_WIN
|
2015-01-09 10:53:46 +02:00
|
|
|
QTest::newRow("C:/data") << "C:/data" << "C:/" << "";
|
|
|
|
QTest::newRow("C:/") << "C:/" << "" << "";
|
|
|
|
QTest::newRow("//./com1") << "//./com1" << "/" << "";
|
2018-10-22 16:34:31 +02:00
|
|
|
QTest::newRow("//?/path") << "//?/path" << "/" << "Qt 4 cannot handle this path.";
|
2015-02-02 22:29:51 +02:00
|
|
|
QTest::newRow("/Global?\?/UNC/host") << "/Global?\?/UNC/host" << "/Global?\?/UNC/host"
|
2018-10-22 16:34:31 +02:00
|
|
|
<< "Qt 4 cannot handle this path.";
|
2012-01-19 15:26:54 +01:00
|
|
|
QTest::newRow("//server/directory/file")
|
2015-01-09 10:53:46 +02:00
|
|
|
<< "//server/directory/file" << "//server/directory" << "";
|
|
|
|
QTest::newRow("//server/directory") << "//server/directory" << "//server" << "";
|
|
|
|
QTest::newRow("//server") << "//server" << "" << "";
|
2012-01-19 15:26:54 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_fileutils::parentDir()
|
|
|
|
{
|
|
|
|
QFETCH(QString, path);
|
|
|
|
QFETCH(QString, parentPath);
|
|
|
|
QFETCH(QString, expectFailMessage);
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath result = FilePath::fromString(path).parentDir();
|
2012-01-19 15:26:54 +01:00
|
|
|
if (!expectFailMessage.isEmpty())
|
|
|
|
QEXPECT_FAIL("", expectFailMessage.toUtf8().constData(), Continue);
|
|
|
|
QCOMPARE(result.toString(), parentPath);
|
|
|
|
}
|
|
|
|
|
2012-10-04 06:06:09 +02:00
|
|
|
void tst_fileutils::isChildOf_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("path");
|
|
|
|
QTest::addColumn<QString>("childPath");
|
|
|
|
QTest::addColumn<bool>("result");
|
|
|
|
|
2015-01-09 10:53:46 +02:00
|
|
|
QTest::newRow("empty path") << "" << "/tmp" << false;
|
|
|
|
QTest::newRow("root only") << "/" << "/tmp" << true;
|
|
|
|
QTest::newRow("/tmp/dir") << "/tmp" << "/tmp/dir" << true;
|
|
|
|
QTest::newRow("relative/path") << "relative" << "relative/path" << true;
|
|
|
|
QTest::newRow("/tmpdir") << "/tmp" << "/tmpdir" << false;
|
|
|
|
QTest::newRow("same") << "/tmp/dir" << "/tmp/dir" << false;
|
2012-10-04 06:06:09 +02:00
|
|
|
|
|
|
|
// Windows stuff:
|
|
|
|
#ifdef Q_OS_WIN
|
2015-01-09 10:53:46 +02:00
|
|
|
QTest::newRow("C:/data") << "C:/" << "C:/data" << true;
|
|
|
|
QTest::newRow("C:/") << "" << "C:/" << false;
|
|
|
|
QTest::newRow("//./com1") << "/" << "//./com1" << true;
|
|
|
|
QTest::newRow("//?/path") << "/" << "//?/path" << true;
|
2015-02-02 22:29:51 +02:00
|
|
|
QTest::newRow("/Global?\?/UNC/host") << "/Global?\?/UNC/host"
|
|
|
|
<< "/Global?\?/UNC/host/file" << true;
|
2012-10-04 06:06:09 +02:00
|
|
|
QTest::newRow("//server/directory/file")
|
2015-01-09 10:53:46 +02:00
|
|
|
<< "//server/directory" << "//server/directory/file" << true;
|
2012-10-04 06:06:09 +02:00
|
|
|
QTest::newRow("//server/directory")
|
2015-01-09 10:53:46 +02:00
|
|
|
<< "//server" << "//server/directory" << true;
|
2012-10-04 06:06:09 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_fileutils::isChildOf()
|
|
|
|
{
|
|
|
|
QFETCH(QString, path);
|
|
|
|
QFETCH(QString, childPath);
|
|
|
|
QFETCH(bool, result);
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool res = FilePath::fromString(childPath).isChildOf(FilePath::fromString(path));
|
2012-10-04 06:06:09 +02:00
|
|
|
QCOMPARE(res, result);
|
|
|
|
}
|
|
|
|
|
2015-01-10 23:23:15 +02:00
|
|
|
void tst_fileutils::fileName_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("path");
|
|
|
|
QTest::addColumn<int>("components");
|
|
|
|
QTest::addColumn<QString>("result");
|
|
|
|
|
|
|
|
QTest::newRow("empty 1") << "" << 0 << "";
|
|
|
|
QTest::newRow("empty 2") << "" << 1 << "";
|
|
|
|
QTest::newRow("basic") << "/foo/bar/baz" << 0 << "baz";
|
|
|
|
QTest::newRow("2 parts") << "/foo/bar/baz" << 1 << "bar/baz";
|
|
|
|
QTest::newRow("root no depth") << "/foo" << 0 << "foo";
|
|
|
|
QTest::newRow("root full") << "/foo" << 1 << "/foo";
|
|
|
|
QTest::newRow("root included") << "/foo/bar/baz" << 2 << "/foo/bar/baz";
|
|
|
|
QTest::newRow("too many parts") << "/foo/bar/baz" << 5 << "/foo/bar/baz";
|
|
|
|
QTest::newRow("windows root") << "C:/foo/bar/baz" << 2 << "C:/foo/bar/baz";
|
|
|
|
QTest::newRow("smb share") << "//server/share/file" << 2 << "//server/share/file";
|
|
|
|
QTest::newRow("no slashes") << "foobar" << 0 << "foobar";
|
|
|
|
QTest::newRow("no slashes with depth") << "foobar" << 1 << "foobar";
|
|
|
|
QTest::newRow("multiple slashes 1") << "/foo/bar////baz" << 0 << "baz";
|
|
|
|
QTest::newRow("multiple slashes 2") << "/foo/bar////baz" << 1 << "bar////baz";
|
|
|
|
QTest::newRow("multiple slashes 3") << "/foo////bar/baz" << 2 << "/foo////bar/baz";
|
|
|
|
QTest::newRow("single char 1") << "/a/b/c" << 0 << "c";
|
|
|
|
QTest::newRow("single char 2") << "/a/b/c" << 1 << "b/c";
|
|
|
|
QTest::newRow("single char 3") << "/a/b/c" << 2 << "/a/b/c";
|
|
|
|
QTest::newRow("slash at end 1") << "/a/b/" << 0 << "";
|
|
|
|
QTest::newRow("slash at end 2") << "/a/b/" << 1 << "b/";
|
|
|
|
QTest::newRow("slashes at end 1") << "/a/b//" << 0 << "";
|
|
|
|
QTest::newRow("slashes at end 2") << "/a/b//" << 1 << "b//";
|
|
|
|
QTest::newRow("root only 1") << "/" << 0 << "";
|
|
|
|
QTest::newRow("root only 2") << "/" << 1 << "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_fileutils::fileName()
|
|
|
|
{
|
|
|
|
QFETCH(QString, path);
|
|
|
|
QFETCH(int, components);
|
|
|
|
QFETCH(QString, result);
|
2019-05-28 13:49:26 +02:00
|
|
|
QCOMPARE(FilePath::fromString(path).fileName(components), result);
|
2015-01-10 23:23:15 +02:00
|
|
|
}
|
|
|
|
|
2012-01-19 15:26:54 +01:00
|
|
|
QTEST_APPLESS_MAIN(tst_fileutils)
|
|
|
|
#include "tst_fileutils.moc"
|