testconfiguration.cpp 9.01 KB
Newer Older
1
2
/****************************************************************************
**
Christian Stenger's avatar
Christian Stenger committed
3
4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
Christian Stenger's avatar
Christian Stenger committed
6
** This file is part of Qt Creator.
7
**
Christian Stenger's avatar
Christian Stenger committed
8
9
10
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
11
** Software or, alternatively, in accordance with the terms contained in
Christian Stenger's avatar
Christian Stenger committed
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
Christian Stenger's avatar
Christian Stenger committed
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23
24
25
26
**
****************************************************************************/

#include "testconfiguration.h"
27
#include "testoutputreader.h"
28
29
#include "testrunconfiguration.h"
#include "testrunner.h"
30

31
#include <cpptools/cppmodelmanager.h>
Marco Bubke's avatar
Marco Bubke committed
32
#include <cpptools/projectinfo.h>
33

34
#include <projectexplorer/buildconfiguration.h>
35
36
#include <projectexplorer/buildtargetinfo.h>
#include <projectexplorer/environmentaspect.h>
37
38
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/runnables.h>
39
40
41
42
43
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>

using namespace ProjectExplorer;
44
45
46
47

namespace Autotest {
namespace Internal {

48
TestConfiguration::TestConfiguration()
49
50
51
52
53
54
55
56
{
}

TestConfiguration::~TestConfiguration()
{
    m_testCases.clear();
}

57
static bool isLocal(RunConfiguration *runConfiguration)
58
{
59
60
61
    Target *target = runConfiguration ? runConfiguration->target() : 0;
    Kit *kit = target ? target->kit() : 0;
    return DeviceTypeKitInformation::deviceTypeId(kit) == ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE;
62
63
}

64
void TestConfiguration::completeTestInformation(int runMode)
65
{
66
    QTC_ASSERT(!m_projectFile.isEmpty(), return);
67
68
69
70
71
72
73
74
75

    Project *project = SessionManager::startupProject();
    if (!project)
        return;

    Target *target = project->activeTarget();
    if (!target)
        return;

76
77
78
79
80
81
82
83
84
85
86
    const auto cppMM = CppTools::CppModelManager::instance();
    const QVector<CppTools::ProjectPart::Ptr> projectParts = cppMM->projectInfo(project).projectParts();
    const QVector<CppTools::ProjectPart::Ptr> relevantParts
            = Utils::filtered(projectParts, [this] (const CppTools::ProjectPart::Ptr &part) {
        return part->selectedForBuilding && part->projectFile == m_projectFile;
    });
    const QSet<QString> buildSystemTargets
            = Utils::transform<QSet>(relevantParts, [] (const CppTools::ProjectPart::Ptr &part) {
        return part->buildSystemTarget;
    });

87
    const Utils::FileName fn = Utils::FileName::fromString(m_projectFile);
88
    const BuildTargetInfo targetInfo
89
90
91
92
93
            = Utils::findOrDefault(target->applicationTargets().list,
                                   [&buildSystemTargets, &fn] (const BuildTargetInfo &bti) {
        return Utils::anyOf(buildSystemTargets, [&fn, &bti](const QString &b) {
            return b == bti.targetName || (b.contains(bti.targetName) && bti.projectFilePath == fn);
        });
94
95
96
97
98
99
100
101
102
103
    });
    const Utils::FileName executable = targetInfo.targetFilePath; // empty if BTI is default created
    for (RunConfiguration *runConfig : target->runConfigurations()) {
        if (!isLocal(runConfig)) // TODO add device support
            continue;

        if (buildSystemTargets.contains(runConfig->buildSystemTarget())) {
            Runnable runnable = runConfig->runnable();
            if (!runnable.is<StandardRunnable>())
                continue;
104
            StandardRunnable stdRunnable = runnable.as<StandardRunnable>();
105
106
107
108
109
110
111
112
113
            // TODO this might pick up the wrong executable
            m_executableFile = stdRunnable.executable;
            m_displayName = runConfig->displayName();
            m_workingDir = Utils::FileUtils::normalizePathName(stdRunnable.workingDirectory);
            m_environment = stdRunnable.environment;
            m_project = project;
            if (runMode == TestRunner::Debug)
                m_runConfig = new TestRunConfiguration(runConfig->target(), this);
            break;
114
115
        }
    }
116
117
118
    // RunConfiguration for this target could be explicitly removed or not created at all
    if (m_displayName.isEmpty() && !executable.isEmpty()) {
        // we failed to find a valid runconfiguration - but we've got the executable already
119
        if (auto rc = target->activeRunConfiguration()) {
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
            if (isLocal(rc)) { // FIXME for now only Desktop support
                Runnable runnable = rc->runnable();
                if (runnable.is<StandardRunnable>()) {
                    StandardRunnable stdRunnable = runnable.as<StandardRunnable>();
                    m_environment = stdRunnable.environment;
                    // when guessing we might have no extension
                    const QString &exeString = executable.toString();
                    if (Utils::HostOsInfo::isWindowsHost() && !exeString.toLower().endsWith(".exe"))
                        m_executableFile = Utils::HostOsInfo::withExecutableSuffix(exeString);
                    else
                        m_executableFile = exeString;
                    m_project = project;
                    m_guessedConfiguration = true;
                    if (runMode == TestRunner::Debug)
                        m_runConfig = new TestRunConfiguration(rc->target(), this);
                }
136
            }
137
138
139
        }
    }

140
141
142
143
144
    if (auto buildConfig = target->activeBuildConfiguration()) {
        const QString buildBase = buildConfig->buildDirectory().toString();
        const QString projBase = project->projectDirectory().toString();
        if (m_projectFile.startsWith(projBase))
            m_buildDir = QFileInfo(buildBase + m_projectFile.mid(projBase.length())).absolutePath();
145
146
    }

147
148
149
    if (m_displayName.isEmpty()) // happens e.g. when guessing the TestConfiguration or error
        m_displayName = buildSystemTargets.isEmpty() ? "unknown" : *buildSystemTargets.begin();
}
150

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 * @brief sets the test cases for this test configuration.
 *
 * Watch out for special handling of test configurations, because this method also
 * updates the test case count to the current size of \a testCases.
 *
 * @param testCases list of names of the test functions / test cases
 */
void TestConfiguration::setTestCases(const QStringList &testCases)
{
    m_testCases.clear();
    m_testCases << testCases;
    m_testCaseCount = m_testCases.size();
}

void TestConfiguration::setTestCaseCount(int count)
{
    m_testCaseCount = count;
}

171
void TestConfiguration::setExecutableFile(const QString &executableFile)
172
{
173
    m_executableFile = executableFile;
174
175
}

176
void TestConfiguration::setProjectFile(const QString &projectFile)
177
{
178
    m_projectFile = projectFile;
179
180
181
182
183
184
185
}

void TestConfiguration::setWorkingDirectory(const QString &workingDirectory)
{
    m_workingDir = workingDirectory;
}

186
187
188
189
190
void TestConfiguration::setBuildDirectory(const QString &buildDirectory)
{
    m_buildDir = buildDirectory;
}

191
192
193
194
195
void TestConfiguration::setDisplayName(const QString &displayName)
{
    m_displayName = displayName;
}

196
197
198
199
200
void TestConfiguration::setEnvironment(const Utils::Environment &env)
{
    m_environment = env;
}

201
void TestConfiguration::setProject(Project *project)
202
203
204
205
{
    m_project = project;
}

206
207
208
209
210
void TestConfiguration::setGuessedConfiguration(bool guessed)
{
    m_guessedConfiguration = guessed;
}

211
212
QString TestConfiguration::executableFilePath() const
{
213
    if (m_executableFile.isEmpty())
214
215
        return QString();

216
    QFileInfo commandFileInfo(m_executableFile);
217
218
219
    if (commandFileInfo.isExecutable() && commandFileInfo.path() != ".") {
        return commandFileInfo.absoluteFilePath();
    } else if (commandFileInfo.path() == "."){
220
        QString fullCommandFileName = m_executableFile;
221
        // TODO: check if we can use searchInPath() from Utils::Environment
222
223
        const QStringList &pathList = m_environment.toProcessEnvironment().value("PATH").split(
                    Utils::HostOsInfo::pathListSeparator());
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

        foreach (const QString &path, pathList) {
            QString filePath(path + QDir::separator() + fullCommandFileName);
            if (QFileInfo(filePath).isExecutable())
                return commandFileInfo.absoluteFilePath();
        }
    }
    return QString();
}

QString TestConfiguration::workingDirectory() const
{
    if (!m_workingDir.isEmpty()) {
        const QFileInfo info(m_workingDir);
        if (info.isDir()) // ensure wanted working dir does exist
            return info.absoluteFilePath();
    }

    const QString executable = executableFilePath();
    return executable.isEmpty() ? executable : QFileInfo(executable).absolutePath();
}

246
247
} // namespace Internal
} // namespace Autotest