From d7cc9c731b67daec85d7292efee7f19f363d502d Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Mon, 9 Sep 2024 15:09:11 +0200 Subject: [PATCH] Utils: Add TemporaryFilePath helper Change-Id: Ib2529e415e23acf6ec95ca903cf8bdfa0ecbf2c2 Reviewed-by: Marcus Tillmanns --- src/libs/utils/filepath.cpp | 50 ++++++++++++++++++++++ src/libs/utils/filepath.h | 25 +++++++++++ tests/auto/utils/filepath/tst_filepath.cpp | 42 ++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index c750034d0c1..3f731a46bdc 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -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> TemporaryFilePath::create( + const FilePath &templatePath) +{ + expected_str result = templatePath.createTempFile(); + if (!result) + return make_unexpected(result.error()); + return std::unique_ptr(new TemporaryFilePath(templatePath, *result)); +} + +TemporaryFilePath::TemporaryFilePath(const FilePath &templatePath, const FilePath &filePath) + : d(std::make_unique()) +{ + 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 diff --git a/src/libs/utils/filepath.h b/src/libs/utils/filepath.h index 8d7cea85523..4233265d654 100644 --- a/src/libs/utils/filepath.h +++ b/src/libs/utils/filepath.h @@ -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> 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 d; +}; + class QTCREATOR_UTILS_EXPORT FilePath { public: diff --git a/tests/auto/utils/filepath/tst_filepath.cpp b/tests/auto/utils/filepath/tst_filepath.cpp index b90615a5483..4db8a4afc7a 100644 --- a/tests/auto/utils/filepath/tst_filepath.cpp +++ b/tests/auto/utils/filepath/tst_filepath.cpp @@ -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)