Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmTestGenerator.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmTestGenerator.h"
4
5
#include <cstddef> // IWYU pragma: keep
6
#include <memory>
7
#include <ostream>
8
#include <string>
9
#include <utility>
10
#include <vector>
11
12
#include "cmGeneratorExpression.h"
13
#include "cmGeneratorTarget.h"
14
#include "cmList.h"
15
#include "cmListFileCache.h"
16
#include "cmLocalGenerator.h"
17
#include "cmMakefile.h"
18
#include "cmMessageType.h"
19
#include "cmPolicies.h"
20
#include "cmPropertyMap.h"
21
#include "cmRange.h"
22
#include "cmScriptGenerator.h"
23
#include "cmStateTypes.h"
24
#include "cmStringAlgorithms.h"
25
#include "cmSystemTools.h"
26
#include "cmTest.h"
27
#include "cmValue.h"
28
29
namespace /* anonymous */
30
{
31
32
bool needToQuoteTestName(cmMakefile const& mf, std::string const& name)
33
0
{
34
  // Determine if policy CMP0110 is set to NEW.
35
0
  switch (mf.GetPolicyStatus(cmPolicies::CMP0110)) {
36
0
    case cmPolicies::WARN:
37
      // Only warn if a forbidden character is used in the name.
38
0
      if (name.find_first_of("$[] #;\t\n\"\\") != std::string::npos) {
39
0
        mf.IssueMessage(
40
0
          MessageType::AUTHOR_WARNING,
41
0
          cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0110),
42
0
                   "\nThe following name given to add_test() is invalid if "
43
0
                   "CMP0110 is not set or set to OLD:\n  `",
44
0
                   name, "ยด\n"));
45
0
      }
46
0
      CM_FALLTHROUGH;
47
0
    case cmPolicies::OLD:
48
      // OLD behavior is to not quote the test's name.
49
0
      return false;
50
0
    case cmPolicies::NEW:
51
0
    default:
52
      // NEW behavior is to quote the test's name.
53
0
      return true;
54
0
  }
55
0
}
56
57
std::string TestName(cmTest* test)
58
0
{
59
0
  std::string name = test->GetName();
60
0
  if (needToQuoteTestName(*test->GetMakefile(), name)) {
61
0
    name = cmScriptGenerator::Quote(name);
62
0
  }
63
0
  return name;
64
0
}
65
66
} // End: anonymous namespace
67
68
cmTestGenerator::cmTestGenerator(
69
  cmTest* test, std::vector<std::string> const& configurations)
70
0
  : cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations)
71
0
  , Test(test)
72
0
{
73
0
  this->ActionsPerConfig = !test->GetOldStyle();
74
0
  this->TestGenerated = false;
75
0
  this->LG = nullptr;
76
0
}
77
78
0
cmTestGenerator::~cmTestGenerator() = default;
79
80
void cmTestGenerator::Compute(cmLocalGenerator* lg)
81
0
{
82
0
  this->LG = lg;
83
0
}
84
85
bool cmTestGenerator::TestsForConfig(std::string const& config)
86
0
{
87
0
  return this->GeneratesForConfig(config);
88
0
}
89
90
cmTest* cmTestGenerator::GetTest() const
91
0
{
92
0
  return this->Test;
93
0
}
94
95
void cmTestGenerator::GenerateScriptActions(std::ostream& os, Indent indent)
96
0
{
97
0
  if (this->ActionsPerConfig) {
98
    // This is the per-config generation in a single-configuration
99
    // build generator case.  The superclass will call our per-config
100
    // method.
101
0
    this->cmScriptGenerator::GenerateScriptActions(os, indent);
102
0
  } else {
103
    // This is an old-style test, so there is only one config.
104
    // assert(this->Test->GetOldStyle());
105
0
    this->GenerateOldStyle(os, indent);
106
0
  }
107
0
}
108
109
void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
110
                                              std::string const& config,
111
                                              Indent indent)
112
0
{
113
0
  this->TestGenerated = true;
114
115
  // Set up generator expression evaluation context.
116
0
  cmGeneratorExpression ge(*this->Test->GetMakefile()->GetCMakeInstance(),
117
0
                           this->Test->GetBacktrace());
118
119
0
  auto const test_name = TestName(this->Test);
120
121
  // Start the test command.
122
0
  os << indent << "add_test(" << test_name << ' ';
123
124
  // Evaluate command line arguments
125
0
  cmList argv{
126
0
    this->EvaluateCommandLineArguments(this->Test->GetCommand(), ge, config),
127
    // Expand arguments if COMMAND_EXPAND_LISTS is set
128
0
    this->Test->GetCommandExpandLists() ? cmList::ExpandElements::Yes
129
0
                                        : cmList::ExpandElements::No,
130
0
    cmList::EmptyElements::Yes
131
0
  };
132
  // Expanding lists on an empty command may have left it empty
133
0
  if (argv.empty()) {
134
0
    argv.emplace_back();
135
0
  }
136
137
  // Check whether the command executable is a target whose name is to
138
  // be translated.
139
0
  std::string exe = argv[0];
140
0
  cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe);
141
0
  if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
142
    // Use the target file on disk.
143
0
    exe = target->GetFullPath(config);
144
145
0
    auto addLauncher = [this, &config, &ge, &os,
146
0
                        target](std::string const& propertyName) {
147
0
      cmValue launcher = target->GetProperty(propertyName);
148
0
      if (!cmNonempty(launcher)) {
149
0
        return;
150
0
      }
151
0
      auto const propVal = ge.Parse(*launcher)->Evaluate(this->LG, config);
152
0
      cmList launcherWithArgs(propVal, cmList::ExpandElements::Yes,
153
0
                              this->Test->GetCMP0178() == cmPolicies::NEW
154
0
                                ? cmList::EmptyElements::Yes
155
0
                                : cmList::EmptyElements::No);
156
0
      if (!launcherWithArgs.empty() && !launcherWithArgs[0].empty()) {
157
0
        if (this->Test->GetCMP0178() == cmPolicies::WARN) {
158
0
          cmList argsWithEmptyValuesPreserved(
159
0
            propVal, cmList::ExpandElements::Yes, cmList::EmptyElements::Yes);
160
0
          if (launcherWithArgs != argsWithEmptyValuesPreserved) {
161
0
            this->Test->GetMakefile()->IssueMessage(
162
0
              MessageType::AUTHOR_WARNING,
163
0
              cmStrCat("The ", propertyName, " property of target '",
164
0
                       target->GetName(),
165
0
                       "' contains empty list items. Those empty items are "
166
0
                       "being silently discarded to preserve backward "
167
0
                       "compatibility.\n",
168
0
                       cmPolicies::GetPolicyWarning(cmPolicies::CMP0178)));
169
0
          }
170
0
        }
171
0
        std::string launcherExe(launcherWithArgs[0]);
172
0
        cmSystemTools::ConvertToUnixSlashes(launcherExe);
173
0
        os << cmScriptGenerator::Quote(launcherExe) << " ";
174
0
        for (std::string const& arg :
175
0
             cmMakeRange(launcherWithArgs).advance(1)) {
176
0
          os << cmScriptGenerator::Quote(arg) << " ";
177
0
        }
178
0
      }
179
0
    };
180
181
    // Prepend with the test launcher if specified.
182
0
    addLauncher("TEST_LAUNCHER");
183
184
    // Prepend with the emulator when cross compiling if required.
185
0
    if (!this->GetTest()->GetCMP0158IsNew() ||
186
0
        this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) {
187
0
      addLauncher("CROSSCOMPILING_EMULATOR");
188
0
    }
189
0
  } else {
190
    // Use the command name given.
191
0
    cmSystemTools::ConvertToUnixSlashes(exe);
192
0
  }
193
194
  // Generate the command line with full escapes.
195
0
  os << cmScriptGenerator::Quote(exe);
196
197
0
  for (auto const& arg : cmMakeRange(argv).advance(1)) {
198
0
    os << " " << cmScriptGenerator::Quote(arg);
199
0
  }
200
201
  // Finish the test command.
202
0
  os << ")\n";
203
204
  // Output properties for the test.
205
0
  os << indent << "set_tests_properties(" << test_name << " PROPERTIES ";
206
0
  for (auto const& i : this->Test->GetProperties().GetList()) {
207
0
    os << " " << i.first << " "
208
0
       << cmScriptGenerator::Quote(
209
0
            ge.Parse(i.second)->Evaluate(this->LG, config));
210
0
  }
211
0
  this->GenerateInternalProperties(os);
212
0
  os << ")\n";
213
0
}
214
215
void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent)
216
0
{
217
0
  os << indent << "add_test(" << TestName(this->Test) << " NOT_AVAILABLE)\n";
218
0
}
219
220
bool cmTestGenerator::NeedsScriptNoConfig() const
221
0
{
222
0
  return (this->TestGenerated &&    // test generated for at least one config
223
0
          this->ActionsPerConfig && // test is config-aware
224
0
          this->Configurations.empty() &&      // test runs in all configs
225
0
          !this->ConfigurationTypes->empty()); // config-dependent command
226
0
}
227
228
void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent)
229
0
{
230
0
  this->TestGenerated = true;
231
232
0
  auto const test_name = TestName(this->Test);
233
234
  // Get the test command line to be executed.
235
0
  std::vector<std::string> const& command = this->Test->GetCommand();
236
237
0
  std::string exe = command[0];
238
0
  cmSystemTools::ConvertToUnixSlashes(exe);
239
0
  fout << indent << "add_test(" << test_name << " \"" << exe << "\"";
240
241
0
  for (std::string const& arg : cmMakeRange(command).advance(1)) {
242
    // Just double-quote all arguments so they are re-parsed
243
    // correctly by the test system.
244
0
    fout << " \"";
245
0
    for (char c : arg) {
246
      // Escape quotes within arguments.  We should escape
247
      // backslashes too but we cannot because it makes the result
248
      // inconsistent with previous behavior of this command.
249
0
      if (c == '"') {
250
0
        fout << '\\';
251
0
      }
252
0
      fout << c;
253
0
    }
254
0
    fout << '"';
255
0
  }
256
0
  fout << ")\n";
257
258
  // Output properties for the test.
259
0
  fout << indent << "set_tests_properties(" << test_name << " PROPERTIES ";
260
0
  for (auto const& i : this->Test->GetProperties().GetList()) {
261
0
    fout << " " << i.first << " " << cmScriptGenerator::Quote(i.second);
262
0
  }
263
0
  this->GenerateInternalProperties(fout);
264
0
  fout << ")\n";
265
0
}
266
267
void cmTestGenerator::GenerateInternalProperties(std::ostream& os)
268
0
{
269
0
  cmListFileBacktrace bt = this->Test->GetBacktrace();
270
0
  if (bt.Empty()) {
271
0
    return;
272
0
  }
273
274
0
  os << " "
275
0
     << "_BACKTRACE_TRIPLES"
276
0
     << " \"";
277
278
0
  bool prependTripleSeparator = false;
279
0
  while (!bt.Empty()) {
280
0
    auto const& entry = bt.Top();
281
0
    if (prependTripleSeparator) {
282
0
      os << ";";
283
0
    }
284
0
    os << entry.FilePath << ";" << entry.Line << ";" << entry.Name;
285
0
    bt = bt.Pop();
286
0
    prependTripleSeparator = true;
287
0
  }
288
289
0
  os << '"';
290
0
}
291
292
std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments(
293
  std::vector<std::string> const& argv, cmGeneratorExpression& ge,
294
  std::string const& config) const
295
0
{
296
  // Evaluate executable name and arguments
297
0
  auto evaluatedRange =
298
0
    cmMakeRange(argv).transform([&](std::string const& arg) {
299
0
      return ge.Parse(arg)->Evaluate(this->LG, config);
300
0
    });
301
302
0
  return { evaluatedRange.begin(), evaluatedRange.end() };
303
0
}