From 11790f41a70a8184a470f77e857f85b47481d7cb Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 21 Feb 2017 14:53:30 +0100 Subject: tst_qurl: replace all QDir::currentPath() calls with local variable Change-Id: I70e4547ba87292c29dfab59950aa1214be8015a5 Reviewed-by: Thiago Macieira --- tests/auto/corelib/io/qurl/tst_qurl.cpp | 37 +++++++++++++++++---------------- 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'tests/auto/corelib/io/qurl') diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index 994427ba12..01573e046e 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -3062,49 +3062,50 @@ void tst_QUrl::fromUserInputWithCwd_data() // Null QTest::newRow("null") << QString() << QString() << QUrl() << QUrl(); - // Existing file - QDirIterator it(QDir::currentPath(), QDir::NoDotDot | QDir::AllEntries); + // Existing files + const QString base = QDir::currentPath(); + QDirIterator it(base, QDir::NoDotDot | QDir::AllEntries); int c = 0; while (it.hasNext()) { it.next(); QUrl url = QUrl::fromLocalFile(it.filePath()); if (it.fileName() == QLatin1String(".")) { - url = QUrl::fromLocalFile(QDir::currentPath() + url = QUrl::fromLocalFile(base #ifdef Q_OS_WINRT + QLatin1Char('/') #endif ); // fromUserInput cleans the path } QTest::newRow(("file-" + QByteArray::number(c)).constData()) - << it.fileName() << QDir::currentPath() << url << url; + << it.fileName() << base << url << url; QTest::newRow(("file-" + QByteArray::number(c) + "-dot").constData()) << it.fileName() << QStringLiteral(".") << url << url; ++c; } #ifndef Q_OS_WINRT // WinRT cannot cd outside current / sandbox - QDir parent = QDir::current(); + QDir parent(base); QVERIFY(parent.cdUp()); QUrl parentUrl = QUrl::fromLocalFile(parent.path()); - QTest::newRow("dotdot") << ".." << QDir::currentPath() << parentUrl << parentUrl; + QTest::newRow("dotdot") << ".." << base << parentUrl << parentUrl; #endif - QTest::newRow("nonexisting") << "nonexisting" << QDir::currentPath() << QUrl("http://nonexisting") << QUrl::fromLocalFile(QDir::currentPath() + "/nonexisting"); - QTest::newRow("short-url") << "example.org" << QDir::currentPath() << QUrl("http://example.org") << QUrl::fromLocalFile(QDir::currentPath() + "/example.org"); - QTest::newRow("full-url") << "http://example.org" << QDir::currentPath() << QUrl("http://example.org") << QUrl("http://example.org"); - QTest::newRow("absolute") << "/doesnotexist.txt" << QDir::currentPath() << QUrl("file:///doesnotexist.txt") << QUrl("file:///doesnotexist.txt"); + QTest::newRow("nonexisting") << "nonexisting" << base << QUrl("http://nonexisting") << QUrl::fromLocalFile(base + "/nonexisting"); + QTest::newRow("short-url") << "example.org" << base << QUrl("http://example.org") << QUrl::fromLocalFile(base + "/example.org"); + QTest::newRow("full-url") << "http://example.org" << base << QUrl("http://example.org") << QUrl("http://example.org"); + QTest::newRow("absolute") << "/doesnotexist.txt" << base << QUrl("file:///doesnotexist.txt") << QUrl("file:///doesnotexist.txt"); #ifdef Q_OS_WIN - QTest::newRow("windows-absolute") << "c:/doesnotexist.txt" << QDir::currentPath() << QUrl("file:///c:/doesnotexist.txt") << QUrl("file:///c:/doesnotexist.txt"); + QTest::newRow("windows-absolute") << "c:/doesnotexist.txt" << base << QUrl("file:///c:/doesnotexist.txt") << QUrl("file:///c:/doesnotexist.txt"); #endif // IPv4 & IPv6 // same as fromUserInput, but needs retesting - QTest::newRow("ipv4-1") << "127.0.0.1" << QDir::currentPath() << QUrl("http://127.0.0.1") << QUrl::fromLocalFile(QDir::currentPath() + "/127.0.0.1"); - QTest::newRow("ipv6-0") << "::" << QDir::currentPath() << QUrl("http://[::]") << QUrl("http://[::]"); - QTest::newRow("ipv6-1") << "::1" << QDir::currentPath() << QUrl("http://[::1]") << QUrl("http://[::1]"); - QTest::newRow("ipv6-2") << "1::1" << QDir::currentPath() << QUrl("http://[1::1]") << QUrl("http://[1::1]"); - QTest::newRow("ipv6-3") << "1::" << QDir::currentPath() << QUrl("http://[1::]") << QUrl("http://[1::]"); - QTest::newRow("ipv6-4") << "c::" << QDir::currentPath() << QUrl("http://[c::]") << QUrl("http://[c::]"); - QTest::newRow("ipv6-5") << "c:f00:ba4::" << QDir::currentPath() << QUrl("http://[c:f00:ba4::]") << QUrl("http://[c:f00:ba4::]"); + QTest::newRow("ipv4-1") << "127.0.0.1" << base << QUrl("http://127.0.0.1") << QUrl::fromLocalFile(base + "/127.0.0.1"); + QTest::newRow("ipv6-0") << "::" << base << QUrl("http://[::]") << QUrl("http://[::]"); + QTest::newRow("ipv6-1") << "::1" << base << QUrl("http://[::1]") << QUrl("http://[::1]"); + QTest::newRow("ipv6-2") << "1::1" << base << QUrl("http://[1::1]") << QUrl("http://[1::1]"); + QTest::newRow("ipv6-3") << "1::" << base << QUrl("http://[1::]") << QUrl("http://[1::]"); + QTest::newRow("ipv6-4") << "c::" << base << QUrl("http://[c::]") << QUrl("http://[c::]"); + QTest::newRow("ipv6-5") << "c:f00:ba4::" << base << QUrl("http://[c:f00:ba4::]") << QUrl("http://[c:f00:ba4::]"); } void tst_QUrl::fromUserInputWithCwd() -- cgit v1.2.3 From 8cb9314971cdc61137855a6b374b4a4468dff7e7 Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 21 Feb 2017 15:05:29 +0100 Subject: tst_qurl: use temp dir and create our own files for testing This allows to test specific filenames without polluting the current dir. Change-Id: Ieb99019a2e37e30f294d85c5d80af1de1b919019 Reviewed-by: Thiago Macieira --- tests/auto/corelib/io/qurl/tst_qurl.cpp | 47 ++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'tests/auto/corelib/io/qurl') diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index 01573e046e..e0e6c6f281 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -46,6 +46,7 @@ class tst_QUrl : public QObject Q_OBJECT private slots: + void initTestCase(); void effectiveTLDs_data(); void effectiveTLDs(); void getSetCheck(); @@ -182,8 +183,15 @@ private slots: private: void testThreadingHelper(); + + QTemporaryDir m_tempDir; }; +void tst_QUrl::initTestCase() +{ + QVERIFY2(m_tempDir.isValid(), qPrintable(m_tempDir.errorString())); +} + // Testing get/set functions void tst_QUrl::getSetCheck() { @@ -3062,26 +3070,35 @@ void tst_QUrl::fromUserInputWithCwd_data() // Null QTest::newRow("null") << QString() << QString() << QUrl() << QUrl(); - // Existing files - const QString base = QDir::currentPath(); - QDirIterator it(base, QDir::NoDotDot | QDir::AllEntries); - int c = 0; - while (it.hasNext()) { - it.next(); - QUrl url = QUrl::fromLocalFile(it.filePath()); - if (it.fileName() == QLatin1String(".")) { - url = QUrl::fromLocalFile(base + // Use a tempdir with files, for testing specific file names + // We use canonicalPath() on the dir path because ::getcwd() canonicalizes, + // so we get a canonical base path for URLs with "." as working directory. + const QString base = QDir(m_tempDir.path()).canonicalPath(); + QDir::setCurrent(base); // for the tests that use "." as working dir + + // "." + { + const QUrl url = QUrl::fromLocalFile(base #ifdef Q_OS_WINRT + QLatin1Char('/') #endif ); // fromUserInput cleans the path - } - QTest::newRow(("file-" + QByteArray::number(c)).constData()) - << it.fileName() << base << url << url; - QTest::newRow(("file-" + QByteArray::number(c) + "-dot").constData()) - << it.fileName() << QStringLiteral(".") << url << url; - ++c; + QTest::newRow("dot-in-path") << "." << base << url << url; + QTest::newRow("dot-in-dot") << "." << QStringLiteral(".") << url << url; } + + // Existing files + for (const char *fileName : {"file.txt", "file#a.txt", "file .txt"}) { + const QString filePath = base + '/' + fileName; + QFile file(filePath); + QVERIFY2(file.open(QIODevice::WriteOnly), qPrintable(filePath)); + file.write("Hello world\n"); + + const QUrl url = QUrl::fromLocalFile(filePath); + QTest::newRow(fileName) << fileName << base << url << url; + QTest::newRow(QByteArray(fileName) + "-in-dot") << fileName << QStringLiteral(".") << url << url; + } + #ifndef Q_OS_WINRT // WinRT cannot cd outside current / sandbox QDir parent(base); QVERIFY(parent.cdUp()); -- cgit v1.2.3 From 9ffc9e306f639b005dafd67faa122ad0db1b7b86 Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 21 Feb 2017 15:08:45 +0100 Subject: QUrl::fromUserInput(with cwd) fix handling of files with trailing spaces The call to trimmed() makes sense for URLs typed in a browser's location bar, but its use in every code path made it impossible to open a file with a trailing space in command-line tools that uses fromUserInput(cwd) to handle command-line arguments, as recommended. For instance kde-open5 "file.txt " would fail. Change-Id: Ie61182684521d91f077d3e76f95b7240965ab405 Reviewed-by: Thiago Macieira --- tests/auto/corelib/io/qurl/tst_qurl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/auto/corelib/io/qurl') diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index e0e6c6f281..ee18151e4a 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -3088,7 +3088,7 @@ void tst_QUrl::fromUserInputWithCwd_data() } // Existing files - for (const char *fileName : {"file.txt", "file#a.txt", "file .txt"}) { + for (const char *fileName : {"file.txt", "file#a.txt", "file .txt", "file.txt "}) { const QString filePath = base + '/' + fileName; QFile file(filePath); QVERIFY2(file.open(QIODevice::WriteOnly), qPrintable(filePath)); -- cgit v1.2.3