From f06742cf41151417154b54f41d43d0db45c4812c Mon Sep 17 00:00:00 2001 From: louib Date: Fri, 9 Nov 2018 21:59:16 -0500 Subject: [PATCH] CLI Merge: Only save database file when modified. (#2466) * Merge: detect if database was changed. * Adding unit test. * Only saving on change. --- src/cli/Merge.cpp | 16 ++++++++++------ tests/TestCli.cpp | 9 +++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/cli/Merge.cpp b/src/cli/Merge.cpp index e81b7aa99..65a03f38b 100644 --- a/src/cli/Merge.cpp +++ b/src/cli/Merge.cpp @@ -84,14 +84,18 @@ int Merge::execute(const QStringList& arguments) } Merger merger(db2.data(), db1.data()); - merger.merge(); + bool databaseChanged = 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; + 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; } diff --git a/tests/TestCli.cpp b/tests/TestCli.cpp index b1c3a82e8..36ca479d5 100644 --- a/tests/TestCli.cpp +++ b/tests/TestCli.cpp @@ -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()