Skip to content

Commit

Permalink
Merge bitcoin#23781: test: Fix system_tests/run_command on Windows
Browse files Browse the repository at this point in the history
edd0313 test: Improve "invalid_command" subtest in system_tests for Windows (Hennadii Stepanov)
fb1b059 test: Fix "non-zero exit code" subtest in system_tests for Windows (Hennadii Stepanov)
0aad33d test: Fix "false" subtest in system_tests for Windows (Hennadii Stepanov)
507c009 test: Fix "echo" subtest in the system_tests for Windows (Hennadii Stepanov)

Pull request description:

  An attempt to fix bitcoin#23775.

  With this PR on Windows 10 Pro 21H1 (build 19043.1348):
  ```
  C:\Users\hebasto\bitcoin>src\test_bitcoin.exe --run_test=system_tests/run_command
  Running 1 test case...

  *** No errors detected

  C:\Users\hebasto\bitcoin>src\test_bitcoin.exe
  Running 482 test cases...

  *** No errors detected

  ```

ACKs for top commit:
  sipsorcery:
    tACK edd0313
  Tru3Nrg:
    tACK bitcoin@edd0313

Tree-SHA512: 66a4f2372858011ff862b71c6530bedb8bc731b18595636fac9affc9189d9320f212c68b62498f2b57ee7a07f59e842dbec085b76a7419791d1a06c8e80e7744
  • Loading branch information
laanwj authored and PastaPastaPasta committed Feb 14, 2024
1 parent 9ef2ee5 commit bf3d2d4
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/test/system_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,6 @@ BOOST_AUTO_TEST_CASE(dummy)

#ifdef HAVE_BOOST_PROCESS

bool checkMessage(const std::runtime_error& ex)
{
// On Linux & Mac: "No such file or directory"
// On Windows: "The system cannot find the file specified."
const std::string what(ex.what());
BOOST_CHECK(what.find("file") != std::string::npos);
return true;
}

bool checkMessageFalse(const std::runtime_error& ex)
{
BOOST_CHECK_EQUAL(ex.what(), std::string("RunCommandParseJSON error: process(false) returned 1: \n"));
return true;
}

bool checkMessageStdErr(const std::runtime_error& ex)
{
const std::string what(ex.what());
BOOST_CHECK(what.find("RunCommandParseJSON error:") != std::string::npos);
return checkMessage(ex);
}

BOOST_AUTO_TEST_CASE(run_command)
{
{
Expand All @@ -53,10 +31,8 @@ BOOST_AUTO_TEST_CASE(run_command)
}
{
#ifdef WIN32
// Windows requires single quotes to prevent escaping double quotes from the JSON...
const UniValue result = RunCommandParseJSON("echo '{\"success\": true}'");
const UniValue result = RunCommandParseJSON("cmd.exe /c echo {\"success\": true}");
#else
// ... but Linux and macOS echo a single quote if it's used
const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
#endif
BOOST_CHECK(result.isObject());
Expand All @@ -66,15 +42,45 @@ BOOST_AUTO_TEST_CASE(run_command)
}
{
// An invalid command is handled by Boost
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, checkMessage); // Command failed
#ifdef WIN32
const std::string expected{"The system cannot find the file specified."};
#else
const std::string expected{"No such file or directory"};
#endif
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, [&](const boost::process::process_error& e) {
const std::string what(e.what());
BOOST_CHECK(what.find("RunCommandParseJSON error:") == std::string::npos);
BOOST_CHECK(what.find(expected) != std::string::npos);
return true;
});
}
{
// Return non-zero exit code, no output to stderr
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("false"), std::runtime_error, checkMessageFalse);
#ifdef WIN32
const std::string command{"cmd.exe /c call"};
#else
const std::string command{"false"};
#endif
BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
BOOST_CHECK(std::string(e.what()).find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos);
return true;
});
}
{
// Return non-zero exit code, with error message for stderr
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("ls nosuchfile"), std::runtime_error, checkMessageStdErr);
#ifdef WIN32
const std::string command{"cmd.exe /c dir nosuchfile"};
const std::string expected{"File Not Found"};
#else
const std::string command{"ls nosuchfile"};
const std::string expected{"No such file or directory"};
#endif
BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
const std::string what(e.what());
BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned", command)) != std::string::npos);
BOOST_CHECK(what.find(expected) != std::string::npos);
return true;
});
}
{
BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON
Expand Down

0 comments on commit bf3d2d4

Please sign in to comment.