@@ -9,10 +9,35 @@ const os = require('os')
9
9
const path = require ( 'path' )
10
10
const hat = require ( 'hat' )
11
11
const fs = require ( 'fs' )
12
+ const tempWrite = require ( 'temp-write' )
12
13
const pkg = require ( '../../package.json' )
13
14
14
15
const skipOnWindows = isWindows ( ) ? it . skip : it
16
+ const daemonReady = ( daemon , cb ) => {
17
+ let r = null
18
+ const p = new Promise ( ( resolve , reject ) => {
19
+ daemon . stdout . on ( 'data' , async ( data ) => {
20
+ if ( data . toString ( ) . includes ( 'Daemon is ready' ) ) {
21
+ try {
22
+ r = await cb ( )
23
+ } catch ( err ) {
24
+ reject ( err )
25
+ }
26
+ daemon . cancel ( )
27
+ }
28
+ } )
29
+ daemon . stderr . on ( 'data' , ( ) => reject ( new Error ( 'Daemon didnt start' ) ) )
30
+ daemon . then ( ( ) => resolve ( r ) ) . catch ( err => {
31
+ if ( r && err . killed ) {
32
+ return resolve ( r )
33
+ }
34
+
35
+ reject ( err )
36
+ } )
37
+ } )
15
38
39
+ return p
40
+ }
16
41
const checkLock = ( repo ) => {
17
42
// skip on windows
18
43
// https://github.com/ipfs/js-ipfsd-ctl/pull/155#issuecomment-326983530
@@ -190,14 +215,6 @@ describe('daemon', () => {
190
215
checkLock ( repoPath )
191
216
} )
192
217
193
- it ( 'gives error if user hasn\'t run init before' , async function ( ) {
194
- this . timeout ( 100 * 1000 )
195
-
196
- const err = await ipfs . fail ( 'daemon' )
197
-
198
- expect ( err . stdout ) . to . include ( 'no initialized ipfs repo found in ' + repoPath )
199
- } )
200
-
201
218
it ( 'should be silent' , async function ( ) {
202
219
this . timeout ( 100 * 1000 )
203
220
await ipfs ( 'init' )
@@ -265,4 +282,39 @@ describe('daemon', () => {
265
282
expect ( err . stdout ) . to . include ( `Node.js version: ${ process . versions . node } ` )
266
283
}
267
284
} )
285
+
286
+ it ( 'should init by default' , async function ( ) {
287
+ this . timeout ( 100 * 1000 )
288
+
289
+ expect ( fs . existsSync ( repoPath ) ) . to . be . false ( )
290
+
291
+ const daemon = ipfs ( 'daemon' )
292
+ let stdout = ''
293
+
294
+ daemon . stdout . on ( 'data' , ( data ) => {
295
+ stdout += data . toString ( 'utf8' )
296
+
297
+ if ( stdout . includes ( 'Daemon is ready' ) ) {
298
+ daemon . kill ( )
299
+ }
300
+ } )
301
+
302
+ try {
303
+ await daemon
304
+ throw new Error ( 'Did not kill process' )
305
+ } catch ( err ) {
306
+ expect ( err . killed ) . to . be . true ( )
307
+ }
308
+
309
+ expect ( fs . existsSync ( repoPath ) ) . to . be . true ( )
310
+ } )
311
+
312
+ it ( 'should init with custom config' , async function ( ) {
313
+ this . timeout ( 100 * 1000 )
314
+ const configPath = tempWrite . sync ( '{"Addresses": {"API": "/ip4/127.0.0.1/tcp/9999"}}' , 'config.json' )
315
+ const daemon = ipfs ( `daemon --init-config ${ configPath } ` )
316
+
317
+ const r = await daemonReady ( daemon , ( ) => ipfs ( 'config \'Addresses.API\'' ) )
318
+ expect ( r ) . to . be . eq ( '/ip4/127.0.0.1/tcp/9999\n' )
319
+ } )
268
320
} )
0 commit comments