-
Notifications
You must be signed in to change notification settings - Fork 2
/
tap-es.js
196 lines (161 loc) · 4.86 KB
/
tap-es.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// tap-es
var duration = true;
var addScriptName = false;
/*
A helper function to clean array reference and type of content
-----
Param arr : Array to be de-referenced
Param inst : Type of content
*/
function cleanArray( arr, inst ) {
// Clean array reference and make sure arr is type of array
var a = (Array.isArray(arr)) ? arr.splice(0) : [arr];
var i = a.length;
while (i--) {
if (! a[i] instanceof inst) {
a.splice(i, 1);
};
};
return a;
};
/*
Single Test
-----
Param tests : String: Path to script
Param targets : Array, String: [list of] Adobe application target
Param comparator : Number, String, Boolean, Function: Test comparator
*/
function Test( description, scripts, targets, comparator ) {
var _Test = this;
// Object must be created with new
if( !(_Test instanceof Test) ){
return new Test( tests, targets, comparator );
};
// String: Test description
_Test.description = String( description );
// String: Path to JSX script
_Test.scripts = cleanArray( scripts, String );
// Array: of target strings
_Test.targets = cleanArray( targets, String );
// Parse test comparator
switch (typeof comparator) {
case 'string':
case 'number':
case 'boolean':
case 'function':
_Test.comparator = comparator;
break;
case 'object':
console.log('WARNING: type of comparator cannot be set to objects, using default instead.');
default:
_Test.comparator = true;
break;
};
};
/*
The Deck holds all tests that need to be taped
-----
Param tests : Array of objects (Test)
Param resultPath : String: Path to export file (eg /path/to/results.md)
*/
function Deck( tests ) {
var _Deck = this;
// Object must be created with new
if( !(_Deck instanceof Deck) ){
return new Deck( tests, resultPath );
};
// Array: Containing tests
_Deck.tests = new Array();
_Deck.add = function( tests ) {
if ( Array.isArray(tests) ){
_Deck.tests.concat(cleanArray( Test, tests.slice(0) ));
} else if(tests instanceof Test) {
_Deck.tests.push(tests);
};
};
_Deck.get = function() {
return _Deck.tests.slice(0);
};
_Deck.reset = function() {
_Deck.tests = new Array();
};
_Deck.add( tests );
};
//----------------------------------------------------------------------
var exports = {};
//----------------------------------------------------------------------
var reportDuration = exports.reportDuration = function( durationBool ) {
duration = Boolean(durationBool);
return exports;
};
var reportScriptName = exports.reportScriptName = function( scriptNameBool ) {
addScriptName = Boolean(scriptNameBool);
return exports;
};
var deck = exports.deck = new Deck();
var reset = exports.reset = function() {
deck.reset();
return exports;
};
/*
Function: Get all tests loaded in the deck
-----
Returns: Array of Test objects
*/
var get = exports.get = function() {
return deck.get();
};
/*
Function: Get all tests loaded in the deck
-----
Returns: Array of paths
// Note I have exported this function mainly as a hook for testing
*/
var resolveGlob = exports.resolveGlob = function( pathStr ) {
var glob = require("glob"), isGlob = require('is-glob');
var filePaths = [];
if( isGlob( pathStr ) ) {
filePaths = glob.sync( pathStr ).slice(0);
} else {
filePaths.push( pathStr );
};
return filePaths;
};
/*
Function: Test Generator
-----
Param scripts : String, Array: [list of] Path to script, or glob path
Param targets : Array, String: [list of] Adobe application target
Param comparator : Number, String, Boolean, Function: Test comparator
*/
var add = exports.add = function( description, scripts, targets, comparator) {
deck.add( new Test( description, scripts, targets, comparator ) );
};
/*
Function: Test Runner
-----
Param output: String: Path to export file (eg /path/to/results.md)
*/
var run = exports.run = function( output ) {
var shell = require('shelljs'), serialize = require('serialize-javascript');
var tests = deck.get(), flatDeck = [];
var x = tests.length;
while(x--) {
var test = tests[x];
var s = test.scripts.length;
while(s--) {
var scripts = resolveGlob(test.scripts[s]);
flatDeck.push({description: test.description, scripts: scripts, targets: test.targets, comparator: test.comparator, addScriptName: addScriptName});
};
};
var cmd = 'node '+ __dirname +'/run-tap.js -b "' + encodeURI(escape(serialize(flatDeck,{unsafe:true}))) + '"';
if( output === undefined ) {
shell.exec(cmd);
} else {
shell.exec(cmd).exec("tap-markdown --duration " + String(duration), {silent:true}).to( String(output), {async:true});
};
reset();
};
//----------------------------------------------------------------------
module.exports = exports;
//----------------------------------------------------------------------