Coverage Report

Created: 2026-04-29 07:01

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 "cmDiagnostics.h"
13
#include "cmGeneratorExpression.h"
14
#include "cmGeneratorTarget.h"
15
#include "cmList.h"
16
#include "cmListFileCache.h"
17
#include "cmLocalGenerator.h"
18
#include "cmMakefile.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.IssueDiagnostic(
40
0
          cmDiagnostics::CMD_AUTHOR,
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 == nullptr || !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->Test != nullptr && 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::GenerateCommand(std::ostream& os,
110
                                      std::vector<std::string> const& command,
111
                                      std::string const& config, bool expand,
112
                                      cmGeneratorExpression& ge,
113
                                      cmPolicies::PolicyStatus cmp0158,
114
                                      cmPolicies::PolicyStatus cmp0178)
115
0
{
116
  // Evaluate command line arguments
117
0
  cmList argv{
118
0
    this->EvaluateCommandLineArguments(command, ge, config),
119
    // Expand arguments if COMMAND_EXPAND_LISTS is set
120
0
    expand ? cmList::ExpandElements::Yes : cmList::ExpandElements::No,
121
0
    cmList::EmptyElements::Yes,
122
0
  };
123
  // Expanding lists on an empty command may have left it empty
124
0
  if (argv.empty()) {
125
0
    argv.emplace_back();
126
0
  }
127
128
  // Check whether the command executable is a target whose name is to
129
  // be translated.
130
0
  std::string exe = argv[0];
131
0
  cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe);
132
0
  if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
133
    // Use the target file on disk.
134
0
    exe = target->GetFullPath(config);
135
136
0
    auto addLauncher = [&](std::string const& propertyName) {
137
0
      cmValue launcher = target->GetProperty(propertyName);
138
0
      if (!cmNonempty(launcher)) {
139
0
        return;
140
0
      }
141
0
      auto const propVal = ge.Parse(*launcher)->Evaluate(this->LG, config);
142
0
      cmList launcherWithArgs(propVal, cmList::ExpandElements::Yes,
143
0
                              cmp0178 == cmPolicies::NEW
144
0
                                ? cmList::EmptyElements::Yes
145
0
                                : cmList::EmptyElements::No);
146
0
      if (!launcherWithArgs.empty() && !launcherWithArgs[0].empty()) {
147
0
        if (cmp0178 == cmPolicies::WARN) {
148
0
          cmList argsWithEmptyValuesPreserved(
149
0
            propVal, cmList::ExpandElements::Yes, cmList::EmptyElements::Yes);
150
0
          if (launcherWithArgs != argsWithEmptyValuesPreserved) {
151
0
            this->LG->GetMakefile()->IssueDiagnostic(
152
0
              cmDiagnostics::CMD_AUTHOR,
153
0
              cmStrCat("The ", propertyName, " property of target '",
154
0
                       target->GetName(),
155
0
                       "' contains empty list items. Those empty items are "
156
0
                       "being silently discarded to preserve backward "
157
0
                       "compatibility.\n",
158
0
                       cmPolicies::GetPolicyWarning(cmPolicies::CMP0178)));
159
0
          }
160
0
        }
161
0
        std::string launcherExe(launcherWithArgs[0]);
162
0
        cmSystemTools::ConvertToUnixSlashes(launcherExe);
163
0
        os << cmScriptGenerator::Quote(launcherExe) << " ";
164
0
        for (std::string const& arg :
165
0
             cmMakeRange(launcherWithArgs).advance(1)) {
166
0
          os << cmScriptGenerator::Quote(arg) << " ";
167
0
        }
168
0
      }
169
0
    };
170
171
    // Prepend with the test launcher if specified.
172
0
    addLauncher("TEST_LAUNCHER");
173
174
    // Prepend with the emulator when cross compiling if required.
175
0
    if (cmp0158 != cmPolicies::NEW ||
176
0
        this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) {
177
0
      addLauncher("CROSSCOMPILING_EMULATOR");
178
0
    }
179
0
  } else {
180
    // Use the command name given.
181
0
    cmSystemTools::ConvertToUnixSlashes(exe);
182
0
  }
183
184
  // Generate the command line with full escapes.
185
0
  os << cmScriptGenerator::Quote(exe);
186
187
0
  for (auto const& arg : cmMakeRange(argv).advance(1)) {
188
0
    os << " " << cmScriptGenerator::Quote(arg);
189
0
  }
190
0
}
191
192
void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
193
                                              std::string const& config,
194
                                              Indent indent)
195
0
{
196
0
  this->TestGenerated = true;
197
198
  // Set up generator expression evaluation context.
199
0
  cmGeneratorExpression ge(*this->Test->GetMakefile()->GetCMakeInstance(),
200
0
                           this->Test->GetBacktrace());
201
202
0
  auto const test_name = TestName(this->Test);
203
0
  os << indent << "add_test(" << test_name << ' ';
204
0
  this->GenerateCommand(
205
0
    os, this->Test->GetCommand(), config, this->Test->GetCommandExpandLists(),
206
0
    ge, this->GetTest()->GetCMP0158(), this->Test->GetCMP0178());
207
0
  os << ")\n";
208
209
  // Output properties for the test.
210
0
  os << indent << "set_tests_properties(" << test_name << " PROPERTIES ";
211
0
  for (auto const& i : this->Test->GetProperties().GetList()) {
212
0
    os << " " << i.first << " "
213
0
       << cmScriptGenerator::Quote(
214
0
            ge.Parse(i.second)->Evaluate(this->LG, config));
215
0
  }
216
0
  os << ' ';
217
0
  this->GenerateBacktrace(os, this->Test->GetBacktrace());
218
0
  os << ")\n";
219
0
}
220
221
void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent)
222
0
{
223
0
  os << indent << "add_test(" << TestName(this->Test) << " NOT_AVAILABLE)\n";
224
0
}
225
226
bool cmTestGenerator::NeedsScriptNoConfig() const
227
0
{
228
0
  return (this->TestGenerated &&    // test generated for at least one config
229
0
          this->ActionsPerConfig && // test is config-aware
230
0
          this->Configurations.empty() &&      // test runs in all configs
231
0
          !this->ConfigurationTypes->empty()); // config-dependent command
232
0
}
233
234
void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent)
235
0
{
236
0
  this->TestGenerated = true;
237
238
0
  auto const test_name = TestName(this->Test);
239
240
  // Get the test command line to be executed.
241
0
  std::vector<std::string> const& command = this->Test->GetCommand();
242
243
0
  std::string exe = command[0];
244
0
  cmSystemTools::ConvertToUnixSlashes(exe);
245
0
  fout << indent << "add_test(" << test_name << " \"" << exe << "\"";
246
247
0
  for (std::string const& arg : cmMakeRange(command).advance(1)) {
248
    // Just double-quote all arguments so they are re-parsed
249
    // correctly by the test system.
250
0
    fout << " \"";
251
0
    for (char c : arg) {
252
      // Escape quotes within arguments.  We should escape
253
      // backslashes too but we cannot because it makes the result
254
      // inconsistent with previous behavior of this command.
255
0
      if (c == '"') {
256
0
        fout << '\\';
257
0
      }
258
0
      fout << c;
259
0
    }
260
0
    fout << '"';
261
0
  }
262
0
  fout << ")\n";
263
264
  // Output properties for the test.
265
0
  fout << indent << "set_tests_properties(" << test_name << " PROPERTIES ";
266
0
  for (auto const& i : this->Test->GetProperties().GetList()) {
267
0
    fout << " " << i.first << " " << cmScriptGenerator::Quote(i.second);
268
0
  }
269
0
  fout << ' ';
270
0
  this->GenerateBacktrace(fout, this->Test->GetBacktrace());
271
0
  fout << ")\n";
272
0
}
273
274
void cmTestGenerator::GenerateBacktrace(std::ostream& os,
275
                                        cmListFileBacktrace bt)
276
0
{
277
0
  if (bt.Empty()) {
278
0
    return;
279
0
  }
280
281
0
  os << "_BACKTRACE_TRIPLES \"";
282
283
0
  bool prependTripleSeparator = false;
284
0
  while (!bt.Empty()) {
285
0
    auto const& entry = bt.Top();
286
0
    if (prependTripleSeparator) {
287
0
      os << ";";
288
0
    }
289
0
    os << entry.FilePath << ";" << entry.Line << ";" << entry.Name;
290
0
    bt = bt.Pop();
291
0
    prependTripleSeparator = true;
292
0
  }
293
294
0
  os << '"';
295
0
}
296
297
std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments(
298
  std::vector<std::string> const& argv, cmGeneratorExpression& ge,
299
  std::string const& config) const
300
0
{
301
  // Evaluate executable name and arguments
302
0
  auto evaluatedRange =
303
0
    cmMakeRange(argv).transform([&](std::string const& arg) {
304
0
      return ge.Parse(arg)->Evaluate(this->LG, config);
305
0
    });
306
307
0
  return { evaluatedRange.begin(), evaluatedRange.end() };
308
0
}