Repository: bitcoin/bitcoin
File: test/functional/tool_utils.py (Python)

Code before:
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit.
"""Exercise the utils via json-defined tests."""

from test_framework.test_framework import BitcoinTestFramework

import difflib
import json
import logging
import os
import subprocess
from pathlib import Path


class ToolUtils(BitcoinTestFramework):
    def set_test_params(self):
        self.num_nodes = 0  # No node/datadir needed

    def setup_network(self):
        pass

    def skip_test_if_missing_module(self):
        self.skip_if_no_bitcoin_tx()
        self.skip_if_no_bitcoin_util()

    def run_test(self):
        self.testcase_dir = Path(self.config["environment"]["SRCDIR"]) / "test" / "functional" / "data" / "util"
        self.bins = self.get_binaries()
        with open(self.testcase_dir / "bitcoin-util-test.json", encoding="utf8") as f:
            input_data = json.loads(f.read())

        for i, test_obj in enumerate(input_data):
            self.log.debug(f"Running [{i}]: " + test_obj["description"])
            self.test_one(test_obj)

    def test_one(self, testObj):
        """Runs a single test, comparing output and RC to expected output and RC.

        Raises an error if input can't be read, executable fails, or output/RC
        are not as expected. Error is caught by bctester() and reported.
        """
        # Get the exec names and arguments
        if testObj["exec"] == "./bitcoin-util":
            execrun = self.bins.util_argv() + testObj["args"]
        elif testObj["exec"] == "./bitcoin-tx":
            execrun = self.bins.tx_argv() + testObj["args"]

        # Read the input data (if there is any)
        inputData = None
        if "input" in testObj:
            with open(self.testcase_dir / testObj["input"], encoding="utf8") as f:

Review comment (suggestion):
[This](https://github.com/bitcoin/bitcoin/blob/fa2f1c55b7da729eb6bb9f88bf48459235cc2664/test/CMakeLists.txt#L39-L47) code effectively symlinks/copies the entire `test/functional` subdirectory into the build tree.

Why not use it the same way we do in other functional tests? For example, grep for `"os.path.realpath(__file__)"`.

Code after:
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit.
"""Exercise the utils via json-defined tests."""

from test_framework.test_framework import BitcoinTestFramework

import difflib
import json
import os
import subprocess
from pathlib import Path


class ToolUtils(BitcoinTestFramework):
    def set_test_params(self):
        self.num_nodes = 0  # No node/datadir needed

    def setup_network(self):
        pass

    def skip_test_if_missing_module(self):
        self.skip_if_no_bitcoin_tx()
        self.skip_if_no_bitcoin_util()

    def run_test(self):
        self.testcase_dir = Path(self.config["environment"]["SRCDIR"]) / "test" / "functional" / "data" / "util"
        self.bins = self.get_binaries()
        with open(self.testcase_dir / "bitcoin-util-test.json", encoding="utf8") as f:
            input_data = json.loads(f.read())

        for i, test_obj in enumerate(input_data):
            self.log.debug(f"Running [{i}]: " + test_obj["description"])
            self.test_one(test_obj)

    def test_one(self, testObj):
        """Runs a single test, comparing output and RC to expected output and RC.

        Raises an error if input can't be read, executable fails, or output/RC
        are not as expected. Error is caught by bctester() and reported.
        """
        # Get the exec names and arguments
        if testObj["exec"] == "./bitcoin-util":
            execrun = self.bins.util_argv() + testObj["args"]
        elif testObj["exec"] == "./bitcoin-tx":
            execrun = self.bins.tx_argv() + testObj["args"]

        # Read the input data (if there is any)
        inputData = None
        if "input" in testObj:
            with open(self.testcase_dir / testObj["input"], encoding="utf8") as f:
                inputData = f.read()