Skip to content

Commit

Permalink
test: fix TestShell initialization and reset()
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonOdiwuor committed Dec 18, 2024
1 parent ebe4cac commit 318b2a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
9 changes: 6 additions & 3 deletions test/functional/test-shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
18 changes: 10 additions & 8 deletions test/functional/test_framework/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
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.
Expand Down Expand Up @@ -61,21 +69,15 @@ def reset(self):
print("Shutdown TestShell before resetting!")
else:
self.num_nodes = None
super().__init__()
super().__init__(dummy_testshell_file)

instance = None

def __new__(cls):
# 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

Expand Down

0 comments on commit 318b2a2

Please sign in to comment.