Make QDir::relativeFilePath() return "." for a path to itself.

The rationale being that the empty string is not a valid path component.

[ChangeLog][QtCore][QDir] QDir::relativeFilePath() now returns "."
instead of an empty string if the given path is the same as the
directory.

Change-Id: Ibcf31904b2ae5edf5639d4c2e5ba234365d347fd
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
Christian Kandeler 2015-06-04 16:02:06 +02:00
parent 0e41a58fb8
commit ec8c1dcf14
3 changed files with 11 additions and 6 deletions

View File

@ -777,6 +777,8 @@ QString QDir::relativeFilePath(const QString &fileName) const
result += QLatin1Char('/');
}
if (result.isEmpty())
return QLatin1String(".");
return result;
}

View File

@ -1366,6 +1366,9 @@ void tst_QDir::relativeFilePath_data()
QTest::newRow("11") << "" << "" << "";
QTest::newRow("same path 1") << "/tmp" << "/tmp" << ".";
QTest::newRow("same path 2") << "//tmp" << "/tmp/" << ".";
#if (defined(Q_OS_WIN) && !defined(Q_OS_WINCE))
QTest::newRow("12") << "C:/foo/bar" << "ding" << "ding";
QTest::newRow("13") << "C:/foo/bar" << "C:/ding/dong" << "../../ding/dong";
@ -1373,10 +1376,10 @@ void tst_QDir::relativeFilePath_data()
QTest::newRow("15") << "C:/foo/bar" << "D:/ding/dong" << "D:/ding/dong";
QTest::newRow("16") << "C:" << "C:/ding/dong" << "ding/dong";
QTest::newRow("17") << "C:/" << "C:/ding/dong" << "ding/dong";
QTest::newRow("18") << "C:" << "C:" << "";
QTest::newRow("19") << "C:/" << "C:" << "";
QTest::newRow("20") << "C:" << "C:/" << "";
QTest::newRow("21") << "C:/" << "C:/" << "";
QTest::newRow("18") << "C:" << "C:" << ".";
QTest::newRow("19") << "C:/" << "C:" << ".";
QTest::newRow("20") << "C:" << "C:/" << ".";
QTest::newRow("21") << "C:/" << "C:/" << ".";
QTest::newRow("22") << "C:" << "C:file.txt" << "file.txt";
QTest::newRow("23") << "C:/" << "C:file.txt" << "file.txt";
QTest::newRow("24") << "C:" << "C:/file.txt" << "file.txt";

View File

@ -1356,7 +1356,7 @@ void tst_qmakelib::addReplaceFunctions(const QString &qindir)
QTest::newRow("$$relative_path(): relative file to empty")
<< "VAR = $$relative_path(dir/..)"
<< "VAR ="
<< "VAR = ."
<< ""
<< true;
@ -1368,7 +1368,7 @@ void tst_qmakelib::addReplaceFunctions(const QString &qindir)
QTest::newRow("$$relative_path(): empty file & path")
<< "VAR = $$relative_path('', /root/sub)"
<< "VAR ="
<< "VAR = ."
<< ""
<< true;