test: fix TestShell initialization and reset()

This commit is contained in:
Brandon Odiwuor 2024-12-03 12:42:56 +03:00
parent ebe4cac38b
commit 318b2a2f90
2 changed files with 16 additions and 11 deletions

View file

@ -24,13 +24,16 @@ user inputs. Such environments include the Python3 command line interpreter or
## 2. Importing `TestShell` from the Bitcoin Core repository
We can import the `TestShell` by adding the path of the Bitcoin Core
We can import the `TestShell` by adding the path of the configured Bitcoin Core
`test_framework` module to the beginning of the PATH variable, and then
importing the `TestShell` class from the `test_shell` sub-package.
importing the `TestShell` class from the `test_shell` sub-package. Since
the build system creates a copy of the `test_framework` module into a new `build/`
directory along with the required configuration file, the path to the build copy
must be used.
```
>>> import sys
>>> sys.path.insert(0, "/path/to/bitcoin/test/functional")
>>> sys.path.insert(0, "/path/to/bitcoin/build/test/functional")
>>> from test_framework.test_shell import TestShell
```

View file

@ -7,6 +7,14 @@ import pathlib
from test_framework.test_framework import BitcoinTestFramework
# BitcoinTestFramework instances are supposed to be constructed with the path
# of the calling test in order to find shared data like configuration and the
# cache. Since TestShell is meant for interactive use, there is no concrete
# test; passing a dummy name is fine though, as only the containing directory
# is relevant for successful initialization.
tests_directory = pathlib.Path(__file__).absolute().parent.parent
dummy_testshell_file = tests_directory / "testshell_dummy.py"
class TestShell:
"""Wrapper Class for BitcoinTestFramework.
@ -61,7 +69,7 @@ class TestShell:
print("Shutdown TestShell before resetting!")
else:
self.num_nodes = None
super().__init__()
super().__init__(dummy_testshell_file)
instance = None
@ -69,13 +77,7 @@ class TestShell:
# This implementation enforces singleton pattern, and will return the
# previously initialized instance if available
if not TestShell.instance:
# BitcoinTestFramework instances are supposed to be constructed with the path
# of the calling test in order to find shared data like configuration and the
# cache. Since TestShell is meant for interactive use, there is no concrete
# test; passing a dummy name is fine though, as only the containing directory
# is relevant for successful initialization.
tests_directory = pathlib.Path(__file__).resolve().parent.parent
TestShell.instance = TestShell.__TestShell(tests_directory / "testshell_dummy.py")
TestShell.instance = TestShell.__TestShell(dummy_testshell_file)
TestShell.instance.running = False
return TestShell.instance