From 8aeb967fcd9694057236094648022d26230e6a21 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Tue, 10 Sep 2024 14:12:14 +0200 Subject: [PATCH] CmdBridge: Fix createTempFile go' os.CreateTemp() will fail if no placeholder is present, and a file with the same path exists. So we force adding a placeholder at the end if the user didn't already add one. Change-Id: I4b9833487f4a963aa3e85ef48df5ad8e4382db40 Reviewed-by: hjk --- .../gocmdbridge/client/bridgedfileaccess.cpp | 8 ++++++- tests/manual/cmdbridge/tst_cmdbridge.cpp | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/libs/gocmdbridge/client/bridgedfileaccess.cpp b/src/libs/gocmdbridge/client/bridgedfileaccess.cpp index 9793821eb06..01ccf3029af 100644 --- a/src/libs/gocmdbridge/client/bridgedfileaccess.cpp +++ b/src/libs/gocmdbridge/client/bridgedfileaccess.cpp @@ -592,12 +592,18 @@ expected_str FileAccess::createTempFile(const FilePath &filePath) if (path[i] != 'X') break; path = path.left(i + 1) + "*"; + } else { + path += ".*"; } Utils::expected_str> f = m_client->createTempFile(path); QTC_ASSERT_EXPECTED(f, return {}); f->waitForFinished(); - return f->result(); + + expected_str result = f->result(); + if (!result) + return result; + return filePath.withNewPath(result->path()); } catch (const std::exception &e) { return make_unexpected( Tr::tr("Error creating temporary file: %1").arg(QString::fromLocal8Bit(e.what()))); diff --git a/tests/manual/cmdbridge/tst_cmdbridge.cpp b/tests/manual/cmdbridge/tst_cmdbridge.cpp index f04e5082ed6..d4c96b1d14e 100644 --- a/tests/manual/cmdbridge/tst_cmdbridge.cpp +++ b/tests/manual/cmdbridge/tst_cmdbridge.cpp @@ -131,6 +131,29 @@ private slots: QVERIFY(!fileAccess.exists(*tempFile)); } + void testTempFileWithoutPlaceholder() + { + CmdBridge::FileAccess fileAccess; + auto res = fileAccess.deployAndInit( + FilePath::fromUserInput(libExecPath), FilePath::fromUserInput("/")); + + QVERIFY(res); + + const FilePath pattern = FilePath::fromUserInput(QDir::tempPath()) / "test.txt"; + + // We have to create a file with the pattern name to test the automatic placeholder + // adding. go' os.CreateTemp() will fail if no placeholder is present, and the file exists. + pattern.writeFileContents("Test"); + + auto tempFile = fileAccess.createTempFile(pattern); + + QVERIFY(tempFile); + QVERIFY(fileAccess.exists(*tempFile)); + QVERIFY(fileAccess.removeFile(*tempFile)); + QVERIFY(!fileAccess.exists(*tempFile)); + QVERIFY(tempFile->fileName().startsWith("test.txt.")); + } + void testFileContents() { CmdBridge::FileAccess fileAccess;