1+ const { expect} = require ( 'chai' )
2+
3+ const { buildCommandExecutor} = require ( '../../lib/commandExecutorBuilder' )
4+
5+ describe ( 'buildCommandExecutor spawn' , ( ) => {
6+
7+ it ( 'addSpecificOptionsBeforeRun' , async ( ) => {
8+ {
9+ const cmd = { command : 'node' , args : [ '-e' , "console.log(\'test\')" ] }
10+ const failedByAssert = [ ]
11+ let holder = null
12+
13+ const addSpecificOptionsBeforeRun = ( cmd ) => {
14+ cmd . args . push ( '&&' , 'echo' , '"FOO"' )
15+ holder = { }
16+ holder . cmd = { ...cmd }
17+ const cmdExecutableCB = ( ) => { holder . exec = true }
18+ return { cmd, cmdExecutableCB}
19+ }
20+
21+ const executeCommandAsync = buildCommandExecutor ( failedByAssert , { spawn : true , addSpecificOptionsBeforeRun} )
22+ await executeCommandAsync ( cmd )
23+ expect ( holder . cmd . args ) . to . contains ( '&&' )
24+ expect ( holder . cmd . args ) . to . contains ( 'echo' )
25+ expect ( holder . cmd . args ) . to . contains ( '"FOO"' )
26+ expect ( holder . exec ) . to . eq ( undefined )
27+ }
28+ {
29+ const cmd = { command : 'node' , args : [ '-e' , "console.log(\'test\'); process.exit(100)" ] }
30+ const failedByAssert = [ ]
31+ let holder = null
32+
33+ const addSpecificOptionsBeforeRun = ( cmd ) => {
34+ cmd . args . push ( '&&' , 'echo' , '"FOO"' )
35+ holder = { }
36+ holder . cmd = { ...cmd }
37+ const cmdExecutableCB = ( ) => { holder . exec = true }
38+ return { cmd, cmdExecutableCB}
39+ }
40+
41+ const executeCommandAsync = buildCommandExecutor ( failedByAssert , { spawn : true , addSpecificOptionsBeforeRun} )
42+ await executeCommandAsync ( cmd )
43+ expect ( holder . cmd . args ) . to . contains ( '&&' )
44+ expect ( holder . cmd . args ) . to . contains ( 'echo' )
45+ expect ( holder . cmd . args ) . to . contains ( '"FOO"' )
46+ expect ( holder . exec ) . to . eq ( true )
47+ }
48+ } )
49+
50+ it ( 'reformatCommand' , async ( ) => {
51+ const failedByAssert = [ ]
52+ let holder = null
53+
54+ const cmd = { command : 'node' , args : [ '-e' , "console.log(\'test\'); process.exit(1)" ] }
55+
56+ const reformatCommand = ( cmd , stack ) => {
57+ holder = { }
58+ holder . cmd = cmd
59+ holder . stack = stack
60+ }
61+
62+ const executeCommandAsync = buildCommandExecutor ( failedByAssert , { spawn : true , reformatCommand} )
63+ await executeCommandAsync ( cmd )
64+ expect ( holder . cmd ) . to . eq ( cmd )
65+ expect ( holder . stack ) . to . eql ( 'test\n' )
66+ } )
67+
68+ it ( 'stackAnalize' , async ( ) => {
69+ const failedByAssert = [ ]
70+ let holder = null
71+
72+ const cmd = { command : 'node' , args : [ '-e' , "console.log('test'); process.exit(1)" ] }
73+
74+ const reformatCommand = ( cmd , stack ) => {
75+ if ( ! holder ) holder = { }
76+ holder . cmd = cmd
77+ holder . stack = stack
78+ }
79+
80+ const stackAnalize = ( stack ) => {
81+ holder = { }
82+ holder . fromStackAnalize = stack
83+ return false
84+ }
85+ const executeCommandAsync = buildCommandExecutor ( failedByAssert , {
86+ spawn : true ,
87+ reformatCommand,
88+ stackAnalize
89+ } )
90+ await executeCommandAsync ( cmd )
91+ expect ( holder . fromStackAnalize ) . to . eq ( 'test\n' )
92+ expect ( holder . cmd ) . to . eq ( cmd )
93+ expect ( holder . stack ) . to . eq ( 'test\n' )
94+ } )
95+
96+ it ( 'longestProcessTime' , async ( ) => {
97+ {
98+ const failedByAssert = [ ]
99+ let holder = null
100+ const stackAnalize = ( stack ) => {
101+ holder = { }
102+ holder . fromStackAnalize = stack
103+ return false
104+ }
105+ const cmd = {
106+ command : 'node' , args : [ '-e' , `(async function() {await new Promise((res) => setTimeout(() => {
107+ res(process.exit(1))
108+ }, 25000))})()`
109+ ]
110+ }
111+
112+ const executeCommandAsync = buildCommandExecutor ( failedByAssert , {
113+ spawn : true ,
114+ stackAnalize,
115+ longestProcessTime : 1
116+ } )
117+ await executeCommandAsync ( cmd )
118+ expect ( holder ) . to . eql ( { fromStackAnalize : '' } )
119+ }
120+ {
121+ const failedByAssert = [ ]
122+ let holder = null
123+ const stackAnalize = ( stack ) => {
124+ holder = { }
125+ holder . fromStackAnalize = stack
126+ return false
127+ }
128+ const cmd = {
129+ command : 'node' , args : [ '-e' , `(async function() {await new Promise((res) => setTimeout(() => {
130+ res(process.exit(1))
131+ }, 25000))})()`
132+ ]
133+ }
134+
135+ const executeCommandAsync = buildCommandExecutor ( failedByAssert , {
136+ spawn : true ,
137+ stackAnalize,
138+ longestProcessTime : 10000
139+ } )
140+ await executeCommandAsync ( cmd )
141+ expect ( holder ) . to . not . eq ( null )
142+ }
143+ } )
144+ } )
0 commit comments