aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/test/init.cxx
blob: f62ddcc3922ef8d7195a5040ee81530e38c4857d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// file      : libbuild2/test/init.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/test/init.hxx>

#include <libbuild2/scope.hxx>
#include <libbuild2/target.hxx>
#include <libbuild2/rule.hxx>
#include <libbuild2/diagnostics.hxx>

#include <libbuild2/config/utility.hxx>

#include <libbuild2/script/timeout.hxx>

#include <libbuild2/test/module.hxx>
#include <libbuild2/test/target.hxx>
#include <libbuild2/test/operation.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  namespace test
  {
    void
    boot (scope& rs, const location&, module_boot_extra& extra)
    {
      tracer trace ("test::boot");

      l5 ([&]{trace << "for " << rs;});

      // Enter module variables. Do it during boot in case they get assigned
      // in bootstrap.build.
      //
      auto& vp (rs.var_pool ());

      common_data d {

        // Tests to execute.
        //
        // Specified as <target>@<path-id> pairs with both sides being
        // optional. The variable is untyped (we want a list of name-pairs),
        // overridable, and with global visibility. The target is relative (in
        // essence a prerequisite) which is resolved from the (root) scope
        // where the config.test value is defined.
        //
        vp.insert ("config.test"),

        // Test working directory before/after cleanup (see Testscript spec
        // for semantics).
        //
        vp.insert<name_pair> ("config.test.output"),

        // Test operation and individual test execution timeouts (see the
        // manual for semantics).
        //
        vp.insert<string> ("config.test.timeout"),

        // Test command runner path and options (see the manual for semantics).
        //
        vp.insert<strings> ("config.test.runner"),

        // The test variable is a name which can be a path (with the
        // true/false special values) or a target name.
        //
        vp.insert<name>    ("test", variable_visibility::target),
        vp.insert<strings> ("test.options"),
        vp.insert<strings> ("test.arguments"),

        // Test command runner path and options extracted from
        // config.test.runner.
        //
        vp.insert<process_path> ("test.runner.path"),
        vp.insert<strings>      ("test.runner.options"),

        // Prerequisite-specific.
        //
        // test.stdin and test.stdout can be used to mark a prerequisite as a
        // file to redirect stdin from and to compare stdout to, respectively.
        // test.roundtrip is a shortcut to mark a prerequisite as both stdin
        // and stdout.
        //
        // Prerequisites marked with test.input are treated as additional test
        // inputs: they are made sure to be up to date and their paths are
        // passed as additional command line arguments (after test.options and
        // test.arguments). Their primary use is to pass inputs that may have
        // varying file names/paths, for example:
        //
        // exe{parent}: exe{child}: test.input = true
        //
        // Note that currently this mechanism is only available to simple
        // tests though we could also support it for testscript (e.g., by
        // appending the input paths to test.arguments or by passing them in a
        // separate test.inputs variable).
        //
        vp.insert<bool> ("test.stdin",     variable_visibility::prereq),
        vp.insert<bool> ("test.stdout",    variable_visibility::prereq),
        vp.insert<bool> ("test.roundtrip", variable_visibility::prereq),
        vp.insert<bool> ("test.input",     variable_visibility::prereq),

        // Test target platform.
        //
        vp.insert<target_triplet> ("test.target")
      };

      // This one is used by other modules/rules.
      //
      vp.insert<bool> ("for_test", variable_visibility::prereq);

      // These are only used in testscript.
      //
      vp.insert<cmdline> ("test.redirects");
      vp.insert<cmdline> ("test.cleanups");

      // Unless already set, default test.target to build.host. Note that it
      // can still be overriden by the user, e.g., in root.build.
      //
      {
        value& v (rs.assign (d.test_target));

        if (!v || v.empty ())
          v = *rs.ctx.build_host;
      }

      // Register our operations.
      //
      rs.insert_operation (test_id,            op_test,            &d.var_test);
      rs.insert_operation (update_for_test_id, op_update_for_test, &d.var_test);

      extra.set_module (new module (move (d)));
    }

    bool
    init (scope& rs,
          scope&,
          const location& l,
          bool first,
          bool,
          module_init_extra& extra)
    {
      tracer trace ("test::init");

      if (!first)
      {
        warn (l) << "multiple test module initializations";
        return true;
      }

      l5 ([&]{trace << "for " << rs;});

      auto& m (extra.module_as<module> ());

      // Configuration.
      //
      using config::lookup_config;

      // Adjust module priority so that the config.test.* values are saved at
      // the end of config.build.
      //
      config::save_module (rs, "test", INT32_MAX);

      // config.test
      //
      if (lookup l = lookup_config (rs, m.config_test))
      {
        // Figure out which root scope it came from.
        //
        scope* s (&rs);
        for (;
             s != nullptr && !l.belongs (*s);
             s = s->parent_scope ()->root_scope ())
        assert (s != nullptr);

        m.test_ = &cast<names> (l);
        m.root_ = s;
      }

      // config.test.output
      //
      if (lookup l = lookup_config (rs, m.config_test_output))
      {
        const name_pair& p (cast<name_pair> (l));

        // If second half is empty, then first is the after value.
        //
        const name& a (p.second.empty () ? p.first  : p.second); // after
        const name& b (p.second.empty () ? p.second : p.first);  // before

        // Parse and validate.
        //
        if (!b.simple ())
          fail << "invalid config.test.output before value '" << b << "'";

        if (!a.simple ())
          fail << "invalid config.test.output after value '" << a << "'";

        if      (a.value == "clean") m.after = output_after::clean;
        else if (a.value == "keep")  m.after = output_after::keep;
        else fail << "invalid config.test.output after value '" << a << "'";

        if      (b.value == "fail")  m.before = output_before::fail;
        else if (b.value == "warn")  m.before = output_before::warn;
        else if (b.value == "clean") m.before = output_before::clean;
        else if (b.value == "")      m.before = output_before::clean;
        else fail << "invalid config.test.output before value '" << b << "'";
      }

      // config.test.timeout
      //
      if (lookup l = lookup_config (rs, m.config_test_timeout))
      {
        const string& t (cast<string> (l));

        const char* ot ("config.test.timeout test operation timeout value");
        const char* tt ("config.test.timeout test timeout value");

        size_t p (t.find ('/'));
        if (p != string::npos)
        {
          // Note: either of the timeouts can be omitted but not both.
          //
          if (t.size () == 1)
            fail << "invalid config.test.timeout value '" << t << "'";

          if (p != 0)
            m.operation_timeout = parse_timeout (string (t, 0, p), ot);

          if (p != t.size () - 1)
            m.test_timeout = parse_timeout (string (t, p + 1), tt);
        }
        else
          m.operation_timeout = parse_timeout (t, ot);
      }

      // config.test.runner
      //
      {
        value& pv (rs.assign (m.test_runner_path));
        value& ov (rs.assign (m.test_runner_options));

        if (lookup l = lookup_config (rs, m.config_test_runner))
        {
          const strings& args (cast<strings> (l));

          // Extract the runner process path.
          //
          {
            const string& s (args.empty () ? empty_string : args.front ());

            path p;
            try { p = path (s); } catch (const invalid_path&) {}

            if (p.empty ())
              fail << "invalid runner path '" << s << "' in "
                   << m.config_test_runner;

            pv = run_search (p, false /* init */);
            m.runner_path = &pv.as<process_path> ();
          }

          // Extract the runner options.
          //
          {
            ov = strings (++args.begin (), args.end ());
            m.runner_options = &ov.as<strings> ();
          }
        }
        else
        {
          pv = nullptr;
          ov = nullptr;
        }
      }

      //@@ TODO: Need ability to specify extra diff options (e.g.,
      //   --strip-trailing-cr, now hardcoded).
      //
      //@@ TODO: Pring report.

      // Environment.
      //
      // The only external program that we currently use is diff. None of the
      // implementations we looked at (GNU diffutils, FreeBSD) use any
      // environment variables.

      // Register target types.
      //
      {
        auto& tt (rs.insert_target_type<testscript> ());
        rs.insert_target_type_file ("testscript", tt);
      }

      // Register our test running rule.
      //
      {
        default_rule& dr (m);

        // Note: register for mtime_target to take priority over the fallback
        // rule below.
        //
        rs.insert_rule<target>       (perform_test_id, "test", dr);
        rs.insert_rule<mtime_target> (perform_test_id, "test", dr);
        rs.insert_rule<alias>        (perform_test_id, "test", dr);

        // Register the fallback file rule for the update-for-test operation,
        // similar to update.
        //
        rs.global_scope ().insert_rule<mtime_target> (
          perform_test_id, "test.file", file_rule::instance);
      }

      return true;
    }

    static const module_functions mod_functions[] =
    {
      {"test",  &boot,   &init},
      {nullptr, nullptr, nullptr}
    };

    const module_functions*
    build2_test_load ()
    {
      return mod_functions;
    }
  }
}