mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-12-04 15:39:34 +01:00
CLI: Replace locate command with search
* Introduce search CLI command to replace locate command. Search can provide the same functionality but in a more fine-grained fashion * Replace use of Group::locate in code: Use EntrySearcher in clip cli command best-match option. This removes the matching against group hierarchy of an entry which is kind of nonsense as clip expects exactly one match. Matching against groups can be done using search command. * Remove obsolete Group::locate method
This commit is contained in:
committed by
Jonathan White
parent
ec81d2bc3f
commit
e8f2c9d126
@@ -41,12 +41,12 @@
|
||||
#include "cli/Import.h"
|
||||
#include "cli/Info.h"
|
||||
#include "cli/List.h"
|
||||
#include "cli/Locate.h"
|
||||
#include "cli/Merge.h"
|
||||
#include "cli/Move.h"
|
||||
#include "cli/Open.h"
|
||||
#include "cli/Remove.h"
|
||||
#include "cli/RemoveGroup.h"
|
||||
#include "cli/Search.h"
|
||||
#include "cli/Show.h"
|
||||
#include "cli/Utils.h"
|
||||
|
||||
@@ -226,7 +226,6 @@ void TestCli::testBatchCommands()
|
||||
QVERIFY(Commands::getCommand("generate"));
|
||||
QVERIFY(Commands::getCommand("help"));
|
||||
QVERIFY(Commands::getCommand("import"));
|
||||
QVERIFY(Commands::getCommand("locate"));
|
||||
QVERIFY(Commands::getCommand("ls"));
|
||||
QVERIFY(Commands::getCommand("merge"));
|
||||
QVERIFY(Commands::getCommand("mkdir"));
|
||||
@@ -235,6 +234,7 @@ void TestCli::testBatchCommands()
|
||||
QVERIFY(Commands::getCommand("rm"));
|
||||
QVERIFY(Commands::getCommand("rmdir"));
|
||||
QVERIFY(Commands::getCommand("show"));
|
||||
QVERIFY(Commands::getCommand("search"));
|
||||
QVERIFY(!Commands::getCommand("doesnotexist"));
|
||||
QCOMPARE(Commands::getCommands().size(), 22);
|
||||
}
|
||||
@@ -254,7 +254,6 @@ void TestCli::testInteractiveCommands()
|
||||
QVERIFY(Commands::getCommand("exit"));
|
||||
QVERIFY(Commands::getCommand("generate"));
|
||||
QVERIFY(Commands::getCommand("help"));
|
||||
QVERIFY(Commands::getCommand("locate"));
|
||||
QVERIFY(Commands::getCommand("ls"));
|
||||
QVERIFY(Commands::getCommand("merge"));
|
||||
QVERIFY(Commands::getCommand("mkdir"));
|
||||
@@ -264,6 +263,7 @@ void TestCli::testInteractiveCommands()
|
||||
QVERIFY(Commands::getCommand("rm"));
|
||||
QVERIFY(Commands::getCommand("rmdir"));
|
||||
QVERIFY(Commands::getCommand("show"));
|
||||
QVERIFY(Commands::getCommand("search"));
|
||||
QVERIFY(!Commands::getCommand("doesnotexist"));
|
||||
QCOMPARE(Commands::getCommands().size(), 22);
|
||||
}
|
||||
@@ -1274,55 +1274,6 @@ void TestCli::testList()
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray());
|
||||
}
|
||||
|
||||
void TestCli::testLocate()
|
||||
{
|
||||
Locate locateCmd;
|
||||
QVERIFY(!locateCmd.name.isEmpty());
|
||||
QVERIFY(locateCmd.getDescriptionLine().contains(locateCmd.name));
|
||||
|
||||
setInput("a");
|
||||
execCmd(locateCmd, {"locate", m_dbFile->fileName(), "Sample"});
|
||||
m_stderr->readLine(); // Skip password prompt
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray());
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/Sample Entry\n"));
|
||||
|
||||
// Quiet option
|
||||
setInput("a");
|
||||
execCmd(locateCmd, {"locate", m_dbFile->fileName(), "-q", "Sample"});
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray());
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/Sample Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(locateCmd, {"locate", m_dbFile->fileName(), "Does Not Exist"});
|
||||
m_stderr->readLine(); // skip password prompt
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray("No results for that search term.\n"));
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray());
|
||||
|
||||
// write a modified database
|
||||
auto db = readDatabase();
|
||||
QVERIFY(db);
|
||||
auto* group = db->rootGroup()->findGroupByPath("/General/");
|
||||
QVERIFY(group);
|
||||
auto* entry = new Entry();
|
||||
entry->setUuid(QUuid::createUuid());
|
||||
entry->setTitle("New Entry");
|
||||
group->addEntry(entry);
|
||||
|
||||
TemporaryFile tmpFile;
|
||||
tmpFile.open();
|
||||
tmpFile.close();
|
||||
db->saveAs(tmpFile.fileName());
|
||||
|
||||
setInput("a");
|
||||
execCmd(locateCmd, {"locate", tmpFile.fileName(), "New"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/General/New Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(locateCmd, {"locate", tmpFile.fileName(), "Entry"});
|
||||
QCOMPARE(m_stdout->readAll(),
|
||||
QByteArray("/Sample Entry\n/General/New Entry\n/Homebanking/Subgroup/Subgroup Entry\n"));
|
||||
}
|
||||
|
||||
void TestCli::testMerge()
|
||||
{
|
||||
Merge mergeCmd;
|
||||
@@ -1694,6 +1645,76 @@ void TestCli::testRemoveQuiet()
|
||||
QVERIFY(!db->rootGroup()->findEntryByPath(QString("/%1/Sample Entry").arg(Group::tr("Recycle Bin"))));
|
||||
}
|
||||
|
||||
void TestCli::testSearch()
|
||||
{
|
||||
Search searchCmd;
|
||||
QVERIFY(!searchCmd.name.isEmpty());
|
||||
QVERIFY(searchCmd.getDescriptionLine().contains(searchCmd.name));
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", m_dbFile->fileName(), "Sample"});
|
||||
m_stderr->readLine(); // Skip password prompt
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray());
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/Sample Entry\n"));
|
||||
|
||||
// Quiet option
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", m_dbFile->fileName(), "-q", "Sample"});
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray());
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/Sample Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", m_dbFile->fileName(), "Does Not Exist"});
|
||||
m_stderr->readLine(); // skip password prompt
|
||||
QCOMPARE(m_stderr->readAll(), QByteArray("No results for that search term.\n"));
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray());
|
||||
|
||||
// write a modified database
|
||||
auto db = readDatabase();
|
||||
QVERIFY(db);
|
||||
auto* group = db->rootGroup()->findGroupByPath("/General/");
|
||||
QVERIFY(group);
|
||||
auto* entry = new Entry();
|
||||
entry->setUuid(QUuid::createUuid());
|
||||
entry->setTitle("New Entry");
|
||||
group->addEntry(entry);
|
||||
|
||||
TemporaryFile tmpFile;
|
||||
tmpFile.open();
|
||||
tmpFile.close();
|
||||
db->saveAs(tmpFile.fileName());
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", tmpFile.fileName(), "title:New"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/General/New Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", tmpFile.fileName(), "title:Entry"});
|
||||
QCOMPARE(m_stdout->readAll(),
|
||||
QByteArray("/Sample Entry\n/General/New Entry\n/Homebanking/Subgroup/Subgroup Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", tmpFile.fileName(), "group:General"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/General/New Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", tmpFile.fileName(), "group:NewDatabase"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/Sample Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", tmpFile.fileName(), "group:/NewDatabase"});
|
||||
QCOMPARE(m_stdout->readAll(),
|
||||
QByteArray("/Sample Entry\n/General/New Entry\n/Homebanking/Subgroup/Subgroup Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", tmpFile.fileName(), "url:bank"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/Homebanking/Subgroup/Subgroup Entry\n"));
|
||||
|
||||
setInput("a");
|
||||
execCmd(searchCmd, {"search", tmpFile.fileName(), "u:User Name"});
|
||||
QCOMPARE(m_stdout->readAll(), QByteArray("/Sample Entry\n/Homebanking/Subgroup/Subgroup Entry\n"));
|
||||
}
|
||||
|
||||
void TestCli::testShow()
|
||||
{
|
||||
Show showCmd;
|
||||
|
||||
@@ -65,7 +65,6 @@ private slots:
|
||||
void testHelp();
|
||||
void testInteractiveCommands();
|
||||
void testList();
|
||||
void testLocate();
|
||||
void testMerge();
|
||||
void testMergeWithKeys();
|
||||
void testMove();
|
||||
@@ -73,6 +72,7 @@ private slots:
|
||||
void testRemove();
|
||||
void testRemoveGroup();
|
||||
void testRemoveQuiet();
|
||||
void testSearch();
|
||||
void testShow();
|
||||
void testInvalidDbFiles();
|
||||
void testYubiKeyOption();
|
||||
|
||||
@@ -690,66 +690,6 @@ void TestGroup::testPrint()
|
||||
QVERIFY(output.contains(QString("subgroup/entry3\n")));
|
||||
}
|
||||
|
||||
void TestGroup::testLocate()
|
||||
{
|
||||
Database* db = new Database();
|
||||
|
||||
Entry* entry1 = new Entry();
|
||||
entry1->setTitle("entry1");
|
||||
entry1->setGroup(db->rootGroup());
|
||||
|
||||
Entry* entry2 = new Entry();
|
||||
entry2->setTitle("entry2");
|
||||
entry2->setGroup(db->rootGroup());
|
||||
|
||||
Group* group1 = new Group();
|
||||
group1->setName("group1");
|
||||
group1->setParent(db->rootGroup());
|
||||
|
||||
Group* group2 = new Group();
|
||||
group2->setName("group2");
|
||||
group2->setParent(group1);
|
||||
|
||||
Entry* entry3 = new Entry();
|
||||
entry3->setTitle("entry3");
|
||||
entry3->setGroup(group1);
|
||||
|
||||
Entry* entry43 = new Entry();
|
||||
entry43->setTitle("entry43");
|
||||
entry43->setGroup(group1);
|
||||
|
||||
Entry* google = new Entry();
|
||||
google->setTitle("Google");
|
||||
google->setGroup(group2);
|
||||
|
||||
QStringList results = db->rootGroup()->locate("entry");
|
||||
QVERIFY(results.size() == 4);
|
||||
QVERIFY(results.contains("/group1/entry43"));
|
||||
|
||||
results = db->rootGroup()->locate("entry1");
|
||||
QVERIFY(results.size() == 1);
|
||||
QVERIFY(results.contains("/entry1"));
|
||||
|
||||
results = db->rootGroup()->locate("Entry1");
|
||||
QVERIFY(results.size() == 1);
|
||||
QVERIFY(results.contains("/entry1"));
|
||||
|
||||
results = db->rootGroup()->locate("invalid");
|
||||
QVERIFY(results.isEmpty());
|
||||
|
||||
results = db->rootGroup()->locate("google");
|
||||
QVERIFY(results.size() == 1);
|
||||
QVERIFY(results.contains("/group1/group2/Google"));
|
||||
|
||||
results = db->rootGroup()->locate("group1");
|
||||
QVERIFY(results.size() == 3);
|
||||
QVERIFY(results.contains("/group1/entry3"));
|
||||
QVERIFY(results.contains("/group1/entry43"));
|
||||
QVERIFY(results.contains("/group1/group2/Google"));
|
||||
|
||||
delete db;
|
||||
}
|
||||
|
||||
void TestGroup::testAddEntryWithPath()
|
||||
{
|
||||
Database* db = new Database();
|
||||
|
||||
@@ -39,7 +39,6 @@ private slots:
|
||||
void testFindEntry();
|
||||
void testFindGroupByPath();
|
||||
void testPrint();
|
||||
void testLocate();
|
||||
void testAddEntryWithPath();
|
||||
void testIsRecycled();
|
||||
void testCopyDataFrom();
|
||||
|
||||
Reference in New Issue
Block a user