kprocesstest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. This file is part of the KDE libraries
  3. SPDX-FileCopyrightText: 2007 Oswald Buddenhagen <ossi@kde.org>
  4. SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
  5. SPDX-License-Identifier: LGPL-2.0-or-later
  6. */
  7. #include "kprocesstest_helper.h"
  8. #include <QFile>
  9. #include <QObject>
  10. #include <QStandardPaths>
  11. #include <QTest>
  12. #include <kprocess.h>
  13. #include <signal.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. class KProcessTest : public QObject
  17. {
  18. Q_OBJECT
  19. private Q_SLOTS:
  20. void test_channels();
  21. void test_setShellCommand();
  22. void test_inheritance();
  23. };
  24. // IOCCC nomination pending
  25. static QString callHelper(KProcess::OutputChannelMode how)
  26. {
  27. QProcess p;
  28. p.setProcessChannelMode(QProcess::MergedChannels);
  29. QString helper = QCoreApplication::applicationDirPath() + QStringLiteral("/kprocesstest_helper");
  30. #ifdef Q_OS_WIN
  31. helper += QStringLiteral(".exe");
  32. #endif
  33. Q_ASSERT(QFile::exists(helper));
  34. p.start(helper, QStringList() << QString::number(how) << QStringLiteral("--nocrashhandler"));
  35. p.waitForFinished();
  36. return QString::fromLatin1(p.readAllStandardOutput());
  37. }
  38. #define EO EOUT "\n"
  39. #define EE EERR "\n"
  40. #define TESTCHAN(me, ms, pout, rout, rerr) \
  41. e = QStringLiteral("mode: " ms "\n" POUT pout ROUT rout RERR rerr); \
  42. a = QStringLiteral("mode: " ms "\n") + callHelper(KProcess::me); \
  43. QCOMPARE(a, e)
  44. void KProcessTest::test_channels()
  45. {
  46. #ifdef Q_OS_UNIX
  47. QString e;
  48. QString a;
  49. TESTCHAN(SeparateChannels, "separate", "", EO, EE);
  50. TESTCHAN(ForwardedChannels, "forwarded", EO EE, "", "");
  51. TESTCHAN(OnlyStderrChannel, "forwarded stdout", EO, "", EE);
  52. TESTCHAN(OnlyStdoutChannel, "forwarded stderr", EE, EO, "");
  53. TESTCHAN(MergedChannels, "merged", "", EO EE, "");
  54. #else
  55. Q_UNUSED(callHelper);
  56. QSKIP("This test needs a UNIX system");
  57. #endif
  58. }
  59. void KProcessTest::test_setShellCommand()
  60. {
  61. // Condition copied from kprocess.cpp
  62. #if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__) && !defined(__GNU__)
  63. QSKIP("This test needs a free UNIX system");
  64. #else
  65. KProcess p;
  66. p.setShellCommand(QStringLiteral("cat"));
  67. QCOMPARE(p.program().count(), 1);
  68. QCOMPARE(p.program().at(0), QStandardPaths::findExecutable(QStringLiteral("cat")));
  69. QVERIFY(p.program().at(0).endsWith(QLatin1String("/cat")));
  70. p.setShellCommand(QStringLiteral("true || false"));
  71. QCOMPARE(p.program(), QStringList() << QStringLiteral("/bin/sh") << QStringLiteral("-c") << QString::fromLatin1("true || false"));
  72. #endif
  73. }
  74. void KProcessTest::test_inheritance()
  75. {
  76. KProcess kproc;
  77. QProcess *qproc = &kproc;
  78. const QString program = QStringLiteral("foobar");
  79. const QStringList arguments{QStringLiteral("meow")};
  80. kproc.setProgram(program, arguments);
  81. QCOMPARE(qproc->program(), program);
  82. QCOMPARE(qproc->arguments(), arguments);
  83. kproc.clearProgram();
  84. QCOMPARE(qproc->program(), QString());
  85. QCOMPARE(qproc->arguments(), QStringList());
  86. kproc << program << arguments;
  87. QCOMPARE(qproc->program(), program);
  88. QCOMPARE(qproc->arguments(), arguments);
  89. kproc.clearProgram();
  90. QCOMPARE(qproc->program(), QString());
  91. QCOMPARE(qproc->arguments(), QStringList());
  92. #ifdef Q_OS_UNIX
  93. kproc.setShellCommand(QStringLiteral("/bin/true meow"));
  94. QCOMPARE(qproc->program(), QStringLiteral("/bin/true"));
  95. QCOMPARE(qproc->arguments(), arguments);
  96. kproc.clearProgram();
  97. QCOMPARE(qproc->program(), QString());
  98. QCOMPARE(qproc->arguments(), QStringList());
  99. #endif
  100. }
  101. QTEST_MAIN(KProcessTest)
  102. #include "kprocesstest.moc"