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:
Jarek Kobus
2022-03-16 20:21:49 +01:00
parent de8a67a412
commit 481df466e2
3 changed files with 64 additions and 18 deletions

View File

@@ -95,7 +95,11 @@ void FileSystemAccessTest::initTestCase()
QVERIFY(!newDev.isNull());
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.exists());
}
void FileSystemAccessTest::cleanupTestCase()
@@ -106,7 +110,43 @@ void FileSystemAccessTest::cleanupTestCase()
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();
QVERIFY(filePath.exists());
@@ -142,7 +182,7 @@ void FileSystemAccessTest::testBytesAvailable()
void FileSystemAccessTest::testFileActions()
{
FilePath testFilePath = createFile("test");
const FilePath testFilePath = createFile("test");
QVERIFY(testFilePath.exists());
QVERIFY(testFilePath.isFile());
@@ -153,15 +193,17 @@ void FileSystemAccessTest::testFileActions()
QVERIFY(testFilePath.isReadableFile());
QVERIFY(testFilePath.isExecutableFile());
QByteArray content("Test");
const QByteArray content("Test");
testFilePath.writeFileContents(content);
// ToDo: remove ".contains", make fileContents exact equal content
QVERIFY(testFilePath.fileContents().contains(content));
QCOMPARE(testFilePath.fileContents(), content);
QVERIFY(testFilePath.renameFile(baseFilePath() / "test1"));
const FilePath newTestFilePath = baseFilePath() / "test1";
// 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());
QCOMPARE(newTestFilePath.fileContents(), content);
QVERIFY(!testFilePath.removeFile());
QVERIFY(newTestFilePath.exists());
QVERIFY(newTestFilePath.removeFile());

View File

@@ -45,12 +45,15 @@ class FileSystemAccessTest : public QObject
private slots:
void initTestCase();
void cleanupTestCase();
void testDirStatuses();
void testCreateRemoteFile_data();
void testCreateRemoteFile();
void testDirStatus();
void testBytesAvailable();
void testFileActions();
void cleanupTestCase();
private:
TestLinuxDeviceFactory m_testLinuxDeviceFactory;
bool m_skippedAtWhole = false;

View File

@@ -231,11 +231,14 @@ public:
bool runInShell(const CommandLine &cmd, const QByteArray &data = {})
{
QTC_ASSERT(m_shell, return false);
const QByteArray prefix = !data.isEmpty() ? QByteArray("echo " + data + " | ")
: QByteArray("");
QTC_CHECK(m_shell->readAllStandardOutput().isNull()); // clean possible left-overs
m_shell->readAllStandardOutput(); // clean possible left-overs
m_shell->write(prefix + cmd.toUserOutput().toUtf8() + "\necho $?\n");
const QByteArray prefix = !data.isEmpty()
? 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());
m_shell->waitForReadyRead();
const QByteArray output = m_shell->readAllStandardOutput();
@@ -243,18 +246,19 @@ public:
bool ok = false;
const int result = output.toInt(&ok);
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)
{
QTC_ASSERT(m_shell, return {});
QTC_CHECK(m_shell->readAllStandardOutput().isNull()); // clean possible left-overs
static int val = 0;
const QByteArray delim = QString::number(++val, 16).toUtf8();
DEBUG("RUN2 " << cmd);
m_shell->readAllStandardOutput(); // clean possible left-overs
const QByteArray marker = "___QTC___" + delim + "_OUTPUT_MARKER___";
DEBUG(" CMD: " << 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
{
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);
}