summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElvis Angelaccio <[email protected]>2016-03-30 16:00:45 +0200
committerElvis Angelaccio <[email protected]>2016-03-30 16:00:45 +0200
commit482e771859ff64c96fe22f73895bb492c8314bf2 (patch)
tree0455969c28215ab853856f73d290f4003e12bbb7
parent331776a2830c3a76b68d866e6cc020e6f30c3225 (diff)
cliinterface: refactor the replacement of ListArgs
This also adds unit tests for cli7z, clirar and cliunarchiver Differential Revision: D1251
-rw-r--r--autotests/plugins/cli7zplugin/cli7ztest.cpp46
-rw-r--r--autotests/plugins/cli7zplugin/cli7ztest.h2
-rw-r--r--autotests/plugins/clirarplugin/clirartest.cpp46
-rw-r--r--autotests/plugins/clirarplugin/clirartest.h2
-rw-r--r--autotests/plugins/cliunarchiverplugin/cliunarchivertest.cpp44
-rw-r--r--autotests/plugins/cliunarchiverplugin/cliunarchivertest.h2
-rw-r--r--kerfuffle/cliinterface.cpp70
-rw-r--r--kerfuffle/cliinterface.h2
-rw-r--r--plugins/cliunarchiverplugin/cliplugin.cpp3
9 files changed, 172 insertions, 45 deletions
diff --git a/autotests/plugins/cli7zplugin/cli7ztest.cpp b/autotests/plugins/cli7zplugin/cli7ztest.cpp
index f520d66..bbb7d16 100644
--- a/autotests/plugins/cli7zplugin/cli7ztest.cpp
+++ b/autotests/plugins/cli7zplugin/cli7ztest.cpp
@@ -178,6 +178,52 @@ void Cli7zTest::testList()
plugin->deleteLater();
}
+void Cli7zTest::testListArgs_data()
+{
+ QTest::addColumn<QString>("archiveName");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QStringList>("expectedArgs");
+
+ QTest::newRow("unencrypted")
+ << QStringLiteral("/tmp/foo.7z")
+ << QString()
+ << QStringList {
+ QStringLiteral("l"),
+ QStringLiteral("-slt"),
+ QStringLiteral("/tmp/foo.7z")
+ };
+
+ QTest::newRow("header-encrypted")
+ << QStringLiteral("/tmp/foo.7z")
+ << QStringLiteral("1234")
+ << QStringList {
+ QStringLiteral("l"),
+ QStringLiteral("-slt"),
+ QStringLiteral("-p1234"),
+ QStringLiteral("/tmp/foo.7z")
+ };
+}
+
+void Cli7zTest::testListArgs()
+{
+ QFETCH(QString, archiveName);
+ CliPlugin *plugin = new CliPlugin(this, {QVariant(archiveName)});
+ QVERIFY(plugin);
+
+ const QStringList listArgs = { QStringLiteral("l"),
+ QStringLiteral("-slt"),
+ QStringLiteral("$PasswordSwitch"),
+ QStringLiteral("$Archive") };
+
+ QFETCH(QString, password);
+ const auto replacedArgs = plugin->substituteListVariables(listArgs, password);
+
+ QFETCH(QStringList, expectedArgs);
+ QCOMPARE(replacedArgs, expectedArgs);
+
+ plugin->deleteLater();
+}
+
void Cli7zTest::testExtractArgs_data()
{
QTest::addColumn<QString>("archiveName");
diff --git a/autotests/plugins/cli7zplugin/cli7ztest.h b/autotests/plugins/cli7zplugin/cli7ztest.h
index 6c6f6cb..ed3ab67 100644
--- a/autotests/plugins/cli7zplugin/cli7ztest.h
+++ b/autotests/plugins/cli7zplugin/cli7ztest.h
@@ -41,6 +41,8 @@ private Q_SLOTS:
void testArchive();
void testList_data();
void testList();
+ void testListArgs_data();
+ void testListArgs();
void testExtractArgs_data();
void testExtractArgs();
diff --git a/autotests/plugins/clirarplugin/clirartest.cpp b/autotests/plugins/clirarplugin/clirartest.cpp
index e0bc6f0..bb3c828 100644
--- a/autotests/plugins/clirarplugin/clirartest.cpp
+++ b/autotests/plugins/clirarplugin/clirartest.cpp
@@ -212,6 +212,52 @@ void CliRarTest::testList()
rarPlugin->deleteLater();
}
+void CliRarTest::testListArgs_data()
+{
+ QTest::addColumn<QString>("archiveName");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QStringList>("expectedArgs");
+
+ QTest::newRow("unencrypted")
+ << QStringLiteral("/tmp/foo.rar")
+ << QString()
+ << QStringList {
+ QStringLiteral("vt"),
+ QStringLiteral("-v"),
+ QStringLiteral("/tmp/foo.rar")
+ };
+
+ QTest::newRow("header-encrypted")
+ << QStringLiteral("/tmp/foo.rar")
+ << QStringLiteral("1234")
+ << QStringList {
+ QStringLiteral("vt"),
+ QStringLiteral("-v"),
+ QStringLiteral("-p1234"),
+ QStringLiteral("/tmp/foo.rar")
+ };
+}
+
+void CliRarTest::testListArgs()
+{
+ QFETCH(QString, archiveName);
+ CliPlugin *plugin = new CliPlugin(this, {QVariant(archiveName)});
+ QVERIFY(plugin);
+
+ const QStringList listArgs = { QStringLiteral("vt"),
+ QStringLiteral("-v"),
+ QStringLiteral("$PasswordSwitch"),
+ QStringLiteral("$Archive") };
+
+ QFETCH(QString, password);
+ const auto replacedArgs = plugin->substituteListVariables(listArgs, password);
+
+ QFETCH(QStringList, expectedArgs);
+ QCOMPARE(replacedArgs, expectedArgs);
+
+ plugin->deleteLater();
+}
+
void CliRarTest::testExtractArgs_data()
{
QTest::addColumn<QString>("archiveName");
diff --git a/autotests/plugins/clirarplugin/clirartest.h b/autotests/plugins/clirarplugin/clirartest.h
index e338384..6d13bf4 100644
--- a/autotests/plugins/clirarplugin/clirartest.h
+++ b/autotests/plugins/clirarplugin/clirartest.h
@@ -42,6 +42,8 @@ private Q_SLOTS:
void testArchive();
void testList_data();
void testList();
+ void testListArgs_data();
+ void testListArgs();
void testExtractArgs_data();
void testExtractArgs();
diff --git a/autotests/plugins/cliunarchiverplugin/cliunarchivertest.cpp b/autotests/plugins/cliunarchiverplugin/cliunarchivertest.cpp
index becf9f8..3286850 100644
--- a/autotests/plugins/cliunarchiverplugin/cliunarchivertest.cpp
+++ b/autotests/plugins/cliunarchiverplugin/cliunarchivertest.cpp
@@ -173,6 +173,50 @@ void CliUnarchiverTest::testList()
unarPlugin->deleteLater();
}
+void CliUnarchiverTest::testListArgs_data()
+{
+ QTest::addColumn<QString>("archiveName");
+ QTest::addColumn<QString>("password");
+ QTest::addColumn<QStringList>("expectedArgs");
+
+ QTest::newRow("unencrypted")
+ << QStringLiteral("/tmp/foo.rar")
+ << QString()
+ << QStringList {
+ QStringLiteral("-json"),
+ QStringLiteral("/tmp/foo.rar")
+ };
+
+ QTest::newRow("header-encrypted")
+ << QStringLiteral("/tmp/foo.rar")
+ << QStringLiteral("1234")
+ << QStringList {
+ QStringLiteral("-json"),
+ QStringLiteral("/tmp/foo.rar"),
+ QStringLiteral("-password"),
+ QStringLiteral("1234")
+ };
+}
+
+void CliUnarchiverTest::testListArgs()
+{
+ QFETCH(QString, archiveName);
+ CliPlugin *plugin = new CliPlugin(this, {QVariant(archiveName)});
+ QVERIFY(plugin);
+
+ const QStringList listArgs = { QStringLiteral("-json"),
+ QStringLiteral("$Archive"),
+ QStringLiteral("$PasswordSwitch") };
+
+ QFETCH(QString, password);
+ const auto replacedArgs = plugin->substituteListVariables(listArgs, password);
+
+ QFETCH(QStringList, expectedArgs);
+ QCOMPARE(replacedArgs, expectedArgs);
+
+ plugin->deleteLater();
+}
+
void CliUnarchiverTest::testExtraction_data()
{
QTest::addColumn<QString>("archivePath");
diff --git a/autotests/plugins/cliunarchiverplugin/cliunarchivertest.h b/autotests/plugins/cliunarchiverplugin/cliunarchivertest.h
index d2a1824..4d8727e 100644
--- a/autotests/plugins/cliunarchiverplugin/cliunarchivertest.h
+++ b/autotests/plugins/cliunarchiverplugin/cliunarchivertest.h
@@ -36,6 +36,8 @@ private Q_SLOTS:
void testArchive();
void testList_data();
void testList();
+ void testListArgs_data();
+ void testListArgs();
void testExtraction_data();
void testExtraction();
void testExtractArgs_data();
diff --git a/kerfuffle/cliinterface.cpp b/kerfuffle/cliinterface.cpp
index dd96fe9..e0716e4 100644
--- a/kerfuffle/cliinterface.cpp
+++ b/kerfuffle/cliinterface.cpp
@@ -127,8 +127,7 @@ bool CliInterface::list()
cacheParameterList();
m_operationMode = List;
- QStringList args = m_param.value(ListArgs).toStringList();
- substituteListVariables(args);
+ const auto args = substituteListVariables(m_param.value(ListArgs).toStringList(), password());
if (!runProcess(m_param.value(ListProgram).toStringList(), args)) {
failOperation();
@@ -639,6 +638,33 @@ bool CliInterface::moveToDestination(const QDir &tempDir, const QDir &destDir, b
return true;
}
+QStringList CliInterface::substituteListVariables(const QStringList &listArgs, const QString &password)
+{
+ // Required if we call this function from unit tests.
+ cacheParameterList();
+
+ QStringList args;
+ foreach (const QString& arg, listArgs) {
+ if (arg == QLatin1String("$Archive")) {
+ args << filename();
+ continue;
+ }
+
+ if (arg == QLatin1String("$PasswordSwitch")) {
+ args << passwordSwitch(password);
+ continue;
+ }
+
+ // Simple argument (e.g. -slt in 7z), nothing to substitute, just add it to the list.
+ args << arg;
+ }
+
+ // Remove empty strings, if any.
+ args.removeAll(QString());
+
+ return args;
+}
+
QStringList CliInterface::substituteCopyVariables(const QStringList &extractArgs, const QVariantList &files, bool preservePaths, const QString &password, const QString &rootNode)
{
// Required if we call this function from unit tests.
@@ -659,7 +685,6 @@ QStringList CliInterface::substituteCopyVariables(const QStringList &extractArgs
}
if (arg == QLatin1String("$PasswordSwitch")) {
-
args << passwordSwitch(password);
continue;
}
@@ -1093,45 +1118,6 @@ bool CliInterface::doResume()
return false;
}
-void CliInterface::substituteListVariables(QStringList& params)
-{
- for (int i = 0; i < params.size(); ++i) {
- const QString parameter = params.at(i);
-
- if (parameter == QLatin1String( "$Archive" )) {
- params[i] = filename();
- }
-
- if (parameter == QLatin1String("$PasswordSwitch")) {
- //if the PasswordSwitch argument has been added, we at least
- //assume that the format of the switch has been added as well
- Q_ASSERT(m_param.contains(PasswordSwitch));
-
- //we will set it afterwards, if there is a password
- params.removeAt(i);
-
- QString pass = password();
-
- if (!pass.isEmpty()) {
- QStringList theSwitch = m_param.value(PasswordSwitch).toStringList();
-
- for (int j = 0; j < theSwitch.size(); ++j) {
- //get the argument part
- QString newArg = theSwitch.at(j);
-
- //substitute the $Password
- newArg.replace(QLatin1String("$Password"), pass);
-
- //put it in the arg list
- params.insert(i + j, newArg);
- ++i;
- }
- }
- --i;
- }
- }
-}
-
QString CliInterface::escapeFileName(const QString& fileName) const
{
return fileName;
diff --git a/kerfuffle/cliinterface.h b/kerfuffle/cliinterface.h
index 232abf7..199d216 100644
--- a/kerfuffle/cliinterface.h
+++ b/kerfuffle/cliinterface.h
@@ -317,6 +317,7 @@ public:
*/
bool moveToDestination(const QDir &tempDir, const QDir &destDir, bool preservePaths);
+ QStringList substituteListVariables(const QStringList &listArgs, const QString &password);
QStringList substituteCopyVariables(const QStringList &extractArgs, const QVariantList &files, bool preservePaths, const QString &password, const QString &rootNode);
/**
@@ -343,7 +344,6 @@ protected:
virtual void handleLine(const QString& line);
virtual void cacheParameterList();
- void substituteListVariables(QStringList& params);
/**
* Run @p programName with the given @p arguments.
diff --git a/plugins/cliunarchiverplugin/cliplugin.cpp b/plugins/cliunarchiverplugin/cliplugin.cpp
index aebba34..d3eb96a 100644
--- a/plugins/cliunarchiverplugin/cliplugin.cpp
+++ b/plugins/cliunarchiverplugin/cliplugin.cpp
@@ -50,8 +50,7 @@ bool CliPlugin::list()
cacheParameterList();
m_operationMode = List;
- QStringList args = m_param.value(ListArgs).toStringList();
- substituteListVariables(args);
+ const auto args = substituteListVariables(m_param.value(ListArgs).toStringList(), password());
if (!runProcess(m_param.value(ListProgram).toStringList(), args)) {
failOperation();