aboutsummaryrefslogtreecommitdiff
path: root/build2/test/init.cxx
blob: 93ca1e4be553be4da4d1ac697266f596d2ce39b0 (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
// file      : build2/test/init.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/test/init.hxx>

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

#include <build2/config/utility.hxx>

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

using namespace std;
using namespace butl;

namespace build2
{
  namespace test
  {
    void
    boot (scope& rs, const location&, unique_ptr<module_base>&)
    {
      tracer trace ("test::boot");

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

      // Register the test operation.
      //
      rs.operations.insert (test_id, test);

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

      // 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 inheritable. 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", true);

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

      // Note: none are overridable.
      //
      // 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<name>    ("test.input",     variable_visibility::project);
      vp.insert<name>    ("test.output",    variable_visibility::project);
      vp.insert<name>    ("test.roundtrip", variable_visibility::project);
      vp.insert<strings> ("test.options",   variable_visibility::project);
      vp.insert<strings> ("test.arguments", variable_visibility::project);

      // These are only used in testscript.
      //
      vp.insert<strings> ("test.redirects", variable_visibility::project);
      vp.insert<strings> ("test.cleanups",  variable_visibility::project);

      // Test target platform.
      //
      // 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 (
            vp.insert<target_triplet> (
              "test.target", variable_visibility::project)));

        if (!v || v.empty ())
          v = cast<target_triplet> ((*global_scope)["build.host"]);
      }
    }

    bool
    init (scope& rs,
          scope&,
          const location& l,
          unique_ptr<module_base>& mod,
          bool first,
          bool,
          const variable_map& config_hints)
    {
      tracer trace ("test::init");

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

      const dir_path& out_root (rs.out_path ());
      l5 ([&]{trace << "for " << out_root;});

      assert (mod == nullptr);
      mod.reset (new module ());
      module& m (static_cast<module&> (*mod));

      // Configure.
      //
      assert (config_hints.empty ()); // We don't known any hints.

      // 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 = config::omitted (rs, "config.test").first)
      {
        // 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 = config::omitted (rs, "config.test.output").first)
      {
        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 << "'";
      }

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

      // Register target types.
      //
      {
        auto& t (rs.target_types);

        t.insert<testscript> ();
      }

      // Register rules.
      //
      {
        const rule&        r (m);
        const alias_rule& ar (m);

        // Register our test running rule.
        //
        rs.rules.insert<target> (perform_test_id, "test", r);
        rs.rules.insert<alias> (perform_test_id, "test", ar);

        // Register our rule for the dist meta-operation. We need to do this
        // because we may have ad hoc prerequisites (test input/output files)
        // that need to be entered into the target list.
        //
        rs.rules.insert<target> (dist_id, test_id, "test", r);
      }

      return true;
    }
  }
}