diff options
Diffstat (limited to 'tests/auto/qprocess')
-rw-r--r-- | tests/auto/qprocess/qprocess.pro | 1 | ||||
-rw-r--r-- | tests/auto/qprocess/testProcessEnvironment/main.cpp | 27 | ||||
-rw-r--r-- | tests/auto/qprocess/testProcessEnvironment/testProcessEnvironment.pro | 12 | ||||
-rw-r--r-- | tests/auto/qprocess/tst_qprocess.cpp | 106 |
4 files changed, 146 insertions, 0 deletions
diff --git a/tests/auto/qprocess/qprocess.pro b/tests/auto/qprocess/qprocess.pro index 4600f02..ed59f10 100644 --- a/tests/auto/qprocess/qprocess.pro +++ b/tests/auto/qprocess/qprocess.pro @@ -3,6 +3,7 @@ SUBDIRS = testProcessCrash \ testProcessEcho \ testProcessEcho2 \ testProcessEcho3 \ + testProcessEnvironment \ testProcessLoopback \ testProcessNormal \ testProcessOutput \ diff --git a/tests/auto/qprocess/testProcessEnvironment/main.cpp b/tests/auto/qprocess/testProcessEnvironment/main.cpp new file mode 100644 index 0000000..098d13e --- /dev/null +++ b/tests/auto/qprocess/testProcessEnvironment/main.cpp @@ -0,0 +1,27 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +****************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char **argv) +{ +#if defined(_WIN32_WCE) + // no environment in Windows CE + return 0; +#else + if (argc == 1) + return 1; + + char *env = getenv(argv[1]); + if (env) { + printf("%s", env); + return 0; + } + return 1; +#endif +} diff --git a/tests/auto/qprocess/testProcessEnvironment/testProcessEnvironment.pro b/tests/auto/qprocess/testProcessEnvironment/testProcessEnvironment.pro new file mode 100644 index 0000000..14ddae5 --- /dev/null +++ b/tests/auto/qprocess/testProcessEnvironment/testProcessEnvironment.pro @@ -0,0 +1,12 @@ +SOURCES = main.cpp +CONFIG -= qt +CONFIG += console +DESTDIR = ./ + +mac { + CONFIG -= app_bundle +} + +# no install rule for application used by test +INSTALLS = + diff --git a/tests/auto/qprocess/tst_qprocess.cpp b/tests/auto/qprocess/tst_qprocess.cpp index cb2b1bf..ef78c2e 100644 --- a/tests/auto/qprocess/tst_qprocess.cpp +++ b/tests/auto/qprocess/tst_qprocess.cpp @@ -124,6 +124,8 @@ private slots: void spaceArgsTest_data(); void spaceArgsTest(); void exitCodeTest(); + void setEnvironment_data(); + void setEnvironment(); void systemEnvironment(); void spaceInName(); void lockupsInStartDetached(); @@ -1617,13 +1619,117 @@ void tst_QProcess::removeFileWhileProcessIsRunning() } //----------------------------------------------------------------------------- +void tst_QProcess::setEnvironment_data() +{ + QTest::addColumn<QString>("name"); + QTest::addColumn<QString>("value"); + + QTest::newRow("setting-empty") << "tst_QProcess" << ""; + QTest::newRow("setting") << "tst_QProcess" << "value"; + +#ifdef Q_OS_WIN + QTest::newRow("unsetting") << "PROMPT" << QString(); + QTest::newRow("overriding") << "PROMPT" << "value"; +#else + QTest::newRow("unsetting") << "PATH" << QString(); + QTest::newRow("overriding") << "PATH" << "value"; +#endif +} + +void tst_QProcess::setEnvironment() +{ +#if !defined (Q_OS_WINCE) + // there is no concept of system variables on Windows CE as there is no console + + // make sure our environment variables are correct + QVERIFY(qgetenv("tst_QProcess").isEmpty()); + QVERIFY(!qgetenv("PATH").isEmpty()); +#ifdef Q_OS_WIN + QVERIFY(!qgetenv("PROMPT").isEmpty()); +#endif + + QFETCH(QString, name); + QFETCH(QString, value); + QString executable = QDir::currentPath() + "/testProcessEnvironment/testProcessEnvironment"; + + { + QProcess process; + QStringList environment = QProcess::systemEnvironment(); + if (value.isNull()) { + int pos; + QRegExp rx(name + "=.*"); +#ifdef Q_OS_WIN + rx.setCaseSensitivity(Qt::CaseInsensitive); +#endif + while ((pos = environment.indexOf(rx)) != -1) + environment.removeAt(pos); + } else { + environment.append(name + '=' + value); + } + process.setEnvironment(environment); + process.start(executable, QStringList() << name); + + QVERIFY(process.waitForFinished()); + if (value.isNull()) + QCOMPARE(process.exitCode(), 1); + else if (!value.isEmpty()) + QCOMPARE(process.exitCode(), 0); + + QCOMPARE(process.readAll(), value.toLocal8Bit()); + } + + // re-do the test but set the environment twice, to make sure + // that the latter addition overrides + // this test doesn't make sense in unsetting + if (!value.isNull()) { + QProcess process; + QStringList environment = QProcess::systemEnvironment(); + environment.prepend(name + "=This is not the right value"); + environment.append(name + '=' + value); + process.setEnvironment(environment); + process.start(executable, QStringList() << name); + + QVERIFY(process.waitForFinished()); + if (!value.isEmpty()) + QCOMPARE(process.exitCode(), 0); + + QCOMPARE(process.readAll(), value.toLocal8Bit()); + } + + // use the hash variant now + { + QProcess process; + QHash<QString, QString> environment = QProcess::systemEnvironmentHash(); + if (value.isNull()) + environment.remove(name); + else + environment.insert(name, value); + process.setEnvironmentHash(environment); + process.start(executable, QStringList() << name); + + QVERIFY(process.waitForFinished()); + if (value.isNull()) + QCOMPARE(process.exitCode(), 1); + else if (!value.isEmpty()) + QCOMPARE(process.exitCode(), 0); + + QCOMPARE(process.readAll(), value.toLocal8Bit()); + } +#endif +} +//----------------------------------------------------------------------------- void tst_QProcess::systemEnvironment() { #if defined (Q_OS_WINCE) // there is no concept of system variables on Windows CE as there is no console QVERIFY(QProcess::systemEnvironment().isEmpty()); + QVERIFY(QProcess::systemEnvironmentHash().isEmpty()); #else QVERIFY(!QProcess::systemEnvironment().isEmpty()); + QVERIFY(!QProcess::systemEnvironmentHash().isEmpty()); + + QVERIFY(QProcess::systemEnvironmentHash().contains("PATH")); + QVERIFY(!QProcess::systemEnvironment().filter(QRegExp("^PATH=", Qt::CaseInsensitive)).isEmpty()); #endif } |