aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/test/script/script.hxx
blob: 53e4329d9b3d32d296a1a4597f60ab1d01214880 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// file      : libbuild2/test/script/script.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBUILD2_TEST_SCRIPT_SCRIPT_HXX
#define LIBBUILD2_TEST_SCRIPT_SCRIPT_HXX

#include <set>

#include <libbuild2/types.hxx>
#include <libbuild2/forward.hxx>
#include <libbuild2/utility.hxx>

#include <libbuild2/variable.hxx>

#include <libbuild2/script/script.hxx>

#include <libbuild2/test/target.hxx>

namespace build2
{
  namespace test
  {
    namespace script
    {
      using build2::script::line;
      using build2::script::lines;
      using build2::script::redirect;
      using build2::script::redirect_type;
      using build2::script::line_type;
      using build2::script::command_expr;
      using build2::script::expr_term;
      using build2::script::command;
      using build2::script::deadline;
      using build2::script::timeout;

      class parser; // Required by VC for 'friend class parser' declaration.

      // command_type
      //
      enum class command_type {test, setup, teardown};

      // description
      //
      struct description
      {
        string id;
        string summary;
        string details;

        bool
        empty () const
        {
          return id.empty () && summary.empty () && details.empty ();
        }
      };

      // scope
      //
      class script;

      class scope_base // Make sure certain things are initialized early.
      {
      public:
        script& root; // Self for the root (script) scope.

        // Note that if we pass the variable name as a string, then it will
        // be looked up in the wrong pool.
        //
        variable_map vars;

      protected:
        scope_base (script&);

        const dir_path*
        wd_path () const;

        const target_triplet&
        test_tt () const;
      };

      enum class scope_state {unknown, passed, failed};

      class scope: public scope_base, public build2::script::environment
      {
      public:
        scope* const parent; // NULL for the root (script) scope.

        // The chain of if-else scope alternatives. See also if_cond_ below.
        //
        unique_ptr<scope> if_chain;

        const path& id_path;     // Id path ($@, relative in POSIX form).

        optional<description> desc;

        scope_state state = scope_state::unknown;

        void
        set_variable (string&& name,
                      names&&,
                      const string& attrs,
                      const location&) override;

        // Noop since the temporary directory is a working directory and so
        // is created before the scope commands execution.
        //
        virtual void
        create_temp_dir () override {assert (false);};

        // Return true if this is a test program path.
        //
        // Note that currently the test program is only specified via the test
        // variable ($0 effectively). In the future we may invent some other
        // means of marking a program as a test (builtin, etc).
        //
        bool
        test_program (const path&);

        // Variables.
        //
      public:
        // Lookup the variable starting from this scope, continuing with outer
        // scopes, then the target being tested, then the testscript target,
        // and then outer buildfile scopes (including testscript-type/pattern
        // specific).
        //
        using lookup_type = build2::lookup;

        lookup_type
        lookup (const variable&) const;

        // As above but only look for buildfile variables. If target_only is
        // false then also look in scopes of the test target (this should only
        // be done if the variable's visibility is target).
        //
        lookup_type
        lookup_in_buildfile (const string&, bool target_only = true) const;

        // Return a value suitable for assignment. If the variable does not
        // exist in this scope's variable map, then a new one with the NULL
        // value is added and returned. Otherwise the existing value is
        // returned.
        //
        value&
        assign (const variable& var) {return vars.assign (var);}

        // Return a value suitable for append/prepend. If the variable does
        // not exist in this scope's variable map, then outer scopes are
        // searched for the same variable. If found then a new variable with
        // the found value is added to this scope and returned. Otherwise this
        // function proceeds as assign() above.
        //
        value&
        append (const variable&);

        // Reset special $*, $N variables based on the test.* values.
        //
        void
        reset_special ();

      protected:
        scope (const string& id, scope* parent, script& root);

        // Pre-parse data.
        //
      public:
        virtual bool
        empty () const = 0;

      protected:
        friend class parser;

        location start_loc_;
        location end_loc_;

        optional<line> if_cond_;

        // Test program paths.
        //
        // Currently always contains a single element (see test_program() for
        // details). While in the future there can be more of them, the zero
        // index will always refer to the test variable value and can
        // potentially be NULL (see reset_special() for details).
        //
        small_vector<const path*, 1> test_programs_;
      };

      // group
      //
      class group: public scope
      {
      public:
        group (const string& id, group& p): scope (id, &p, p.root) {}

      public:
        vector<unique_ptr<scope>> scopes;

        // The test group execution deadline and the individual test timeout.
        //
        optional<deadline> group_deadline;
        optional<timeout>  test_timeout;

        // Parse the argument having the '[<group-timeout>]/[<test-timeout>]'
        // form, where the values are expressed in seconds and either of them
        // (but not both) can be omitted, and set the group deadline and test
        // timeout respectively, if specified. Reset them to nullopt on zero.
        //
        virtual void
        set_timeout (const string&, bool success, const location&) override;

        // Return the nearest of the own deadline and the enclosing groups
        // deadlines.
        //
        virtual optional<deadline>
        effective_deadline () override;

      protected:
        group (const string& id, script& r): scope (id, nullptr, r) {}

        // Pre-parse data.
        //
      public:
        virtual bool
        empty () const override
        {
          return
            !if_cond_ && // The condition expression can have side-effects.
            setup_.empty () &&
            tdown_.empty () &&
            find_if (scopes.begin (), scopes.end (),
                     [] (const unique_ptr<scope>& s)
                     {
                       return !s->empty ();
                     }) == scopes.end ();
        }

      private:
        friend class parser;

        lines setup_;
        lines tdown_;
      };

      // test
      //
      class test: public scope
      {
      public:
        test (const string& id, group& p): scope (id, &p, p.root) {}

      public:
        // The whole test and the remaining test fragment execution deadlines.
        //
        // The former is based on the minimum of the test timeouts set for the
        // enclosing scopes and is calculated on the first deadline() call.
        // The later is set by set_timeout() from the timeout builtin call
        // during the test execution.
        //
        optional<optional<deadline>> test_deadline; // calculated<specified<>>
        optional<deadline>           fragment_deadline;

        // Parse the specified in seconds timeout and set the remaining test
        // fragment execution deadline. Reset it to nullopt on zero.
        //
        virtual void
        set_timeout (const string&, bool success, const location&) override;

        // Return the nearest of the test and fragment execution deadlines,
        // calculating the former on the first call.
        //
        virtual optional<deadline>
        effective_deadline () override;

        // Pre-parse data.
        //
      public:
        virtual bool
        empty () const override
        {
          return tests_.empty ();
        }

      private:
        friend class parser;

        lines tests_;
      };

      // script
      //
      class script_base // Make sure certain things are initialized early.
      {
      protected:
        script_base (const target& test_target,
                     const testscript& script_target);

      public:
        const target&        test_target;   // Target we are testing.
        const build2::scope& target_scope;  // Base scope of test target.
        const testscript&    script_target; // Target of the testscript file.

      public:
        variable_pool var_pool;
        mutable shared_mutex var_pool_mutex;

        // Used to compose a test command.
        //
        // Changing any of their values requires resetting the $* and $N
        // special aliases.
        //
        const variable& test_var;      // test
        const variable& options_var;   // test.options
        const variable& arguments_var; // test.arguments
        const variable& redirects_var; // test.redirects
        const variable& cleanups_var;  // test.cleanups

        bool
        test_command_var (const string& name) const
        {
          return name == test_var.name      ||
                 name == options_var.name   ||
                 name == arguments_var.name ||
                 name == redirects_var.name ||
                 name == cleanups_var.name;
        }

        const variable& wd_var;       // $~
        const variable& id_var;       // $@
        const variable& cmd_var;      // $*
        const variable* cmdN_var[10]; // $N
      };

      class script: public script_base, public group
      {
      public:
        // The test operation deadline and the individual test timeout (see
        // the config.test.timeout variable for details).
        //
        optional<deadline> operation_deadline;
        optional<timeout>  test_timeout;

      public:
        script (const target& test_target,
                const testscript& script_target,
                const dir_path& root_wd);

        script (script&&) = delete;
        script (const script&) = delete;
        script& operator= (script&&) = delete;
        script& operator= (const script&) = delete;

        // Return the nearest of the test operation and group execution
        // deadlines.
        //
        virtual optional<deadline>
        effective_deadline () override;

        // Pre-parse data.
        //
      private:
        friend class parser;

        // Testscript file paths. Specifically, replay_token::file points to
        // these path names.
        //
        struct compare_paths
        {
          bool operator() (const path_name_value& x,
                           const path_name_value& y) const
          {
            // Note that these path names are always paths, so we compare them
            // as paths.
            //
            return x.path < y.path;
          }
        };

        std::set<path_name_value, compare_paths> paths_;
      };
    }
  }
}

#endif // LIBBUILD2_TEST_SCRIPT_SCRIPT_HXX