Skip to content

Commit 89737bd

Browse files
authored
Print exception thrown while trying to connect to IPFS while running test cases; refactor to use pytest fixtures
1 parent 331bcb9 commit 89737bd

File tree

2 files changed

+30
-35
lines changed

2 files changed

+30
-35
lines changed

test/functional/conftest.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Note that this file is special in that py.test will automatically import this file and gather
22
# its list of fixtures even if it is not directly imported into the corresponding test case.
3-
import pathlib
43

4+
import pathlib
55
import pytest
6+
import sys
7+
import typing as ty
68

79
import ipfshttpclient
810

@@ -15,57 +17,52 @@ def fake_dir() -> pathlib.Path:
1517
return TEST_DIR.joinpath('fake_dir')
1618

1719

18-
__is_available = None
19-
def is_available(): # noqa
20+
@pytest.fixture(scope='session')
21+
def ipfs_is_available() -> bool:
2022
"""
2123
Return whether the IPFS daemon is reachable or not
2224
"""
23-
global __is_available
2425

25-
if not isinstance(__is_available, bool):
26-
try:
27-
ipfshttpclient.connect()
28-
except ipfshttpclient.exceptions.Error:
29-
__is_available = False
30-
else:
31-
__is_available = True
26+
try:
27+
with ipfshttpclient.connect():
28+
pass
29+
except ipfshttpclient.exceptions.Error as e:
30+
print('\nFailed to connect to IPFS client', file=sys.stderr)
31+
print(e, file=sys.stderr)
3232

33-
return __is_available
33+
return False
34+
else:
35+
return True
3436

3537

3638
def sort_by_key(items, key="Name"):
3739
return sorted(items, key=lambda x: x[key])
3840

3941

40-
def get_client(offline=False):
41-
if is_available():
42-
return ipfshttpclient.Client(offline=offline)
42+
def _generate_client(
43+
ipfs_is_available: bool,
44+
offline: bool
45+
) -> ty.Generator[ipfshttpclient.Client, None, None]:
46+
if ipfs_is_available:
47+
with ipfshttpclient.Client(offline=offline) as client:
48+
yield client
4349
else:
4450
pytest.skip("Running IPFS node required")
4551

4652

4753
@pytest.fixture(scope="function")
48-
def client():
49-
"""Create a client with function lifetimme to connect to the IPFS daemon.
50-
51-
Each test function should instantiate a fresh client, so use this
52-
fixture in test functions."""
53-
with get_client() as client:
54-
yield client
54+
def client(ipfs_is_available: bool):
55+
yield from _generate_client(ipfs_is_available, False)
5556

5657

5758
@pytest.fixture(scope="function")
58-
def offline_client():
59-
"""Create a client in offline mode with function lifetimme"""
60-
with get_client(offline=True) as client:
61-
yield client
59+
def offline_client(ipfs_is_available: bool):
60+
yield from _generate_client(ipfs_is_available, True)
6261

6362

6463
@pytest.fixture(scope="module")
65-
def module_offline_client():
66-
"""Create a client in offline mode with module lifetime."""
67-
with get_client(offline=True) as client:
68-
yield client
64+
def module_offline_client(ipfs_is_available: bool):
65+
yield from _generate_client(ipfs_is_available, True)
6966

7067

7168
@pytest.fixture

test/functional/test_other.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import ipfshttpclient
22

3-
import conftest
43

5-
6-
def test_ipfs_node_available():
4+
def test_ipfs_node_available(ipfs_is_available):
75
"""
86
Dummy test to ensure that running the tests without a daemon produces a failure, since we
97
think it's unlikely that people running tests want this
108
"""
11-
assert conftest.is_available(), \
9+
assert ipfs_is_available, \
1210
"Functional tests require an IPFS node to be available at: {0}" \
1311
.format(ipfshttpclient.DEFAULT_ADDR)
1412

@@ -21,4 +19,4 @@ def test_add_json(client, cleanup_pins):
2119

2220
# have to test the string added to IPFS, deserializing JSON will not
2321
# test order of keys
24-
assert '{"Action":"Open","Name":"IPFS","Pubkey":7,"Type":"PR"}' == client.cat(res).decode("utf-8")
22+
assert '{"Action":"Open","Name":"IPFS","Pubkey":7,"Type":"PR"}' == client.cat(res).decode("utf-8")

0 commit comments

Comments
 (0)