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, 'integration', 'fixtures');
|
10 | var tmpDir = 'tmp';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function 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];
|
25 | var child = cp.spawn(node, [grunt, '--verbose', '--stack'], {cwd: dir});
|
26 | done(null, child);
|
27 | }
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function 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 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | exports.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 |
|
90 |
|
91 |
|
92 |
|
93 | exports.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 |
|
108 | chai.Assertion.includeStack = true;
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | exports.assert = chai.assert;
|