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
55import pytest
6+ import sys
7+ import typing as ty
68
79import 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 ('\n Failed 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
3638def 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
0 commit comments