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
|
// file : libbuild2/test/script/parser.test.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <iostream>
#include <libbuild2/types.hxx>
#include <libbuild2/utility.hxx>
#include <libbuild2/target.hxx>
#include <libbuild2/context.hxx>
#include <libbuild2/scheduler.hxx>
#include <libbuild2/file-cache.hxx>
#include <libbuild2/test/target.hxx>
#include <libbuild2/test/script/token.hxx>
#include <libbuild2/test/script/parser.hxx>
#include <libbuild2/test/script/runner.hxx>
#undef NDEBUG
#include <cassert>
using namespace std;
namespace build2
{
namespace test
{
namespace script
{
// Here we assume we are running serially.
//
class print_runner: public runner
{
public:
print_runner (bool scope, bool id, bool line, bool iterations)
: scope_ (scope),
id_ (id),
line_ (line),
iterations_ (iterations) {}
virtual bool
test (scope&) const override
{
return true;
}
virtual pair<const process_path*, const strings*>
test_runner () override
{
return make_pair (nullptr, nullptr);
}
virtual void
enter (scope& s, const location&) override
{
if (s.desc)
{
const auto& d (*s.desc);
if (!d.id.empty ())
cout << ind_ << ": id:" << d.id << endl;
if (!d.summary.empty ())
cout << ind_ << ": sm:" << d.summary << endl;
if (!d.details.empty ())
{
if (!d.id.empty () || !d.summary.empty ())
cout << ind_ << ":" << endl; // Blank.
const auto& s (d.details);
for (size_t b (0), e (0), n; e != string::npos; b = e + 1)
{
e = s.find ('\n', b);
n = ((e != string::npos ? e : s.size ()) - b);
cout << ind_ << ':';
if (n != 0)
{
cout << ' ';
cout.write (s.c_str () + b, static_cast<streamsize> (n));
}
cout << endl;
}
}
}
if (scope_)
{
cout << ind_ << "{";
if (id_ && !s.id_path.empty ()) // Skip empty root scope id.
cout << " # " << s.id_path.string ();
cout << endl;
ind_ += " ";
}
}
virtual void
run (scope& env,
const command_expr& e, command_type t,
const iteration_index* ii, size_t i,
const function<command_function>& cf,
const location& ll) override
{
// If the functions is specified, then just execute it with an empty
// stdin so it can perform the housekeeping (stop replaying tokens,
// increment line index, etc).
//
if (cf != nullptr)
{
assert (e.size () == 1 && !e[0].pipe.empty ());
const command& c (e[0].pipe.back ());
// Must be enforced by the caller.
//
assert (!c.out && !c.err && !c.exit);
cf (env, c.arguments,
fdopen_null (), nullptr /* pipe */,
nullopt /* deadline */,
ll);
}
const char* s (nullptr);
switch (t)
{
case command_type::test: s = ""; break;
case command_type::setup: s = "+"; break;
case command_type::teardown: s = "-"; break;
}
cout << ind_ << s << e;
if (line_ || iterations_)
print_line_info (ii, i);
cout << endl;
}
virtual bool
run_cond (scope&,
const command_expr& e,
const iteration_index* ii, size_t i,
const location&) override
{
cout << ind_ << "? " << e;
if (line_ || iterations_)
print_line_info (ii, i);
cout << endl;
return e.back ().pipe.back ().program.recall.string () == "true";
}
virtual void
leave (scope&, const location&) override
{
if (scope_)
{
ind_.resize (ind_.size () - 2);
cout << ind_ << "}" << endl;
}
}
private:
void
print_line_info (const iteration_index* ii, size_t i) const
{
cout << " #";
if (line_)
cout << ' ' << i;
if (iterations_ && ii != nullptr)
{
string s;
for (const iteration_index* i (ii); i != nullptr; i = i->prev)
s.insert (0, " i" + to_string (i->index));
cout << s;
}
}
private:
bool scope_;
bool id_;
bool line_;
bool iterations_;
string ind_;
};
// Usage: argv[0] [-s] [-i] [-l] [-r] [<testscript-name>]
//
int
main (int argc, char* argv[])
{
tracer trace ("main");
// Fake build system driver, default verbosity.
//
init_diag (1);
init (nullptr, argv[0], true);
// Serial execution.
//
scheduler sched (1);
global_mutexes mutexes (1);
file_cache fcache (true);
context ctx (sched, mutexes, fcache);
bool scope (false);
bool id (false);
bool line (false);
bool iterations (false);
path name;
for (int i (1); i != argc; ++i)
{
string a (argv[i]);
if (a == "-s")
scope = true;
else if (a == "-i")
id = true;
else if (a == "-l")
line = true;
else if (a == "-r")
iterations = true;
else
{
name = path (move (a));
break;
}
}
if (name.empty ())
name = path ("testscript");
assert (!id || scope); // Id can only be printed with scope.
try
{
cin.exceptions (istream::failbit | istream::badbit);
// Enter mock targets. Use fixed names and paths so that we can use
// them in expected results. Strictly speaking target paths should
// be absolute. However, the testscript implementation doesn't
// really care.
//
file& tt (
ctx.targets.insert_implied<file> (work,
dir_path (),
"driver",
string (),
trace));
value& v (
tt.assign (
ctx.var_pool.rw ().insert<target_triplet> ("test.target")));
v = *ctx.build_host;
testscript& st (
ctx.targets.insert_implied<testscript> (
work,
dir_path (),
name.leaf ().base ().string (),
name.leaf ().extension (),
trace));
tt.path (path ("driver"));
st.path (name);
// Parse and run.
//
parser p (ctx);
script s (tt, st, dir_path (work) /= "test-driver");
p.pre_parse (cin, s);
print_runner r (scope, id, line, iterations);
p.execute (s, r);
}
catch (const failed&)
{
return 1;
}
return 0;
}
}
}
}
int
main (int argc, char* argv[])
{
return build2::test::script::main (argc, argv);
}
|