CLI Merge: Only save database file when modified. (#2466)

* Merge: detect if database was changed.
* Adding unit test.
* Only saving on change.
This commit is contained in:
louib 2018-11-09 21:59:16 -05:00 committed by Jonathan White
parent a7dd9f19f4
commit f06742cf41
2 changed files with 19 additions and 6 deletions

View File

@ -84,14 +84,18 @@ int Merge::execute(const QStringList& arguments)
} }
Merger merger(db2.data(), db1.data()); Merger merger(db2.data(), db1.data());
merger.merge(); bool databaseChanged = merger.merge();
if (databaseChanged) {
QString errorMessage = db1->saveToFile(args.at(0)); QString errorMessage = db1->saveToFile(args.at(0));
if (!errorMessage.isEmpty()) { if (!errorMessage.isEmpty()) {
err << QObject::tr("Unable to save database to file : %1").arg(errorMessage) << endl; err << QObject::tr("Unable to save database to file : %1").arg(errorMessage) << endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
out << "Successfully merged the database files." << endl; out << "Successfully merged the database files." << endl;
} else {
out << "Database was not modified by merge operation." << endl;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -698,6 +698,15 @@ void TestCli::testMerge()
QVERIFY(entry1); QVERIFY(entry1);
QCOMPARE(entry1->title(), QString("Some Website")); QCOMPARE(entry1->title(), QString("Some Website"));
QCOMPARE(entry1->password(), QString("secretsecretsecret")); QCOMPARE(entry1->password(), QString("secretsecretsecret"));
// making sure that the message is different if the database was not
// modified by the merge operation.
pos = m_stdoutFile->pos();
Utils::Test::setNextPassword("a");
mergeCmd.execute({"merge", "-s", sourceFile.fileName(), sourceFile.fileName()});
m_stdoutFile->seek(pos);
m_stdoutFile->readLine();
QCOMPARE(m_stdoutFile->readAll(), QByteArray("Database was not modified by merge operation.\n"));
} }
void TestCli::testRemove() void TestCli::testRemove()