cmCTestTestHandler.h 10 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

Bill Hoffman's avatar
Bill Hoffman committed
105
106
107
108
  // 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
109
110
  struct cmCTestTestProperties
  {
111
112
    std::string Name;
    std::string Directory;
Andy Cedilnik's avatar
Andy Cedilnik committed
113
    std::vector<std::string> Args;
114
    std::vector<std::string> RequiredFiles;
115
    std::vector<std::string> Depends;
116
    std::vector<std::string> AttachedFiles;
117
    std::vector<std::string> AttachOnFail;
118
    std::vector<std::pair<cmsys::RegularExpression, std::string>>
119
      ErrorRegularExpressions;
120
    std::vector<std::pair<cmsys::RegularExpression, std::string>>
121
      RequiredRegularExpressions;
122
123
    std::vector<std::pair<cmsys::RegularExpression, std::string>>
      SkipRegularExpressions;
124
    std::vector<std::pair<cmsys::RegularExpression, std::string>>
125
      TimeoutRegularExpressions;
126
    std::map<std::string, std::string> Measurements;
Andy Cedilnik's avatar
Andy Cedilnik committed
127
128
    bool IsInBasedOnREOptions;
    bool WillFail;
Betsy McPhail's avatar
Betsy McPhail committed
129
    bool Disabled;
130
    float Cost;
131
    int PreviousRuns;
132
    bool RunSerial;
Wouter Klouwen's avatar
Wouter Klouwen committed
133
    cmDuration Timeout;
134
    bool ExplicitTimeout;
Wouter Klouwen's avatar
Wouter Klouwen committed
135
    cmDuration AlternateTimeout;
136
    int Index;
137
    // Requested number of process slots
138
    int Processors;
139
140
    bool WantAffinity;
    std::vector<size_t> Affinity;
141
142
    // return code of test which will mark test as "not run"
    int SkipReturnCode;
143
    std::vector<std::string> Environment;
144
    std::vector<std::string> Labels;
Zach's avatar
Zach committed
145
    std::set<std::string> LockedResources;
146
147
148
149
    std::set<std::string> FixturesSetup;
    std::set<std::string> FixturesCleanup;
    std::set<std::string> FixturesRequired;
    std::set<std::string> RequireSuccessDepends;
150
151
    // Private test generator properties used to track backtraces
    cmListFileBacktrace Backtrace;
Andy Cedilnik's avatar
Andy Cedilnik committed
152
153
  };

154
155
156
157
  struct cmCTestTestResult
  {
    std::string Name;
    std::string Path;
158
    std::string Reason;
159
    std::string FullCommandLine;
Wouter Klouwen's avatar
Wouter Klouwen committed
160
    cmDuration ExecutionTime;
161
    std::int64_t ReturnValue;
162
    int Status;
163
    std::string ExceptionStatus;
164
    bool CompressOutput;
165
166
    std::string CompletionStatus;
    std::string Output;
167
    std::string DartString;
168
    int TestCount;
169
170
171
    cmCTestTestProperties* Properties;
  };

172
173
  struct cmCTestTestResultLess
  {
174
175
    bool operator()(const cmCTestTestResult& lhs,
                    const cmCTestTestResult& rhs) const
176
    {
177
      return lhs.TestCount < rhs.TestCount;
178
179
180
181
    }
  };

  // add configurations to a search path for an executable
182
183
184
185
  static void AddConfigurations(cmCTest* ctest,
                                std::vector<std::string>& attempted,
                                std::vector<std::string>& attemptedConfigs,
                                std::string filepath, std::string& filename);
186
187

  // full signature static method to find an executable
188
189
190
191
  static std::string FindExecutable(cmCTest* ctest, const char* testCommand,
                                    std::string& resultingConfig,
                                    std::vector<std::string>& extraPaths,
                                    std::vector<std::string>& failed);
192

193
  using ListOfTests = std::vector<cmCTestTestProperties>;
194

Andy Cedilnik's avatar
Andy Cedilnik committed
195
protected:
196
197
  using SetOfTests =
    std::set<cmCTestTestHandler::cmCTestTestResult, cmCTestTestResultLess>;
198

199
  // compute a final test list
200
201
  virtual int PreProcessHandler();
  virtual int PostProcessHandler();
202
  virtual void GenerateTestCommand(std::vector<std::string>& args, int test);
203
  int ExecuteCommands(std::vector<std::string>& vec);
204

205
206
207
208
209
210
211
212
213
  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();

214
215
216
217
  void WriteTestResultHeader(cmXMLWriter& xml,
                             cmCTestTestResult const& result);
  void WriteTestResultFooter(cmXMLWriter& xml,
                             cmCTestTestResult const& result);
218
  // Write attached test files into the xml
219
  void AttachFiles(cmXMLWriter& xml, cmCTestTestResult& result);
220

221
222
223
  //! Clean test output to specified length
  bool CleanTestOutput(std::string& output, size_t length);

Wouter Klouwen's avatar
Wouter Klouwen committed
224
  cmDuration ElapsedTestingTime;
225

226
  using TestResultsVector = std::vector<cmCTestTestResult>;
227
  TestResultsVector TestResults;
Ken Martin's avatar
Ken Martin committed
228

229
  std::vector<std::string> CustomTestsIgnore;
230
231
  std::string StartTest;
  std::string EndTest;
232
233
  std::chrono::system_clock::time_point StartTestTime;
  std::chrono::system_clock::time_point EndTestTime;
Andy Cedilnik's avatar
Andy Cedilnik committed
234
  bool MemCheck;
235
236
  int CustomMaximumPassedTestOutputSize;
  int CustomMaximumFailedTestOutputSize;
237
  int MaxIndex;
238

239
public:
240
241
  enum
  { // Program statuses
242
243
244
245
246
247
248
249
250
251
252
    NOT_RUN = 0,
    TIMEOUT,
    SEGFAULT,
    ILLEGAL,
    INTERRUPT,
    NUMERICAL,
    OTHER_FAULT,
    FAILED,
    BAD_COMMAND,
    COMPLETED
  };
Ken Martin's avatar
Ken Martin committed
253

254
private:
Ken Martin's avatar
Ken Martin committed
255
256
257
  /**
   * Generate the Dart compatible output
   */
258
  virtual void GenerateDartOutput(cmXMLWriter& xml);
Ken Martin's avatar
Ken Martin committed
259

260
  void PrintLabelOrSubprojectSummary(bool isSubProject);
261

Ken Martin's avatar
Ken Martin committed
262
  /**
263
   * Run the tests for a directory and any subdirectories
Ken Martin's avatar
Ken Martin committed
264
   */
265
266
  void ProcessDirectory(std::vector<std::string>& passed,
                        std::vector<std::string>& failed);
Andy Cedilnik's avatar
Andy Cedilnik committed
267

Ken Martin's avatar
Ken Martin committed
268
269
270
  /**
   * Get the list of tests in directory and subdirectories.
   */
271
  void GetListOfTests();
272
273
274
  // 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
275

276
277
278
279
  // compute the lists of tests that will actually run
  // based on LastTestFailed.log
  void ComputeTestListForRerunFailed();

280
281
282
283
284
  // 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;

285
286
  void UpdateMaxTestNameWidth();

287
288
289
290
291
  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
292
293
294
  /**
   * Find the executable for a test
   */
295
  std::string FindTheExecutable(const char* exe);
Ken Martin's avatar
Ken Martin committed
296

297
  std::string GetTestStatus(cmCTestTestResult const&);
Bill Hoffman's avatar
Bill Hoffman committed
298
  void ExpandTestsToRunInformation(size_t numPossibleTests);
299
  void ExpandTestsToRunInformationForRerunFailed();
Ken Martin's avatar
Ken Martin committed
300

301
302
  std::vector<std::string> CustomPreTest;
  std::vector<std::string> CustomPostTest;
Ken Martin's avatar
Ken Martin committed
303

304
  std::vector<int> TestsToRun;
Ken Martin's avatar
Ken Martin committed
305

306
307
  bool UseIncludeLabelRegExpFlag;
  bool UseExcludeLabelRegExpFlag;
Andy Cedilnik's avatar
Andy Cedilnik committed
308
309
310
  bool UseIncludeRegExpFlag;
  bool UseExcludeRegExpFlag;
  bool UseExcludeRegExpFirst;
311
312
  std::string IncludeLabelRegExp;
  std::string ExcludeLabelRegExp;
Andy Cedilnik's avatar
Andy Cedilnik committed
313
314
  std::string IncludeRegExp;
  std::string ExcludeRegExp;
315
316
317
  std::string ExcludeFixtureRegExp;
  std::string ExcludeFixtureSetupRegExp;
  std::string ExcludeFixtureCleanupRegExp;
318
319
  cmsys::RegularExpression IncludeLabelRegularExpression;
  cmsys::RegularExpression ExcludeLabelRegularExpression;
Andy Cedilnik's avatar
Andy Cedilnik committed
320
321
  cmsys::RegularExpression IncludeTestsRegularExpression;
  cmsys::RegularExpression ExcludeTestsRegularExpression;
Ken Martin's avatar
Ken Martin committed
322

323
  void GenerateRegressionImages(cmXMLWriter& xml, const std::string& dart);
Bill Hoffman's avatar
Bill Hoffman committed
324
  cmsys::RegularExpression DartStuff1;
325
326
327
  void CheckLabelFilter(cmCTestTestProperties& it);
  void CheckLabelFilterExclude(cmCTestTestProperties& it);
  void CheckLabelFilterInclude(cmCTestTestProperties& it);
Ken Martin's avatar
Ken Martin committed
328
329

  std::string TestsToRunString;
Andy Cedilnik's avatar
Andy Cedilnik committed
330
331
  bool UseUnion;
  ListOfTests TestList;
332
  size_t TotalNumberOfTests;
Andy Cedilnik's avatar
Andy Cedilnik committed
333
  cmsys::RegularExpression DartStuff;
Andy Cedilnik's avatar
Andy Cedilnik committed
334

Andy Cedilnik's avatar
Andy Cedilnik committed
335
  std::ostream* LogFile;
336
337

  bool RerunFailed;
Ken Martin's avatar
Ken Martin committed
338
339
340
};

#endif