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 <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-09-10 14:12:14 +02:00
parent 66768d1471
commit 8aeb967fcd
2 changed files with 30 additions and 1 deletions

View File

@@ -592,12 +592,18 @@ expected_str<FilePath> FileAccess::createTempFile(const FilePath &filePath)
if (path[i] != 'X')
break;
path = path.left(i + 1) + "*";
} else {
path += ".*";
}
Utils::expected_str<QFuture<Utils::FilePath>> f = m_client->createTempFile(path);
QTC_ASSERT_EXPECTED(f, return {});
f->waitForFinished();
return f->result();
expected_str<FilePath> 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())));

View File

@@ -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;