2018-03-14 18:15:48 +00:00
|
|
|
/*
|
|
|
|
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>
|
|
|
|
|
2018-03-16 10:40:03 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2018-03-14 18:15:48 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <queue>
|
|
|
|
|
2018-07-10 08:36:36 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2018-03-14 18:15:48 +00:00
|
|
|
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;
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
struct TestStats
|
2018-03-14 18:15:48 +00:00
|
|
|
{
|
|
|
|
int successCount;
|
|
|
|
int runCount;
|
|
|
|
operator bool() const { return successCount == runCount; }
|
|
|
|
};
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
class TestTool
|
2018-03-14 18:15:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2018-06-08 12:17:50 +00:00
|
|
|
TestTool(
|
|
|
|
TestCase::TestCaseCreator _testCaseCreator,
|
|
|
|
string const& _name,
|
|
|
|
fs::path const& _path,
|
|
|
|
bool _formatted
|
|
|
|
): m_testCaseCreator(_testCaseCreator), m_formatted(_formatted), m_name(_name), m_path(_path)
|
2018-03-14 18:15:48 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
enum class Result
|
|
|
|
{
|
|
|
|
Success,
|
|
|
|
Failure,
|
2018-04-03 10:05:26 +00:00
|
|
|
Exception
|
2018-03-14 18:15:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Result process();
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
static TestStats processPath(
|
|
|
|
TestCase::TestCaseCreator _testCaseCreator,
|
2018-03-14 18:15:48 +00:00
|
|
|
fs::path const& _basepath,
|
|
|
|
fs::path const& _path,
|
|
|
|
bool const _formatted
|
|
|
|
);
|
|
|
|
|
|
|
|
static string editor;
|
|
|
|
private:
|
|
|
|
enum class Request
|
|
|
|
{
|
|
|
|
Skip,
|
|
|
|
Rerun,
|
|
|
|
Quit
|
|
|
|
};
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
Request handleResponse(bool const _exception);
|
2018-03-14 18:15:48 +00:00
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
TestCase::TestCaseCreator m_testCaseCreator;
|
2018-03-14 18:15:48 +00:00
|
|
|
bool const m_formatted;
|
|
|
|
string const m_name;
|
|
|
|
fs::path const m_path;
|
2018-06-08 12:17:50 +00:00
|
|
|
unique_ptr<TestCase> m_test;
|
2018-03-14 18:15:48 +00:00
|
|
|
};
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
string TestTool::editor;
|
2018-03-14 18:15:48 +00:00
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
TestTool::Result TestTool::process()
|
2018-03-14 18:15:48 +00:00
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
std::stringstream outputMessages;
|
|
|
|
|
|
|
|
(FormattedScope(cout, m_formatted, {BOLD}) << m_name << ": ").flush();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-06-08 12:17:50 +00:00
|
|
|
m_test = m_testCaseCreator(m_path.string());
|
2018-03-14 18:15:48 +00:00
|
|
|
success = m_test->run(outputMessages, " ", m_formatted);
|
|
|
|
}
|
2018-06-12 09:06:14 +00:00
|
|
|
catch(boost::exception const& _e)
|
2018-03-14 18:15:48 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
FormattedScope(cout, m_formatted, {BOLD, RED}) <<
|
2018-06-12 09:06:14 +00:00
|
|
|
"Exception during syntax test: " << boost::diagnostic_information(_e) << endl;
|
2018-04-03 10:05:26 +00:00
|
|
|
return Result::Exception;
|
|
|
|
}
|
2018-04-06 16:37:01 +00:00
|
|
|
catch (std::exception const& _e)
|
|
|
|
{
|
2018-06-12 09:06:14 +00:00
|
|
|
FormattedScope(cout, m_formatted, {BOLD, RED}) <<
|
|
|
|
"Exception during syntax test: " << _e.what() << endl;
|
2018-04-06 16:37:01 +00:00
|
|
|
return Result::Exception;
|
|
|
|
}
|
2018-05-17 12:57:21 +00:00
|
|
|
catch (...)
|
2018-04-03 10:05:26 +00:00
|
|
|
{
|
|
|
|
FormattedScope(cout, m_formatted, {BOLD, RED}) <<
|
2018-06-12 09:06:14 +00:00
|
|
|
"Unknown exception during syntax test." << endl;
|
2018-04-03 10:05:26 +00:00
|
|
|
return Result::Exception;
|
2018-03-14 18:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-06-08 12:17:50 +00:00
|
|
|
m_test->printSource(cout, " ", m_formatted);
|
2018-03-14 18:15:48 +00:00
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
cout << endl << outputMessages.str() << endl;
|
2018-04-03 10:05:26 +00:00
|
|
|
return Result::Failure;
|
2018-03-14 18:15:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
TestTool::Request TestTool::handleResponse(bool const _exception)
|
2018-03-14 18:15:48 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
if (_exception)
|
2018-03-14 18:15:48 +00:00
|
|
|
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':
|
2018-04-03 10:05:26 +00:00
|
|
|
if (_exception)
|
2018-03-14 18:15:48 +00:00
|
|
|
break;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << endl;
|
|
|
|
ofstream file(m_path.string(), ios::trunc);
|
2018-06-08 12:17:50 +00:00
|
|
|
m_test->printSource(file);
|
2018-03-14 18:15:48 +00:00
|
|
|
file << "// ----" << endl;
|
2018-06-08 12:17:50 +00:00
|
|
|
m_test->printUpdatedExpectations(file, "// ");
|
2018-03-14 18:15:48 +00:00
|
|
|
return Request::Rerun;
|
|
|
|
}
|
|
|
|
case 'e':
|
|
|
|
cout << endl << endl;
|
2018-06-08 12:17:50 +00:00
|
|
|
if (system((TestTool::editor + " \"" + m_path.string() + "\"").c_str()))
|
2018-03-14 18:15:48 +00:00
|
|
|
cerr << "Error running editor command." << endl << endl;
|
|
|
|
return Request::Rerun;
|
|
|
|
case 'q':
|
|
|
|
cout << endl;
|
|
|
|
return Request::Quit;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
TestStats TestTool::processPath(
|
|
|
|
TestCase::TestCaseCreator _testCaseCreator,
|
2018-03-14 18:15:48 +00:00
|
|
|
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()
|
|
|
|
))
|
2018-06-08 12:17:50 +00:00
|
|
|
if (fs::is_directory(entry.path()) || TestCase::isTestFilename(entry.path().filename()))
|
2018-03-16 10:40:03 +00:00
|
|
|
paths.push(currentPath / entry.path().filename());
|
2018-03-14 18:15:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-08 12:17:50 +00:00
|
|
|
TestTool testTool(_testCaseCreator, currentPath.string(), fullpath, _formatted);
|
2018-03-14 18:15:48 +00:00
|
|
|
++runCount;
|
|
|
|
auto result = testTool.process();
|
|
|
|
|
|
|
|
switch(result)
|
|
|
|
{
|
|
|
|
case Result::Failure:
|
2018-04-03 10:05:26 +00:00
|
|
|
case Result::Exception:
|
|
|
|
switch(testTool.handleResponse(result == Result::Exception))
|
2018-03-14 18:15:48 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { successCount, runCount };
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-10 08:36:36 +00:00
|
|
|
void setupTerminal()
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
|
|
|
// Set output mode to handle virtual terminal (ANSI escape sequences)
|
|
|
|
// ignore any error, as this is just a "nice-to-have"
|
|
|
|
// only windows needs to be taken care of, as other platforms (Linux/OSX) support them natively.
|
|
|
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (hOut == INVALID_HANDLE_VALUE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DWORD dwMode = 0;
|
|
|
|
if (!GetConsoleMode(hOut, &dwMode))
|
|
|
|
return;
|
|
|
|
|
|
|
|
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
|
|
if (!SetConsoleMode(hOut, dwMode))
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-14 18:15:48 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-07-10 08:36:36 +00:00
|
|
|
setupTerminal();
|
|
|
|
|
2018-03-14 18:15:48 +00:00
|
|
|
if (getenv("EDITOR"))
|
2018-06-08 12:17:50 +00:00
|
|
|
TestTool::editor = getenv("EDITOR");
|
2018-03-15 17:20:06 +00:00
|
|
|
else if (fs::exists("/usr/bin/editor"))
|
2018-06-08 12:17:50 +00:00
|
|
|
TestTool::editor = "/usr/bin/editor";
|
2018-03-14 18:15:48 +00:00
|
|
|
|
|
|
|
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")
|
2018-06-08 12:17:50 +00:00
|
|
|
("editor", po::value<string>(&TestTool::editor), "editor for opening contracts");
|
2018-03-14 18:15:48 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2018-06-08 12:17:50 +00:00
|
|
|
catch (std::exception const& _exception)
|
2018-03-14 18:15:48 +00:00
|
|
|
{
|
|
|
|
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))
|
|
|
|
{
|
2018-06-08 12:17:50 +00:00
|
|
|
auto stats = TestTool::processPath(SyntaxTest::create, testPath / "libsolidity", "syntaxTests", formatted);
|
2018-03-14 18:15:48 +00:00
|
|
|
|
|
|
|
cout << endl << "Summary: ";
|
|
|
|
FormattedScope(cout, formatted, {BOLD, stats ? GREEN : RED}) <<
|
|
|
|
stats.successCount << "/" << stats.runCount;
|
|
|
|
cout << " tests successful." << endl;
|
|
|
|
|
|
|
|
return stats ? 0 : 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-08 12:17:50 +00:00
|
|
|
cerr << "Syntax tests not found. Use the --testpath argument." << endl;
|
2018-03-14 18:15:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|