Treat --help, --license and --version as separate input modes

This commit is contained in:
Kamil Śliwak
2021-10-14 13:45:32 +02:00
parent 3713d6df22
commit 924c31d849
6 changed files with 160 additions and 106 deletions
+37 -2
View File
@@ -29,6 +29,8 @@
#include <libsolutil/JSON.h>
#include <boost/algorithm/string.hpp>
#include <range/v3/view/transform.hpp>
#include <map>
@@ -110,9 +112,42 @@ namespace solidity::frontend::test
BOOST_AUTO_TEST_SUITE(CommandLineInterfaceTest)
BOOST_AUTO_TEST_CASE(help)
{
OptionsReaderAndMessages result = parseCommandLineAndReadInputFiles({"solc", "--help"}, "", /* _processInput */ true);
BOOST_TEST(!result.success);
BOOST_TEST(boost::starts_with(result.stdoutContent, "solc, the Solidity commandline compiler."));
BOOST_TEST(result.stderrContent == "");
BOOST_TEST(result.options.input.mode == InputMode::Help);
}
BOOST_AUTO_TEST_CASE(license)
{
OptionsReaderAndMessages result = parseCommandLineAndReadInputFiles({"solc", "--license"}, "", /* _processInput */ true);
BOOST_TEST(result.success);
BOOST_TEST(boost::starts_with(result.stdoutContent, "Most of the code is licensed under GPLv3"));
BOOST_TEST(result.stderrContent == "");
BOOST_TEST(result.options.input.mode == InputMode::License);
}
BOOST_AUTO_TEST_CASE(version)
{
OptionsReaderAndMessages result = parseCommandLineAndReadInputFiles({"solc", "--version"}, "", /* _processInput */ true);
BOOST_TEST(result.success);
BOOST_TEST(boost::ends_with(result.stdoutContent, "Version: " + solidity::frontend::VersionString + "\n"));
BOOST_TEST(result.stderrContent == "");
BOOST_TEST(result.options.input.mode == InputMode::Version);
}
BOOST_AUTO_TEST_CASE(multiple_input_modes)
{
array<string, 6> inputModeOptions = {
array<string, 9> inputModeOptions = {
"--help",
"--license",
"--version",
"--standard-json",
"--link",
"--assemble",
@@ -122,7 +157,7 @@ BOOST_AUTO_TEST_CASE(multiple_input_modes)
};
string expectedMessage =
"The following options are mutually exclusive: "
"--standard-json, --link, --assemble, --strict-assembly, --yul, --import-ast. "
"--help, --license, --version, --standard-json, --link, --assemble, --strict-assembly, --yul, --import-ast. "
"Select at most one.\n";
for (string const& mode1: inputModeOptions)
+31 -31
View File
@@ -30,8 +30,6 @@
#include <libsmtutil/SolverInterface.h>
#include <libsolidity/interface/Version.h>
#include <boost/algorithm/string.hpp>
#include <map>
#include <optional>
#include <ostream>
@@ -48,16 +46,12 @@ using namespace solidity::yul;
namespace
{
optional<CommandLineOptions> parseCommandLine(vector<string> const& _commandLine, ostream& _stdout, ostream& _stderr)
optional<CommandLineOptions> parseCommandLine(vector<string> const& _commandLine, ostream& _stderr)
{
vector<char const*> argv = test::makeArgv(_commandLine);
CommandLineParser cliParser(_stdout, _stderr);
bool success = cliParser.parse(
static_cast<int>(_commandLine.size()),
argv.data(),
false // interactiveTerminal
);
CommandLineParser cliParser(_stderr);
bool success = cliParser.parse(static_cast<int>(_commandLine.size()), argv.data());
if (!success)
return nullopt;
@@ -81,24 +75,34 @@ BOOST_AUTO_TEST_CASE(no_options)
expectedOptions.modelChecker.initialize = true;
expectedOptions.modelChecker.settings = {};
stringstream sout, serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, sout, serr);
stringstream serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, serr);
BOOST_TEST(sout.str() == "");
BOOST_TEST(serr.str() == "");
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST(parsedOptions.value() == expectedOptions);
}
BOOST_AUTO_TEST_CASE(help)
BOOST_AUTO_TEST_CASE(help_license_version)
{
stringstream sout, serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine({"solc", "--help"}, sout, serr);
map<string, InputMode> expectedModePerOption = {
{"--help", InputMode::Help},
{"--license", InputMode::License},
{"--version", InputMode::Version},
};
BOOST_TEST(serr.str() == "");
BOOST_TEST(boost::starts_with(sout.str(), "solc, the Solidity commandline compiler."));
BOOST_TEST(sout.str().find("Usage: solc [options] [input_file...]") != string::npos);
BOOST_TEST(!parsedOptions.has_value());
for (auto const& [option, expectedMode]: expectedModePerOption)
{
stringstream serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine({"solc", option}, serr);
CommandLineOptions expectedOptions;
expectedOptions.input.mode = expectedMode;
BOOST_TEST(serr.str() == "");
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST(parsedOptions.value() == expectedOptions);
}
}
BOOST_AUTO_TEST_CASE(cli_mode_options)
@@ -218,10 +222,9 @@ BOOST_AUTO_TEST_CASE(cli_mode_options)
5,
};
stringstream sout, serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, sout, serr);
stringstream serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, serr);
BOOST_TEST(sout.str() == "");
BOOST_TEST(serr.str() == "");
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST(parsedOptions.value() == expectedOptions);
@@ -335,10 +338,9 @@ BOOST_AUTO_TEST_CASE(assembly_mode_options)
expectedOptions.optimizer.expectedExecutionsPerDeployment = 1000;
}
stringstream sout, serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, sout, serr);
stringstream serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, serr);
BOOST_TEST(sout.str() == "");
BOOST_TEST(serr.str() == "Warning: Yul is still experimental. Please use the output with care.\n");
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST(parsedOptions.value() == expectedOptions);
@@ -404,10 +406,9 @@ BOOST_AUTO_TEST_CASE(standard_json_mode_options)
expectedOptions.compiler.combinedJsonRequests->abi = true;
expectedOptions.compiler.combinedJsonRequests->binary = true;
stringstream sout, serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, sout, serr);
stringstream serr;
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, serr);
BOOST_TEST(sout.str() == "");
BOOST_TEST(serr.str() == "");
BOOST_REQUIRE(parsedOptions.has_value());
BOOST_TEST(parsedOptions.value() == expectedOptions);
@@ -424,16 +425,15 @@ BOOST_AUTO_TEST_CASE(experimental_via_ir_invalid_input_modes)
};
for (string const& inputModeOption: inputModeOptions)
{
stringstream sout, serr;
stringstream serr;
vector<string> commandLine = {
"solc",
"--experimental-via-ir",
"file",
inputModeOption,
};
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, sout, serr);
optional<CommandLineOptions> parsedOptions = parseCommandLine(commandLine, serr);
BOOST_TEST(sout.str() == "");
BOOST_TEST(serr.str() == "The option --experimental-via-ir is only supported in the compiler mode.\n");
BOOST_REQUIRE(!parsedOptions.has_value());
}