diff --git a/src/gui/app.cpp b/src/gui/app.cpp index 415fb2c32..77d31e735 100644 --- a/src/gui/app.cpp +++ b/src/gui/app.cpp @@ -79,7 +79,7 @@ App::App(int &argc, char ** argv) noexcept } } - processCommandlineOptions(); + processCommandlineOptions(m_parser, arguments()); } bool App::event(QEvent *event) noexcept @@ -149,13 +149,13 @@ void App::showUi() noexcept /// /// When appropriate this function will call QCommandLineParser::showHelp() /// terminating the program. -void App::processCommandlineOptions() noexcept +void App::processCommandlineOptions(QCommandLineParser& parser, QStringList arguments) noexcept { - m_parser.addOption(QCommandLineOption("nvim", + parser.addOption(QCommandLineOption("nvim", QCoreApplication::translate("main", "nvim executable path"), QCoreApplication::translate("main", "nvim_path"), "nvim")); - m_parser.addOption(QCommandLineOption("timeout", + parser.addOption(QCommandLineOption("timeout", QCoreApplication::translate("main", "Error if nvim does not responde after count milliseconds"), QCoreApplication::translate("main", "ms"), "20000")); @@ -163,83 +163,83 @@ void App::processCommandlineOptions() noexcept // Some platforms use --qwindowgeometry, while other platforms use the --geometry. // Make the correct help message is displayed. if (hasGeometryArg()) { - m_parser.addOption(QCommandLineOption("geometry", + parser.addOption(QCommandLineOption("geometry", QCoreApplication::translate("main", "Set initial window geometry"), QCoreApplication::translate("main", "width>xx 1) { qWarning() << "Options --server, --spawn and --embed are mutually exclusive\n"; ::exit(-1); } - if (!m_parser.positionalArguments().isEmpty() && - (m_parser.isSet("embed") || m_parser.isSet("server"))) { + if (!parser.positionalArguments().isEmpty() && + (parser.isSet("embed") || parser.isSet("server"))) { qWarning() << "--embed and --server do not accept positional arguments\n"; ::exit(-1); } - if (m_parser.positionalArguments().isEmpty() && m_parser.isSet("spawn")) { + if (parser.positionalArguments().isEmpty() && parser.isSet("spawn")) { qWarning() << "--spawn requires at least one positional argument\n"; ::exit(-1); } bool valid_timeout; - int timeout_opt = m_parser.value("timeout").toInt(&valid_timeout); + int timeout_opt = parser.value("timeout").toInt(&valid_timeout); if (!valid_timeout || timeout_opt <= 0) { - qWarning() << "Invalid argument for --timeout" << m_parser.value("timeout"); + qWarning() << "Invalid argument for --timeout" << parser.value("timeout"); ::exit(-1); } } @@ -337,13 +337,13 @@ static QString GetNeovimVersionInfo(const QString& nvim) noexcept return nvimproc.readAllStandardOutput(); } -void App::showVersionInfo() noexcept +void App::showVersionInfo(QCommandLineParser& parser) noexcept { QString versionInfo; QTextStream out{ &versionInfo }; - const QString nvimExecutable { (m_parser.isSet("nvim")) ? - m_parser.value("nvim") : "nvim" }; + const QString nvimExecutable { (parser.isSet("nvim")) ? + parser.value("nvim") : "nvim" }; out << "NVIM-QT v" << PROJECT_VERSION << endl; out << "Build type: " << CMAKE_BUILD_TYPE << endl; diff --git a/src/gui/app.h b/src/gui/app.h index 0fef09f42..c8f978c17 100644 --- a/src/gui/app.h +++ b/src/gui/app.h @@ -21,14 +21,15 @@ class App: public QApplication bool event(QEvent *event) noexcept; void showUi() noexcept; void connectToRemoteNeovim() noexcept; - void checkArgumentsMayTerminate() noexcept; + QCommandLineParser& commandLineParser() { return m_parser; } + static void checkArgumentsMayTerminate(QCommandLineParser&) noexcept; + static void processCommandlineOptions(QCommandLineParser&, QStringList) noexcept; private: - QString getRuntimePath() noexcept; - QStringList getNeovimArgs() noexcept; - void processCommandlineOptions() noexcept; + static QString getRuntimePath() noexcept; + static QStringList getNeovimArgs() noexcept; void setupRequestTimeout() noexcept; - void showVersionInfo() noexcept; + static void showVersionInfo(QCommandLineParser&) noexcept; QCommandLineParser m_parser; std::shared_ptr m_connector; diff --git a/src/gui/main.cpp b/src/gui/main.cpp index 3542ebef0..7017cb49a 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -30,7 +30,7 @@ int ui_main(int argc, char **argv) NeovimQt::App app(argc, argv); - app.checkArgumentsMayTerminate(); + app.checkArgumentsMayTerminate(app.commandLineParser()); app.connectToRemoteNeovim(); @@ -51,9 +51,10 @@ int cli_main(int argc, char **argv) argsNoFork << argv[i]; } - NeovimQt::App app(argc, argv); - - app.checkArgumentsMayTerminate(); + QCoreApplication app(argc, argv); + QCommandLineParser p; + NeovimQt::App::processCommandlineOptions(p, app.arguments()); + NeovimQt::App::checkArgumentsMayTerminate(p); if (!QProcess::startDetached(app.applicationFilePath(), argsNoFork)) { qWarning() << "Unable to fork into background"; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a60b91b2a..56db548da 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,7 @@ set(QTLIBS Qt5::Network Qt5::Svg Qt5::Test) include_directories(${CMAKE_SOURCE_DIR}/src) add_definitions(-DCMAKE_SOURCE_DIR=\"${CMAKE_SOURCE_DIR}\") +add_definitions(-DNVIM_QT_BINARY=\"$\") if (WIN32 AND USE_STATIC_QT) add_definitions(-DUSE_STATIC_QT) endif () @@ -37,6 +38,7 @@ add_xtest(tst_callallmethods) add_xtest(tst_encoding) add_xtest(tst_msgpackiodevice) add_xtest_gui(tst_shell) +add_xtest_gui(tst_main) # Platform Specific Input Tests add_xtest(tst_input_mac diff --git a/test/tst_main.cpp b/test/tst_main.cpp new file mode 100644 index 000000000..998b37f28 --- /dev/null +++ b/test/tst_main.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +namespace NeovimQt { + +class Test: public QObject +{ + Q_OBJECT +private slots: + + void runsWithoutDISPLAY_data() { + QTest::addColumn("arguments"); + QTest::addColumn("expected_exitcode"); + + QTest::newRow("--help") << QStringList({NVIM_QT_BINARY, "--help"}) << 0; + QTest::newRow("--version") << QStringList({NVIM_QT_BINARY, "--help"}) << 0; + } + + void runsWithoutDISPLAY() { + QFETCH(QStringList, arguments); + QFETCH(int, expected_exitcode); + + QProcess p; + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + env.remove("DISPLAY"); + p.setProcessEnvironment(env); + p.setProgram(NVIM_QT_BINARY); + p.setArguments(arguments); + p.start(); + bool finished = p.waitForFinished(); + auto status = p.exitStatus(); + int exit_code = p.exitCode(); + + qDebug() << finished << status << exit_code; + QCOMPARE(finished, true); + QCOMPARE(status, QProcess::NormalExit); + QCOMPARE(exit_code, expected_exitcode); + } + +protected: +}; + +} // Namespace NeovimQt +QTEST_MAIN(NeovimQt::Test) +#include "tst_main.moc"