File indexing completed on 2024-05-19 05:51:38

0001 /* This file is part of the KDE project
0002  *
0003  *  SPDX-FileCopyrightText: 2019 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "externaltooltest.h"
0009 #include "../kateexternaltool.h"
0010 #include "../katetoolrunner.h"
0011 
0012 #include <QString>
0013 #include <QTest>
0014 
0015 #include <KConfig>
0016 #include <KConfigGroup>
0017 
0018 QTEST_MAIN(ExternalToolTest)
0019 
0020 void ExternalToolTest::initTestCase()
0021 {
0022 }
0023 
0024 void ExternalToolTest::cleanupTestCase()
0025 {
0026 }
0027 
0028 void ExternalToolTest::testLoadSave()
0029 {
0030     KConfig config;
0031     KConfigGroup cg(&config, QStringLiteral("tool"));
0032 
0033     KateExternalTool tool;
0034     tool.category = QStringLiteral("Git Tools");
0035     tool.name = QStringLiteral("git cola");
0036     tool.icon = QStringLiteral("git-cola");
0037     tool.executable = QStringLiteral("git-cola");
0038     tool.arguments = QStringLiteral("none");
0039     tool.input = QStringLiteral("in");
0040     tool.workingDir = QStringLiteral("/usr/bin");
0041     tool.mimetypes = QStringList{QStringLiteral("everything")};
0042     tool.hasexec = true;
0043     tool.actionName = QStringLiteral("asdf");
0044     tool.cmdname = QStringLiteral("git-cola");
0045     tool.saveMode = KateExternalTool::SaveMode::None;
0046 
0047     tool.save(cg);
0048 
0049     KateExternalTool clonedTool;
0050     clonedTool.load(cg);
0051     QCOMPARE(tool, clonedTool);
0052 }
0053 
0054 void ExternalToolTest::testRunListDirectory()
0055 {
0056     // Skip, if 'ls' is not installed
0057     if (QStandardPaths::findExecutable(QStringLiteral("ls")).isEmpty()) {
0058         QSKIP("'ls' not found - skipping test");
0059     }
0060 
0061     std::unique_ptr<KateExternalTool> tool(new KateExternalTool());
0062     tool->category = QStringLiteral("Tools");
0063     tool->name = QStringLiteral("ls");
0064     tool->icon = QStringLiteral("none");
0065     tool->executable = QStringLiteral("ls");
0066     tool->arguments = QStringLiteral("/usr");
0067     tool->workingDir = QStringLiteral("/tmp");
0068     tool->mimetypes = QStringList{};
0069     tool->hasexec = true;
0070     tool->actionName = QStringLiteral("ls");
0071     tool->cmdname = QStringLiteral("ls");
0072     tool->saveMode = KateExternalTool::SaveMode::None;
0073     std::unique_ptr<KateExternalTool> tool2(new KateExternalTool(*tool));
0074 
0075     // 1. /tmp $ ls /usr
0076     KateToolRunner runner1(std::move(tool), nullptr);
0077     runner1.run();
0078     runner1.waitForFinished();
0079     QVERIFY(runner1.outputData().contains(QStringLiteral("bin")));
0080 
0081     // 2. /usr $ ls
0082     tool2->arguments.clear();
0083     tool2->workingDir = QStringLiteral("/usr");
0084     KateToolRunner runner2(std::move(tool2), nullptr);
0085     runner2.run();
0086     runner2.waitForFinished();
0087     QVERIFY(runner2.outputData().contains(QStringLiteral("bin")));
0088 
0089     // 1. and 2. must give the same result
0090     QCOMPARE(runner1.outputData(), runner2.outputData());
0091 }
0092 
0093 void ExternalToolTest::testRunTac()
0094 {
0095     // Skip, if 'tac' is not installed
0096     if (QStandardPaths::findExecutable(QStringLiteral("tac")).isEmpty()) {
0097         QSKIP("'tac' not found - skipping test");
0098     }
0099 
0100     std::unique_ptr<KateExternalTool> tool(new KateExternalTool());
0101     tool->name = QStringLiteral("tac");
0102     tool->executable = QStringLiteral("tac");
0103     tool->input = QStringLiteral("a\nb\nc\n");
0104     tool->saveMode = KateExternalTool::SaveMode::None;
0105 
0106     // run tac to reverse order
0107     KateToolRunner runner(std::move(tool), nullptr);
0108     runner.run();
0109     runner.waitForFinished();
0110     QCOMPARE(runner.outputData(), QStringLiteral("c\nb\na\n"));
0111 }
0112 
0113 #include "moc_externaltooltest.cpp"
0114 
0115 // kate: space-indent on; indent-width 4; replace-tabs on;