cmCTestTestHandler.h 10.5 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
Ken Martin's avatar
Ken Martin committed
3
4
5
#ifndef cmCTestTestHandler_h
#define cmCTestTestHandler_h

6
#include "cmConfigure.h" // IWYU pragma: keep
7

8
#include <chrono>
9
#include <cstdint>
10
11
12
13
14
15
#include <iosfwd>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
Ken Martin's avatar
Ken Martin committed
16

17
18
19
20
21
22
23
24
#include <stddef.h>

#include "cmsys/RegularExpression.hxx"

#include "cmCTestGenericHandler.h"
#include "cmDuration.h"
#include "cmListFileCache.h"

25
class cmCTest;
Ken Martin's avatar
Ken Martin committed
26
class cmMakefile;
27
class cmXMLWriter;
Ken Martin's avatar
Ken Martin committed
28
29
30
31
32

/** \class cmCTestTestHandler
 * \brief A class that handles ctest -S invocations
 *
 */
33
class cmCTestTestHandler : public cmCTestGenericHandler
Ken Martin's avatar
Ken Martin committed
34
{
35
  friend class cmCTestRunTest;
36
  friend class cmCTestMultiProcessHandler;
37

Ken Martin's avatar
Ken Martin committed
38
public:
39
  using Superclass = cmCTestGenericHandler;
Ken Martin's avatar
Ken Martin committed
40

41
  /**
Ken Martin's avatar
Ken Martin committed
42
43
   * The main entry point for this class
   */
44
  int ProcessHandler() override;
Andy Cedilnik's avatar
Andy Cedilnik committed
45

46
  /**
luz.paz's avatar
luz.paz committed
47
   * When both -R and -I are used should the resulting test list be the
48
49
50
   * intersection or the union of the lists. By default it is the
   * intersection.
   */
Andy Cedilnik's avatar
Andy Cedilnik committed
51
  void SetUseUnion(bool val) { this->UseUnion = val; }
52

53
54
55
56
57
58
  /**
   * Set whether or not CTest should only execute the tests that failed
   * on the previous run.  By default this is false.
   */
  void SetRerunFailed(bool val) { this->RerunFailed = val; }

59
60
61
  /**
   * This method is called when reading CTest custom file
   */
62
  void PopulateCustomVectors(cmMakefile* mf) override;
Andy Cedilnik's avatar
Andy Cedilnik committed
63

64
  //! Control the use of the regular expresisons, call these methods to turn
65
  /// them on
Ken Martin's avatar
Ken Martin committed
66
67
  void UseIncludeRegExp();
  void UseExcludeRegExp();
68
69
  void SetIncludeRegExp(const char*);
  void SetExcludeRegExp(const char*);
Andy Cedilnik's avatar
Andy Cedilnik committed
70

71
72
  void SetMaxIndex(int n) { this->MaxIndex = n; }
  int GetMaxIndex() { return this->MaxIndex; }
Ken Martin's avatar
Ken Martin committed
73

74
  void SetTestOutputSizePassed(int n)
75
76
77
  {
    this->CustomMaximumPassedTestOutputSize = n;
  }
78
  void SetTestOutputSizeFailed(int n)
79
80
81
  {
    this->CustomMaximumFailedTestOutputSize = n;
  }
82

83
  //! pass the -I argument down
Ken Martin's avatar
Ken Martin committed
84
85
  void SetTestsToRunInformation(const char*);

86
  cmCTestTestHandler();
Ken Martin's avatar
Ken Martin committed
87

88
89
90
  /*
   * Add the test to the list of tests to be executed
   */
91
  bool AddTest(const std::vector<std::string>& args);
92

93
94
95
96
97
  /*
   * Set tests properties
   */
  bool SetTestsProperties(const std::vector<std::string>& args);

98
99
100
101
102
  /**
   * Set directory properties
   */
  bool SetDirectoryProperties(const std::vector<std::string>& args);

103
  void Initialize() override;
104

105
106
107
108
109
110
111
112
113
114
  struct cmCTestTestResourceRequirement
  {
    std::string ResourceType;
    int SlotsNeeded;
    int UnitsNeeded;

    bool operator==(const cmCTestTestResourceRequirement& other) const;
    bool operator!=(const cmCTestTestResourceRequirement& other) const;
  };

Bill Hoffman's avatar
Bill Hoffman committed
115
116
117
118
  // NOTE: This struct is Saved/Restored
  // in cmCTestTestHandler, if you add to this class
  // then you must add the new members to that code or
  // ctest -j N will break for that feature
Andy Cedilnik's avatar
Andy Cedilnik committed
119
120
  struct cmCTestTestProperties
  {
121
122
    std::string Name;
    std::string Directory;
Andy Cedilnik's avatar
Andy Cedilnik committed
123
    std::vector<std::string> Args;
124
    std::vector<std::string> RequiredFiles;
125
    std::vector<std::string> Depends;
126
    std::vector<std::string> AttachedFiles;
127
    std::vector<std::string> AttachOnFail;
128
    std::vector<std::pair<cmsys::RegularExpression, std::string>>
129
      ErrorRegularExpressions;
130
    std::vector<std::pair<cmsys::RegularExpression, std::string>>
131
      RequiredRegularExpressions;
132
133
    std::vector<std::pair<cmsys::RegularExpression, std::string>>
      SkipRegularExpressions;
134
    std::vector<std::pair<cmsys::RegularExpression, std::string>>
135
      TimeoutRegularExpressions;
136
    std::map<std::string, std::string> Measurements;
Andy Cedilnik's avatar
Andy Cedilnik committed
137
138
    bool IsInBasedOnREOptions;
    bool WillFail;
Betsy McPhail's avatar
Betsy McPhail committed
139
    bool Disabled;
140
    float Cost;
141
    int PreviousRuns;
142
    bool RunSerial;
Wouter Klouwen's avatar
Wouter Klouwen committed
143
    cmDuration Timeout;
144
    bool ExplicitTimeout;
Wouter Klouwen's avatar
Wouter Klouwen committed
145
    cmDuration AlternateTimeout;
146
    int Index;
147
    // Requested number of process slots
148
    int Processors;
149
150
    bool WantAffinity;
    std::vector<size_t> Affinity;
151
152
    // return code of test which will mark test as "not run"
    int SkipReturnCode;
153
    std::vector<std::string> Environment;
154
    std::vector<std::string> Labels;
Zach's avatar
Zach committed
155
    std::set<std::string> LockedResources;
156
157
158
159
    std::set<std::string> FixturesSetup;
    std::set<std::string> FixturesCleanup;
    std::set<std::string> FixturesRequired;
    std::set<std::string> RequireSuccessDepends;
160
    std::vector<std::vector<cmCTestTestResourceRequirement>> Processes;
161
162
    // Private test generator properties used to track backtraces
    cmListFileBacktrace Backtrace;
Andy Cedilnik's avatar
Andy Cedilnik committed
163
164
  };

165
166
167
168
  struct cmCTestTestResult
  {
    std::string Name;
    std::string Path;
169
    std::string Reason;
170
    std::string FullCommandLine;
Wouter Klouwen's avatar
Wouter Klouwen committed
171
    cmDuration ExecutionTime;
172
    std::int64_t ReturnValue;
173
    int Status;
174
    std::string ExceptionStatus;
175
    bool CompressOutput;
176
177
    std::string CompletionStatus;
    std::string Output;
178
    std::string DartString;
179
    int TestCount;
180
181
182
    cmCTestTestProperties* Properties;
  };

183
184
  struct cmCTestTestResultLess
  {
185
186
    bool operator()(const cmCTestTestResult& lhs,
                    const cmCTestTestResult& rhs) const
187
    {
188
      return lhs.TestCount < rhs.TestCount;
189
190
191
192
    }
  };

  // add configurations to a search path for an executable
193
194
195
196
  static void AddConfigurations(cmCTest* ctest,
                                std::vector<std::string>& attempted,
                                std::vector<std::string>& attemptedConfigs,
                                std::string filepath, std::string& filename);
197
198

  // full signature static method to find an executable
199
200
201
202
  static std::string FindExecutable(cmCTest* ctest, const char* testCommand,
                                    std::string& resultingConfig,
                                    std::vector<std::string>& extraPaths,
                                    std::vector<std::string>& failed);
203

204
205
206
207
  static bool ParseProcessesProperty(
    const std::string& val,
    std::vector<std::vector<cmCTestTestResourceRequirement>>& processes);

208
  using ListOfTests = std::vector<cmCTestTestProperties>;
209

Andy Cedilnik's avatar
Andy Cedilnik committed
210
protected:
211
212
  using SetOfTests =
    std::set<cmCTestTestHandler::cmCTestTestResult, cmCTestTestResultLess>;
213

214
  // compute a final test list
215
216
  virtual int PreProcessHandler();
  virtual int PostProcessHandler();
217
  virtual void GenerateTestCommand(std::vector<std::string>& args, int test);
218
  int ExecuteCommands(std::vector<std::string>& vec);
219

220
221
222
223
224
225
226
227
228
  bool ProcessOptions();
  void LogTestSummary(const std::vector<std::string>& passed,
                      const std::vector<std::string>& failed,
                      const cmDuration& durationInSecs);
  void LogDisabledTests(const std::vector<cmCTestTestResult>& disabledTests);
  void LogFailedTests(const std::vector<std::string>& failed,
                      const SetOfTests& resultsSet);
  bool GenerateXML();

229
230
231
232
  void WriteTestResultHeader(cmXMLWriter& xml,
                             cmCTestTestResult const& result);
  void WriteTestResultFooter(cmXMLWriter& xml,
                             cmCTestTestResult const& result);
233
  // Write attached test files into the xml
234
  void AttachFiles(cmXMLWriter& xml, cmCTestTestResult& result);
235

236
237
238
  //! Clean test output to specified length
  bool CleanTestOutput(std::string& output, size_t length);

Wouter Klouwen's avatar
Wouter Klouwen committed
239
  cmDuration ElapsedTestingTime;
240

241
  using TestResultsVector = std::vector<cmCTestTestResult>;
242
  TestResultsVector TestResults;
Ken Martin's avatar
Ken Martin committed
243

244
  std::vector<std::string> CustomTestsIgnore;
245
246
  std::string StartTest;
  std::string EndTest;
247
248
  std::chrono::system_clock::time_point StartTestTime;
  std::chrono::system_clock::time_point EndTestTime;
Andy Cedilnik's avatar
Andy Cedilnik committed
249
  bool MemCheck;
250
251
  int CustomMaximumPassedTestOutputSize;
  int CustomMaximumFailedTestOutputSize;
252
  int MaxIndex;
253

254
public:
255
256
  enum
  { // Program statuses
257
258
259
260
261
262
263
264
265
266
267
    NOT_RUN = 0,
    TIMEOUT,
    SEGFAULT,
    ILLEGAL,
    INTERRUPT,
    NUMERICAL,
    OTHER_FAULT,
    FAILED,
    BAD_COMMAND,
    COMPLETED
  };
Ken Martin's avatar
Ken Martin committed
268

269
private:
Ken Martin's avatar
Ken Martin committed
270
271
272
  /**
   * Generate the Dart compatible output
   */
273
  virtual void GenerateDartOutput(cmXMLWriter& xml);
Ken Martin's avatar
Ken Martin committed
274

275
  void PrintLabelOrSubprojectSummary(bool isSubProject);
276

Ken Martin's avatar
Ken Martin committed
277
  /**
278
   * Run the tests for a directory and any subdirectories
Ken Martin's avatar
Ken Martin committed
279
   */
280
281
  void ProcessDirectory(std::vector<std::string>& passed,
                        std::vector<std::string>& failed);
Andy Cedilnik's avatar
Andy Cedilnik committed
282

Ken Martin's avatar
Ken Martin committed
283
284
285
  /**
   * Get the list of tests in directory and subdirectories.
   */
286
  void GetListOfTests();
287
288
289
  // compute the lists of tests that will actually run
  // based on union regex and -I stuff
  void ComputeTestList();
Brad King's avatar
Brad King committed
290

291
292
293
294
  // compute the lists of tests that will actually run
  // based on LastTestFailed.log
  void ComputeTestListForRerunFailed();

295
296
297
298
299
  // add required setup/cleanup tests not already in the
  // list of tests to be run and update dependencies between
  // tests to account for fixture setup/cleanup
  void UpdateForFixtures(ListOfTests& tests) const;

300
301
  void UpdateMaxTestNameWidth();

302
303
304
305
306
  bool GetValue(const char* tag, std::string& value, std::istream& fin);
  bool GetValue(const char* tag, int& value, std::istream& fin);
  bool GetValue(const char* tag, size_t& value, std::istream& fin);
  bool GetValue(const char* tag, bool& value, std::istream& fin);
  bool GetValue(const char* tag, double& value, std::istream& fin);
Ken Martin's avatar
Ken Martin committed
307
308
309
  /**
   * Find the executable for a test
   */
310
  std::string FindTheExecutable(const char* exe);
Ken Martin's avatar
Ken Martin committed
311

312
  std::string GetTestStatus(cmCTestTestResult const&);
Bill Hoffman's avatar
Bill Hoffman committed
313
  void ExpandTestsToRunInformation(size_t numPossibleTests);
314
  void ExpandTestsToRunInformationForRerunFailed();
Ken Martin's avatar
Ken Martin committed
315

316
317
  std::vector<std::string> CustomPreTest;
  std::vector<std::string> CustomPostTest;
Ken Martin's avatar
Ken Martin committed
318

319
  std::vector<int> TestsToRun;
Ken Martin's avatar
Ken Martin committed
320

321
322
  bool UseIncludeLabelRegExpFlag;
  bool UseExcludeLabelRegExpFlag;
Andy Cedilnik's avatar
Andy Cedilnik committed
323
324
325
  bool UseIncludeRegExpFlag;
  bool UseExcludeRegExpFlag;
  bool UseExcludeRegExpFirst;
326
327
  std::string IncludeLabelRegExp;
  std::string ExcludeLabelRegExp;
Andy Cedilnik's avatar
Andy Cedilnik committed
328
329
  std::string IncludeRegExp;
  std::string ExcludeRegExp;
330
331
332
  std::string ExcludeFixtureRegExp;
  std::string ExcludeFixtureSetupRegExp;
  std::string ExcludeFixtureCleanupRegExp;
333
334
  cmsys::RegularExpression IncludeLabelRegularExpression;
  cmsys::RegularExpression ExcludeLabelRegularExpression;
Andy Cedilnik's avatar
Andy Cedilnik committed
335
336
  cmsys::RegularExpression IncludeTestsRegularExpression;
  cmsys::RegularExpression ExcludeTestsRegularExpression;
Ken Martin's avatar
Ken Martin committed
337

338
  void GenerateRegressionImages(cmXMLWriter& xml, const std::string& dart);
Bill Hoffman's avatar
Bill Hoffman committed
339
  cmsys::RegularExpression DartStuff1;
340
341
342
  void CheckLabelFilter(cmCTestTestProperties& it);
  void CheckLabelFilterExclude(cmCTestTestProperties& it);
  void CheckLabelFilterInclude(cmCTestTestProperties& it);
Ken Martin's avatar
Ken Martin committed
343
344

  std::string TestsToRunString;
Andy Cedilnik's avatar
Andy Cedilnik committed
345
346
  bool UseUnion;
  ListOfTests TestList;
347
  size_t TotalNumberOfTests;
Andy Cedilnik's avatar
Andy Cedilnik committed
348
  cmsys::RegularExpression DartStuff;
Andy Cedilnik's avatar
Andy Cedilnik committed
349

Andy Cedilnik's avatar
Andy Cedilnik committed
350
  std::ostream* LogFile;
351
352

  bool RerunFailed;
Ken Martin's avatar
Ken Martin committed
353
354
355
};

#endif