UNPKG

2.82 kBJavaScriptView Raw
1var path = require('path');
2var cp = require('child_process');
3var fs = require('fs');
4
5var chai = require('chai');
6var tmp = require('tmp');
7var wrench = require('wrench');
8
9var fixtures = path.join(__dirname, 'integration', 'fixtures');
10var tmpDir = 'tmp';
11
12
13/**
14 * Spawn a Grunt process.
15 * @param {string} dir Directory with gruntfile.js.
16 * @param {function(Error, Process)} done Callback.
17 */
18function spawnGrunt(dir, done) {
19 var gruntfile = path.join(dir, 'gruntfile.js');
20 if (!fs.existsSync(gruntfile)) {
21 done(new Error('Cannot find gruntfile.js: ' + gruntfile));
22 } else {
23 var node = process.argv[0];
24 var grunt = process.argv[1]; // assumes grunt drives these tests
25 var child = cp.spawn(node, [grunt, '--verbose', '--stack'], {cwd: dir});
26 done(null, child);
27 }
28}
29
30
31/**
32 * Set up before running tests.
33 * @param {string} name Fixture name.
34 * @param {function} done Callback.
35 */
36function cloneFixture(name, done) {
37 var fixture = path.join(fixtures, name);
38 if (!fs.existsSync(tmpDir)) {
39 fs.mkdirSync(tmpDir);
40 }
41
42 tmp.dir({dir: tmpDir}, function(error, dir) {
43 if (error) {
44 return done(error);
45 }
46 var scratch = path.join(dir, name);
47 wrench.copyDirRecursive(fixture, scratch, function(error) {
48 done(error, scratch);
49 });
50 });
51}
52
53
54/**
55 * Clone a fixture and run the default Grunt task in it.
56 * @param {string} name Fixture name.
57 * @param {function(Error, scratch)} done Called with an error if the task
58 * fails. Called with the cloned fixture directory if the task succeeds.
59 */
60exports.buildFixture = function(name, done) {
61 cloneFixture(name, function(error, scratch) {
62 if (error) {
63 return done(error);
64 }
65 spawnGrunt(scratch, function(error, child) {
66 if (error) {
67 return done(error);
68 }
69 var messages = [];
70 child.stderr.on('data', function(chunk) {
71 messages.push(chunk.toString());
72 });
73 child.stdout.on('data', function(chunk) {
74 messages.push(chunk.toString());
75 });
76 child.on('close', function(code) {
77 if (code !== 0) {
78 done(new Error('Task failed: ' + messages.join('')));
79 } else {
80 done(null, scratch);
81 }
82 });
83 });
84 });
85};
86
87
88/**
89 * Clean up after running tests.
90 * @param {string} scratch Path to scratch directory.
91 * @param {function} done Callback.
92 */
93exports.afterFixture = function(scratch, done) {
94 var error;
95 if (scratch) {
96 try {
97 wrench.rmdirSyncRecursive(scratch, false);
98 wrench.rmdirSyncRecursive(tmpDir, false);
99 } catch (err) {
100 error = err;
101 }
102 }
103 done(error);
104};
105
106
107/** @type {boolean} */
108chai.Assertion.includeStack = true;
109
110
111/**
112 * Chai's assert function configured to include stacks on failure.
113 * @type {function}
114 */
115exports.assert = chai.assert;