mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #12283 from ethereum/soltest-graceful-error-handling
Graceful error handling in soltest/isoltest + improved soltestAssert()
This commit is contained in:
+26
-19
@@ -159,26 +159,33 @@ bool CommonOptions::parse(int argc, char const* const* argv)
|
||||
po::variables_map arguments;
|
||||
addOptions();
|
||||
|
||||
po::command_line_parser cmdLineParser(argc, argv);
|
||||
cmdLineParser.options(options);
|
||||
auto parsedOptions = cmdLineParser.run();
|
||||
po::store(parsedOptions, arguments);
|
||||
po::notify(arguments);
|
||||
try
|
||||
{
|
||||
po::command_line_parser cmdLineParser(argc, argv);
|
||||
cmdLineParser.options(options);
|
||||
auto parsedOptions = cmdLineParser.run();
|
||||
po::store(parsedOptions, arguments);
|
||||
po::notify(arguments);
|
||||
|
||||
for (auto const& parsedOption: parsedOptions.options)
|
||||
if (parsedOption.position_key >= 0)
|
||||
{
|
||||
if (
|
||||
parsedOption.original_tokens.empty() ||
|
||||
(parsedOption.original_tokens.size() == 1 && parsedOption.original_tokens.front().empty())
|
||||
)
|
||||
continue; // ignore empty options
|
||||
std::stringstream errorMessage;
|
||||
errorMessage << "Unrecognized option: ";
|
||||
for (auto const& token: parsedOption.original_tokens)
|
||||
errorMessage << token;
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error(errorMessage.str()));
|
||||
}
|
||||
for (auto const& parsedOption: parsedOptions.options)
|
||||
if (parsedOption.position_key >= 0)
|
||||
{
|
||||
if (
|
||||
parsedOption.original_tokens.empty() ||
|
||||
(parsedOption.original_tokens.size() == 1 && parsedOption.original_tokens.front().empty())
|
||||
)
|
||||
continue; // ignore empty options
|
||||
std::stringstream errorMessage;
|
||||
errorMessage << "Unrecognized option: ";
|
||||
for (auto const& token: parsedOption.original_tokens)
|
||||
errorMessage << token;
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error(errorMessage.str()));
|
||||
}
|
||||
}
|
||||
catch (po::error const& exception)
|
||||
{
|
||||
solThrow(ConfigException, exception.what());
|
||||
}
|
||||
|
||||
if (vmPaths.empty())
|
||||
{
|
||||
|
||||
@@ -75,6 +75,9 @@ struct CommonOptions
|
||||
langutil::EVMVersion evmVersion() const;
|
||||
|
||||
virtual void addOptions();
|
||||
// @returns true if the program should continue, false if it should exit immediately without
|
||||
// reporting an error.
|
||||
// Throws ConfigException or std::runtime_error if parsing fails.
|
||||
virtual bool parse(int argc, char const* const* argv);
|
||||
// Throws a ConfigException on error
|
||||
virtual void validate() const;
|
||||
|
||||
+74
-56
@@ -204,88 +204,106 @@ int registerTests(
|
||||
return numTestsAdded;
|
||||
}
|
||||
|
||||
void initializeOptions()
|
||||
bool initializeOptions()
|
||||
{
|
||||
auto const& suite = boost::unit_test::framework::master_test_suite();
|
||||
|
||||
auto options = std::make_unique<solidity::test::CommonOptions>();
|
||||
solAssert(options->parse(suite.argc, suite.argv), "Failed to parse options!");
|
||||
bool shouldContinue = options->parse(suite.argc, suite.argv);
|
||||
if (!shouldContinue)
|
||||
return false;
|
||||
options->validate();
|
||||
|
||||
solidity::test::CommonOptions::setSingleton(std::move(options));
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TODO: Prototype -- why isn't this declared in the boost headers?
|
||||
// TODO: replace this with a (global) fixture.
|
||||
test_suite* init_unit_test_suite( int /*argc*/, char* /*argv*/[] );
|
||||
test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]);
|
||||
|
||||
test_suite* init_unit_test_suite( int /*argc*/, char* /*argv*/[] )
|
||||
test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[])
|
||||
{
|
||||
using namespace solidity::test;
|
||||
|
||||
master_test_suite_t& master = framework::master_test_suite();
|
||||
master.p_name.value = "SolidityTests";
|
||||
|
||||
initializeOptions();
|
||||
|
||||
if (!solidity::test::loadVMs(solidity::test::CommonOptions::get()))
|
||||
exit(1);
|
||||
|
||||
if (solidity::test::CommonOptions::get().disableSemanticTests)
|
||||
cout << endl << "--- SKIPPING ALL SEMANTICS TESTS ---" << endl << endl;
|
||||
|
||||
if (!solidity::test::CommonOptions::get().enforceGasTest)
|
||||
cout << endl << "WARNING :: Gas Cost Expectations are not being enforced" << endl << endl;
|
||||
|
||||
Batcher batcher(CommonOptions::get().selectedBatch, CommonOptions::get().batches);
|
||||
if (CommonOptions::get().batches > 1)
|
||||
cout << "Batch " << CommonOptions::get().selectedBatch << " out of " << CommonOptions::get().batches << endl;
|
||||
|
||||
// Batch the boost tests
|
||||
BoostBatcher boostBatcher(batcher);
|
||||
traverse_test_tree(master, boostBatcher, true);
|
||||
|
||||
// Include the interactive tests in the automatic tests as well
|
||||
for (auto const& ts: g_interactiveTestsuites)
|
||||
try
|
||||
{
|
||||
auto const& options = solidity::test::CommonOptions::get();
|
||||
bool shouldContinue = initializeOptions();
|
||||
if (!shouldContinue)
|
||||
exit(EXIT_SUCCESS);
|
||||
|
||||
if (ts.smt && options.disableSMT)
|
||||
continue;
|
||||
if (!solidity::test::loadVMs(solidity::test::CommonOptions::get()))
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
if (ts.needsVM && solidity::test::CommonOptions::get().disableSemanticTests)
|
||||
continue;
|
||||
if (solidity::test::CommonOptions::get().disableSemanticTests)
|
||||
cout << endl << "--- SKIPPING ALL SEMANTICS TESTS ---" << endl << endl;
|
||||
|
||||
//TODO
|
||||
//solAssert(
|
||||
registerTests(
|
||||
master,
|
||||
options.testPath / ts.path,
|
||||
ts.subpath,
|
||||
options.enforceViaYul,
|
||||
options.enforceCompileToEwasm,
|
||||
ts.labels,
|
||||
ts.testCaseCreator,
|
||||
batcher
|
||||
);
|
||||
// > 0, std::string("no ") + ts.title + " tests found");
|
||||
if (!solidity::test::CommonOptions::get().enforceGasTest)
|
||||
cout << endl << "WARNING :: Gas Cost Expectations are not being enforced" << endl << endl;
|
||||
|
||||
Batcher batcher(CommonOptions::get().selectedBatch, CommonOptions::get().batches);
|
||||
if (CommonOptions::get().batches > 1)
|
||||
cout << "Batch " << CommonOptions::get().selectedBatch << " out of " << CommonOptions::get().batches << endl;
|
||||
|
||||
// Batch the boost tests
|
||||
BoostBatcher boostBatcher(batcher);
|
||||
traverse_test_tree(master, boostBatcher, true);
|
||||
|
||||
// Include the interactive tests in the automatic tests as well
|
||||
for (auto const& ts: g_interactiveTestsuites)
|
||||
{
|
||||
auto const& options = solidity::test::CommonOptions::get();
|
||||
|
||||
if (ts.smt && options.disableSMT)
|
||||
continue;
|
||||
|
||||
if (ts.needsVM && solidity::test::CommonOptions::get().disableSemanticTests)
|
||||
continue;
|
||||
|
||||
//TODO
|
||||
//solAssert(
|
||||
registerTests(
|
||||
master,
|
||||
options.testPath / ts.path,
|
||||
ts.subpath,
|
||||
options.enforceViaYul,
|
||||
options.enforceCompileToEwasm,
|
||||
ts.labels,
|
||||
ts.testCaseCreator,
|
||||
batcher
|
||||
);
|
||||
// > 0, std::string("no ") + ts.title + " tests found");
|
||||
}
|
||||
|
||||
if (solidity::test::CommonOptions::get().disableSemanticTests)
|
||||
{
|
||||
for (auto suite: {
|
||||
"ABIDecoderTest",
|
||||
"ABIEncoderTest",
|
||||
"SolidityAuctionRegistrar",
|
||||
"SolidityWallet",
|
||||
"GasMeterTests",
|
||||
"GasCostTests",
|
||||
"SolidityEndToEndTest",
|
||||
"SolidityOptimizer"
|
||||
})
|
||||
removeTestSuite(suite);
|
||||
}
|
||||
}
|
||||
|
||||
if (solidity::test::CommonOptions::get().disableSemanticTests)
|
||||
catch (solidity::test::ConfigException const& exception)
|
||||
{
|
||||
for (auto suite: {
|
||||
"ABIDecoderTest",
|
||||
"ABIEncoderTest",
|
||||
"SolidityAuctionRegistrar",
|
||||
"SolidityWallet",
|
||||
"GasMeterTests",
|
||||
"GasCostTests",
|
||||
"SolidityEndToEndTest",
|
||||
"SolidityOptimizer"
|
||||
})
|
||||
removeTestSuite(suite);
|
||||
cerr << exception.what() << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
catch (std::runtime_error const& exception)
|
||||
{
|
||||
cerr << exception.what() << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
|
||||
@@ -15,20 +15,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <libsolutil/AnsiColorized.h>
|
||||
#include <libsolutil/Assertions.h>
|
||||
#include <libsolutil/CommonData.h>
|
||||
#include <libsolutil/Exceptions.h>
|
||||
|
||||
#include <boost/preprocessor/cat.hpp>
|
||||
#include <boost/preprocessor/facilities/empty.hpp>
|
||||
#include <boost/preprocessor/facilities/overload.hpp>
|
||||
|
||||
namespace solidity::frontend::test
|
||||
{
|
||||
|
||||
#define soltestAssert(CONDITION, DESCRIPTION) \
|
||||
do \
|
||||
{ \
|
||||
if (!(CONDITION)) \
|
||||
BOOST_THROW_EXCEPTION(std::runtime_error(DESCRIPTION)); \
|
||||
} \
|
||||
while (false)
|
||||
struct InternalSoltestError: virtual util::Exception {};
|
||||
|
||||
#if !BOOST_PP_VARIADICS_MSVC
|
||||
#define soltestAssert(...) BOOST_PP_OVERLOAD(soltestAssert_,__VA_ARGS__)(__VA_ARGS__)
|
||||
#else
|
||||
#define soltestAssert(...) BOOST_PP_CAT(BOOST_PP_OVERLOAD(soltestAssert_,__VA_ARGS__)(__VA_ARGS__),BOOST_PP_EMPTY())
|
||||
#endif
|
||||
|
||||
#define soltestAssert_1(CONDITION) \
|
||||
soltestAssert_2((CONDITION), "")
|
||||
|
||||
#define soltestAssert_2(CONDITION, DESCRIPTION) \
|
||||
assertThrowWithDefaultDescription( \
|
||||
(CONDITION), \
|
||||
::solidity::frontend::test::InternalSoltestError, \
|
||||
(DESCRIPTION), \
|
||||
"Soltest assertion failed" \
|
||||
)
|
||||
|
||||
class TestParserError: virtual public util::Exception
|
||||
{
|
||||
|
||||
@@ -75,9 +75,9 @@ void IsolTestOptions::addOptions()
|
||||
|
||||
bool IsolTestOptions::parse(int _argc, char const* const* _argv)
|
||||
{
|
||||
bool const res = CommonOptions::parse(_argc, _argv);
|
||||
bool const shouldContinue = CommonOptions::parse(_argc, _argv);
|
||||
|
||||
if (showHelp || !res)
|
||||
if (showHelp || !shouldContinue)
|
||||
{
|
||||
std::cout << options << std::endl;
|
||||
return false;
|
||||
@@ -85,7 +85,7 @@ bool IsolTestOptions::parse(int _argc, char const* const* _argv)
|
||||
|
||||
enforceGasTest = enforceGasTest || (evmVersion() == langutil::EVMVersion{} && !useABIEncoderV1);
|
||||
|
||||
return res;
|
||||
return shouldContinue;
|
||||
}
|
||||
|
||||
void IsolTestOptions::validate() const
|
||||
|
||||
+20
-8
@@ -433,8 +433,9 @@ int main(int argc, char const *argv[])
|
||||
{
|
||||
auto options = std::make_unique<IsolTestOptions>();
|
||||
|
||||
if (!options->parse(argc, argv))
|
||||
return -1;
|
||||
bool shouldContinue = options->parse(argc, argv);
|
||||
if (!shouldContinue)
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
options->validate();
|
||||
CommonOptions::setSingleton(std::move(options));
|
||||
@@ -443,7 +444,7 @@ int main(int argc, char const *argv[])
|
||||
auto& options = dynamic_cast<IsolTestOptions const&>(CommonOptions::get());
|
||||
|
||||
if (!solidity::test::loadVMs(options))
|
||||
return 1;
|
||||
return EXIT_FAILURE;
|
||||
|
||||
if (options.disableSemanticTests)
|
||||
cout << endl << "--- SKIPPING ALL SEMANTICS TESTS ---" << endl << endl;
|
||||
@@ -479,7 +480,7 @@ int main(int argc, char const *argv[])
|
||||
if (stats)
|
||||
global_stats += *stats;
|
||||
else
|
||||
return 1;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
cout << endl << "Summary: ";
|
||||
@@ -497,11 +498,22 @@ int main(int argc, char const *argv[])
|
||||
if (options.disableSemanticTests)
|
||||
cout << "\nNOTE: Skipped semantics tests.\n" << endl;
|
||||
|
||||
return global_stats ? 0 : 1;
|
||||
return global_stats ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
||||
catch (std::exception const& _exception)
|
||||
catch (boost::program_options::error const& exception)
|
||||
{
|
||||
cerr << _exception.what() << endl;
|
||||
return 1;
|
||||
cerr << exception.what() << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
catch (std::runtime_error const& exception)
|
||||
{
|
||||
cerr << exception.what() << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
cerr << "Unhandled exception caught." << endl;
|
||||
cerr << boost::current_exception_diagnostic_information() << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user