From adc4894fca0eeeb5a302009ca2bf597e80e28914 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Date: Wed, 25 Nov 2015 14:30:41 +0100
Subject: [PATCH] tst_QProcess: Use a QTemporaryDir for files to be created.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Ensure the test works in a working directory with read-only
permission and that the file names are unique.
The test can then be executed repeatedly by COIN even in case
left-over hanging process helpers still lock the files. Also
disambiguate the "data" files used by various tests.

Task-number: QTBUG-47370
Change-Id: I3b9c7b70828da78f400196fcbba27bc61ea4538f
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@theqtcompany.com>
---
 .../auto/corelib/io/qprocess/tst_qprocess.cpp | 30 +++++++++++--------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
index ca5f527b8b6..712e3440d80 100644
--- a/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
+++ b/tests/auto/corelib/io/qprocess/tst_qprocess.cpp
@@ -38,6 +38,7 @@
 #include <QtCore/QDir>
 #include <QtCore/QFile>
 #include <QtCore/QThread>
+#include <QtCore/QTemporaryDir>
 #include <QtCore/QRegExp>
 #include <QtCore/QDebug>
 #include <QtCore/QMetaType>
@@ -172,6 +173,7 @@ protected slots:
 
 private:
     qint64 bytesAvailable;
+    QTemporaryDir m_temporaryDir;
 #endif //QT_NO_PROCESS
 };
 
@@ -180,6 +182,7 @@ void tst_QProcess::initTestCase()
 #ifdef QT_NO_PROCESS
     QSKIP("This test requires QProcess support");
 #else
+    QVERIFY2(m_temporaryDir.isValid(), qPrintable(m_temporaryDir.errorString()));
     // chdir to our testdata path and execute helper apps relative to that.
     QString testdata_dir = QFileInfo(QFINDTESTDATA("testProcessNormal")).absolutePath();
     QVERIFY2(QDir::setCurrent(testdata_dir), qPrintable("Could not chdir to " + testdata_dir));
@@ -1709,7 +1712,7 @@ void tst_QProcess::failToStartEmptyArgs()
 // Reading and writing to a process is not supported on Qt/CE
 void tst_QProcess::removeFileWhileProcessIsRunning()
 {
-    QFile file("removeFile.txt");
+    QFile file(m_temporaryDir.path() + QLatin1String("/removeFile.txt"));
     QVERIFY(file.open(QFile::WriteOnly));
 
     QProcess process;
@@ -1940,13 +1943,13 @@ void tst_QProcess::setStandardInputFile()
 {
     static const char data[] = "A bunch\1of\2data\3\4\5\6\7...";
     QProcess process;
-    QFile file("data");
+    QFile file(m_temporaryDir.path() + QLatin1String("/data-sif"));
 
     QVERIFY(file.open(QIODevice::WriteOnly));
     file.write(data, sizeof data);
     file.close();
 
-    process.setStandardInputFile("data");
+    process.setStandardInputFile(file.fileName());
     process.start("testProcessEcho/testProcessEcho");
 
     QVERIFY(process.waitForFinished());
@@ -2008,7 +2011,7 @@ void tst_QProcess::setStandardOutputFile()
     QIODevice::OpenMode mode = append ? QIODevice::Append : QIODevice::Truncate;
 
     // create the destination file with data
-    QFile file("data");
+    QFile file(m_temporaryDir.path() + QLatin1String("/data-stdof-") + QLatin1String(QTest::currentDataTag()));
     QVERIFY(file.open(QIODevice::WriteOnly));
     file.write(data, sizeof data - 1);
     file.close();
@@ -2017,9 +2020,9 @@ void tst_QProcess::setStandardOutputFile()
     QProcess process;
     process.setReadChannelMode(channelMode);
     if (channelToTest == QProcess::StandardOutput)
-        process.setStandardOutputFile("data", mode);
+        process.setStandardOutputFile(file.fileName(), mode);
     else
-        process.setStandardErrorFile("data", mode);
+        process.setStandardErrorFile(file.fileName(), mode);
 
     process.start("testProcessEcho2/testProcessEcho2");
     process.write(testdata, sizeof testdata);
@@ -2066,10 +2069,11 @@ void tst_QProcess::setStandardOutputFileAndWaitForBytesWritten()
 {
     static const char testdata[] = "Test data.";
 
-    QFile file("data");
+    QFile file(m_temporaryDir.path() + QLatin1String("/data-stdofawfbw"));
     QProcess process;
     process.setStandardOutputFile(file.fileName());
     process.start("testProcessEcho2/testProcessEcho2");
+    QVERIFY2(process.waitForStarted(), qPrintable(process.errorString()));
     process.write(testdata, sizeof testdata);
     process.waitForBytesWritten();
     QVERIFY(process.waitForFinished());
@@ -2143,14 +2147,15 @@ void tst_QProcess::fileWriterProcess()
 
     QTime stopWatch;
     stopWatch.start();
-    const QString fileName = QLatin1String("fileWriterProcess.txt");
+    const QString fileName = m_temporaryDir.path() + QLatin1String("/fileWriterProcess.txt");
+    const QString binary = QDir::currentPath() + QLatin1String("/fileWriterProcess/fileWriterProcess");
 
     do {
         if (QFile::exists(fileName))
             QVERIFY(QFile::remove(fileName));
         QProcess process;
-        process.start("fileWriterProcess/fileWriterProcess",
-                      QIODevice::ReadWrite | QIODevice::Text);
+        process.setWorkingDirectory(m_temporaryDir.path());
+        process.start(binary, QIODevice::ReadWrite | QIODevice::Text);
         process.write(stdinStr);
         process.closeWriteChannel();
         while (process.bytesToWrite()) {
@@ -2173,8 +2178,9 @@ void tst_QProcess::detachedWorkingDirectoryAndPid()
     QTest::qSleep(1000);
 #endif
 
-    QFile infoFile(QDir::currentPath() + QLatin1String("/detachedinfo.txt"));
-    infoFile.remove();
+    QFile infoFile(m_temporaryDir.path() + QLatin1String("/detachedinfo.txt"));
+    if (infoFile.exists())
+        QVERIFY(infoFile.remove());
 
     QString workingDir = QDir::currentPath() + "/testDetached";
 
-- 
GitLab