1
1
# Note that this file is special in that py.test will automatically import this file and gather
2
2
# its list of fixtures even if it is not directly imported into the corresponding test case.
3
- import pathlib
4
3
4
+ import pathlib
5
5
import pytest
6
+ import sys
7
+ import typing as ty
6
8
7
9
import ipfshttpclient
8
10
@@ -15,57 +17,52 @@ def fake_dir() -> pathlib.Path:
15
17
return TEST_DIR .joinpath ('fake_dir' )
16
18
17
19
18
- __is_available = None
19
- def is_available (): # noqa
20
+ @ pytest . fixture ( scope = 'session' )
21
+ def ipfs_is_available () -> bool :
20
22
"""
21
23
Return whether the IPFS daemon is reachable or not
22
24
"""
23
- global __is_available
24
25
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 )
32
32
33
- return __is_available
33
+ return False
34
+ else :
35
+ return True
34
36
35
37
36
38
def sort_by_key (items , key = "Name" ):
37
39
return sorted (items , key = lambda x : x [key ])
38
40
39
41
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
43
49
else :
44
50
pytest .skip ("Running IPFS node required" )
45
51
46
52
47
53
@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 )
55
56
56
57
57
58
@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 )
62
61
63
62
64
63
@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 )
69
66
70
67
71
68
@pytest .fixture
0 commit comments