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
|
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libdevcore/CommonIO.h>
#include <test/libsolidity/AnalysisFramework.h>
#include <test/libsolidity/SyntaxTest.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <queue>
using namespace dev;
using namespace dev::solidity;
using namespace dev::solidity::test;
using namespace dev::solidity::test::formatting;
using namespace std;
namespace po = boost::program_options;
namespace fs = boost::filesystem;
struct SyntaxTestStats
{
int successCount;
int runCount;
operator bool() const { return successCount == runCount; }
};
class SyntaxTestTool
{
public:
SyntaxTestTool(string const& _name, fs::path const& _path, bool _formatted):
m_formatted(_formatted), m_name(_name), m_path(_path)
{}
enum class Result
{
Success,
Failure,
InputOutputError,
Exception
};
Result process();
static SyntaxTestStats processPath(
fs::path const& _basepath,
fs::path const& _path,
bool const _formatted
);
static string editor;
private:
enum class Request
{
Skip,
Rerun,
Quit
};
Request handleResponse(bool const _exception);
void printContract() const;
bool const m_formatted;
string const m_name;
fs::path const m_path;
unique_ptr<SyntaxTest> m_test;
};
string SyntaxTestTool::editor;
void SyntaxTestTool::printContract() const
{
stringstream stream(m_test->source());
string line;
while (getline(stream, line))
cout << " " << line << endl;
cout << endl;
}
SyntaxTestTool::Result SyntaxTestTool::process()
{
bool success;
std::stringstream outputMessages;
(FormattedScope(cout, m_formatted, {BOLD}) << m_name << ": ").flush();
try
{
m_test = unique_ptr<SyntaxTest>(new SyntaxTest(m_path.string()));
}
catch (std::exception const& _e)
{
FormattedScope(cout, m_formatted, {BOLD, RED}) << "cannot read test: " << _e.what() << endl;
return Result::InputOutputError;
}
try
{
success = m_test->run(outputMessages, " ", m_formatted);
}
catch(CompilerError const& _e)
{
FormattedScope(cout, m_formatted, {BOLD, RED}) <<
"Exception: " << SyntaxTest::errorMessage(_e) << endl;
return Result::Exception;
}
catch(InternalCompilerError const& _e)
{
FormattedScope(cout, m_formatted, {BOLD, RED}) <<
"InternalCompilerError: " << SyntaxTest::errorMessage(_e) << endl;
return Result::Exception;
}
catch(FatalError const& _e)
{
FormattedScope(cout, m_formatted, {BOLD, RED}) <<
"FatalError: " << SyntaxTest::errorMessage(_e) << endl;
return Result::Exception;
}
catch(UnimplementedFeatureError const& _e)
{
FormattedScope(cout, m_formatted, {BOLD, RED}) <<
"UnimplementedFeatureError: " << SyntaxTest::errorMessage(_e) << endl;
return Result::Exception;
}
catch(...)
{
FormattedScope(cout, m_formatted, {BOLD, RED}) <<
"Unknown Exception" << endl;
return Result::Exception;
}
if (success)
{
FormattedScope(cout, m_formatted, {BOLD, GREEN}) << "OK" << endl;
return Result::Success;
}
else
{
FormattedScope(cout, m_formatted, {BOLD, RED}) << "FAIL" << endl;
FormattedScope(cout, m_formatted, {BOLD, CYAN}) << " Contract:" << endl;
printContract();
cout << outputMessages.str() << endl;
return Result::Failure;
}
}
SyntaxTestTool::Request SyntaxTestTool::handleResponse(bool const _exception)
{
if (_exception)
cout << "(e)dit/(s)kip/(q)uit? ";
else
cout << "(e)dit/(u)pdate expectations/(s)kip/(q)uit? ";
cout.flush();
while (true)
{
switch(readStandardInputChar())
{
case 's':
cout << endl;
return Request::Skip;
case 'u':
if (_exception)
break;
else
{
cout << endl;
ofstream file(m_path.string(), ios::trunc);
file << m_test->source();
file << "// ----" << endl;
if (!m_test->errorList().empty())
m_test->printErrorList(file, m_test->errorList(), "// ", false);
return Request::Rerun;
}
case 'e':
cout << endl << endl;
if (system((editor + " \"" + m_path.string() + "\"").c_str()))
cerr << "Error running editor command." << endl << endl;
return Request::Rerun;
case 'q':
cout << endl;
return Request::Quit;
default:
break;
}
}
}
SyntaxTestStats SyntaxTestTool::processPath(
fs::path const& _basepath,
fs::path const& _path,
bool const _formatted
)
{
std::queue<fs::path> paths;
paths.push(_path);
int successCount = 0;
int runCount = 0;
while (!paths.empty())
{
auto currentPath = paths.front();
fs::path fullpath = _basepath / currentPath;
if (fs::is_directory(fullpath))
{
paths.pop();
for (auto const& entry: boost::iterator_range<fs::directory_iterator>(
fs::directory_iterator(fullpath),
fs::directory_iterator()
))
if (fs::is_directory(entry.path()) || SyntaxTest::isTestFilename(entry.path().filename()))
paths.push(currentPath / entry.path().filename());
}
else
{
SyntaxTestTool testTool(currentPath.string(), fullpath, _formatted);
++runCount;
auto result = testTool.process();
switch(result)
{
case Result::Failure:
case Result::Exception:
switch(testTool.handleResponse(result == Result::Exception))
{
case Request::Quit:
return { successCount, runCount };
case Request::Rerun:
cout << "Re-running test case..." << endl;
--runCount;
break;
case Request::Skip:
paths.pop();
break;
}
break;
case Result::Success:
paths.pop();
++successCount;
break;
default:
// non-recoverable error; continue with next test case
paths.pop();
break;
}
}
}
return { successCount, runCount };
}
int main(int argc, char *argv[])
{
if (getenv("EDITOR"))
SyntaxTestTool::editor = getenv("EDITOR");
else if (fs::exists("/usr/bin/editor"))
SyntaxTestTool::editor = "/usr/bin/editor";
fs::path testPath;
bool formatted = true;
po::options_description options(
R"(isoltest, tool for interactively managing test contracts.
Usage: isoltest [Options] --testpath path
Interactively validates test contracts.
Allowed options)",
po::options_description::m_default_line_length,
po::options_description::m_default_line_length - 23);
options.add_options()
("help", "Show this help screen.")
("testpath", po::value<fs::path>(&testPath), "path to test files")
("no-color", "don't use colors")
("editor", po::value<string>(&SyntaxTestTool::editor), "editor for opening contracts");
po::variables_map arguments;
try
{
po::command_line_parser cmdLineParser(argc, argv);
cmdLineParser.options(options);
po::store(cmdLineParser.run(), arguments);
if (arguments.count("help"))
{
cout << options << endl;
return 0;
}
if (arguments.count("no-color"))
formatted = false;
po::notify(arguments);
}
catch (po::error const& _exception)
{
cerr << _exception.what() << endl;
return 1;
}
if (testPath.empty())
{
auto const searchPath =
{
fs::current_path() / ".." / ".." / ".." / "test",
fs::current_path() / ".." / ".." / "test",
fs::current_path() / ".." / "test",
fs::current_path() / "test",
fs::current_path()
};
for (auto const& basePath : searchPath)
{
fs::path syntaxTestPath = basePath / "libsolidity" / "syntaxTests";
if (fs::exists(syntaxTestPath) && fs::is_directory(syntaxTestPath))
{
testPath = basePath;
break;
}
}
}
fs::path syntaxTestPath = testPath / "libsolidity" / "syntaxTests";
if (fs::exists(syntaxTestPath) && fs::is_directory(syntaxTestPath))
{
auto stats = SyntaxTestTool::processPath(testPath / "libsolidity", "syntaxTests", formatted);
cout << endl << "Summary: ";
FormattedScope(cout, formatted, {BOLD, stats ? GREEN : RED}) <<
stats.successCount << "/" << stats.runCount;
cout << " tests successful." << endl;
return stats ? 0 : 1;
}
else
{
cerr << "Test path not found. Use the --testpath argument." << endl;
return 1;
}
}
|