aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/test/script/runner.cxx
blob: eb30e0229fce6845df8a804cf81c7e6f25ed95ce (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
// file      : libbuild2/test/script/runner.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/test/script/runner.hxx>

#include <libbuild2/script/run.hxx>

#include <libbuild2/test/common.hxx>

namespace build2
{
  namespace test
  {
    namespace script
    {
      using namespace build2::script;

      bool default_runner::
      test (scope& s) const
      {
        return common_.test (s.root.test_target, s.id_path);
      }

      pair<const process_path*, const strings*> default_runner::
      test_runner ()
      {
        return make_pair (common_.runner_path, common_.runner_options);
      }

      void default_runner::
      enter (scope& sp, const location&)
      {
        context& ctx (sp.context);

        auto df = make_diag_frame (
          [&sp](const diag_record& dr)
          {
            // Let's not depend on how the path representation can be improved
            // for readability on printing.
            //
            dr << info << "test id: " << sp.id_path.posix_string ();
          });

        // Scope working directory shall be empty (the script working
        // directory is cleaned up by the test rule prior the script
        // execution).
        //
        // Create the root working directory containing the .buildignore file
        // to make sure that it is ignored by name patterns (see buildignore
        // description for details).
        //
        // @@ Shouldn't we add an optional location parameter to mkdir() and
        // alike utility functions so the failure message can contain
        // location info?
        //
        fs_status<mkdir_status> r (
          sp.parent == nullptr
          ? mkdir_buildignore (
            ctx,
            *sp.work_dir.path,
            sp.root.target_scope.root_scope ()->root_extra->buildignore_file,
            2)
          : mkdir (*sp.work_dir.path, 2));

        if (r == mkdir_status::already_exists)
          fail << diag_path (sp.work_dir) << " already exists" <<
            info << "are tests stomping on each other's feet?";

        // We don't change the current directory here but indicate that the
        // scope test commands will be executed in that directory.
        //
        if (verb >= 2)
          text << "cd " << *sp.work_dir.path;
      }

      void default_runner::
      leave (scope& sp, const location& ll)
      {
        auto df = make_diag_frame (
          [&sp](const diag_record& dr)
          {
            // Let's not depend on how the path representation can be improved
            // for readability on printing.
            //
            dr << info << "test id: " << sp.id_path.posix_string ();
          });

        // Perform registered cleanups if requested.
        //
        if (common_.after == output_after::clean)
        {
          clean (sp, ll);

          context& ctx (sp.context);

          rmdir_status r (
            sp.parent == nullptr
            ?  rmdir_buildignore (ctx,
                                  *sp.work_dir.path,
                                  sp.root.target_scope.root_scope ()->
                                    root_extra->buildignore_file,
                                  2)
            : rmdir (ctx, *sp.work_dir.path, 2));

          if (r != rmdir_status::success)
          {
            diag_record dr (fail (ll));

            dr << diag_path (sp.work_dir)
               << (r == rmdir_status::not_exist
                   ? " does not exist"
                   : " is not empty");

            if (r == rmdir_status::not_empty)
              print_dir (dr, *sp.work_dir.path, ll);
          }
        }

        // Return to the parent scope directory or to the out_base one for the
        // script scope.
        //
        if (verb >= 2)
          text << "cd " << (sp.parent != nullptr
                            ? *sp.parent->work_dir.path
                            : sp.work_dir.path->directory ());
      }

      void default_runner::
      run (scope& sp,
           const command_expr& expr, command_type ct,
           size_t li, const location& ll)
      {
        // Noop for teardown commands if keeping tests output is requested.
        //
        if (ct == command_type::teardown &&
            common_.after == output_after::keep)
          return;

        if (verb >= 3)
        {
          char c ('\0');

          switch (ct)
          {
          case command_type::test:     c = ' '; break;
          case command_type::setup:    c = '+'; break;
          case command_type::teardown: c = '-'; break;
          }

          text << ": " << c << expr;
        }

        // Print test id once per test expression.
        //
        auto df = make_diag_frame (
          [&sp](const diag_record& dr)
          {
            // Let's not depend on how the path representation can be improved
            // for readability on printing.
            //
            dr << info << "test id: " << sp.id_path.posix_string ();
          });

        build2::script::run (sp, expr, li, ll);
      }

      bool default_runner::
      run_if (scope& sp,
              const command_expr& expr,
              size_t li, const location& ll)
      {
        if (verb >= 3)
          text << ": ?" << expr;

        // Print test id once per test expression.
        //
        auto df = make_diag_frame (
          [&sp](const diag_record& dr)
          {
            // Let's not depend on how the path representation can be improved
            // for readability on printing.
            //
            dr << info << "test id: " << sp.id_path.posix_string ();
          });

        return build2::script::run_if (sp, expr, li, ll);
      }
    }
  }
}