1 | 'use strict';
|
2 |
|
3 | var grunt = require('../../lib/grunt');
|
4 |
|
5 | var fs = require('fs');
|
6 | var path = require('path');
|
7 |
|
8 | var Tempfile = require('temporary/lib/file');
|
9 |
|
10 | exports['util.callbackify'] = {
|
11 | 'return': function(test) {
|
12 | test.expect(1);
|
13 |
|
14 | function add(a, b) {
|
15 | return a + b;
|
16 | }
|
17 | grunt.util.callbackify(add)(1, 2, function(result) {
|
18 | test.equal(result, 3, 'should be the correct result.');
|
19 | test.done();
|
20 | });
|
21 | },
|
22 | 'callback (sync)': function(test) {
|
23 | test.expect(1);
|
24 |
|
25 | function add(a, b, done) {
|
26 | done(a + b);
|
27 | }
|
28 | grunt.util.callbackify(add)(1, 2, function(result) {
|
29 | test.equal(result, 3, 'should be the correct result.');
|
30 | test.done();
|
31 | });
|
32 | },
|
33 | 'callback (async)': function(test) {
|
34 | test.expect(1);
|
35 |
|
36 | function add(a, b, done) {
|
37 | setTimeout(done.bind(null, a + b), 0);
|
38 | }
|
39 | grunt.util.callbackify(add)(1, 2, function(result) {
|
40 | test.equal(result, 3, 'should be the correct result.');
|
41 | test.done();
|
42 | });
|
43 | }
|
44 | };
|
45 |
|
46 | exports['util'] = {
|
47 | 'error': function(test) {
|
48 | test.expect(9);
|
49 | var origError = new Error('Original error.');
|
50 |
|
51 | var err = grunt.util.error('Test message.');
|
52 | test.ok(err instanceof Error, 'Should be an Error.');
|
53 | test.equal(err.name, 'Error', 'Should be an Error.');
|
54 | test.equal(err.message, 'Test message.', 'Should have the correct message.');
|
55 |
|
56 | err = grunt.util.error('Test message.', origError);
|
57 | test.ok(err instanceof Error, 'Should be an Error.');
|
58 | test.equal(err.name, 'Error', 'Should be an Error.');
|
59 | test.equal(err.message, 'Test message.', 'Should have the correct message.');
|
60 | test.equal(err.origError, origError, 'Should reflect the original error.');
|
61 |
|
62 | var newError = new Error('Test message.');
|
63 | err = grunt.util.error(newError, origError);
|
64 | test.equal(err, newError, 'Should be the passed-in Error.');
|
65 | test.equal(err.origError, origError, 'Should reflect the original error.');
|
66 | test.done();
|
67 | },
|
68 | 'linefeed': function(test) {
|
69 | test.expect(1);
|
70 | if (process.platform === 'win32') {
|
71 | test.equal(grunt.util.linefeed, '\r\n', 'linefeed should be operating-system appropriate.');
|
72 | } else {
|
73 | test.equal(grunt.util.linefeed, '\n', 'linefeed should be operating-system appropriate.');
|
74 | }
|
75 | test.done();
|
76 | },
|
77 | 'normalizelf': function(test) {
|
78 | test.expect(1);
|
79 | if (process.platform === 'win32') {
|
80 | test.equal(grunt.util.normalizelf('foo\nbar\r\nbaz\r\n\r\nqux\n\nquux'), 'foo\r\nbar\r\nbaz\r\n\r\nqux\r\n\r\nquux', 'linefeeds should be normalized');
|
81 | } else {
|
82 | test.equal(grunt.util.normalizelf('foo\nbar\r\nbaz\r\n\r\nqux\n\nquux'), 'foo\nbar\nbaz\n\nqux\n\nquux', 'linefeeds should be normalized');
|
83 | }
|
84 | test.done();
|
85 | }
|
86 | };
|
87 |
|
88 | exports['util.spawn'] = {
|
89 | setUp: function(done) {
|
90 | this.script = path.resolve('test/fixtures/spawn.js');
|
91 | done();
|
92 | },
|
93 | 'exit code 0': function(test) {
|
94 | test.expect(6);
|
95 | grunt.util.spawn({
|
96 | cmd: process.execPath,
|
97 | args: [ this.script, 0 ],
|
98 | }, function(err, result, code) {
|
99 | test.equals(err, null);
|
100 | test.equals(code, 0);
|
101 | test.equals(result.stdout, 'stdout');
|
102 | test.equals(result.stderr, 'stderr');
|
103 | test.equals(result.code, 0);
|
104 | test.equals(String(result), 'stdout');
|
105 | test.done();
|
106 | });
|
107 | },
|
108 | 'exit code 0, fallback': function(test) {
|
109 | test.expect(6);
|
110 | grunt.util.spawn({
|
111 | cmd: process.execPath,
|
112 | args: [ this.script, 0 ],
|
113 | fallback: 'ignored if exit code is 0'
|
114 | }, function(err, result, code) {
|
115 | test.equals(err, null);
|
116 | test.equals(code, 0);
|
117 | test.equals(result.stdout, 'stdout');
|
118 | test.equals(result.stderr, 'stderr');
|
119 | test.equals(result.code, 0);
|
120 | test.equals(String(result), 'stdout');
|
121 | test.done();
|
122 | });
|
123 | },
|
124 | 'non-zero exit code': function(test) {
|
125 | test.expect(7);
|
126 | grunt.util.spawn({
|
127 | cmd: process.execPath,
|
128 | args: [ this.script, 123 ],
|
129 | }, function(err, result, code) {
|
130 | test.ok(err instanceof Error);
|
131 | test.equals(err.message, 'stderr');
|
132 | test.equals(code, 123);
|
133 | test.equals(result.stdout, 'stdout');
|
134 | test.equals(result.stderr, 'stderr');
|
135 | test.equals(result.code, 123);
|
136 | test.equals(String(result), 'stderr');
|
137 | test.done();
|
138 | });
|
139 | },
|
140 | 'non-zero exit code, fallback': function(test) {
|
141 | test.expect(6);
|
142 | grunt.util.spawn({
|
143 | cmd: process.execPath,
|
144 | args: [ this.script, 123 ],
|
145 | fallback: 'custom fallback'
|
146 | }, function(err, result, code) {
|
147 | test.equals(err, null);
|
148 | test.equals(code, 123);
|
149 | test.equals(result.stdout, 'stdout');
|
150 | test.equals(result.stderr, 'stderr');
|
151 | test.equals(result.code, 123);
|
152 | test.equals(String(result), 'custom fallback');
|
153 | test.done();
|
154 | });
|
155 | },
|
156 | 'cmd not found': function(test) {
|
157 | test.expect(3);
|
158 | grunt.util.spawn({
|
159 | cmd: 'nodewtfmisspelled',
|
160 | }, function(err, result, code) {
|
161 | test.ok(err instanceof Error);
|
162 | test.equals(code, 127);
|
163 | test.equals(result.code, 127);
|
164 | test.done();
|
165 | });
|
166 | },
|
167 | 'cmd not found, fallback': function(test) {
|
168 | test.expect(4);
|
169 | grunt.util.spawn({
|
170 | cmd: 'nodewtfmisspelled',
|
171 | fallback: 'use a fallback or good luck'
|
172 | }, function(err, result, code) {
|
173 | test.equals(err, null);
|
174 | test.equals(code, 127);
|
175 | test.equals(result.code, 127);
|
176 | test.equals(String(result), 'use a fallback or good luck');
|
177 | test.done();
|
178 | });
|
179 | },
|
180 | 'cmd not in path': function(test) {
|
181 | test.expect(6);
|
182 | var win32 = process.platform === 'win32';
|
183 | grunt.util.spawn({
|
184 | cmd: 'test\\fixtures\\exec' + (win32 ? '.cmd' : '.sh'),
|
185 | }, function(err, result, code) {
|
186 | test.equals(err, null);
|
187 | test.equals(code, 0);
|
188 | test.equals(result.stdout, 'done');
|
189 | test.equals(result.stderr, '');
|
190 | test.equals(result.code, 0);
|
191 | test.equals(String(result), 'done');
|
192 | test.done();
|
193 | });
|
194 | },
|
195 | 'cmd not in path (with cwd)': function(test) {
|
196 | test.expect(6);
|
197 | var win32 = process.platform === 'win32';
|
198 | grunt.util.spawn({
|
199 | cmd: './exec' + (win32 ? '.cmd' : '.sh'),
|
200 | opts: {cwd: 'test/fixtures'},
|
201 | }, function(err, result, code) {
|
202 | test.equals(err, null);
|
203 | test.equals(code, 0);
|
204 | test.equals(result.stdout, 'done');
|
205 | test.equals(result.stderr, '');
|
206 | test.equals(result.code, 0);
|
207 | test.equals(String(result), 'done');
|
208 | test.done();
|
209 | });
|
210 | },
|
211 | 'grunt': function(test) {
|
212 | test.expect(3);
|
213 | grunt.util.spawn({
|
214 | grunt: true,
|
215 | args: [ '--gruntfile', 'test/fixtures/Gruntfile-print-text.js', 'print:foo' ],
|
216 | }, function(err, result, code) {
|
217 | test.equals(err, null);
|
218 | test.equals(code, 0);
|
219 | test.ok(/^OUTPUT: foo/m.test(result.stdout), 'stdout should contain output indicating the grunt task was run.');
|
220 | test.done();
|
221 | });
|
222 | },
|
223 | 'grunt (with cwd)': function(test) {
|
224 | test.expect(3);
|
225 | grunt.util.spawn({
|
226 | grunt: true,
|
227 | args: [ '--gruntfile', 'Gruntfile-print-text.js', 'print:foo' ],
|
228 | opts: {cwd: 'test/fixtures'},
|
229 | }, function(err, result, code) {
|
230 | test.equals(err, null);
|
231 | test.equals(code, 0);
|
232 | test.ok(/^OUTPUT: foo/m.test(result.stdout), 'stdout should contain output indicating the grunt task was run.');
|
233 | test.done();
|
234 | });
|
235 | },
|
236 | 'custom stdio stream(s)': function(test) {
|
237 | test.expect(6);
|
238 | var stdoutFile = new Tempfile();
|
239 | var stderrFile = new Tempfile();
|
240 | var stdout = fs.openSync(stdoutFile.path, 'a');
|
241 | var stderr = fs.openSync(stderrFile.path, 'a');
|
242 | var child = grunt.util.spawn({
|
243 | cmd: process.execPath,
|
244 | args: [ this.script, 0 ],
|
245 | opts: {stdio: [null, stdout, stderr]},
|
246 | }, function(err, result, code) {
|
247 | test.equals(code, 0);
|
248 | test.equals(String(fs.readFileSync(stdoutFile.path)), 'stdout\n', 'Child process stdout should have been captured via custom stream.');
|
249 | test.equals(String(fs.readFileSync(stderrFile.path)), 'stderr\n', 'Child process stderr should have been captured via custom stream.');
|
250 | stdoutFile.unlinkSync();
|
251 | stderrFile.unlinkSync();
|
252 | test.equals(result.stdout, '', 'Nothing will be passed to the stdout string when spawn stdio is a custom stream.');
|
253 | test.done();
|
254 | });
|
255 | test.ok(!child.stdout, 'child should not have a stdout property.');
|
256 | test.ok(!child.stderr, 'child should not have a stderr property.');
|
257 | },
|
258 | };
|
259 |
|
260 | exports['util.underscore.string'] = function(test) {
|
261 | test.expect(4);
|
262 | test.equals(grunt.util._.trim(' foo '), 'foo', 'Should have trimmed the string.');
|
263 | test.equals(grunt.util._.capitalize('foo'), 'Foo', 'Should have capitalized the first letter.');
|
264 | test.equals(grunt.util._.words('one two three').length, 3, 'Should have counted three words.');
|
265 | test.ok(grunt.util._.isBlank(' '), 'Should be blank.');
|
266 | test.done();
|
267 | };
|