Repository: bitcoin/bitcoin
File: src/test/util/setup_common.h (C/C++)

Code before:
};

/**
 * Make a test setup that has disk access to the debug.log file disabled. Can
 * be used in "hot loops", for example fuzzing or benchmarking.
 */
template <class T = const BasicTestingSetup>
std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, TestOpts opts = {})
{
    opts.extra_args = Cat(
        {
            "-nodebuglogfile",
            "-nodebug",
        },
        opts.extra_args);

    return std::make_unique<T>(chain_type, opts);
}

CBlock getBlock13b8a();

// Make types usable in BOOST_CHECK_*
std::ostream& operator<<(std::ostream& os, const arith_uint256& num);
std::ostream& operator<<(std::ostream& os, const uint160& num);
std::ostream& operator<<(std::ostream& os, const uint256& num);

/**
 * BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
 * Use as
 * BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
 */
class HasReason
{
public:
    explicit HasReason(const std::string& reason) : m_reason(reason) {}
    bool operator()(const std::exception& e) const
    {
        return std::string(e.what()).find(m_reason) != std::string::npos;
    };

private:
    const std::string m_reason;
};

#endif // BITCOIN_TEST_UTIL_SETUP_COMMON_H


Review comment (performance):
nit: missing `optional` and `ostream` includes

<details>
<summary>iwyu</summary>

```
/ci_container_base/src/test/util/setup_common.h should add these lines:
#include <stddef.h>                  // for size_t
#include <stdint.h>                  // for uint32_t
#include <exception>                 // for exception
#include <memory>                    // for make_unique, unique_ptr
#include <optional>                  // for optional
#include <ostream>                   // for ostream, operator<<, basic_ios, basic_ostream
#include <utility>                   // for pair
#include "common/args.h"
#include "consensus/amount.h"        // for CAmount, COIN
#include "node/context.h"
#include "random.h"                  // for FastRandomContext, GetRandHash
#include "util/chaintype.h"          // for ChainType (ptr only)
class uint160;
class uint256;

/ci_container_base/src/test/util/setup_common.h should remove these lines:
- #include <kernel/context.h>  // lines 9-9
- #include <pubkey.h>  // lines 14-14
- #include <util/check.h>  // lines 18-18
- #include <stdexcept>  // lines 15-15
- class FastRandomContext;  // lines 31-31
- struct CMutableTransaction;  // lines 109-109

The full include-list for /ci_container_base/src/test/util/setup_common.h:
#include <common/args.h>             // for ArgsManager
#include <key.h>                     // for CKey
#include <node/caches.h>             // for CacheSizes
#include <node/context.h>            // for NodeContext
#include <primitives/transaction.h>  // for CTransactionRef, CMutableTransaction
#include <stddef.h>                  // for size_t
#include <stdint.h>                  // for uint32_t
#include <test/util/random.h>        // for SeedRandomStateForTest, SeedRand
#include <util/chaintype.h>          // for ChainType
#include <util/fs.h>                 // for path
#include <util/signalinterrupt.h>    // for SignalInterrupt
#include <util/string.h>             // for string, basic_string<>::npos
#include <util/vector.h>             // for Cat
#include <exception>                 // for exception
#include <functional>                // for function
#include <memory>                    // for make_unique, unique_ptr
#include <optional>                  // for optional
#include <ostream>                   // for ostream, operator<<, basic_ios, basic_ostream
#include <type_traits>               // for enable_if, is_enum, underlying_type
#include <utility>                   // for pair
#include <vector>                    // for vector
#include "common/args.h"
#include "consensus/amount.h"        // for CAmount, COIN
#include "node/context.h"
#include "random.h"                  // for FastRandomContext, GetRandHash
#include "util/chaintype.h"          // for ChainType (ptr only)
class CBlock;  // lines 108-108
class CFeeRate;  // lines 29-29
class CScript;  // lines 110-110
class Chainstate;  // lines 30-30
class arith_uint256;  // lines 28-28
class uint160;
class uint256;
```
</details>

Code after:
 */
template <class T = const BasicTestingSetup>
std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, TestOpts opts = {})
{
    opts.extra_args = Cat(
        {
            "-nodebuglogfile",
            "-nodebug",
        },
        opts.extra_args);

    return std::make_unique<T>(chain_type, opts);
}

CBlock getBlock13b8a();

// Make types usable in BOOST_CHECK_* @{
namespace std {
template <typename T> requires std::is_enum_v<T>
inline std::ostream& operator<<(std::ostream& os, const T& e)
{
    return os << static_cast<std::underlying_type_t<T>>(e);
}

template <typename T>
inline std::ostream& operator<<(std::ostream& os, const std::optional<T>& v)
{
    return v ? os << *v
             : os << "std::nullopt";
}
} // namespace std

std::ostream& operator<<(std::ostream& os, const arith_uint256& num);
std::ostream& operator<<(std::ostream& os, const uint160& num);
std::ostream& operator<<(std::ostream& os, const uint256& num);
// @}

/**
 * BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
 * Use as
 * BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
 */
class HasReason
{
public:
    explicit HasReason(const std::string& reason) : m_reason(reason) {}
    bool operator()(const std::exception& e) const
    {
        return std::string(e.what()).find(m_reason) != std::string::npos;
    };