AutoTest: Improve options and environment variable handling

Filter out all interfering options and environment variables when
running tests.

Task-number: QTCREATORBUG-21169
Change-Id: I0cac36907d79aa74ac96873f868eb7a7a3666794
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2019-05-22 13:55:31 +02:00
parent d1e00284a8
commit 561d857c35

View File

@@ -51,43 +51,70 @@ TestOutputReader *BoostTestConfiguration::outputReader(const QFutureInterface<Te
settings->logLevel, settings->reportLevel); settings->logLevel, settings->reportLevel);
} }
enum class InterferingType { Options, EnvironmentVariables };
static QStringList interfering(InterferingType type)
{
const QStringList knownInterfering { "log_level", "log_format", "log_sink",
"report_level", "report_format", "report_sink",
"output_format", "color_output", "no_color_output",
"catch_system_errors", "no_catch_system_errors",
"detect_fp_exceptions", "no_detect_fp_exceptions",
"detect_memory_leaks", "random", "run_test",
"show_progress", "result_code", "no_result_code",
"help", "list_content", "list_labels", "version"
};
switch (type) {
case InterferingType::Options:
return Utils::transform(knownInterfering, [](const QString &item) {
return QString("--" + item);
});
case InterferingType::EnvironmentVariables:
return Utils::transform(knownInterfering, [](const QString &item) {
return QString("BOOST_TEST_" + item).toUpper();
});
}
return QStringList();
}
static QStringList filterInterfering(const QStringList &provided, QStringList *omitted) static QStringList filterInterfering(const QStringList &provided, QStringList *omitted)
{ {
// TODO complete these... const QStringList knownInterfering = interfering(InterferingType::Options);
const QSet<QString> knownInterfering { "--log_level=", const QString interferingSingleWithParam = "efklortcpsx?";
"--report_level=", QStringList allowed;
"--color_output=", "--no_color_output", bool filterNextArg = false;
"--catch_system_errors", "--detect_fp_exceptions", bool ignoreRest = false;
"--detect_memory_leaks", "--random=" for (auto arg : provided) {
}; bool filterArg = filterNextArg;
QSet<QString> allowed = Utils::filtered(provided.toSet(), [&knownInterfering](const QString &arg) { filterNextArg = false;
return Utils::allOf(knownInterfering, [&arg](const QString &interfering) { if (ignoreRest) {
return !arg.startsWith(interfering); allowed.append(arg);
}); continue;
}); }
bool interferes = false;
// TODO handle single letter options correctly if (filterArg && !arg.startsWith('-')) {
if (omitted) { interferes = true;
QSet<QString> providedSet = provided.toSet(); } else if (arg.startsWith("--")) {
providedSet.subtract(allowed); if (arg.size() == 2)
omitted->append(providedSet.toList()); ignoreRest = true;
else
interferes = knownInterfering.contains(arg.left(arg.indexOf('=')));
} else if (arg.startsWith('-') && arg.size() > 1) {
interferes = interferingSingleWithParam.contains(arg.at(1));
filterNextArg = interferes;
}
if (!interferes)
allowed.append(arg);
else if (omitted)
omitted->append(arg);
} }
return allowed.toList(); return allowed;
} }
QStringList BoostTestConfiguration::argumentsForTestRunner(QStringList *omitted) const QStringList BoostTestConfiguration::argumentsForTestRunner(QStringList *omitted) const
{ {
QStringList arguments;
if (AutotestPlugin::settings()->processArgs) {
arguments << filterInterfering(runnable().commandLineArguments.split(
' ', QString::SkipEmptyParts), omitted);
}
// TODO improve the test case gathering and arguments building to avoid too long command lines
for (const QString &test : testCases())
arguments << "-t" << test;
auto boostSettings = getBoostSettings(); auto boostSettings = getBoostSettings();
QStringList arguments;
arguments << "-l" << BoostTestSettings::logLevelToOption(boostSettings->logLevel); arguments << "-l" << BoostTestSettings::logLevelToOption(boostSettings->logLevel);
arguments << "-r" << BoostTestSettings::reportLevelToOption(boostSettings->reportLevel); arguments << "-r" << BoostTestSettings::reportLevelToOption(boostSettings->reportLevel);
arguments << "--no_color_output"; // ensure that colored output is not used as default arguments << "--no_color_output"; // ensure that colored output is not used as default
@@ -101,19 +128,24 @@ QStringList BoostTestConfiguration::argumentsForTestRunner(QStringList *omitted)
arguments << "--detect_fp_exceptions"; arguments << "--detect_fp_exceptions";
if (!boostSettings->memLeaks) if (!boostSettings->memLeaks)
arguments << "--detect_memory_leaks=0"; arguments << "--detect_memory_leaks=0";
// TODO improve the test case gathering and arguments building to avoid too long command lines
for (const QString &test : testCases())
arguments << "-t" << test;
if (AutotestPlugin::settings()->processArgs) {
arguments << filterInterfering(runnable().commandLineArguments.split(
' ', QString::SkipEmptyParts), omitted);
}
return arguments; return arguments;
} }
Utils::Environment BoostTestConfiguration::filteredEnvironment(const Utils::Environment &original) const Utils::Environment BoostTestConfiguration::filteredEnvironment(const Utils::Environment &original) const
{ {
// TODO complete these.. const QStringList interferingEnv = interfering(InterferingType::EnvironmentVariables);
const QStringList interfering{"BOOST_TEST_LOG_FORMAT", "BOOST_TEST_REPORT_FORMAT",
"BOOST_TEST_COLOR_OUTPUT", "BOOST_TEST_CATCH_SYSTEM_ERRORS",
"BOOST_TEST_DETECT_FP_EXCEPTIONS", "BOOST_TEST_RANDOM",
"BOOST_TEST_DETECT_MEMORY_LEAK"};
Utils::Environment result = original; Utils::Environment result = original;
for (const QString &key : interfering) for (const QString &key : interferingEnv)
result.unset(key); result.unset(key);
return result; return result;
} }