1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <utils/algorithm.h>
#include <utils/environment.h>
#include <QTest>
using namespace Utils;
Q_DECLARE_METATYPE(Utils::OsType)
class tst_Environment : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void environment_data();
void environment();
void environmentSetup_data();
void environmentSetup();
void environmentSetWindows();
void environmentSetWindowsFuzzy();
void environmentSetUnix();
void environmentSetNewWindows();
void environmentSetNewUnix();
void environmentUnsetWindows();
void environmentUnsetWindowsFuzzy();
void environmentUnsetUnix();
void environmentUnsetUnknownWindows();
void environmentUnsetUnknownUnix();
void expansion_data();
void expansion();
void incrementalChanges();
void pathChanges_data();
void pathChanges();
void find_data();
void find();
private:
Environment env;
};
void tst_Environment::initTestCase()
{
env.set("word", "hi");
}
void tst_Environment::environment_data()
{
QTest::addColumn<QString>("in");
QTest::addColumn<QString>("out");
static const struct {
const char * const in;
const char * const out;
} vals[] = {
#ifdef Q_OS_WIN
{"", ""},
{"hi", "hi"},
{"hi%", "hi%"},
{"hi% ho", "hi% ho"},
{"hi%here ho", "hi%here ho"},
{"hi%here%ho", "hi%here%ho"},
{"hi%word%", "hihi"},
{"hi%foo%word%", "hi%foohi"},
{"%word%word%ho", "hiword%ho"},
{"hi%word%x%word%ho", "hihixhiho"},
{"hi%word%xx%word%ho", "hihixxhiho"},
{"hi%word%%word%ho", "hihihiho"},
#else
{"", ""},
{"hi", "hi"},
{"hi$", "hi$"},
{"hi${", "hi${"},
{"hi${word", "hi${word"},
{"hi${word}", "hihi"},
{"hi${word}ho", "hihiho"},
{"hi$wo", "hi$wo"},
{"hi$word", "hihi"},
{"hi$word ho", "hihi ho"},
{"$word", "hi"},
{"hi${word}$word", "hihihi"},
#endif
};
for (unsigned i = 0; i < sizeof(vals)/sizeof(vals[0]); i++)
QTest::newRow(vals[i].in) << QString::fromLatin1(vals[i].in)
<< QString::fromLatin1(vals[i].out);
}
void tst_Environment::environment()
{
QFETCH(QString, in);
QFETCH(QString, out);
QCOMPARE(env.expandVariables(in), out);
}
void tst_Environment::environmentSetup_data()
{
QTest::addColumn<int>("osType");
QTest::addColumn<QStringList>("in");
QTest::addColumn<QStringList>("out");
QTest::newRow("EmptyWin")
<< static_cast<int>(Utils::OsTypeWindows) << QStringList() << QStringList();
QTest::newRow("EmptyLinux")
<< static_cast<int>(Utils::OsTypeLinux) << QStringList() << QStringList();
QTest::newRow("SimpleWin")
<< static_cast<int>(Utils::OsTypeWindows) << QStringList({"Foo=bar"}) << QStringList({"Foo=bar"});
QTest::newRow("EmptyLinux")
<< static_cast<int>(Utils::OsTypeLinux) << QStringList({"Foo=bar"}) << QStringList({"Foo=bar"});
QTest::newRow("MultiWin")
<< static_cast<int>(Utils::OsTypeWindows)
<< QStringList({"Foo=bar", "Hi=HO"}) << QStringList({"Foo=bar", "Hi=HO"});
QTest::newRow("MultiLinux")
<< static_cast<int>(Utils::OsTypeLinux)
<< QStringList({"Foo=bar", "Hi=HO"}) << QStringList({"Foo=bar", "Hi=HO"});
QTest::newRow("DuplicateWin")
<< static_cast<int>(Utils::OsTypeWindows)
<< QStringList({"Foo=bar", "FOO=HO"}) << QStringList({"Foo=HO"});
QTest::newRow("DuplicateLinux")
<< static_cast<int>(Utils::OsTypeLinux)
<< QStringList({"Foo=bar", "FOO=HO"}) << QStringList({"FOO=HO", "Foo=bar"});
}
void tst_Environment::environmentSetup()
{
QFETCH(int, osType);
QFETCH(QStringList, in);
QFETCH(QStringList, out);
Environment env(in, static_cast<Utils::OsType>(osType));
QCOMPARE(env.toStringList(), out);
}
void tst_Environment::environmentSetWindows()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeWindows);
env.set("Foo", "baz");
QCOMPARE(env.toStringList(), QStringList({"Foo=baz", "Hi=HO"}));
}
void tst_Environment::environmentSetWindowsFuzzy()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeWindows);
env.set("FOO", "baz");
QCOMPARE(env.toStringList(), QStringList({"Foo=baz", "Hi=HO"}));
}
void tst_Environment::environmentSetUnix()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeLinux);
env.set("Foo", "baz");
QCOMPARE(env.toStringList(), QStringList({"Foo=baz", "Hi=HO"}));
}
void tst_Environment::environmentSetNewWindows()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeWindows);
env.set("bar", "baz");
QCOMPARE(env.toStringList(), QStringList({"bar=baz", "Foo=bar", "Hi=HO"}));
}
void tst_Environment::environmentSetNewUnix()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeLinux);
env.set("bar", "baz");
QCOMPARE(env.toStringList(), QStringList({"Foo=bar", "Hi=HO", "bar=baz"}));
}
void tst_Environment::environmentUnsetWindows()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeWindows);
env.unset("Foo");
QCOMPARE(env.toStringList(), QStringList({"Hi=HO"}));
}
void tst_Environment::environmentUnsetWindowsFuzzy()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeWindows);
env.unset("FOO");
QCOMPARE(env.toStringList(), QStringList({"Hi=HO"}));
}
void tst_Environment::environmentUnsetUnix()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeLinux);
env.unset("Foo");
QCOMPARE(env.toStringList(), QStringList({"Hi=HO"}));
}
void tst_Environment::environmentUnsetUnknownWindows()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeWindows);
env.unset("baz");
QCOMPARE(env.toStringList(), QStringList({"Foo=bar", "Hi=HO"}));
}
void tst_Environment::environmentUnsetUnknownUnix()
{
Environment env(QStringList({"Foo=bar", "Hi=HO"}), Utils::OsTypeLinux);
env.unset("baz");
QCOMPARE(env.toStringList(), QStringList({"Foo=bar", "Hi=HO"}));
}
void tst_Environment::expansion_data()
{
QTest::addColumn<Utils::OsType>("osType");
QTest::addColumn<QString>("eu");
QTest::addColumn<QString>("ew");
QTest::newRow("win") << Utils::OsTypeWindows << "${v}" << "blubb";
QTest::newRow("lin") << Utils::OsTypeLinux << "blubb" << "%v%";
}
void tst_Environment::expansion()
{
QFETCH(Utils::OsType, osType);
QFETCH(QString, eu);
QFETCH(QString, ew);
const Environment env(QStringList{"eu=${v}", "ew=%v%", "v=blubb"}, osType);
QCOMPARE(env.expandedValueForKey("eu"), eu);
QCOMPARE(env.expandedValueForKey("ew"), ew);
}
void tst_Environment::incrementalChanges()
{
const Environment origEnv({{"VAR1", "VALUE1"}, {"VAR2", "VALUE2"}, {"PATH", "/usr/bin"}});
const EnvironmentItems changes({
{"VAR1", QString(), EnvironmentItem::Comment},
{"VAR1", QString(), EnvironmentItem::Unset},
{"VAR2", "VALUE2", EnvironmentItem::SetDisabled},
{"PATH+=/bin", QString(), EnvironmentItem::Comment},
{"PATH", "/usr/local/bin", EnvironmentItem::Append},
{"PATH", "/tmp", EnvironmentItem::Prepend},
{"PATH=/opt/bin", QString(), EnvironmentItem::Comment}});
const QStringList changesStringList = EnvironmentItem::toStringList(changes);
const int comments = Utils::count(changesStringList,
[](const QString &line) { return line.startsWith("##"); });
QCOMPARE(comments, 3);
// Check values after change application.
Environment newEnv = origEnv;
newEnv.modify(changes);
QVERIFY(!newEnv.hasKey("VAR1"));
QCOMPARE(newEnv.value("VAR2"), QString());
Environment::FindResult res = newEnv.find("VAR2");
QCOMPARE(res->value, "VALUE2");
QVERIFY(!res->enabled);
const QChar sep = HostOsInfo::pathListSeparator();
QCOMPARE(newEnv.value("PATH"),
QString("/tmp").append(sep).append("/usr/bin").append(sep).append("/usr/local/bin"));
// Check apply/diff round-trips.
const EnvironmentItems diff = origEnv.diff(newEnv);
const EnvironmentItems reverseDiff = newEnv.diff(origEnv);
Environment newEnv2 = origEnv;
newEnv2.modify(diff);
QCOMPARE(newEnv, newEnv2);
newEnv2.modify(reverseDiff);
QCOMPARE(newEnv2, origEnv);
// Check conversion round-trips.
QCOMPARE(EnvironmentItem::fromStringList(changesStringList), changes);
QCOMPARE(EnvironmentItem::fromStringList(EnvironmentItem::toStringList(diff)), diff);
QCOMPARE(EnvironmentItem::fromStringList(EnvironmentItem::toStringList(reverseDiff)), reverseDiff);
QCOMPARE(EnvironmentItem::itemsFromVariantList(EnvironmentItem::toVariantList(changes)), changes);
QCOMPARE(EnvironmentItem::itemsFromVariantList(EnvironmentItem::toVariantList(diff)), diff);
QCOMPARE(EnvironmentItem::itemsFromVariantList(EnvironmentItem::toVariantList(reverseDiff)),
reverseDiff);
}
void tst_Environment::pathChanges_data()
{
const Environment origEnvLinux({"PATH=/bin:/usr/bin", "VAR=VALUE"}, OsTypeLinux);
const Environment origEnvWin({"PATH=C:\\Windows\\System32;D:\\gnu\\bin", "VAR=VALUE"}, OsTypeWindows);
QTest::addColumn<Environment>("environment");
QTest::addColumn<bool>("prepend"); // if false => append
QTest::addColumn<QString>("variable");
QTest::addColumn<QString>("value");
QTest::addColumn<Environment>("expected");
QTest::newRow("appendOrSetPath existingLeading Unix")
<< origEnvLinux << false << "PATH" << "/bin"
<< Environment({"PATH=/bin:/usr/bin:/bin", "VAR=VALUE"}, OsTypeLinux);
QTest::newRow("appendOrSetPath existingLeading Win")
<< origEnvWin << false << "PATH" << "C:\\Windows\\System32"
<< Environment({"PATH=C:\\Windows\\System32;D:\\gnu\\bin;C:\\Windows\\System32",
"VAR=VALUE"}, OsTypeWindows);
QTest::newRow("appendOrSetPath existingTrailing Unix")
<< origEnvLinux << false << "PATH" << "/usr/bin"
<< Environment({"PATH=/bin:/usr/bin", "VAR=VALUE"}, OsTypeLinux);
QTest::newRow("appendOrSetPath existingTrailing Win")
<< origEnvWin << false << "PATH" << "D:\\gnu\\bin"
<< Environment({"PATH=C:\\Windows\\System32;D:\\gnu\\bin",
"VAR=VALUE"}, OsTypeWindows);
QTest::newRow("prependOrSetPath existingLeading Unix")
<< origEnvLinux << true << "PATH" << "/bin"
<< Environment({"PATH=/bin:/usr/bin", "VAR=VALUE"}, OsTypeLinux);
QTest::newRow("prependOrSetPath existingLeading Win")
<< origEnvWin << true << "PATH" << "C:\\Windows\\System32"
<< Environment({"PATH=C:\\Windows\\System32;D:\\gnu\\bin",
"VAR=VALUE"}, OsTypeWindows);
QTest::newRow("prependOrSetPath existingTrailing Unix")
<< origEnvLinux << true << "PATH" << "/usr/bin"
<< Environment({"PATH=/usr/bin:/bin:/usr/bin", "VAR=VALUE"}, OsTypeLinux);
QTest::newRow("prependOrSetPath existingTrailing Win")
<< origEnvWin << true << "PATH" << "D:\\gnu\\bin"
<< Environment({"PATH=D:\\gnu\\bin;C:\\Windows\\System32;D:\\gnu\\bin",
"VAR=VALUE"}, OsTypeWindows);
QTest::newRow("appendOrSetPath non-existing Unix")
<< origEnvLinux << false << "PATH" << "/opt"
<< Environment({"PATH=/bin:/usr/bin:/opt", "VAR=VALUE"}, OsTypeLinux);
QTest::newRow("appendOrSetPath non-existing Win")
<< origEnvWin << false << "PATH" << "C:\\Windows"
<< Environment({"PATH=C:\\Windows\\System32;D:\\gnu\\bin;C:\\Windows",
"VAR=VALUE"}, OsTypeWindows);
QTest::newRow("prependOrSetPath non-existing half-matching Unix")
<< origEnvLinux << true << "PATH" << "/bi"
<< Environment({"PATH=/bi:/bin:/usr/bin", "VAR=VALUE"}, OsTypeLinux);
QTest::newRow("prependOrSetPath non-existing half-matching Win")
<< origEnvWin << true << "PATH" << "C:\\Windows"
<< Environment({"PATH=C:\\Windows;C:\\Windows\\System32;D:\\gnu\\bin",
"VAR=VALUE"}, OsTypeWindows);
}
void tst_Environment::pathChanges()
{
QFETCH(Environment, environment);
QFETCH(bool, prepend);
QFETCH(QString, variable);
QFETCH(QString, value);
QFETCH(Environment, expected);
if (prepend)
environment.prependOrSet(variable, value);
else
environment.appendOrSet(variable, value);
const bool envEqualsExpected = environment == expected;
if (!envEqualsExpected) {
qDebug() << "Actual :" << environment.toStringList();
qDebug() << "Expected:" << expected.toStringList();
}
QVERIFY(envEqualsExpected);
}
void tst_Environment::find_data()
{
QTest::addColumn<Utils::OsType>("osType");
QTest::addColumn<bool>("contains");
QTest::addColumn<QString>("variable");
QTest::newRow("win") << Utils::OsTypeWindows << true << "foo";
QTest::newRow("win") << Utils::OsTypeWindows << true << "Foo";
QTest::newRow("lin") << Utils::OsTypeLinux << true << "Foo";
QTest::newRow("lin") << Utils::OsTypeLinux << false << "foo";
}
void tst_Environment::find()
{
QFETCH(Utils::OsType, osType);
QFETCH(bool, contains);
QFETCH(QString, variable);
Environment env(QStringList({"Foo=bar", "Hi=HO"}), osType);
Environment::FindResult res = env.find(variable);
QCOMPARE(bool(res), contains);
if (contains)
QCOMPARE(res->value, QString("bar"));
}
QTEST_GUILESS_MAIN(tst_Environment)
#include "tst_environment.moc"
|