1 | var path = require('path');
|
2 | var cp = require('child_process');
|
3 | var fs = require('fs');
|
4 |
|
5 | var chai = require('chai');
|
6 | var tmp = require('tmp');
|
7 | var wrench = require('wrench');
|
8 |
|
9 | var fixtures = path.join(__dirname, 'fixtures');
|
10 | var tmpDir = 'tmp';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function 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];
|
24 | var child = cp.spawn(node, [grunt], {cwd: dir});
|
25 | done(null, child);
|
26 | }
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function 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 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | exports.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 |
|
89 |
|
90 |
|
91 |
|
92 | exports.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 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | exports.git = require('../lib/git');
|
111 |
|
112 |
|
113 |
|
114 | chai.Assertion.includeStack = true;
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | exports.assert = chai.assert;
|