11import * as vscode from 'vscode'
2- import { exec } from '../../services/node'
2+ import { exec } from '../../services/node'
33import * as storage from '../../services/storage'
44
55// ensure only latest run_test action is taken
@@ -8,135 +8,135 @@ let currentId = 0
88// quick solution to prevent processing multiple results
99// NOTE: may be possible to kill child process early
1010const shouldExitEarly = ( processId :number ) :boolean => {
11- return currentId !== processId
11+ return currentId !== processId
1212}
1313
14- let _channel :vscode . OutputChannel
14+ let channel :vscode . OutputChannel
1515
1616const getOutputChannel = ( name :string ) :vscode . OutputChannel => {
17- if ( ! _channel ) {
18- _channel = vscode . window . createOutputChannel ( name )
19- }
20- return _channel
17+ if ( ! channel ) {
18+ channel = vscode . window . createOutputChannel ( name )
19+ }
20+ return channel
2121}
2222
2323interface Props {
24- onSuccess ( ) :void
25- onFail ( ) :void
24+ onSuccess ( ) :void
25+ onFail ( ) :void
2626}
2727
28- export default async function runTest ( { onSuccess, onFail} :Props ) :Promise < void > {
29- // increment process id
30- let processId = ++ currentId
31-
32- const outputChannelName = 'Test Output'
33-
34- // TODO: validate test directory from package.json exists
35- // let testFile = path.join('test');
36- // if (!await exists(testFile)) {
37- // return emptyTasks;
38- // }
39-
40- // TODO: verify test runner for args
41- const testArgs = [ '--json' ]
42-
43- // if .git repo, use --onlyChanged
44- // const hasGit = path.join('.git');
45- // if (await exists(hasGit)) {
46- // testArgs.push('--onlyChanged')
47- // }
48-
49- let commandLine = `npm test --${ testArgs . join ( ' ' ) } `
50-
51- try {
52- // capture position early on test start
53- // in case position changes
54- const [ position , { stdout} ] = await Promise . all ( [ storage . getPosition ( ) , exec ( commandLine ) ] )
55- if ( shouldExitEarly ( processId ) ) {
56- // exit early
57- return
58- }
59-
60- if ( stdout ) {
61- let lines = stdout . split ( / \r { 0 , 1 } \n / )
62- console . log ( 'SUCCESS LINES' , lines )
63- for ( let line of lines ) {
64- if ( line . length === 0 ) {
65- continue
66- }
67-
68- const regExp = / ^ { \" n u m F a i l e d T e s t S u i t e s /
69- const matches = regExp . exec ( line )
70- if ( matches && matches . length ) {
71- console . log ( 'MATCHES SUCCESS' )
72- const result = JSON . parse ( line )
73-
74- if ( result . success ) {
75- console . log ( 'SUCCESS' )
76- if ( shouldExitEarly ( processId ) ) {
77- // exit early
78- return
79- }
80- console . log ( 'call onSuccess' )
81- onSuccess ( )
82- } else {
83- console . log ( 'NOT SUCCESS?' )
84- }
85- }
86- }
87- }
88- } catch ( err ) {
89- if ( shouldExitEarly ( processId ) ) {
90- // exit early
91- return
92- }
93- // error contains output & error message
94- // output can be parsed as json
95- const { stdout, stderr} = err
96- console . log ( 'TEST FAILED' , stdout )
97-
98- if ( ! stdout ) {
99- console . error ( 'SOMETHING WENT WRONG WITH A PASSING TEST' )
100- }
101- // test runner failed
102- const channel = getOutputChannel ( outputChannelName )
103-
104- if ( stdout ) {
105- let lines = stdout . split ( / \r { 0 , 1 } \n / )
106-
107- for ( let line of lines ) {
108- if ( line . length === 0 ) {
109- continue
110- }
111-
112- const dataRegExp = / ^ { \" n u m F a i l e d T e s t S u i t e s " /
113- const matches = dataRegExp . exec ( line )
114-
115- if ( matches && matches . length ) {
116- const result = JSON . parse ( line )
117- const firstError = result . testResults . find ( ( t :any ) => t . status === 'failed' )
118-
119- if ( firstError ) {
120- if ( shouldExitEarly ( processId ) ) {
121- // exit early
122- return
123- }
124- console . log ( 'ERROR' , firstError . message )
125- console . log ( 'call onFail' )
126- onFail ( )
127- } else {
128- console . error ( 'NOTE: PARSER DID NOT WORK FOR ' , line )
129- }
130- }
131- }
132- }
133-
134- if ( stderr ) {
135- channel . show ( false )
136- channel . appendLine ( stderr )
137- }
138- // if (err.stdout) {
139- // channel.appendLine(err.stdout);
140- // }
141- }
28+ export default async function runTest ( { onSuccess, onFail} :Props ) :Promise < void > {
29+ // increment process id
30+ const processId = ++ currentId
31+
32+ const outputChannelName = 'Test Output'
33+
34+ // TODO: validate test directory from package.json exists
35+ // let testFile = path.join('test');
36+ // if (!await exists(testFile)) {
37+ // return emptyTasks;
38+ // }
39+
40+ // TODO: verify test runner for args
41+ const testArgs = [ '--json' ]
42+
43+ // if .git repo, use --onlyChanged
44+ // const hasGit = path.join('.git');
45+ // if (await exists(hasGit)) {
46+ // testArgs.push('--onlyChanged')
47+ // }
48+
49+ const commandLine = `npm test --${ testArgs . join ( ' ' ) } `
50+
51+ try {
52+ // capture position early on test start
53+ // in case position changes
54+ const [ position , { stdout} ] = await Promise . all ( [ storage . getPosition ( ) , exec ( commandLine ) ] )
55+ if ( shouldExitEarly ( processId ) ) {
56+ // exit early
57+ return
58+ }
59+
60+ if ( stdout ) {
61+ const lines = stdout . split ( / \r { 0 , 1 } \n / )
62+ console . log ( 'SUCCESS LINES' , lines )
63+ for ( const line of lines ) {
64+ if ( line . length === 0 ) {
65+ continue
66+ }
67+
68+ const regExp = / ^ { \" n u m F a i l e d T e s t S u i t e s /
69+ const matches = regExp . exec ( line )
70+ if ( matches && matches . length ) {
71+ console . log ( 'MATCHES SUCCESS' )
72+ const result = JSON . parse ( line )
73+
74+ if ( result . success ) {
75+ console . log ( 'SUCCESS' )
76+ if ( shouldExitEarly ( processId ) ) {
77+ // exit early
78+ return
79+ }
80+ console . log ( 'call onSuccess' )
81+ onSuccess ( )
82+ } else {
83+ console . log ( 'NOT SUCCESS?' )
84+ }
85+ }
86+ }
87+ }
88+ } catch ( err ) {
89+ if ( shouldExitEarly ( processId ) ) {
90+ // exit early
91+ return
92+ }
93+ // error contains output & error message
94+ // output can be parsed as json
95+ const { stdout, stderr} = err
96+ console . log ( 'TEST FAILED' , stdout )
97+
98+ if ( ! stdout ) {
99+ console . error ( 'SOMETHING WENT WRONG WITH A PASSING TEST' )
100+ }
101+ // test runner failed
102+ const channel = getOutputChannel ( outputChannelName )
103+
104+ if ( stdout ) {
105+ const lines = stdout . split ( / \r { 0 , 1 } \n / )
106+
107+ for ( const line of lines ) {
108+ if ( line . length === 0 ) {
109+ continue
110+ }
111+
112+ const dataRegExp = / ^ { \" n u m F a i l e d T e s t S u i t e s " /
113+ const matches = dataRegExp . exec ( line )
114+
115+ if ( matches && matches . length ) {
116+ const result = JSON . parse ( line )
117+ const firstError = result . testResults . find ( ( t :any ) => t . status === 'failed' )
118+
119+ if ( firstError ) {
120+ if ( shouldExitEarly ( processId ) ) {
121+ // exit early
122+ return
123+ }
124+ console . log ( 'ERROR' , firstError . message )
125+ console . log ( 'call onFail' )
126+ onFail ( )
127+ } else {
128+ console . error ( 'NOTE: PARSER DID NOT WORK FOR ' , line )
129+ }
130+ }
131+ }
132+ }
133+
134+ if ( stderr ) {
135+ channel . show ( false )
136+ channel . appendLine ( stderr )
137+ }
138+ // if (err.stdout) {
139+ // channel.appendLine(err.stdout);
140+ // }
141+ }
142142}