Commit 38e1b6a1 authored by Elvis Angelaccio's avatar Elvis Angelaccio
Browse files

Drop testhelper code in movetest

Instead, manually define what we actually want as expected result.
This brings the number of failures down to only four.
parent 89a7b5bb
......@@ -32,7 +32,7 @@ class MoveTest : public QObject
Q_OBJECT
private:
void addAllFormatsRows(const QString &testName, const QString &archiveName, const QVector<Archive::Entry*> &entries, Archive::Entry *destination) {
void addAllFormatsRows(const QString &testName, const QString &archiveName, const QVector<Archive::Entry*> &targetEntries, Archive::Entry *destination, const QStringList &expectedNewPaths) {
QStringList formats = QStringList()
<< QStringLiteral("7z")
<< QStringLiteral("rar")
......@@ -43,11 +43,23 @@ private:
const QString testNameWithFormat = testName + QStringLiteral(" (") + format + QStringLiteral(")");
QTest::newRow(testNameWithFormat.toUtf8())
<< archiveName + QLatin1Char('.') + format
<< entries
<< destination;
<< targetEntries
<< destination
<< expectedNewPaths;
}
}
// TODO: move this to testhelper.
QStringList getEntryPaths(Archive *archive)
{
QStringList paths;
auto loadJob = Archive::load(archive->fileName());
QObject::connect(loadJob, &Job::newEntry, [&paths](Archive::Entry* entry) { paths << entry->fullPath(); });
TestHelper::startAndWaitForResult(loadJob);
return paths;
}
private Q_SLOTS:
void testMoving_data();
void testMoving();
......@@ -58,15 +70,19 @@ QTEST_GUILESS_MAIN(MoveTest)
void MoveTest::testMoving_data()
{
QTest::addColumn<QString>("archiveName");
QTest::addColumn<QVector<Archive::Entry*>>("files");
QTest::addColumn<QVector<Archive::Entry*>>("targetEntries");
QTest::addColumn<Archive::Entry*>("destination");
QTest::addColumn<QStringList>("expectedNewPaths");
addAllFormatsRows(QStringLiteral("replace a single file"),
QStringLiteral("test"),
QVector<Archive::Entry*> {
new Archive::Entry(this, QStringLiteral("a.txt")),
},
new Archive::Entry(this, QStringLiteral("empty_dir/a.txt")));
new Archive::Entry(this, QStringLiteral("empty_dir/a.txt")),
QStringList {
QStringLiteral("empty_dir/a.txt")
});
addAllFormatsRows(QStringLiteral("replace several files"),
QStringLiteral("test"),
......@@ -74,7 +90,11 @@ void MoveTest::testMoving_data()
new Archive::Entry(this, QStringLiteral("a.txt")),
new Archive::Entry(this, QStringLiteral("b.txt")),
},
new Archive::Entry(this, QStringLiteral("empty_dir/")));
new Archive::Entry(this, QStringLiteral("empty_dir/")),
QStringList {
QStringLiteral("empty_dir/a.txt"),
QStringLiteral("empty_dir/b.txt")
});
addAllFormatsRows(QStringLiteral("replace a root directory"),
QStringLiteral("test"),
......@@ -86,7 +106,14 @@ void MoveTest::testMoving_data()
new Archive::Entry(this, QStringLiteral("dir1/a.txt")),
new Archive::Entry(this, QStringLiteral("dir1/b.txt")),
},
new Archive::Entry(this, QStringLiteral("empty_dir/dir/")));
new Archive::Entry(this, QStringLiteral("empty_dir/")),
QStringList {
QStringLiteral("empty_dir/a.txt"),
QStringLiteral("empty_dir/b.txt"),
QStringLiteral("empty_dir/dir/"),
QStringLiteral("empty_dir/dir/a.txt"),
QStringLiteral("empty_dir/dir/b.txt"),
});
addAllFormatsRows(QStringLiteral("replace a root directory 2"),
QStringLiteral("test"),
......@@ -96,7 +123,12 @@ void MoveTest::testMoving_data()
new Archive::Entry(this, QStringLiteral("dir2/dir/a.txt")),
new Archive::Entry(this, QStringLiteral("dir2/dir/b.txt")),
},
new Archive::Entry(this, QStringLiteral("empty_dir/dir/")));
new Archive::Entry(this, QStringLiteral("empty_dir/")),
QStringList {
QStringLiteral("empty_dir/dir/"),
QStringLiteral("empty_dir/dir/a.txt"),
QStringLiteral("empty_dir/dir/b.txt"),
});
addAllFormatsRows(QStringLiteral("replace a directory"),
QStringLiteral("test"),
......@@ -105,7 +137,12 @@ void MoveTest::testMoving_data()
new Archive::Entry(this, QStringLiteral("dir1/dir/a.txt")),
new Archive::Entry(this, QStringLiteral("dir1/dir/b.txt")),
},
new Archive::Entry(this, QStringLiteral("empty_dir/dir/")));
new Archive::Entry(this, QStringLiteral("empty_dir/dir")),
QStringList {
QStringLiteral("empty_dir/dir/"),
QStringLiteral("empty_dir/dir/a.txt"),
QStringLiteral("empty_dir/dir/b.txt"),
});
addAllFormatsRows(QStringLiteral("replace several directories"),
QStringLiteral("test"),
......@@ -121,7 +158,19 @@ void MoveTest::testMoving_data()
new Archive::Entry(this, QStringLiteral("dir2/dir/a.txt")),
new Archive::Entry(this, QStringLiteral("dir2/dir/b.txt")),
},
new Archive::Entry(this, QStringLiteral("empty_dir/")));
new Archive::Entry(this, QStringLiteral("empty_dir/")),
QStringList {
QStringLiteral("empty_dir/dir1/"),
QStringLiteral("empty_dir/dir1/a.txt"),
QStringLiteral("empty_dir/dir1/b.txt"),
QStringLiteral("empty_dir/dir1/dir/"),
QStringLiteral("empty_dir/dir1/dir/a.txt"),
QStringLiteral("empty_dir/dir1/dir/b.txt"),
QStringLiteral("empty_dir/dir2/"),
QStringLiteral("empty_dir/dir2/dir/"),
QStringLiteral("empty_dir/dir2/dir/a.txt"),
QStringLiteral("empty_dir/dir2/dir/b.txt")
});
addAllFormatsRows(QStringLiteral("replace several entries"),
QStringLiteral("test"),
......@@ -132,7 +181,14 @@ void MoveTest::testMoving_data()
new Archive::Entry(this, QStringLiteral("dir1/a.txt")),
new Archive::Entry(this, QStringLiteral("dir1/b.txt")),
},
new Archive::Entry(this, QStringLiteral("empty_dir/")));
new Archive::Entry(this, QStringLiteral("empty_dir/")),
QStringList {
QStringLiteral("empty_dir/dir/"),
QStringLiteral("empty_dir/dir/a.txt"),
QStringLiteral("empty_dir/dir/b.txt"),
QStringLiteral("empty_dir/a.txt"),
QStringLiteral("empty_dir/b.txt")
});
addAllFormatsRows(QStringLiteral("move a directory to the root"),
QStringLiteral("test"),
......@@ -141,7 +197,12 @@ void MoveTest::testMoving_data()
new Archive::Entry(this, QStringLiteral("dir1/dir/a.txt")),
new Archive::Entry(this, QStringLiteral("dir1/dir/b.txt")),
},
new Archive::Entry(this, QStringLiteral("dir/")));
new Archive::Entry(this, QStringLiteral("dir/")),
QStringList {
QStringLiteral("dir/"),
QStringLiteral("dir/a.txt"),
QStringLiteral("dir/b.txt"),
});
}
void MoveTest::testMoving()
......@@ -164,18 +225,40 @@ void MoveTest::testMoving()
QSKIP("Could not find a plugin to handle the archive. Skipping test.", SkipSingle);
}
QFETCH(QVector<Archive::Entry*>, files);
QFETCH(QVector<Archive::Entry*>, targetEntries);
QFETCH(Archive::Entry*, destination);
QFETCH(QStringList, expectedNewPaths);
QVector<Archive::Entry*> oldEntries = TestHelper::getEntryList(archive);
// Retrieve current paths in the archive.
QStringList oldPaths = getEntryPaths(archive);
CompressionOptions options = CompressionOptions();
// Check that the entries to be moved are in the archive.
foreach (const auto entry, targetEntries) {
QVERIFY(oldPaths.contains(entry->fullPath()));
}
// Check that the expected paths (after the MoveJob) are not in the archive.
foreach (const auto &expectedPath, expectedNewPaths) {
QVERIFY(!oldPaths.contains(expectedPath));
}
CompressionOptions options;
options.setGlobalWorkDir(QFINDTESTDATA("data"));
MoveJob *moveJob = archive->moveFiles(files, destination, options);
MoveJob *moveJob = archive->moveFiles(targetEntries, destination, options);
TestHelper::startAndWaitForResult(moveJob);
QVector<Archive::Entry*> resultedEntries = TestHelper::getEntryList(archive);
TestHelper::verifyMovedEntriesWithDestination(files, destination, oldEntries, resultedEntries);
// Retrieve the resulting paths.
QStringList newPaths = getEntryPaths(archive);
// Check that the expected paths are now in the archive.
foreach (const auto &path, expectedNewPaths) {
QVERIFY(newPaths.contains(path));
}
// Check that the target paths are no longer in the archive.
foreach (const auto entry, targetEntries) {
QVERIFY(!newPaths.contains(entry->fullPath()));
}
loadJob->deleteLater();
archive->deleteLater();
......
......@@ -39,6 +39,7 @@ QVector<Archive::Entry*> TestHelper::getEntryList(Archive *archive)
auto loadJob = Archive::load(archive->fileName());
QObject::connect(loadJob, &Job::newEntry, [&list](Archive::Entry* entry) { list << entry; });
startAndWaitForResult(loadJob);
return list;
}
......@@ -55,22 +56,6 @@ void TestHelper::verifyAddedEntriesWithDestination(const QVector<Archive::Entry*
}
}
void TestHelper::verifyMovedEntriesWithDestination(const QVector<Archive::Entry*> &argumentEntries, const Archive::Entry *destination, const QVector<Archive::Entry*> &oldEntries, const QVector<Archive::Entry*> &newEntries)
{
QStringList expectedPaths = getExpectedMovedEntryPaths(oldEntries, argumentEntries, destination);
QStringList actualPaths = ReadOnlyArchiveInterface::entryFullPaths(newEntries);
foreach (const QString &path, expectedPaths) {
QVERIFY2(actualPaths.contains(path), (QStringLiteral("No ") + path + QStringLiteral(" inside the archive")).toUtf8());
}
foreach (const QString &path, actualPaths) {
QVERIFY2(expectedPaths.contains(path), (QStringLiteral("Entry ") + path + QStringLiteral(" is not expected to be inside the archive")).toUtf8());
}
foreach (const Archive::Entry *entry, argumentEntries) {
const QString path = entry->fullPath();
QVERIFY2(!actualPaths.contains(path), (QStringLiteral("Entry ") + path + QStringLiteral(" is still inside the archive, when it shouldn't be")).toUtf8());
}
}
void TestHelper::verifyCopiedEntriesWithDestination(const QVector<Archive::Entry*> &argumentEntries, const Archive::Entry *destination, const QVector<Archive::Entry*> &oldEntries, const QVector<Archive::Entry*> &newEntries)
{
QStringList expectedPaths = getExpectedCopiedEntryPaths(oldEntries, argumentEntries, destination);
......@@ -108,57 +93,6 @@ QStringList TestHelper::getExpectedNewEntryPaths(const QVector<Archive::Entry*>
return expectedPaths;
}
QStringList TestHelper::getExpectedMovedEntryPaths(const QVector<Archive::Entry*> &entryList, const QVector<Archive::Entry*> &argumentEntries, const Archive::Entry *destination)
{
QStringList expectedPaths = QStringList();
QMap<QString, Archive::Entry*> entryMap = getEntryMap(entryList);
QStringList argumentPaths = ReadOnlyArchiveInterface::entryFullPaths(argumentEntries);
QString lastMovedFolder;
if (ReadOnlyArchiveInterface::entriesWithoutChildren(argumentEntries).count() > 1) {
// Destination path doesn't contain a target entry name, so we have to remember to include it while moving
// folder contents.
int nameLength = 0;
foreach (const Archive::Entry *entry, entryMap) {
const QString entryPath = entry->fullPath();
if (lastMovedFolder.count() > 0 && entryPath.startsWith(lastMovedFolder)) {
expectedPaths << destination->fullPath() + entryPath.right(entryPath.count() - lastMovedFolder.count() + nameLength);
} else if (argumentPaths.contains(entryPath)) {
QString expectedPath = destination->fullPath() + entry->name();
if (entryPath.right(1) == QLatin1String("/")) {
expectedPath += QLatin1Char('/');
nameLength = entry->name().count() + 1; // plus slash
lastMovedFolder = entryPath;
} else {
nameLength = 0;
lastMovedFolder = QString();
}
expectedPaths << expectedPath;
} else {
expectedPaths << entryPath;
nameLength = 0;
lastMovedFolder = QString();
}
}
} else {
foreach (const Archive::Entry *entry, entryMap) {
const QString entryPath = entry->fullPath();
if (lastMovedFolder.count() > 0 && entryPath.startsWith(lastMovedFolder)) {
expectedPaths << destination->fullPath() + entryPath.right(entryPath.count() - lastMovedFolder.count());
} else if (argumentPaths.contains(entryPath)) {
if (entryPath.right(1) == QLatin1String("/")) {
lastMovedFolder = entryPath;
} else if (lastMovedFolder.count() > 0) {
lastMovedFolder = QString();
}
expectedPaths << destination->fullPath();
} else {
expectedPaths << entryPath;
}
}
}
return expectedPaths;
}
QStringList TestHelper::getExpectedCopiedEntryPaths(const QVector<Archive::Entry*> &entryList, const QVector<Archive::Entry*> &argumentEntries, const Archive::Entry *destination)
{
QStringList expectedPaths = QStringList();
......
......@@ -42,7 +42,6 @@ public:
static void startAndWaitForResult(KJob *job);
static QVector<Archive::Entry*> getEntryList(Archive *archive);
static void verifyAddedEntriesWithDestination(const QVector<Archive::Entry*> &argumentEntries, const Archive::Entry *destination, const QVector<Archive::Entry*> &oldEntries, const QVector<Archive::Entry*> &newEntries);
static void verifyMovedEntriesWithDestination(const QVector<Archive::Entry*> &argumentEntries, const Archive::Entry *destination, const QVector<Archive::Entry*> &oldEntries, const QVector<Archive::Entry*> &newEntries);
static void verifyCopiedEntriesWithDestination(const QVector<Archive::Entry*> &argumentEntries, const Archive::Entry *destination, const QVector<Archive::Entry*> &oldEntries, const QVector<Archive::Entry*> &newEntries);
private:
......
......@@ -422,7 +422,7 @@ MoveJob* Archive::moveFiles(const QVector<Archive::Entry*> &files, Archive::Entr
newOptions.setEncryptedArchiveHint(true);
}
qCDebug(ARK) << "Going to move files" << files << "with options" << newOptions;
qCDebug(ARK) << "Going to move files" << files << "to destinatian" << destination << "with options" << newOptions;
Q_ASSERT(!m_iface->isReadOnly());
MoveJob *newJob = new MoveJob(files, destination, newOptions, static_cast<ReadWriteArchiveInterface*>(m_iface));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment