forked from qt-creator/qt-creator
Fix FileSystemAccessTest::testFileActions() flakiness
Always add " > /dev/null 2>&1" infix so that the output of e.g. "rm" command, when failed, is redirected to dev/null. Enclose the input data in runInShell() with quotation marks and escape all the characters in passed data accordingly. Pass the "-e" option to echo in order to turn on the escaping mode. Add a new test for testCreateRemoteFile(). Change-Id: I7db07b57035242279cac7dd77a80ac6dd05bfb1f Reviewed-by: Christian Stenger <christian.stenger@qt.io> Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -95,7 +95,11 @@ void FileSystemAccessTest::initTestCase()
|
|||||||
QVERIFY(!newDev.isNull());
|
QVERIFY(!newDev.isNull());
|
||||||
devMgr->addDevice(newDev);
|
devMgr->addDevice(newDev);
|
||||||
}
|
}
|
||||||
|
if (filePath.exists()) // Do initial cleanup after possible leftovers from previously failed test
|
||||||
|
QVERIFY(filePath.removeRecursively());
|
||||||
|
QVERIFY(!filePath.exists());
|
||||||
QVERIFY(filePath.createDir());
|
QVERIFY(filePath.createDir());
|
||||||
|
QVERIFY(filePath.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileSystemAccessTest::cleanupTestCase()
|
void FileSystemAccessTest::cleanupTestCase()
|
||||||
@@ -106,7 +110,43 @@ void FileSystemAccessTest::cleanupTestCase()
|
|||||||
QVERIFY(baseFilePath().removeRecursively());
|
QVERIFY(baseFilePath().removeRecursively());
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileSystemAccessTest::testDirStatuses()
|
void FileSystemAccessTest::testCreateRemoteFile_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QByteArray>("data");
|
||||||
|
|
||||||
|
QTest::newRow("Spaces") << QByteArray("Line with spaces");
|
||||||
|
QTest::newRow("Newlines") << QByteArray("Some \n\n newlines \n");
|
||||||
|
QTest::newRow("Carriage return") << QByteArray("Line with carriage \r return");
|
||||||
|
QTest::newRow("Tab") << QByteArray("Line with \t tab");
|
||||||
|
QTest::newRow("Apostrophe") << QByteArray("Line with apostrophe's character");
|
||||||
|
QTest::newRow("Quotation marks") << QByteArray("Line with \"quotation marks\"");
|
||||||
|
QTest::newRow("Backslash 1") << QByteArray("Line with \\ backslash");
|
||||||
|
QTest::newRow("Backslash 2") << QByteArray("Line with \\\" backslash");
|
||||||
|
QTest::newRow("Command output") << QByteArray("The date is: $(date +%D)");
|
||||||
|
|
||||||
|
const int charSize = sizeof(char) * 0x100;
|
||||||
|
QByteArray charString(charSize, Qt::Uninitialized);
|
||||||
|
char *data = charString.data();
|
||||||
|
for (int c = 0; c < charSize; ++c)
|
||||||
|
data[c] = c;
|
||||||
|
QTest::newRow("All Characters") << charString;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSystemAccessTest::testCreateRemoteFile()
|
||||||
|
{
|
||||||
|
QFETCH(QByteArray, data);
|
||||||
|
|
||||||
|
const FilePath testFilePath = baseFilePath() / "test_file";
|
||||||
|
|
||||||
|
QVERIFY(!testFilePath.exists());
|
||||||
|
QVERIFY(testFilePath.writeFileContents(data));
|
||||||
|
QVERIFY(testFilePath.exists());
|
||||||
|
QCOMPARE(testFilePath.fileContents(), data);
|
||||||
|
QVERIFY(testFilePath.removeFile());
|
||||||
|
QVERIFY(!testFilePath.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileSystemAccessTest::testDirStatus()
|
||||||
{
|
{
|
||||||
FilePath filePath = baseFilePath();
|
FilePath filePath = baseFilePath();
|
||||||
QVERIFY(filePath.exists());
|
QVERIFY(filePath.exists());
|
||||||
@@ -142,7 +182,7 @@ void FileSystemAccessTest::testBytesAvailable()
|
|||||||
|
|
||||||
void FileSystemAccessTest::testFileActions()
|
void FileSystemAccessTest::testFileActions()
|
||||||
{
|
{
|
||||||
FilePath testFilePath = createFile("test");
|
const FilePath testFilePath = createFile("test");
|
||||||
QVERIFY(testFilePath.exists());
|
QVERIFY(testFilePath.exists());
|
||||||
QVERIFY(testFilePath.isFile());
|
QVERIFY(testFilePath.isFile());
|
||||||
|
|
||||||
@@ -153,15 +193,17 @@ void FileSystemAccessTest::testFileActions()
|
|||||||
QVERIFY(testFilePath.isReadableFile());
|
QVERIFY(testFilePath.isReadableFile());
|
||||||
QVERIFY(testFilePath.isExecutableFile());
|
QVERIFY(testFilePath.isExecutableFile());
|
||||||
|
|
||||||
QByteArray content("Test");
|
const QByteArray content("Test");
|
||||||
testFilePath.writeFileContents(content);
|
testFilePath.writeFileContents(content);
|
||||||
// ToDo: remove ".contains", make fileContents exact equal content
|
QCOMPARE(testFilePath.fileContents(), content);
|
||||||
QVERIFY(testFilePath.fileContents().contains(content));
|
|
||||||
|
|
||||||
QVERIFY(testFilePath.renameFile(baseFilePath() / "test1"));
|
const FilePath newTestFilePath = baseFilePath() / "test1";
|
||||||
// It is Ok that FilePath doesn't change itself after rename.
|
// It is Ok that FilePath doesn't change itself after rename.
|
||||||
FilePath newTestFilePath = baseFilePath() / "test1";
|
// FilePath::renameFile() is a const method!
|
||||||
|
QVERIFY(testFilePath.renameFile(newTestFilePath));
|
||||||
|
QVERIFY(!testFilePath.exists());
|
||||||
QVERIFY(newTestFilePath.exists());
|
QVERIFY(newTestFilePath.exists());
|
||||||
|
QCOMPARE(newTestFilePath.fileContents(), content);
|
||||||
QVERIFY(!testFilePath.removeFile());
|
QVERIFY(!testFilePath.removeFile());
|
||||||
QVERIFY(newTestFilePath.exists());
|
QVERIFY(newTestFilePath.exists());
|
||||||
QVERIFY(newTestFilePath.removeFile());
|
QVERIFY(newTestFilePath.removeFile());
|
||||||
|
@@ -45,12 +45,15 @@ class FileSystemAccessTest : public QObject
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void initTestCase();
|
void initTestCase();
|
||||||
void cleanupTestCase();
|
|
||||||
|
|
||||||
void testDirStatuses();
|
void testCreateRemoteFile_data();
|
||||||
|
void testCreateRemoteFile();
|
||||||
|
void testDirStatus();
|
||||||
void testBytesAvailable();
|
void testBytesAvailable();
|
||||||
void testFileActions();
|
void testFileActions();
|
||||||
|
|
||||||
|
void cleanupTestCase();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TestLinuxDeviceFactory m_testLinuxDeviceFactory;
|
TestLinuxDeviceFactory m_testLinuxDeviceFactory;
|
||||||
bool m_skippedAtWhole = false;
|
bool m_skippedAtWhole = false;
|
||||||
|
@@ -231,11 +231,14 @@ public:
|
|||||||
bool runInShell(const CommandLine &cmd, const QByteArray &data = {})
|
bool runInShell(const CommandLine &cmd, const QByteArray &data = {})
|
||||||
{
|
{
|
||||||
QTC_ASSERT(m_shell, return false);
|
QTC_ASSERT(m_shell, return false);
|
||||||
const QByteArray prefix = !data.isEmpty() ? QByteArray("echo " + data + " | ")
|
QTC_CHECK(m_shell->readAllStandardOutput().isNull()); // clean possible left-overs
|
||||||
: QByteArray("");
|
|
||||||
|
|
||||||
m_shell->readAllStandardOutput(); // clean possible left-overs
|
const QByteArray prefix = !data.isEmpty()
|
||||||
m_shell->write(prefix + cmd.toUserOutput().toUtf8() + "\necho $?\n");
|
? QByteArray("echo '" + data.toBase64() + "' | base64 -d | ") : QByteArray("");
|
||||||
|
const QByteArray suffix = QByteArray(" > /dev/null 2>&1\necho $?\n");
|
||||||
|
const QByteArray command = prefix + cmd.toUserOutput().toUtf8() + suffix;
|
||||||
|
|
||||||
|
m_shell->write(command);
|
||||||
DEBUG("RUN1 " << cmd.toUserOutput());
|
DEBUG("RUN1 " << cmd.toUserOutput());
|
||||||
m_shell->waitForReadyRead();
|
m_shell->waitForReadyRead();
|
||||||
const QByteArray output = m_shell->readAllStandardOutput();
|
const QByteArray output = m_shell->readAllStandardOutput();
|
||||||
@@ -243,18 +246,19 @@ public:
|
|||||||
bool ok = false;
|
bool ok = false;
|
||||||
const int result = output.toInt(&ok);
|
const int result = output.toInt(&ok);
|
||||||
LOG("Run command in shell:" << cmd.toUserOutput() << "result: " << output << " ==>" << result);
|
LOG("Run command in shell:" << cmd.toUserOutput() << "result: " << output << " ==>" << result);
|
||||||
return ok && result == 0;
|
QTC_ASSERT(ok, return false);
|
||||||
|
return !result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray outputForRunInShell(const QString &cmd)
|
QByteArray outputForRunInShell(const QString &cmd)
|
||||||
{
|
{
|
||||||
QTC_ASSERT(m_shell, return {});
|
QTC_ASSERT(m_shell, return {});
|
||||||
|
QTC_CHECK(m_shell->readAllStandardOutput().isNull()); // clean possible left-overs
|
||||||
|
|
||||||
static int val = 0;
|
static int val = 0;
|
||||||
const QByteArray delim = QString::number(++val, 16).toUtf8();
|
const QByteArray delim = QString::number(++val, 16).toUtf8();
|
||||||
|
|
||||||
DEBUG("RUN2 " << cmd);
|
DEBUG("RUN2 " << cmd);
|
||||||
m_shell->readAllStandardOutput(); // clean possible left-overs
|
|
||||||
const QByteArray marker = "___QTC___" + delim + "_OUTPUT_MARKER___";
|
const QByteArray marker = "___QTC___" + delim + "_OUTPUT_MARKER___";
|
||||||
DEBUG(" CMD: " << cmd.toUtf8() + "\necho " + marker + "\n");
|
DEBUG(" CMD: " << cmd.toUtf8() + "\necho " + marker + "\n");
|
||||||
m_shell->write(cmd.toUtf8() + "\necho " + marker + "\n");
|
m_shell->write(cmd.toUtf8() + "\necho " + marker + "\n");
|
||||||
@@ -784,9 +788,6 @@ QByteArray LinuxDevice::fileContents(const FilePath &filePath, qint64 limit, qin
|
|||||||
bool LinuxDevice::writeFileContents(const FilePath &filePath, const QByteArray &data) const
|
bool LinuxDevice::writeFileContents(const FilePath &filePath, const QByteArray &data) const
|
||||||
{
|
{
|
||||||
QTC_ASSERT(handlesFile(filePath), return {});
|
QTC_ASSERT(handlesFile(filePath), return {});
|
||||||
|
|
||||||
// This following would be the generic Unix solution.
|
|
||||||
// But it doesn't pass input. FIXME: Why?
|
|
||||||
return d->runInShell({"dd", {"of=" + filePath.path()}}, data);
|
return d->runInShell({"dd", {"of=" + filePath.path()}}, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user