MCPcopy Create free account
hub / github.com/IJHack/QtPass / findBinaryInPathTempExecutableInTempDir

Method findBinaryInPathTempExecutableInTempDir

tests/auto/util/tst_util.cpp:1371–1425  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1369}
1370
1371void tst_util::findBinaryInPathTempExecutableInTempDir() {
1372 // Avoid writing into real system PATH directories from tests.
1373 // A robust variant of this test requires refactoring Util::_env to allow
1374 // test-time PATH injection before first use.
1375 //
1376 // This test is skipped in restricted environments where writing to the "sh"
1377 // directory is not allowed. An alternative approach (QTemporaryDir + PATH
1378 // manipulation) doesn't work because Util::_env is cached on first use.
1379#ifndef Q_OS_WIN
1380 QString shPath = Util::findBinaryInPath(QStringLiteral("sh"));
1381 if (shPath.isEmpty()) {
1382 QSKIP("Cannot find 'sh' to determine a writable PATH directory");
1383 }
1384 const QString pathDir = QFileInfo(shPath).absolutePath();
1385 const QString uniqueName =
1386 QStringLiteral("qtpass_test_exec_") +
1387 QUuid::createUuid().toString().remove('{').remove('}');
1388 const QString uniquePath = pathDir + QDir::separator() + uniqueName;
1389
1390 QFile exec(uniquePath);
1391 if (!exec.open(QIODevice::WriteOnly)) {
1392 QSKIP("Cannot write to the PATH directory containing 'sh' (need write "
1393 "access)");
1394 }
1395 const QByteArray scriptContent = QByteArrayLiteral("#!/bin/sh\n");
1396 const qint64 bytesWritten = exec.write(scriptContent);
1397 if (bytesWritten != scriptContent.size()) {
1398 exec.close();
1399 exec.remove();
1400 QVERIFY2(false, "Failed to write executable content");
1401 }
1402 exec.close();
1403 exec.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner |
1404 QFileDevice::ExeOwner);
1405
1406 QString result = Util::findBinaryInPath(uniqueName);
1407
1408 // Remove file before assertions so it is always cleaned up.
1409 const bool removed = QFile::remove(uniquePath);
1410 QVERIFY2(
1411 removed,
1412 qPrintable(
1413 QStringLiteral("Failed to clean up test file '%1'").arg(uniquePath)));
1414
1415 QVERIFY2(!result.isEmpty(),
1416 "findBinaryInPath should locate the executable placed in a PATH "
1417 "directory");
1418 QVERIFY2(result.endsWith(uniqueName),
1419 qPrintable(QStringLiteral("Result '%1' should end with '%2'")
1420 .arg(result, uniqueName)));
1421 QVERIFY2(QFileInfo(result).isAbsolute(), "Result must be an absolute path");
1422#else
1423 QSKIP("Temp-executable test is Unix-only");
1424#endif
1425}
1426
1427void tst_util::buildClipboardMimeDataLinux() {
1428#ifdef Q_OS_LINUX

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected