Skip to content

Commit

Permalink
CLI Merge: Only save database file when modified. (#2466)
Browse files Browse the repository at this point in the history
* Merge: detect if database was changed.
* Adding unit test.
* Only saving on change.
  • Loading branch information
louib authored and droidmonkey committed Nov 10, 2018
1 parent a7dd9f1 commit f06742c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/cli/Merge.cpp
Expand Up @@ -84,14 +84,18 @@ int Merge::execute(const QStringList& arguments)
}

Merger merger(db2.data(), db1.data());
merger.merge();

QString errorMessage = db1->saveToFile(args.at(0));
if (!errorMessage.isEmpty()) {
err << QObject::tr("Unable to save database to file : %1").arg(errorMessage) << endl;
return EXIT_FAILURE;
bool databaseChanged = merger.merge();

if (databaseChanged) {
QString errorMessage = db1->saveToFile(args.at(0));
if (!errorMessage.isEmpty()) {
err << QObject::tr("Unable to save database to file : %1").arg(errorMessage) << endl;
return EXIT_FAILURE;
}
out << "Successfully merged the database files." << endl;
} else {
out << "Database was not modified by merge operation." << endl;
}

out << "Successfully merged the database files." << endl;
return EXIT_SUCCESS;
}
9 changes: 9 additions & 0 deletions tests/TestCli.cpp
Expand Up @@ -698,6 +698,15 @@ void TestCli::testMerge()
QVERIFY(entry1);
QCOMPARE(entry1->title(), QString("Some Website"));
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()
Expand Down

0 comments on commit f06742c

Please sign in to comment.