aboutsummaryrefslogtreecommitdiff
path: root/build2/test/script/runner.cxx
blob: 79cbfc45ad734bee6ceeaa6cc72db446650c98a5 (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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// file      : build2/test/script/runner.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/test/script/runner>

#include <set>
#include <iostream> // cerr

#include <build2/filesystem>

using namespace std;

namespace build2
{
  namespace test
  {
    namespace script
    {
      // Check if a path is not empty, the referenced file exists and is not
      // empty.
      //
      static bool
      non_empty (const path& p, const location& cl)
      {
        if (p.empty () || !exists (p))
          return false;

        try
        {
          ifdstream is (p);
          return is.peek () != ifdstream::traits_type::eof ();
        }
        catch (const io_error& e)
        {
          // While there can be no fault of the test command being currently
          // executed let's add the location anyway to ease the
          // troubleshooting. And let's stick to that principle down the road.
          //
          error (cl) << "unable to read " << p << ": " << e.what ();
          throw failed ();
        }
      }

      // Check if the test command output matches the expected result (redirect
      // value). Noop for redirect types other than none, here_string,
      // here_document.
      //
      static void
      check_output (const process_path& pr,
                    const char* nm,
                    const path& op,
                    const redirect& rd,
                    const location& cl,
                    scope& sp)
      {
        if (rd.type == redirect_type::none)
        {
          assert (!op.empty ());

          // Check that there is no output produced.
          //
          if (non_empty (op, cl))
            fail (cl) << pr << " unexpectedly writes to " << nm <<
              info << nm << " is saved to " << op;
        }
        else if (rd.type == redirect_type::here_string ||
                 rd.type == redirect_type::here_document)
        {
          assert (!op.empty ());

          path orp (op + ".orig");

          try
          {
            ofdstream os (orp);
            sp.cleanups.emplace_back (orp);

            os << (rd.type == redirect_type::here_string
                   ? rd.str
                   : rd.doc.doc);

            os.close ();
          }
          catch (const io_error& e)
          {
            fail (cl) << "unable to write " << orp << ": " << e.what ();
          }

          // Use diff utility to compare the output with the expected result.
          //
          path dp ("diff");
          process_path pp (run_search (dp, true));

          cstrings args {
            pp.recall_string (),
            "--strip-trailing-cr",
            "-u",
            orp.string ().c_str (),
            op.string ().c_str (),
            nullptr};

          if (verb >= 2)
            print_process (args);

          try
          {
            // Diff utility prints the differences to stdout. But for the user
            // it is a part of the test failure diagnostics so let's redirect
            // stdout to stderr.
            //
            process p (pp, args.data (), 0, 2);

            try
            {
              if (p.wait ())
                return;

              // Output doesn't match the expected result.
              //
              diag_record d (error (cl));
              d << pr << " " << nm << " doesn't match the expected output";

              auto output_info =
                [&d, &nm, &cl] (const path& p, const char* prefix)
              {
                if (non_empty (p, cl))
                  d << info << prefix << nm << " is saved to " << p;
                else
                  d << info << prefix << nm << " is empty";
              };

              output_info (op, "");
              output_info (orp, "expected ");

              // Fall through.
              //
            }
            catch (const io_error&)
            {
              // Child exit status doesn't matter. Assume the child process
              // issued diagnostics. Just wait for the process completion.
              //
              p.wait (); // Check throw.

              error (cl) << "failed to compare " << nm
                         << " with the expected output";
            }

            // Fall through.
            //
          }
          catch (const process_error& e)
          {
            error (cl) << "unable to execute " << pp << ": " << e.what ();

            if (e.child ())
              exit (1);
          }

          throw failed ();
        }
      }

      void concurrent_runner::
      enter (scope& sp, const location& cl)
      {
        if (!exists (sp.wd_path))
          // @@ Shouldn't we add an optional location parameter to mkdir() and
          // alike utility functions so the failure message can contain
          // location info?
          //
          mkdir (sp.wd_path, 2);
        else if (!empty (sp.wd_path))
          // @@ Shouldn't we have --wipe or smth?
          //
          fail (cl) << "directory " << sp.wd_path << " is not empty" <<
            info << "clean it up and rerun";

        sp.cleanups.emplace_back (sp.wd_path);
      }

      void concurrent_runner::
      leave (scope& sp, const location& cl)
      {
        // Remove files and directories in the order opposite to the order of
        // cleanup registration. Handle paths multiple registration (which is a
        // valid case).
        //
        // Note that we operate with normalized paths here.
        //
        set<path> rp;
        for (auto& p: reverse_iterate (sp.cleanups))
        {
          auto i (rp.emplace (move (p)));
          if (i.second) // Remove the path if seen for the first time.
          {
            const path& p (*i.first);
            if (p.to_directory ())
            {
              dir_path d (path_cast<dir_path> (p));
              rmdir_status r (rmdir (d, 2));

              if (r != rmdir_status::success)
                fail (cl) << "registered for cleanup directory " << d
                          << (r == rmdir_status::not_empty
                              ? " is not empty"
                              : " does not exist");
            }
            else if (rmfile (p, 2) == rmfile_status::not_exist)
              fail (cl) << "registered for cleanup file " << p
                        << " does not exist";
          }
        }
      }

      void concurrent_runner::
      run (scope& sp, const command& c, size_t ci, const location& cl)
      {
        if (verb >= 3)
          text << c;

        // Pre-search the program path so it is reflected in the failure
        // diagnostics. The user can see the original path running the test
        // operation with the verbosity level > 2.
        //
        process_path pp (run_search (c.program, true));
        cstrings args {pp.recall_string ()};

        for (const auto& a: c.arguments)
          args.push_back (a.c_str ());

        args.push_back (nullptr);

        try
        {
          // For stdin 'none' redirect type we somehow need to make sure that
          // the child process doesn't read from stdin. That is tricky to do in
          // a portable way. Here we suppose that the program which
          // (erroneously) tries to read some data from stdin being redirected
          // to /dev/null fails not being able to read the expected data, and
          // so the test doesn't pass through.
          //
          // @@ Obviously doesn't cover the case when the process reads
          //    whatever available.
          // @@ Another approach could be not to redirect stdin and let the
          //    process to hang which can be interpreted as a test failure.
          // @@ Both ways are quite ugly. Is there some better way to do this?
          //

          // Normalize a path. Also make relative path absolute using the
          // scope's working directory unless it is already absolute.
          //
          auto normalize = [&sp, &cl] (path p) -> path
          {
            path r (p.absolute () ? move (p) : sp.wd_path / move (p));

            try
            {
              r.normalize ();
            }
            catch (const invalid_path& e)
            {
              fail (cl) << "invalid file path " << e.path;
            }

            return r;
          };

          int in;
          ifdstream si;

          switch (c.in.type)
          {
          case redirect_type::pass:          in =  0; break;

          case redirect_type::here_string:
          case redirect_type::here_document: in = -1; break;

          case redirect_type::null:
          case redirect_type::none:          in = -2; break;

          case redirect_type::file:
            {
              path p (normalize (c.in.file.path));

              try
              {
                si.open (p);
              }
              catch (const io_error& e)
              {
                fail (cl) << "unable to read " << p << ": " << e.what ();
              }

              in = si.fd ();
              break;
            }

          case redirect_type::merge: assert (false); break;
          }

          // Dealing with stdout and stderr redirect types other than 'null'
          // using pipes is tricky in the general case. Going this path we
          // would need to read both streams in non-blocking manner which we
          // can't (easily) do in a portable way. Using diff utility to get a
          // nice-looking actual/expected outputs difference would complicate
          // things further.
          //
          // So the approach is the following. Child standard streams are
          // redirected to files. When the child exits and the exit status is
          // validated we just sequentially compare each file content with the
          // expected output. The positive side-effect of this approach is that
          // the output of a faulty test command can be provided for
          // troubleshooting.
          //

          // Open a file for command output redirect if requested explicitly
          // (file redirect) or for the purpose of the output validation (none,
          // here_string, here_document), register the file for cleanup, return
          // the file descriptor. Return the specified, default and -2 file
          // descriptors for merge, pass and null redirects respectively not
          // opening a file.
          //
          auto open = [&sp, &ci, &cl, &normalize] (const redirect& r,
                                                   int dfd,
                                                   path& p,
                                                   ofdstream& os) -> int
          {
            assert (dfd == 1 || dfd == 2);

            ofdstream::openmode m (ofdstream::out);

            switch (r.type)
            {
            case redirect_type::pass:  return dfd;
            case redirect_type::null:  return -2;
            case redirect_type::merge: return r.fd;

            case redirect_type::file:
              {
                p = normalize (r.file.path);

                if (r.file.append)
                  m |= ofdstream::app;

                break;
              }

            case redirect_type::none:
            case redirect_type::here_string:
            case redirect_type::here_document:
              {
                path op (dfd == 1 ? "stdout" : "stderr");

                // 0 if belongs to a single-command test scope, otherwise is
                // the command number (start from one) in the test scope.
                //
                if (ci > 0)
                  op += "-" + to_string (ci);

                p = normalize (move (op));
                break;
              }
            }

            try
            {
              os.open (p, m);
            }
            catch (const io_error& e)
            {
              fail (cl) << "unable to write " << p << ": " << e.what ();
            }

            // It is a valid case if the file path is repeatedly registered for
            // cleanup. It is handled during cleanup procedure.
            //
            sp.cleanups.emplace_back (p);
            return os.fd ();
          };

          path stdout;
          ofdstream so;
          int out (open (c.out, 1, stdout, so));

          path stderr;
          ofdstream se;
          int err (open (c.err, 2, stderr, se));

          if (verb >= 2)
            print_process (args);

          process pr (sp.wd_path.string ().c_str (),
                      pp,
                      args.data (),
                      in, out, err);

          try
          {
            si.close ();
            so.close ();
            se.close ();

            if (in == -1) // here_string, here_document redirects.
            {
              ofdstream os (pr.out_fd);
              const redirect& r (c.in);
              os << (r.type == redirect_type::here_string ? r.str : r.doc.doc);
              os.close ();
            }

            // Just wait. The program failure can mean the test success.
            //
            pr.wait ();

            // If there is no correct exit status by whatever reason then dump
            // stderr (if cached), print the proper diagnostics and fail.
            //
            optional<process::status_type> status (move (pr.status));
            bool valid_status (status && *status >= 0 && *status < 256);
            bool eq (c.exit.comparison == exit_comparison::eq);

            bool correct_status (valid_status &&
                                 (*status == c.exit.status) == eq);

            if (!correct_status)
            {
              // Dump cached stderr.
              //
              if (exists (stderr))
              {
                try
                {
                  ifdstream is (stderr);
                  if (is.peek () != ifdstream::traits_type::eof ())
                    cerr << is.rdbuf ();
                }
                catch (const io_error& e)
                {
                  fail (cl) << "unable to read " << stderr << ": "
                            << e.what ();
                }
              }

              // Fail with a proper diagnostics.
              //
              diag_record d (fail (cl));

              if (!status)
                d << pp << " terminated abnormally";
              else if (!valid_status)
                d << pp << " exit status " << *status << " is invalid" <<
                  info << "must be an unsigned integer < 256";
              else if (!correct_status)
                d << pp << " exit status " << *status
                  << (eq ? " != " : " == ")
                  << static_cast<uint16_t> (c.exit.status);
              else
                assert (false);

              if (non_empty (stdout, cl))
                d << info << "stdout is saved to " << stdout;

              if (non_empty (stderr, cl))
                d << info << "stderr is saved to " << stderr;
            }

            // Check if the standard outputs match expectations.
            //
            check_output (pp, "stdout", stdout, c.out, cl, sp);
            check_output (pp, "stderr", stderr, c.err, cl, sp);
          }
          catch (const io_error& e)
          {
            // Child exit status doesn't matter. Just wait for the process
            // completion.
            //
            pr.wait (); // Check throw.

            fail (cl) << "IO operation failed for " << pp << ": " << e.what ();
          }
        }
        catch (const process_error& e)
        {
          error (cl) << "unable to execute " << pp << ": " << e.what ();

          if (e.child ())
            exit (1);

          throw failed ();
        }
      }
    }
  }
}