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

#ifndef _WIN32
#  include <sys/wait.h>  // WIFEXITED(), WEXITSTATUS()
#endif

#include <iostream> // cerr

#include <butl/filesystem> // auto_rm

#include <build2/test/script/runner>

using namespace std;
using namespace butl;

namespace build2
{
  namespace test
  {
    namespace script
    {
      // Test command output cache. The usage is as follows:
      //
      // 1. Call wopen() to open the stream in write mode and register the file
      //    for auto-removal by dtor.
      //
      // 2. Pass the file descriptor to the test command process ctor to
      //    redirect it's output to the cache file.
      //
      // 3. Close the stream.
      //
      // 4. Call ropen() to open the file in read mode to match the content to
      //    a pattern, cancel the file removal if the match fails (so the
      //    output is available for troubleshooting).
      //
      class stream_cache: public ifdstream
      {
      public:
        using path_type = butl::path;

        stream_cache (const char* n): name (n) {}

        // Open stream for writing. Return the file descriptor. Must not be
        // called multiple times.
        //
        int
        wopen ()
        {
          // Otherwise compiler gets confused with basic_ios::fail().
          //
          using build2::fail;

          assert (!exists ());

          try
          {
            // @@ Later it will make sense to create the file in the
            //    test-specific temporary directory.
            //
            path = path_type::temp_path ("build2");
          }
          catch (const system_error& e)
          {
            fail << "unable to create temporary file: " << e.what ();
          }

          try
          {
            open (path, out | trunc);
            cleanup = auto_rm<path_type> (path);
          }
          catch (const io_error& e)
          {
            fail << "unable to write " << path << ": " << e.what ();
          }

          return fd ();
        }

        // Open stream for reading. Return true if the file is not empty,
        // false otherwise. Must not be called before wopen().
        //
        bool
        ropen ()
        {
          // Otherwise compiler gets confused with basic_ios::fail().
          //
          using build2::fail;

          assert (exists ());

          try
          {
            open (path, in);
            return peek () != ifdstream::traits_type::eof ();
          }
          catch (const io_error& e)
          {
            error << "unable to read " << path << ": " << e.what ();
            throw failed ();
          }
        }

        // Return true if wopen() was called, false otherwise.
        //
        bool
        exists () const {return !path.empty ();}

        ~stream_cache () override
        {
          close (); // Close the stream prior to the file deletion.
        }

      public:
        string name;
        path_type path;
        auto_rm<path_type> cleanup;
      };

      // Check if the test command output matches the pattern (redirect value).
      //
      static void
      check_output (const process_path& program,
                    stream_cache& sc,
                    const redirect& rd)
      {
        if (rd.type == redirect_type::none)
        {
          // Check that the cache file is empty.
          //
          if (sc.ropen ())
          {
            sc.cleanup.cancel ();

            fail << program << " unexpectedly writes to " << sc.name <<
                info << sc.name << " is saved to " << sc.path;
          }
        }
        else if (rd.type == redirect_type::here_string ||
                 rd.type == redirect_type::here_document)
        {
          // Use diff utility to compare the output with the pattern.
          //
          path dp ("diff");
          process_path pp (run_search (dp, true));

          cstrings args {
            pp.recall_string (),
            "--strip-trailing-cr",
            "-u",
            "-",
            sc.path.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 pr (pp, args.data (), -1, 2);

            try
            {
              ofdstream os (pr.out_fd);

              auto write_value = [&os, &rd] ()
              {
                os << rd.value;

                // Here-document is always endline-terminated.
                //
                if (rd.type == redirect_type::here_string)
                  os << endl;

                os.close ();
              };

              write_value ();

              if (pr.wait ())
                return;

              // Output doesn't match the pattern string. Keep non-empty output
              // and save the pattern for troubleshooting.
              //
              path p (sc.path + ".pattern");

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

              diag_record d (error);
              d << program << " " << sc.name
                << " doesn't match the pattern";

              if (sc.ropen ())
              {
                sc.cleanup.cancel ();
                d << info << sc.name << " is saved to " << sc.path;
              }
              else
                d << info << sc.name << " is empty";

              // Pattern is never empty (contains at least newline).
              //
              d << info << sc.name << " pattern is saved to " << p;

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

            // Fall through.
            //
          }
          catch (const process_error& e)
          {
            error << "unable to execute " << args[0] << ": " << e.what ();

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

          throw failed ();
        }
      }

      void concurrent_runner::
      run (const command& c)
      {
        if (verb >= 3)
        {
          // @@ When running multiple threads will need to synchronize
          // printing the diagnostics so it don't overlap for concurrent
          // tests. Alternatively we can not bother with that and expect a
          // user to re-run test operation in the single-thread mode.
          //
          // @@ No indentation performed for here-documents. If to fix then
          // probably need to do on diag_record level in a way similar to
          // butl::pager approach.
          //
          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);

        // Normally while handling child process failures (IO errors, non-zero
        // exit status) we suppress the diagnostics supposing that the child
        // issues it's own one. While this is reasonable to expect from known
        // production-quality programs here it can result in the absense of any
        // diagnostics at all. Also the child stderr (and so diagnostics) can
        // be redirected to /dev/null and not be available for the user. This
        // why we will always issue the diagnostics despite the fact sometimes
        // it can look redundant.
        //
        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 do read 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?
          //
          int in (c.in.type == redirect_type::null ||
                  c.in.type == redirect_type::none
                  ? -2
                  : -1);

          // 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 a non-blocking manner which we
          // can't (easily) do in a portable way. Using diff utility to get a
          // nice-looking stream/pattern difference would complicate things
          // further.
          //
          // So the approach is the following. Child standard stream are
          // redirected to files. When the child exits and the exit status is
          // validated we just sequentially compare each file content with the
          // corresponding pattern. The positive side-effect of this approach
          // is that the output of a faulty test command can be provided for
          // troubleshooting.
          //
          stream_cache osc ("stdout");
          int out (c.out.type == redirect_type::null ? -2 : osc.wopen ());

          stream_cache esc ("stderr");
          int err (c.err.type == redirect_type::null ? -2 : esc.wopen ());

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

          process pr (pp, args.data (), in, out, err);

          try
          {
            osc.close ();
            esc.close ();

            if (c.in.type == redirect_type::here_string ||
                c.in.type == redirect_type::here_document)
            {
              ofdstream os (pr.out_fd);
              os << c.in.value;

              // Here-document is always endline-terminated.
              //
              if (c.in.type == redirect_type::here_string)
                os << endl;

              os.close ();
            }

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

            // Check if the process terminated normally and obtain the status
            // if that's the case.
            //
            bool abnorm;
            process::status_type status;

            // @@ Shouldn't we incorporate means for checking for abnormal
            //    termination and getting the real exit status into
            //    libbutl::process?
            //
#ifndef _WIN32
            abnorm = !WIFEXITED (pr.status);
            status = abnorm ? 1 : WEXITSTATUS (pr.status);
#else
            // @@ Is there a reliable way to detect if the process terminated
            //    abnormally on Windows?
            //
            abnorm = false;
            status = pr.status;
#endif
            bool valid_status (!abnorm && status >= 0 && status < 256);

            bool eq (c.exit.comparison == exit_comparison::eq);

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

            // If there is no correct exit status by whatever reason then dump
            // stderr (if cached), keep both stdout and stderr (those which
            // are cached) for troubleshooting, and finally fail.
            //
            if (!correct_status)
            {
              if (esc.exists () && esc.ropen ())
                cerr << esc.rdbuf ();

              // Keep non-empty cache files and fail with a proper diagnostics.
              //
              diag_record d (fail);

              if (abnorm)
                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 ? " != " : " == ") << (int)c.exit.status; //@@
              else
                assert (false);

              auto keep_stream = [&d] (stream_cache& sc)
              {
                if (sc.exists () && sc.ropen ())
                {
                  sc.cleanup.cancel ();
                  d << info << sc.name << " is saved to " << sc.path;
                }
              };

              keep_stream (osc);
              keep_stream (esc);
            }

            check_output (pp, osc, c.out);
            check_output (pp, esc, c.err);
          }
          catch (const io_error& e)
          {
            // Child exit status doesn't matter. Just wait for the process
            // completion.
            //
            pr.wait (); // Check throw.

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

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

          throw failed ();
        }
      }
    }
  }
}