Skip to content

Commit f438230

Browse files
committed
added tests for npm script invocation.
1 parent 9b4cd10 commit f438230

File tree

3 files changed

+102
-123
lines changed

3 files changed

+102
-123
lines changed

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
"description": "Provides an NPM module and script which will load a JSON file searching for an Array entry then executes the scripts.",
66
"license": "MPL-2.0",
77
"author": {
8-
"name": "Mike Leahy"
8+
"name": "typhonrt"
99
},
1010
"repository": {
1111
"type": "git",
1212
"url": "https://github.com/typhonjs-node-npm-scripts/typhonjs-npm-scripts-runner.git"
1313
},
1414
"maintainers": [
1515
{
16-
"name": "Mike Leahy",
16+
"name": "typhonrt",
1717
"email": "[email protected]"
1818
}
1919
],
@@ -32,7 +32,11 @@
3232
"runner"
3333
],
3434
"scripts": {
35-
"test": "node ./test/scripts/test.js"
35+
"test": "node ./test/scripts/test.js",
36+
"test-script": "node ./scripts/runner.js ./test/data/.scriptrc test.data.scripts",
37+
"test-script-bad": "node ./scripts/runner.js ./test/data/.badfile test.data.scripts",
38+
"test-script-js": "node ./scripts/runner.js ./test/data/.scriptrc.js test.data.scripts",
39+
"test-script-message": "node ./scripts/runner.js ./test/data/.scriptrc test.data.scripts 'A custom message -'"
3640
},
3741
"main": "src/runner.js",
3842
"files": [

scripts/runner.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Two command line arguments must be supplied:
77
* ```
8-
* (string) filePath - The path to a JSON formatted file.
8+
* (string|string[]) filePath - The path to a JSON or JS CJS formatted file.
99
* (string) scriptEntry - A entry path separated by `.` relative to the root of the JSON file that is an `Array`.
1010
* ```
1111
*/
@@ -14,12 +14,12 @@ var runner = require('../src/runner');
1414

1515
if (typeof process.argv[2] !== 'string')
1616
{
17-
throw new TypeError('typhonjs-npm-scripts-runner error: argument `file path` is missing or not a `string`.');
17+
throw new TypeError('typhonjs-npm-scripts-runner error: argument[1] `file path` is missing or not a `string`.');
1818
}
1919

2020
if (typeof process.argv[3] !== 'string')
2121
{
22-
throw new TypeError('typhonjs-npm-scripts-runner error: argument `script entry` is missing or not a `string`.');
22+
throw new TypeError("typhonjs-npm-scripts-runner error: argument[2] 'script entry' is missing or not a 'string'.");
2323
}
2424

2525
runner.run(process.argv[2], process.argv[3], process.argv[4]);

test/scripts/test.js

Lines changed: 92 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var fs = require('fs-extra');
8+
var cp = require('child_process');
89

910
fs.emptyDirSync('./test/fixture');
1011

@@ -61,24 +62,7 @@ console.log('Testing bare script treated as JSON:');
6162
runner.run('./test/data/.scriptrc', 'test.data.scripts');
6263

6364
// 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();
8266

8367
// -------------------------
8468

@@ -87,7 +71,8 @@ console.log('\nTesting bare script message prepend:');
8771

8872
runner.run('./test/data/.scriptrc', 'test.data.scripts', 'A custom message -');
8973

90-
fs.emptyDirSync('./test/fixture');
74+
// Verify that two files were copied.
75+
verifyOutput();
9176

9277
// -------------------------
9378

@@ -98,24 +83,7 @@ console.log('\nTesting bad file array first entry:');
9883
runner.run(['./test/data/.badfile1', './test/data/.scriptrc', './test/data/.badfile2'], 'test.data.scripts');
9984

10085
// 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();
11987

12088
// -------------------------
12189

@@ -124,7 +92,8 @@ console.log('\nTesting message prepend:');
12492

12593
runner.run('./test/data/.scriptrc', 'test.data.scripts', 'A custom message -');
12694

127-
fs.emptyDirSync('./test/fixture');
95+
// Verify that two files were copied.
96+
verifyOutput();
12897

12998
// ------------------------------------------------------------------------------------------------------------------
13099

@@ -135,24 +104,7 @@ console.log('\nTesting CJS scriptrc.js require:');
135104
runner.run('./test/data/.scriptrc.js', 'test.data.scripts');
136105

137106
// 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();
156108

157109
// -------------------------
158110

@@ -161,7 +113,8 @@ console.log('\nTesting CJS scriptrc.js require - message prepend:');
161113

162114
runner.run('./test/data/.scriptrc.js', 'test.data.scripts', 'A custom message -');
163115

164-
fs.emptyDirSync('./test/fixture');
116+
// Verify that two files were copied.
117+
verifyOutput();
165118

166119
// -------------------------
167120

@@ -172,24 +125,7 @@ console.log('\nTesting CJS bad first entry for file array:');
172125
runner.run(['./test/data/.badfile1.js', './test/data/.scriptrc.js', './test/data/.badfile2.js'], 'test.data.scripts');
173126

174127
// 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();
193129

194130
// -------------------------
195131

@@ -198,7 +134,8 @@ console.log('\nTesting CJS prepend message:');
198134

199135
runner.run('./test/data/.scriptrc.js', 'test.data.scripts', 'A custom message -');
200136

201-
fs.emptyDirSync('./test/fixture');
137+
// Verify that two files were copied.
138+
verifyOutput();
202139

203140
// -------------------------
204141

@@ -209,30 +146,7 @@ console.log('\nTesting JSON file array only executes first matching script:');
209146
runner.run(['./test/data/.scriptrc', './test/data/.scriptrc2'], 'test.data.scripts');
210147

211148
// 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();
236150

237151
// -------------------------
238152

@@ -243,27 +157,88 @@ console.log('\nTesting CJS file array only executes first matching script:');
243157
runner.run(['./test/data/.scriptrc.js', './test/data/.scriptrc2'], 'test.data.scripts');
244158

245159
// 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)
247185
{
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
250217
{
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+
}
252235
}
253-
254-
// Verify that `./test/fixture/empty3.json` exists.
255-
if (!fs.statSync('./test/fixture/empty2.js').isFile())
236+
catch (err)
256237
{
257-
throw new Error('`./test/fixture/empty2.js` is not a file.');
238+
throw new Error('typhonjs-npm-scripts-runner test error: ' + err);
258239
}
259-
260-
// Verify that `./test/fixture/empty3.json` exists.
261-
if (fs.existsSync('./test/fixture/SHOULD_NOT_EXIST.js'))
240+
finally
262241
{
263-
throw new Error('`./test/fixture/SHOULD_NOT_EXIST.js` is was copied / scriptrc2 was incorrectly executed.');
242+
fs.emptyDirSync('./test/fixture');
264243
}
265-
}
266-
catch (err)
267-
{
268-
throw new Error('typhonjs-npm-scripts-runner test error: ' + err);
269-
}
244+
}

0 commit comments

Comments
 (0)