2
2
from __future__ import print_function
3
3
4
4
import contextlib
5
+ import locale
5
6
import pathlib
6
7
import os
7
8
import random
10
11
import sys
11
12
import time
12
13
13
- import ipfshttpclient
14
+ import six
14
15
15
16
16
17
if not hasattr (contextlib , "suppress" ):
@@ -74,8 +75,17 @@ def _contextlib_suppress(*exceptions):
74
75
# Start daemon #
75
76
################
76
77
78
+ #PY2: Only add `encoding` parameter on Python 3 as it's not available on Unicode-hostile versions
79
+ extra_args = {}
80
+ if not six .PY2 :
81
+ extra_args ["encoding" ] = locale .getpreferredencoding ()
82
+
77
83
# Spawn IPFS daemon in data directory
78
- DAEMON = subprocess .Popen (["ipfs" , "daemon" , "--enable-pubsub-experiment" ])
84
+ DAEMON = subprocess .Popen (["ipfs" , "daemon" , "--enable-pubsub-experiment" ],
85
+ stdout = subprocess .PIPE ,
86
+ stderr = subprocess .STDOUT ,
87
+ ** extra_args
88
+ )
79
89
os .environ ["PY_IPFS_HTTP_CLIENT_TEST_DAEMON_PID" ] = str (DAEMON .pid )
80
90
81
91
# Collect the exit code of `DAEMON` when `SIGCHLD` is received
@@ -85,13 +95,13 @@ def _contextlib_suppress(*exceptions):
85
95
signal .signal (signal .SIGCHLD , lambda * a : DAEMON .poll ())
86
96
87
97
# Wait for daemon to start up
88
- while True :
89
- try :
90
- ipfshttpclient .connect (HOST , PORT )
91
- except ipfshttpclient .exceptions .ConnectionError :
92
- time .sleep (0.05 )
93
- else :
98
+ #PY2: Using `for line in DAEMON.stdout` hangs the process
99
+ line = DAEMON .stdout .readline ()
100
+ while line is not None :
101
+ print (line , end = "" )
102
+ if line .strip () == "Daemon is ready" :
94
103
break
104
+ line = DAEMON .stdout .readline ()
95
105
96
106
97
107
##################
@@ -121,5 +131,11 @@ def _contextlib_suppress(*exceptions):
121
131
DAEMON .kill ()
122
132
123
133
print ("IPFS daemon was still running after test!" , file = sys .stderr )
134
+
135
+ output = list (DAEMON .stdout )
136
+ if output :
137
+ print ("IPFS daemon printed extra messages:" , file = sys .stderr )
138
+ for line in output :
139
+ print ("\t {0}" .format (line ), end = "" , file = sys .stderr )
124
140
125
141
sys .exit (PYTEST_CODE )
0 commit comments