5
5
*/
6
6
7
7
var fs = require ( 'fs-extra' ) ;
8
+ var cp = require ( 'child_process' ) ;
8
9
9
10
fs . emptyDirSync ( './test/fixture' ) ;
10
11
@@ -61,24 +62,7 @@ console.log('Testing bare script treated as JSON:');
61
62
runner . run ( './test/data/.scriptrc' , 'test.data.scripts' ) ;
62
63
63
64
// Verify that two files were copied.
64
- try
65
- {
66
- // Verify that `./test/fixture/empty2.json` exists.
67
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
68
- {
69
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
70
- }
71
-
72
- // Verify that `./test/fixture/empty3.json` exists.
73
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
74
- {
75
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
76
- }
77
- }
78
- catch ( err )
79
- {
80
- throw new Error ( 'typhonjs-npm-scripts-runner test error: ' + err ) ;
81
- }
65
+ verifyOutput ( ) ;
82
66
83
67
// -------------------------
84
68
@@ -87,7 +71,8 @@ console.log('\nTesting bare script message prepend:');
87
71
88
72
runner . run ( './test/data/.scriptrc' , 'test.data.scripts' , 'A custom message -' ) ;
89
73
90
- fs . emptyDirSync ( './test/fixture' ) ;
74
+ // Verify that two files were copied.
75
+ verifyOutput ( ) ;
91
76
92
77
// -------------------------
93
78
@@ -98,24 +83,7 @@ console.log('\nTesting bad file array first entry:');
98
83
runner . run ( [ './test/data/.badfile1' , './test/data/.scriptrc' , './test/data/.badfile2' ] , 'test.data.scripts' ) ;
99
84
100
85
// Verify that two files were copied.
101
- try
102
- {
103
- // Verify that `./test/fixture/empty2.json` exists.
104
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
105
- {
106
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
107
- }
108
-
109
- // Verify that `./test/fixture/empty3.json` exists.
110
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
111
- {
112
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
113
- }
114
- }
115
- catch ( err )
116
- {
117
- throw new Error ( 'typhonjs-npm-scripts-runner test error: ' + err ) ;
118
- }
86
+ verifyOutput ( ) ;
119
87
120
88
// -------------------------
121
89
@@ -124,7 +92,8 @@ console.log('\nTesting message prepend:');
124
92
125
93
runner . run ( './test/data/.scriptrc' , 'test.data.scripts' , 'A custom message -' ) ;
126
94
127
- fs . emptyDirSync ( './test/fixture' ) ;
95
+ // Verify that two files were copied.
96
+ verifyOutput ( ) ;
128
97
129
98
// ------------------------------------------------------------------------------------------------------------------
130
99
@@ -135,24 +104,7 @@ console.log('\nTesting CJS scriptrc.js require:');
135
104
runner . run ( './test/data/.scriptrc.js' , 'test.data.scripts' ) ;
136
105
137
106
// Verify that two files were copied.
138
- try
139
- {
140
- // Verify that `./test/fixture/empty2.json` exists.
141
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
142
- {
143
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
144
- }
145
-
146
- // Verify that `./test/fixture/empty3.json` exists.
147
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
148
- {
149
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
150
- }
151
- }
152
- catch ( err )
153
- {
154
- throw new Error ( 'typhonjs-npm-scripts-runner test error: ' + err ) ;
155
- }
107
+ verifyOutput ( ) ;
156
108
157
109
// -------------------------
158
110
@@ -161,7 +113,8 @@ console.log('\nTesting CJS scriptrc.js require - message prepend:');
161
113
162
114
runner . run ( './test/data/.scriptrc.js' , 'test.data.scripts' , 'A custom message -' ) ;
163
115
164
- fs . emptyDirSync ( './test/fixture' ) ;
116
+ // Verify that two files were copied.
117
+ verifyOutput ( ) ;
165
118
166
119
// -------------------------
167
120
@@ -172,24 +125,7 @@ console.log('\nTesting CJS bad first entry for file array:');
172
125
runner . run ( [ './test/data/.badfile1.js' , './test/data/.scriptrc.js' , './test/data/.badfile2.js' ] , 'test.data.scripts' ) ;
173
126
174
127
// Verify that two files were copied.
175
- try
176
- {
177
- // Verify that `./test/fixture/empty2.json` exists.
178
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
179
- {
180
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
181
- }
182
-
183
- // Verify that `./test/fixture/empty3.json` exists.
184
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
185
- {
186
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
187
- }
188
- }
189
- catch ( err )
190
- {
191
- throw new Error ( 'typhonjs-npm-scripts-runner test error: ' + err ) ;
192
- }
128
+ verifyOutput ( ) ;
193
129
194
130
// -------------------------
195
131
@@ -198,7 +134,8 @@ console.log('\nTesting CJS prepend message:');
198
134
199
135
runner . run ( './test/data/.scriptrc.js' , 'test.data.scripts' , 'A custom message -' ) ;
200
136
201
- fs . emptyDirSync ( './test/fixture' ) ;
137
+ // Verify that two files were copied.
138
+ verifyOutput ( ) ;
202
139
203
140
// -------------------------
204
141
@@ -209,30 +146,7 @@ console.log('\nTesting JSON file array only executes first matching script:');
209
146
runner . run ( [ './test/data/.scriptrc' , './test/data/.scriptrc2' ] , 'test.data.scripts' ) ;
210
147
211
148
// Verify that two files were copied and that 'scriptrc2' is not executed.
212
- try
213
- {
214
- // Verify that `./test/fixture/empty2.json` exists.
215
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
216
- {
217
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
218
- }
219
-
220
- // Verify that `./test/fixture/empty3.json` exists.
221
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
222
- {
223
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
224
- }
225
-
226
- // Verify that `./test/fixture/empty3.json` exists.
227
- if ( fs . existsSync ( './test/fixture/SHOULD_NOT_EXIST.js' ) )
228
- {
229
- throw new Error ( '`./test/fixture/SHOULD_NOT_EXIST.js` is was copied / scriptrc2 was incorrectly executed.' ) ;
230
- }
231
- }
232
- catch ( err )
233
- {
234
- throw new Error ( 'typhonjs-npm-scripts-runner test error: ' + err ) ;
235
- }
149
+ verifyOutput ( ) ;
236
150
237
151
// -------------------------
238
152
@@ -243,27 +157,88 @@ console.log('\nTesting CJS file array only executes first matching script:');
243
157
runner . run ( [ './test/data/.scriptrc.js' , './test/data/.scriptrc2' ] , 'test.data.scripts' ) ;
244
158
245
159
// Verify that two files were copied and that 'scriptrc2' is not executed.
246
- try
160
+ verifyOutput ( ) ;
161
+
162
+ // ------------------------------------------------------------------------------------------------------------------
163
+
164
+ // Test running 'test-script'
165
+ process . stdout . write ( '\nTesting NPM script execution\n' ) ;
166
+
167
+ // Notify what command is being executed then execute it.
168
+ var exec = 'npm run test-script' ;
169
+ process . stdout . write ( '\nexecuting: ' + exec + '\n' ) ;
170
+ cp . execSync ( exec , { stdio : 'inherit' } ) ;
171
+
172
+ // Verify that two files were copied.
173
+ verifyOutput ( ) ;
174
+
175
+ // -------------------------
176
+
177
+ // Notify what command is being executed then execute it.
178
+ exec = 'npm run test-script-bad' ;
179
+ process . stdout . write ( '\nexecuting: ' + exec + '\n' ) ;
180
+
181
+ thrown = false ;
182
+ try { cp . execSync ( exec , { stdio : 'inherit' } ) ; }
183
+ catch ( err ) { thrown = true ; }
184
+ if ( ! thrown )
247
185
{
248
- // Verify that `./test/fixture/empty2.json` exists.
249
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
186
+ throw new Error ( "typhonjs-npm-scripts-runner test error: 'npm run test-script-bad' does not throw error." ) ;
187
+ }
188
+
189
+ // -------------------------
190
+
191
+ // Notify what command is being executed then execute it.
192
+ exec = 'npm run test-script-js' ;
193
+ process . stdout . write ( '\nexecuting: ' + exec + '\n' ) ;
194
+ cp . execSync ( exec , { stdio : 'inherit' } ) ;
195
+
196
+ // Verify that two files were copied.
197
+ verifyOutput ( ) ;
198
+
199
+ // -------------------------
200
+
201
+ // Notify what command is being executed then execute it.
202
+ exec = 'npm run test-script-message' ;
203
+ process . stdout . write ( '\nexecuting: ' + exec + '\n' ) ;
204
+ cp . execSync ( exec , { stdio : 'inherit' } ) ;
205
+
206
+ // Verify that two files were copied.
207
+ verifyOutput ( ) ;
208
+
209
+ // ------------------------------------------------------------------------------------------------------------------
210
+
211
+ /**
212
+ * Verifies testing output in './test/fixture'
213
+ */
214
+ function verifyOutput ( )
215
+ {
216
+ try
250
217
{
251
- throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
218
+ // Verify that `./test/fixture/empty2.json` exists.
219
+ if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
220
+ {
221
+ throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
222
+ }
223
+
224
+ // Verify that `./test/fixture/empty3.json` exists.
225
+ if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
226
+ {
227
+ throw new Error ( '`./test/fixture/empty2.js` is not a file.' ) ;
228
+ }
229
+
230
+ // Verify that `./test/fixture/empty3.json` exists.
231
+ if ( fs . existsSync ( './test/fixture/SHOULD_NOT_EXIST.js' ) )
232
+ {
233
+ throw new Error ( '`./test/fixture/SHOULD_NOT_EXIST.js` is was copied / scriptrc2 was incorrectly executed.' ) ;
234
+ }
252
235
}
253
-
254
- // Verify that `./test/fixture/empty3.json` exists.
255
- if ( ! fs . statSync ( './test/fixture/empty2.js' ) . isFile ( ) )
236
+ catch ( err )
256
237
{
257
- throw new Error ( '`./ test/fixture/empty2.js` is not a file.' ) ;
238
+ throw new Error ( 'typhonjs-npm-scripts-runner test error: ' + err ) ;
258
239
}
259
-
260
- // Verify that `./test/fixture/empty3.json` exists.
261
- if ( fs . existsSync ( './test/fixture/SHOULD_NOT_EXIST.js' ) )
240
+ finally
262
241
{
263
- throw new Error ( '` ./test/fixture/SHOULD_NOT_EXIST.js` is was copied / scriptrc2 was incorrectly executed. ') ;
242
+ fs . emptyDirSync ( ' ./test/fixture') ;
264
243
}
265
- }
266
- catch ( err )
267
- {
268
- throw new Error ( 'typhonjs-npm-scripts-runner test error: ' + err ) ;
269
- }
244
+ }
0 commit comments