forked from tidev/alloy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestUtils.js
175 lines (159 loc) · 4.43 KB
/
testUtils.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
var exec = require('child_process').exec,
fs = require('fs'),
os = require('os'),
wrench = require('wrench'),
path = require('path'),
_ = require('../../Alloy/lib/alloy/underscore')._,
uglifyjs = require('uglify-js'),
U = require('../../Alloy/utils'),
styler = require('../../Alloy/commands/compile/styler');
var alloyRoot = path.join(__dirname,'..','..');
var IS_WIN = /^win/i.test(os.platform());
exports.TIMEOUT_DEFAULT = IS_WIN ? 5000 : 2000;
exports.paths = {
templates: path.join(alloyRoot,'Alloy','template'),
harnessTemplate: path.join(alloyRoot,'test','projects','HarnessTemplate'),
harness: path.join(alloyRoot,'test','projects','Harness')
};
// Recreate the test app harness
//
// Params:
// * callback: the callback function to be executed when the harness
// is successfully recreated.
function resetTestApp(callback) {
var paths = exports.paths;
wrench.rmdirSyncRecursive(paths.harness, true);
wrench.mkdirSyncRecursive(paths.harness, 0777);
wrench.copyDirSyncRecursive(paths.harnessTemplate, paths.harness);
exec('alloy new "' + paths.harness + '"', function(error, stdout, stderr) {
if (error) {
console.error('Failed to create new alloy project at ' + paths.harness);
process.exit();
}
callback();
});
}
// Turns the arguments given to the callback of the exec() function
// into an object literal.
//
// Params:
// * args: The "arguments" object from the callback of an exec() call
//
// Return: An object literal with the error, stdout, and stderr
function getExecObject(args) {
args = Array.prototype.slice.call(args, 0);
return {
error: args[0],
stdout: args[1],
stderr: args[2]
};
}
// Convenience function for handling asynchronous tests that rely on the
// exec() function. The output values from the first runs() block will
// be available as this.output in the second runs() block where the
// actual tests are evaluated.
//
// Params:
// * cmd: The command to run through exec()
// * opts: An object that can contain the following parameters:
// * timeout: How long to wait for the command to execute before declaring
// the test failed
// * test: The actual test function to execute on output returned from exec()
// * reset: If truthy, recreate the default test harness before executing
//
// Return: none
exports.asyncExecTest = function(cmd, opts) {
opts = opts || {};
runs(function() {
var self = this;
self.done = false;
var asyncFunc = function() {
exec(cmd, function() {
self.done = true;
self.output = getExecObject(arguments);
});
};
if (opts.reset) {
resetTestApp(function() {
asyncFunc();
});
} else {
asyncFunc();
}
});
waitsFor(
function() { return this.done; },
'exec("' + cmd + '") timed out', opts.timeout || exports.TIMEOUT_DEFAULT
);
runs(opts.test || function() {
expect(this.output.error).toBeNull();
});
};
// Matchers for Jasmine
function toBeTssFile(expected) {
var actual = this.actual;
var style;
try {
var die = U.die;
U.die = function(msg, e) {
U.die = die;
throw U.createErrorOutput(msg, e);
};
style = styler.loadStyle(actual);
U.die = die;
} catch (e) {
U.die = die || U.die;
return false;
}
if (_.isObject(style)) {
return true;
}
return false;
}
function toBeJavascript(expected) {
try {
return uglifyjs.parse(this.actual);
} catch (e) {
return false;
}
}
function toBeJavascriptFile(expected) {
var actual = this.actual;
var notText = this.isNot ? " not" : "";
this.message = function () {
return "Expected " + actual + notText + " to be a Javascript file";
};
try {
var js = fs.readFileSync(this.actual,'utf8');
return toBeJavascript.call({actual:js}, expected);
} catch (e) {
return false;
}
}
exports.addMatchers = function() {
beforeEach(function() {
this.addMatchers({
toBeJavascript: toBeJavascript,
toBeJavascriptFile: toBeJavascriptFile,
toBeTssFile: toBeTssFile,
toHaveNoUndefinedStyles: function() {
return !_.find(this.actual, function(o) {
return o.key === 'undefined' && o.isApi;
});
},
toHaveSameContentAs: function(expected) {
return U.normalizeReturns(fs.readFileSync(this.actual,'utf8')) ===
U.normalizeReturns(fs.readFileSync(expected,'utf8'));
},
toExist: function(expected) {
return path.existsSync(this.actual);
},
toBeArray: function(expected) {
return _.isArray(this.actual);
},
toBeObject: function(expected) {
return _.isObject(this.actual);
}
});
});
};