kdecore: do not prepend the temporary directory and main component name if template is absolute path from KTemporaryFile::filePath()

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-23 18:05:15 +03:00
parent 8224768f2a
commit b287467137
6 changed files with 54 additions and 38 deletions

View file

@ -100,8 +100,10 @@ QString KTemporaryFile::filePath(const QString &pathtemplate)
result.replace(xindex, 1, QChar::fromLatin1(tmpnamechars[KRandom::randomMax(52)])); result.replace(xindex, 1, QChar::fromLatin1(tmpnamechars[KRandom::randomMax(52)]));
xindex = result.indexOf(xchar, xindex + 1); xindex = result.indexOf(xchar, xindex + 1);
} }
result.prepend(underscorechar); if (!QDir::isAbsolutePath(result)) {
result.prepend(KGlobal::mainComponent().componentName()); result.prepend(underscorechar);
result.prepend(KGlobal::dirs()->saveLocation("tmp")); result.prepend(KGlobal::mainComponent().componentName());
result.prepend(KGlobal::dirs()->saveLocation("tmp"));
}
return result; return result;
} }

View file

@ -138,8 +138,8 @@ public:
* If @p pathtemplate is empty the result will have 10 characters, the * If @p pathtemplate is empty the result will have 10 characters, the
* standard temporary directory prepended along with the main component * standard temporary directory prepended along with the main component
* name. Otherwise any 'X' in @p pathtemplate is replaced with random * name. Otherwise any 'X' in @p pathtemplate is replaced with random
* character, the standard temporary directory prepended along with the * character. The standard temporary directory prepended along with the
* main component name. * main component name unless @p pathtemplate is absolute path.
* *
* @param pathtemplate The template to use when generating filepath. * @param pathtemplate The template to use when generating filepath.
*/ */

View file

@ -27,48 +27,48 @@
void KTempDirTest::testNoDelete() void KTempDirTest::testNoDelete()
{ {
KTempDir dir("test"); KTempDir dir("test");
dir.setAutoRemove(false); dir.setAutoRemove(false);
QVERIFY(dir.status() == 0); QVERIFY(dir.status() == 0);
QVERIFY(dir.exists()); QVERIFY(dir.exists());
QVERIFY(QDir(dir.name()).exists()); QVERIFY(QDir(dir.name()).exists());
dir.unlink(); dir.unlink();
QVERIFY(dir.status() == 0); QVERIFY(dir.status() == 0);
QVERIFY(!dir.exists()); QVERIFY(!dir.exists());
QVERIFY(!QDir(dir.name()).exists()); QVERIFY(!QDir(dir.name()).exists());
} }
void KTempDirTest::testAutoDelete() void KTempDirTest::testAutoDelete()
{ {
KTempDir *dir = new KTempDir("test"); KTempDir *dir = new KTempDir("test");
QVERIFY(dir->status() == 0); QVERIFY(dir->status() == 0);
QVERIFY(dir->exists()); QVERIFY(dir->exists());
QString dName = dir->name(); QString dName = dir->name();
delete dir; delete dir;
QVERIFY(!QDir(dName).exists()); QVERIFY(!QDir(dName).exists());
} }
void KTempDirTest::testCreateSubDir() void KTempDirTest::testCreateSubDir()
{ {
KTempDir *dir = new KTempDir("test"); KTempDir *dir = new KTempDir("test");
QVERIFY(dir->status() == 0); QVERIFY(dir->status() == 0);
QVERIFY(dir->exists()); QVERIFY(dir->exists());
QDir d ( dir->name() ); QDir d ( dir->name() );
QVERIFY(d.exists()); QVERIFY(d.exists());
QVERIFY(d.mkdir(QString("123"))); QVERIFY(d.mkdir(QString("123")));
QVERIFY(d.mkdir(QString("456"))); QVERIFY(d.mkdir(QString("456")));
QString dName = dir->name(); QString dName = dir->name();
delete dir; delete dir;
d.refresh(); d.refresh();
QVERIFY(!QDir(dName).exists()); QVERIFY(!QDir(dName).exists());
QVERIFY(!d.exists(QString("123"))); QVERIFY(!d.exists(QString("123")));
QVERIFY(!d.exists(QString("456"))); QVERIFY(!d.exists(QString("456")));
} }
QTEST_KDEMAIN_CORE(KTempDirTest) QTEST_KDEMAIN_CORE(KTempDirTest)

View file

@ -25,9 +25,10 @@ class KTempDirTest : public QObject
{ {
Q_OBJECT Q_OBJECT
private Q_SLOTS: private Q_SLOTS:
void testNoDelete(); void testNoDelete();
void testAutoDelete(); void testAutoDelete();
void testCreateSubDir(); void testCreateSubDir();
void testCreateSubDir();
}; };
#endif #endif // KTEMPDIRTEST_H

View file

@ -98,3 +98,15 @@ void KTemporaryFileTest::testKTemporaryFile()
//directories we have write access to? //directories we have write access to?
} }
void KTemporaryFileTest::testFilePath()
{
const QString tmpdir = KGlobal::dirs()->saveLocation("tmp");
QString tmpfilepath = KTemporaryFile::filePath();
QVERIFY(tmpfilepath.startsWith(tmpdir));
QVERIFY(!tmpfilepath.contains('X'));
tmpfilepath = KTemporaryFile::filePath("/foo/bar/XXXXX.tmp");
QVERIFY(tmpfilepath.startsWith(QLatin1String("/foo/bar/")));
QVERIFY(!tmpfilepath.contains('X'));
}

View file

@ -17,10 +17,11 @@ private slots:
void initTestCase(); void initTestCase();
void cleanupTestCase(); void cleanupTestCase();
void testKTemporaryFile(); void testKTemporaryFile();
void testFilePath();
private: private:
QString kdeTempDir; QString kdeTempDir;
QString componentName; QString componentName;
}; };
#endif #endif // KTEMPORARYFILETEST_H