Skip to content

Commit 09e2a9e

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#23781: test: Fix system_tests/run_command on Windows
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
1 parent e05c215 commit 09e2a9e

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/test/system_tests.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,6 @@ BOOST_AUTO_TEST_CASE(dummy)
2323

2424
#ifdef HAVE_BOOST_PROCESS
2525

26-
bool checkMessage(const std::runtime_error& ex)
27-
{
28-
// On Linux & Mac: "No such file or directory"
29-
// On Windows: "The system cannot find the file specified."
30-
const std::string what(ex.what());
31-
BOOST_CHECK(what.find("file") != std::string::npos);
32-
return true;
33-
}
34-
35-
bool checkMessageFalse(const std::runtime_error& ex)
36-
{
37-
BOOST_CHECK_EQUAL(ex.what(), std::string("RunCommandParseJSON error: process(false) returned 1: \n"));
38-
return true;
39-
}
40-
41-
bool checkMessageStdErr(const std::runtime_error& ex)
42-
{
43-
const std::string what(ex.what());
44-
BOOST_CHECK(what.find("RunCommandParseJSON error:") != std::string::npos);
45-
return checkMessage(ex);
46-
}
47-
4826
BOOST_AUTO_TEST_CASE(run_command)
4927
{
5028
{
@@ -53,10 +31,8 @@ BOOST_AUTO_TEST_CASE(run_command)
5331
}
5432
{
5533
#ifdef WIN32
56-
// Windows requires single quotes to prevent escaping double quotes from the JSON...
57-
const UniValue result = RunCommandParseJSON("echo '{\"success\": true}'");
34+
const UniValue result = RunCommandParseJSON("cmd.exe /c echo {\"success\": true}");
5835
#else
59-
// ... but Linux and macOS echo a single quote if it's used
6036
const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
6137
#endif
6238
BOOST_CHECK(result.isObject());
@@ -66,15 +42,45 @@ BOOST_AUTO_TEST_CASE(run_command)
6642
}
6743
{
6844
// An invalid command is handled by Boost
69-
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, checkMessage); // Command failed
45+
#ifdef WIN32
46+
const std::string expected{"The system cannot find the file specified."};
47+
#else
48+
const std::string expected{"No such file or directory"};
49+
#endif
50+
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, [&](const boost::process::process_error& e) {
51+
const std::string what(e.what());
52+
BOOST_CHECK(what.find("RunCommandParseJSON error:") == std::string::npos);
53+
BOOST_CHECK(what.find(expected) != std::string::npos);
54+
return true;
55+
});
7056
}
7157
{
7258
// Return non-zero exit code, no output to stderr
73-
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("false"), std::runtime_error, checkMessageFalse);
59+
#ifdef WIN32
60+
const std::string command{"cmd.exe /c call"};
61+
#else
62+
const std::string command{"false"};
63+
#endif
64+
BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
65+
BOOST_CHECK(std::string(e.what()).find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos);
66+
return true;
67+
});
7468
}
7569
{
7670
// Return non-zero exit code, with error message for stderr
77-
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("ls nosuchfile"), std::runtime_error, checkMessageStdErr);
71+
#ifdef WIN32
72+
const std::string command{"cmd.exe /c dir nosuchfile"};
73+
const std::string expected{"File Not Found"};
74+
#else
75+
const std::string command{"ls nosuchfile"};
76+
const std::string expected{"No such file or directory"};
77+
#endif
78+
BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
79+
const std::string what(e.what());
80+
BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned", command)) != std::string::npos);
81+
BOOST_CHECK(what.find(expected) != std::string::npos);
82+
return true;
83+
});
7884
}
7985
{
8086
BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON

0 commit comments

Comments
 (0)