forked from qt-creator/qt-creator
Utils: Add TemporaryFilePath helper
Change-Id: Ib2529e415e23acf6ec95ca903cf8bdfa0ecbf2c2 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -2492,4 +2492,54 @@ QTCREATOR_UTILS_EXPORT QDebug operator<<(QDebug dbg, const FilePath &c)
|
||||
return dbg << c.toString();
|
||||
}
|
||||
|
||||
class TemporaryFilePathPrivate
|
||||
{
|
||||
public:
|
||||
FilePath templatePath;
|
||||
FilePath filePath;
|
||||
bool autoRemove = true;
|
||||
};
|
||||
|
||||
expected_str<std::unique_ptr<TemporaryFilePath>> TemporaryFilePath::create(
|
||||
const FilePath &templatePath)
|
||||
{
|
||||
expected_str<FilePath> result = templatePath.createTempFile();
|
||||
if (!result)
|
||||
return make_unexpected(result.error());
|
||||
return std::unique_ptr<TemporaryFilePath>(new TemporaryFilePath(templatePath, *result));
|
||||
}
|
||||
|
||||
TemporaryFilePath::TemporaryFilePath(const FilePath &templatePath, const FilePath &filePath)
|
||||
: d(std::make_unique<TemporaryFilePathPrivate>())
|
||||
{
|
||||
d->templatePath = templatePath;
|
||||
d->filePath = filePath;
|
||||
}
|
||||
|
||||
TemporaryFilePath::~TemporaryFilePath()
|
||||
{
|
||||
if (d->autoRemove)
|
||||
d->filePath.removeFile();
|
||||
}
|
||||
|
||||
void TemporaryFilePath::setAutoRemove(bool autoRemove)
|
||||
{
|
||||
d->autoRemove = autoRemove;
|
||||
}
|
||||
|
||||
bool TemporaryFilePath::autoRemove() const
|
||||
{
|
||||
return d->autoRemove;
|
||||
}
|
||||
|
||||
FilePath TemporaryFilePath::templatePath() const
|
||||
{
|
||||
return d->templatePath;
|
||||
}
|
||||
|
||||
FilePath TemporaryFilePath::filePath() const
|
||||
{
|
||||
return d->filePath;
|
||||
}
|
||||
|
||||
} // Utils
|
||||
|
@@ -65,6 +65,31 @@ signals:
|
||||
void pathChanged(const Utils::FilePath &path);
|
||||
};
|
||||
|
||||
class TemporaryFilePathPrivate;
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT TemporaryFilePath
|
||||
{
|
||||
public:
|
||||
TemporaryFilePath() = delete;
|
||||
TemporaryFilePath(const TemporaryFilePath &other) = delete;
|
||||
|
||||
~TemporaryFilePath();
|
||||
|
||||
static expected_str<std::unique_ptr<TemporaryFilePath>> create(const FilePath &templatePath);
|
||||
|
||||
void setAutoRemove(bool autoDelete);
|
||||
bool autoRemove() const;
|
||||
|
||||
FilePath templatePath() const;
|
||||
FilePath filePath() const;
|
||||
|
||||
private:
|
||||
TemporaryFilePath(const FilePath &templatePath, const FilePath &filePath);
|
||||
|
||||
private:
|
||||
std::unique_ptr<TemporaryFilePathPrivate> d;
|
||||
};
|
||||
|
||||
class QTCREATOR_UTILS_EXPORT FilePath
|
||||
{
|
||||
public:
|
||||
|
@@ -122,6 +122,8 @@ private slots:
|
||||
|
||||
void asQMapKey();
|
||||
|
||||
void makeTemporaryFile();
|
||||
|
||||
private:
|
||||
QTemporaryDir tempDir;
|
||||
QString rootPath;
|
||||
@@ -1766,6 +1768,46 @@ void tst_filepath::sort_data()
|
||||
<< QStringList{"b://b//b", "a://b//b", "a://a//b", "a://b//a", "a://a//a"};
|
||||
}
|
||||
|
||||
void tst_filepath::makeTemporaryFile()
|
||||
{
|
||||
FilePath tmpFilePath;
|
||||
// Test auto remove
|
||||
{
|
||||
const FilePath tmplate = FilePath::fromUserInput(QDir::tempPath())
|
||||
/ "test-auto-remove-XXXXXX.txt";
|
||||
auto tmpFile = TemporaryFilePath::create(tmplate);
|
||||
QVERIFY(tmpFile);
|
||||
|
||||
QVERIFY(!(*tmpFile)->templatePath().exists());
|
||||
QVERIFY((*tmpFile)->filePath().exists());
|
||||
tmpFilePath = (*tmpFile)->filePath();
|
||||
}
|
||||
QVERIFY(!tmpFilePath.exists());
|
||||
|
||||
// Check !autoRemove
|
||||
{
|
||||
const FilePath tmplate = FilePath::fromUserInput(QDir::tempPath())
|
||||
/ "test-no-auto-remove-XXXXXX.txt";
|
||||
auto tmpFile = TemporaryFilePath::create(tmplate);
|
||||
QVERIFY(tmpFile);
|
||||
(*tmpFile)->setAutoRemove(false);
|
||||
|
||||
QVERIFY(!(*tmpFile)->templatePath().exists());
|
||||
QVERIFY((*tmpFile)->filePath().exists());
|
||||
tmpFilePath = (*tmpFile)->filePath();
|
||||
}
|
||||
QVERIFY(tmpFilePath.exists());
|
||||
QVERIFY(tmpFilePath.removeFile());
|
||||
|
||||
// Check invalid filename
|
||||
{
|
||||
const FilePath tmplate = FilePath::fromUserInput("/Some/non/existing/path")
|
||||
/ "test-invalid-filename-XXXXXX";
|
||||
auto tmpFile = TemporaryFilePath::create(tmplate);
|
||||
QVERIFY(!tmpFile);
|
||||
}
|
||||
}
|
||||
|
||||
} // Utils
|
||||
|
||||
QTEST_GUILESS_MAIN(Utils::tst_filepath)
|
||||
|
Reference in New Issue
Block a user