UNPKG

2.95 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, '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 if (!fs.existsSync(path.join(dir, 'Gruntfile.js'))) {
20 done(new Error('Cannot find Gruntfile.js in dir: ' + dir));
21 } else {
22 var node = process.argv[0];
23 var grunt = process.argv[1]; // assumes grunt drives these tests
24 var child = cp.spawn(node, [grunt], {cwd: dir});
25 done(null, child);
26 }
27}
28
29
30/**
31 * Set up before running tests.
32 * @param {string} name Fixture name.
33 * @param {function} done Callback.
34 */
35function cloneFixture(name, done) {
36 var fixture = path.join(fixtures, name);
37 if (!fs.existsSync(tmpDir)) {
38 fs.mkdirSync(tmpDir);
39 }
40
41 tmp.dir({dir: tmpDir}, function(error, dir) {
42 if (error) {
43 return done(error);
44 }
45 var scratch = path.join(dir, name);
46 wrench.copyDirRecursive(fixture, scratch, function(error) {
47 done(error, scratch);
48 });
49 });
50}
51
52
53/**
54 * Clone a fixture and run the default Grunt task in it.
55 * @param {string} name Fixture name.
56 * @param {function(Error, scratch)} done Called with an error if the task
57 * fails. Called with the cloned fixture directory if the task succeeds.
58 */
59exports.buildFixture = function(name, done) {
60 cloneFixture(name, function(error, scratch) {
61 if (error) {
62 return done(error);
63 }
64 spawnGrunt(scratch, function(error, child) {
65 if (error) {
66 return done(error);
67 }
68 var messages = [];
69 child.stderr.on('data', function(chunk) {
70 messages.push(chunk.toString());
71 });
72 child.stdout.on('data', function(chunk) {
73 messages.push(chunk.toString());
74 });
75 child.on('close', function(code) {
76 if (code !== 0) {
77 done(new Error('Task failed: ' + messages.join('')));
78 } else {
79 done(null, scratch);
80 }
81 });
82 });
83 });
84};
85
86
87/**
88 * Clean up after running tests.
89 * @param {string} scratch Path to scratch directory.
90 * @param {function} done Callback.
91 */
92exports.afterFixture = function(scratch, done) {
93 var error;
94 try {
95 wrench.rmdirSyncRecursive(scratch, false);
96 wrench.rmdirSyncRecursive(tmpDir, false);
97 } catch (err) {
98 error = err;
99 }
100 done(error);
101};
102
103
104/**
105 * Util function for handling spawned git processes as promises.
106 * @param {Array.<string>} args Arguments.
107 * @param {string} cwd Working directory.
108 * @return {Promise} A promise.
109 */
110exports.git = require('../lib/git');
111
112
113/** @type {boolean} */
114chai.Assertion.includeStack = true;
115
116
117/**
118 * Chai's assert function configured to include stacks on failure.
119 * @type {function}
120 */
121exports.assert = chai.assert;