Skip to content

Commit cc6c4fe

Browse files
committed
test: Remove mock_process.cpp
The previous binary used a number of `Boost.Test` features: - `boost::unit_test::disable` - `BOOST_FAIL` - `boost::exit_test_failure` This patch duplicates the previous mock process behavior with no boost features. With the patch we can: - simplify the test config - remove a linted boost include - remove a file that was not actually a test
1 parent 297fd14 commit cc6c4fe

4 files changed

Lines changed: 53 additions & 60 deletions

File tree

src/test/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ add_executable(test_bitcoin
6464
miniminer_tests.cpp
6565
miniscript_tests.cpp
6666
minisketch_tests.cpp
67-
mock_process.cpp
6867
multisig_tests.cpp
6968
bip328_tests.cpp
7069
net_peer_connection_tests.cpp
@@ -192,8 +191,6 @@ function(add_boost_test source_file)
192191
list(TRANSFORM test_suite_macro
193192
REPLACE "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(" ""
194193
)
195-
# The mock_process test suite does not contain unit tests.
196-
list(REMOVE_ITEM test_suite_macro "mock_process")
197194
foreach(test_suite_name IN LISTS test_suite_macro)
198195
add_test(NAME ${test_suite_name}
199196
COMMAND test_bitcoin --run_test=${test_suite_name} --catch_system_error=no --log_level=test_suite -- -printtoconsole=1

src/test/mock_process.cpp

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/test/system_tests.cpp

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,72 @@
99
#include <test/util/common.h>
1010
#include <test/util/setup_common.h>
1111
#include <univalue.h>
12+
#include <util/fs.h>
1213
#include <util/string.h>
1314

15+
#include <cstdlib>
16+
#include <iostream>
17+
#include <string_view>
18+
1419
#ifdef ENABLE_EXTERNAL_SIGNER
1520
#include <util/subprocess.h>
1621
#endif // ENABLE_EXTERNAL_SIGNER
1722

18-
#include <boost/cstdlib.hpp>
1923
#include <boost/test/unit_test.hpp>
2024

2125
#include <string>
2226

27+
namespace {
28+
// When set in the environment, test_bitcoin acts as a mock subprocess for the
29+
// run_command test below instead of running unit tests.
30+
constexpr const char* MOCK_PROCESS_ENV = "BITCOIN_TEST_MOCK_PROCESS";
31+
32+
struct MockProcessDispatcher {
33+
MockProcessDispatcher()
34+
{
35+
const char* name = std::getenv(MOCK_PROCESS_ENV);
36+
if (!name) return;
37+
const std::string_view n{name};
38+
if (n == "valid_json") {
39+
std::cout << R"({"success": true})" << std::endl;
40+
std::_Exit(EXIT_SUCCESS);
41+
}
42+
if (n == "nonzeroexit_nooutput") {
43+
std::_Exit(EXIT_FAILURE);
44+
}
45+
if (n == "nonzeroexit_stderroutput") {
46+
std::cerr << "err" << std::endl;
47+
std::_Exit(EXIT_FAILURE);
48+
}
49+
if (n == "invalid_json") {
50+
std::cout << "{" << std::endl;
51+
std::_Exit(EXIT_SUCCESS);
52+
}
53+
if (n == "pass_stdin_to_stdout") {
54+
std::string s;
55+
std::getline(std::cin, s);
56+
std::cout << s << std::endl;
57+
std::_Exit(EXIT_SUCCESS);
58+
}
59+
std::cerr << "Unknown mock process: " << n << std::endl;
60+
std::_Exit(EXIT_FAILURE);
61+
}
62+
};
63+
const MockProcessDispatcher g_mock_dispatcher;
64+
} // namespace
65+
2366
BOOST_FIXTURE_TEST_SUITE(system_tests, BasicTestingSetup)
2467

2568
#ifdef ENABLE_EXTERNAL_SIGNER
2669

27-
static std::vector<std::string> mock_executable(std::string name)
70+
static std::vector<std::string> mock_executable(const std::string& name)
2871
{
29-
// Invoke the mock_process/* test case with all unsolicited output suppressed.
30-
return {
31-
boost::unit_test::framework::master_test_suite().argv[0],
32-
// Disable false-positive memory leak dumps to stderr
33-
// in debug builds when using the Windows UCRT.
34-
"--detect_memory_leaks=0",
35-
// Disable logging to stdout.
36-
"--log_level=nothing",
37-
// Disable the test report to stderr.
38-
"--report_level=no",
39-
"--run_test=mock_process/" + name,
40-
};
72+
#if defined(WIN32)
73+
_putenv_s(MOCK_PROCESS_ENV, name.c_str());
74+
#else
75+
setenv(MOCK_PROCESS_ENV, name.c_str(), /*overwrite=*/1);
76+
#endif
77+
return {boost::unit_test::framework::master_test_suite().argv[0]};
4178
}
4279

4380
BOOST_AUTO_TEST_CASE(run_command)
@@ -67,7 +104,7 @@ BOOST_AUTO_TEST_CASE(run_command)
67104
const std::vector<std::string> command = mock_executable("nonzeroexit_nooutput");
68105
BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
69106
const std::string what{e.what()};
70-
BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned %d: \n", util::Join(command, " "), boost::exit_test_failure)) != std::string::npos);
107+
BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned %d: \n", util::Join(command, " "), EXIT_FAILURE)) != std::string::npos);
71108
return true;
72109
});
73110
}
@@ -77,7 +114,7 @@ BOOST_AUTO_TEST_CASE(run_command)
77114
const std::string expected{"err"};
78115
BOOST_CHECK_EXCEPTION(RunCommandParseJSON(command), std::runtime_error, [&](const std::runtime_error& e) {
79116
const std::string what(e.what());
80-
BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned %s: %s", util::Join(command, " "), boost::exit_test_failure, "err")) != std::string::npos);
117+
BOOST_CHECK(what.find(strprintf("RunCommandParseJSON error: process(%s) returned %s: %s", util::Join(command, " "), EXIT_FAILURE, "err")) != std::string::npos);
81118
BOOST_CHECK(what.find(expected) != std::string::npos);
82119
return true;
83120
});

test/lint/lint-includes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
] + SHARED_EXCLUDED_SUBTREES
2222

2323
EXPECTED_BOOST_INCLUDES = [
24-
"boost/cstdlib.hpp",
2524
"boost/multi_index/detail/hash_index_iterator.hpp",
2625
"boost/multi_index/hashed_index.hpp",
2726
"boost/multi_index/identity.hpp",

0 commit comments

Comments
 (0)