diff --git a/addons/externaltools/autotests/CMakeLists.txt b/addons/externaltools/autotests/CMakeLists.txt index bf1eda847..d3fbc9966 100644 --- a/addons/externaltools/autotests/CMakeLists.txt +++ b/addons/externaltools/autotests/CMakeLists.txt @@ -1,10 +1,11 @@ include(ECMMarkAsTest) # Project Plugin add_executable(externaltools_test externaltooltest.cpp ../kateexternaltool.cpp + ../katetoolrunner.cpp ) add_test(plugin-externaltools_test externaltools_test) target_link_libraries(externaltools_test kdeinit_kate Qt5::Test) ecm_mark_as_test(externaltools_test) diff --git a/addons/externaltools/autotests/externaltooltest.cpp b/addons/externaltools/autotests/externaltooltest.cpp index 55afda896..d80ebd7f4 100644 --- a/addons/externaltools/autotests/externaltooltest.cpp +++ b/addons/externaltools/autotests/externaltooltest.cpp @@ -1,69 +1,86 @@ /* This file is part of the KDE project * * Copyright 2019 Dominik Haumann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "externaltooltest.h" #include "../kateexternaltool.h" +#include "../katetoolrunner.h" #include #include #include #include QTEST_MAIN(ExternalToolTest) void ExternalToolTest::initTestCase() { } void ExternalToolTest::cleanupTestCase() { } void ExternalToolTest::testLoadSave() { KConfig config; KConfigGroup cg(&config, "tool"); KateExternalTool tool; tool.name = QStringLiteral("git cola"); tool.icon = QStringLiteral("git-cola"); tool.executable = QStringLiteral("git-cola"); tool.arguments = QStringLiteral("none"); tool.command = QStringLiteral("git-cola"); tool.mimetypes = QStringList{ QStringLiteral("everything") }; tool.hasexec = true; tool.actionName = QStringLiteral("asdf"); tool.cmdname = QStringLiteral("git-cola"); tool.saveMode = KateExternalTool::SaveMode::None; tool.save(cg); KateExternalTool copiedTool; copiedTool.load(cg); QCOMPARE(tool.name, copiedTool.name); } -void ExternalToolTest::testToolRunner() +void ExternalToolTest::testRunListDirectory() { -// QCOMPARE(FileUtil::commonParent(QLatin1String("~/dev/proj1"), QLatin1String("~/dev/proj222")), QLatin1String("~/dev/")); + KateExternalTool tool; + tool.name = QStringLiteral("ls"); + tool.icon = QStringLiteral("none"); + tool.executable = QStringLiteral("ls"); + tool.arguments = QStringLiteral("/"); + tool.command = QStringLiteral("ls"); + tool.mimetypes = QStringList{}; + tool.hasexec = true; + tool.actionName = QStringLiteral("ls"); + tool.cmdname = QStringLiteral("ls"); + tool.saveMode = KateExternalTool::SaveMode::None; + + KateToolRunner runner(&tool); + runner.run(); + runner.waitForFinished(); + qDebug() << runner.stdoutData(); + QVERIFY(!runner.stdoutData().isEmpty()); } // kate: space-indent on; indent-width 4; replace-tabs on; diff --git a/addons/externaltools/autotests/externaltooltest.h b/addons/externaltools/autotests/externaltooltest.h index 910f4938d..8d4fd69f5 100644 --- a/addons/externaltools/autotests/externaltooltest.h +++ b/addons/externaltools/autotests/externaltooltest.h @@ -1,41 +1,41 @@ /* This file is part of the KDE project * * Copyright 2019 Dominik Haumann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KATE_TOOLRUNNER_TEST_H #define KATE_TOOLRUNNER_TEST_H #include class ExternalToolTest : public QObject { Q_OBJECT public Q_SLOTS: void initTestCase(); void cleanupTestCase(); private Q_SLOTS: void testLoadSave(); - void testToolRunner(); + void testRunListDirectory(); }; #endif // kate: space-indent on; indent-width 4; replace-tabs on; diff --git a/addons/externaltools/katetoolrunner.cpp b/addons/externaltools/katetoolrunner.cpp index 812ac95b8..1a8b57f7a 100644 --- a/addons/externaltools/katetoolrunner.cpp +++ b/addons/externaltools/katetoolrunner.cpp @@ -1,36 +1,67 @@ /* This file is part of the KDE project * * Copyright 2019 Dominik Haumann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "katetoolrunner.h" +#include "kateexternaltool.h" + KateToolRunner::KateToolRunner(KateExternalTool * tool) : m_tool(tool) + , m_process(new QProcess()) { } KateToolRunner::~KateToolRunner() { + delete m_process; + m_process = nullptr; } void KateToolRunner::run() +{ + m_process->setProcessChannelMode(QProcess::MergedChannels); + + QObject::connect(m_process, &QProcess::readyRead, this, &KateToolRunner::slotReadyRead); + QObject::connect(m_process, static_cast(&QProcess::finished), this, &KateToolRunner::toolFinished); + + m_process->start(m_tool->executable, { m_tool->arguments }); +} + +void KateToolRunner::waitForFinished() +{ + m_process->waitForFinished(); +} + + +QString KateToolRunner::stdoutData() const +{ + return QString::fromLocal8Bit(m_output ); +} + +void KateToolRunner::slotReadyRead() +{ + m_output += m_process->readAll(); +} + +void KateToolRunner::toolFinished(int exitCode, QProcess::ExitStatus exitStatus) { } // kate: space-indent on; indent-width 4; replace-tabs on; diff --git a/addons/externaltools/katetoolrunner.h b/addons/externaltools/katetoolrunner.h index 724ffa90c..b619ba969 100644 --- a/addons/externaltools/katetoolrunner.h +++ b/addons/externaltools/katetoolrunner.h @@ -1,48 +1,69 @@ /* This file is part of the KDE project * * Copyright 2019 Dominik Haumann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KTEXTEDITOR_EXTERNALTOOLRUNNER_H #define KTEXTEDITOR_EXTERNALTOOLRUNNER_H +#include +#include +#include +#include + class KateExternalTool; class QProcess; /** * Helper class to run a KateExternalTool. */ -class KateToolRunner +class KateToolRunner : public QObject { public: KateToolRunner(KateExternalTool * tool); KateToolRunner(const KateToolRunner &) = delete; void operator=(const KateToolRunner &) = delete; ~KateToolRunner(); void run(); + void waitForFinished(); + QString stdoutData() const; + +private Q_SLOTS: + /** + * More tool output is available + */ + void slotReadyRead(); + + /** + * Analysis finished + * @param exitCode analyzer process exit code + * @param exitStatus analyzer process exit status + */ + void toolFinished(int exitCode, QProcess::ExitStatus exitStatus); private: KateExternalTool * m_tool; QProcess * m_process = nullptr; + QByteArray m_output; }; #endif // KTEXTEDITOR_EXTERNALTOOLRUNNER_H // kate: space-indent on; indent-width 4; replace-tabs on;