Skip to content

Commit 1220af5

Browse files
committed
Merge bitcoin/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/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/bitcoin@edd0313 Tree-SHA512: 66a4f2372858011ff862b71c6530bedb8bc731b18595636fac9affc9189d9320f212c68b62498f2b57ee7a07f59e842dbec085b76a7419791d1a06c8e80e7744
2 parents 98a2ddc + edd0313 commit 1220af5

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
@@ -28,28 +28,6 @@ BOOST_AUTO_TEST_CASE(dummy)
2828

2929
#ifdef ENABLE_EXTERNAL_SIGNER
3030

31-
bool checkMessage(const std::runtime_error& ex)
32-
{
33-
// On Linux & Mac: "No such file or directory"
34-
// On Windows: "The system cannot find the file specified."
35-
const std::string what(ex.what());
36-
BOOST_CHECK(what.find("file") != std::string::npos);
37-
return true;
38-
}
39-
40-
bool checkMessageFalse(const std::runtime_error& ex)
41-
{
42-
BOOST_CHECK_EQUAL(ex.what(), std::string("RunCommandParseJSON error: process(false) returned 1: \n"));
43-
return true;
44-
}
45-
46-
bool checkMessageStdErr(const std::runtime_error& ex)
47-
{
48-
const std::string what(ex.what());
49-
BOOST_CHECK(what.find("RunCommandParseJSON error:") != std::string::npos);
50-
return checkMessage(ex);
51-
}
52-
5331
BOOST_AUTO_TEST_CASE(run_command)
5432
{
5533
{
@@ -58,10 +36,8 @@ BOOST_AUTO_TEST_CASE(run_command)
5836
}
5937
{
6038
#ifdef WIN32
61-
// Windows requires single quotes to prevent escaping double quotes from the JSON...
62-
const UniValue result = RunCommandParseJSON("echo '{\"success\": true}'");
39+
const UniValue result = RunCommandParseJSON("cmd.exe /c echo {\"success\": true}");
6340
#else
64-
// ... but Linux and macOS echo a single quote if it's used
6541
const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
6642
#endif
6743
BOOST_CHECK(result.isObject());
@@ -71,15 +47,45 @@ BOOST_AUTO_TEST_CASE(run_command)
7147
}
7248
{
7349
// An invalid command is handled by Boost
74-
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, checkMessage); // Command failed
50+
#ifdef WIN32
51+
const std::string expected{"The system cannot find the file specified."};
52+
#else
53+
const std::string expected{"No such file or directory"};
54+
#endif
55+
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, [&](const boost::process::process_error& e) {
56+
const std::string what(e.what());
57+
BOOST_CHECK(what.find("RunCommandParseJSON error:") == std::string::npos);
58+
BOOST_CHECK(what.find(expected) != std::string::npos);
59+
return true;
60+
});
7561
}
7662
{
7763
// Return non-zero exit code, no output to stderr
78-
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("false"), std::runtime_error, checkMessageFalse);
64+
#ifdef WIN32
65+
const std::string command{"cmd.exe /c call"};
66+
#else
67+
const std::string command{"false"};
68+
#endif
69+
BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
70+
BOOST_CHECK(std::string(e.what()).find(strprintf("RunCommandParseJSON error: process(%s) returned 1: \n", command)) != std::string::npos);
71+
return true;
72+
});
7973
}
8074
{
8175
// Return non-zero exit code, with error message for stderr
82-
BOOST_CHECK_EXCEPTION(RunCommandParseJSON("ls nosuchfile"), std::runtime_error, checkMessageStdErr);
76+
#ifdef WIN32
77+
const std::string command{"cmd.exe /c dir nosuchfile"};
78+
const std::string expected{"File Not Found"};
79+
#else
80+
const std::string command{"ls nosuchfile"};
81+
const std::string expected{"No such file or directory"};
82+
#endif
83+
BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
84+
const std::string what(e.what());
85+
BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned", command)) != std::string::npos);
86+
BOOST_CHECK(what.find(expected) != std::string::npos);
87+
return true;
88+
});
8389
}
8490
{
8591
BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON

0 commit comments

Comments
 (0)