1 | var path = require('path');
|
2 | var fs = require('fs-extra');
|
3 | var sinon = require('sinon');
|
4 | var chai = require('chai');
|
5 | var HappyPlugin = require('./HappyPlugin');
|
6 | var HappyRPCHandler = require('./HappyRPCHandler');
|
7 | var TEMP_DIR = path.resolve(__dirname, '../.happypack__test');
|
8 | var TestUtils = exports;
|
9 | var sandbox;
|
10 | var cleanups = [];
|
11 |
|
12 | sinon.assert.expose(chai.assert, { prefix: "" });
|
13 |
|
14 | beforeEach(function() {
|
15 | sandbox = sinon.sandbox.create();
|
16 |
|
17 | fs.ensureDirSync(TEMP_DIR);
|
18 | fs.emptyDirSync(TEMP_DIR);
|
19 | });
|
20 |
|
21 | afterEach(function() {
|
22 | cleanups.forEach(function(callback) {
|
23 | callback();
|
24 | });
|
25 |
|
26 | cleanups = [];
|
27 |
|
28 | fs.removeSync(TEMP_DIR);
|
29 |
|
30 | sandbox.restore();
|
31 | });
|
32 |
|
33 | exports.HAPPY_LOADER_PATH = path.resolve(__dirname, 'HappyLoader.js');
|
34 | exports.assert = chai.assert;
|
35 | exports.fixturePath = function(fileName) {
|
36 | return path.resolve(__dirname, '__tests__', 'fixtures', fileName);
|
37 | };
|
38 |
|
39 | exports.tempPath = function(fileName) {
|
40 | return path.resolve(TEMP_DIR, interpolateGUID(fileName));
|
41 | };
|
42 |
|
43 | exports.tempDir = function(dirName) {
|
44 | if (dirName.length === 0) {
|
45 | return TEMP_DIR;
|
46 | }
|
47 |
|
48 | var dirPath = exports.tempPath(dirName);
|
49 |
|
50 | fs.ensureDirSync(dirPath);
|
51 |
|
52 | return dirPath;
|
53 | };
|
54 |
|
55 | exports.fixture = function(fileName) {
|
56 | return fs.readFileSync(exports.fixturePath(fileName), 'utf-8');
|
57 | };
|
58 |
|
59 | exports.createLoader = function(impl, query) {
|
60 | return exports.createLoaderFromString('module.exports = ' + impl.toString(), query);
|
61 | };
|
62 |
|
63 | exports.createLoaderFromString = function(string, query) {
|
64 | var loaderName = interpolateGUID('temp_loader__[guid]');
|
65 | var filePath = exports.tempPath(loaderName + '-loader.js');
|
66 |
|
67 | fs.writeFileSync(filePath, string, 'utf-8');
|
68 |
|
69 | return { _name: loaderName, path: filePath, query: query };
|
70 | };
|
71 |
|
72 | exports.createFile = function(fileName, contents) {
|
73 | var filePath = exports.tempPath(fileName);
|
74 |
|
75 | fs.ensureDirSync(path.dirname(filePath));
|
76 | fs.writeFileSync(filePath, contents, 'utf-8');
|
77 |
|
78 | return {
|
79 | getPath: function() {
|
80 | return filePath;
|
81 | },
|
82 |
|
83 | getContents: function() {
|
84 | return contents;
|
85 | }
|
86 | }
|
87 | };
|
88 |
|
89 | exports.clearCache = function() {
|
90 | fs.removeSync('.happypack');
|
91 | };
|
92 |
|
93 | exports.IntegrationSuite = function(mochaSuite) {
|
94 | mochaSuite.timeout(process.env.HAPPY_TEST_TIMEOUT ?
|
95 | parseInt(process.env.HAPPY_TEST_TIMEOUT, 10) :
|
96 | 2000
|
97 | );
|
98 |
|
99 | mochaSuite.beforeEach(function() {
|
100 | HappyPlugin.resetUID();
|
101 | });
|
102 |
|
103 | mochaSuite.afterEach(function() {
|
104 | HappyPlugin.resetUID();
|
105 | TestUtils.clearCache();
|
106 | });
|
107 | };
|
108 |
|
109 | exports.getWebpackOutputBundlePath = function(rawStats, name) {
|
110 | return path.join(rawStats.compilation.outputOptions.path, name || 'bundle.js');
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | exports.spyOnActiveLoader = function(fn) {
|
119 | var registerActiveLoader = HappyRPCHandler.registerActiveLoader;
|
120 | var happyLoader;
|
121 |
|
122 | sandbox.stub(HappyRPCHandler, 'registerActiveLoader', function(id, loader) {
|
123 | happyLoader = loader;
|
124 |
|
125 | if (fn) {
|
126 | fn(happyLoader);
|
127 | }
|
128 |
|
129 | return registerActiveLoader.apply(HappyRPCHandler, arguments);
|
130 | });
|
131 |
|
132 | Object.defineProperty(TestUtils, 'activeLoader', {
|
133 | configurable: true,
|
134 | enumerable: false,
|
135 | get: function() {
|
136 | return happyLoader;
|
137 | }
|
138 | });
|
139 |
|
140 | cleanups.push(function() {
|
141 | happyLoader = null;
|
142 | });
|
143 |
|
144 | return function() { return happyLoader; };
|
145 | };
|
146 |
|
147 | exports.assertNoWebpackErrors = function(err, rawStats, done) {
|
148 | if (err) {
|
149 | done(err);
|
150 | return true;
|
151 | }
|
152 |
|
153 | var stats = rawStats.toJson();
|
154 |
|
155 | if (stats.errors.length) {
|
156 | done(stats.errors);
|
157 | return true;
|
158 | }
|
159 |
|
160 | if (stats.warnings.length) {
|
161 | done(stats.warnings);
|
162 | return true;
|
163 | }
|
164 | };
|
165 |
|
166 | exports.getSinonSandbox = function() {
|
167 | return sandbox;
|
168 | };
|
169 |
|
170 | function interpolateGUID(string) {
|
171 | return string.replace('[guid]', guid());
|
172 | }
|
173 |
|
174 |
|
175 | function guid() {
|
176 | function s4() {
|
177 | return Math.floor((1 + Math.random()) * 0x10000)
|
178 | .toString(16)
|
179 | .substring(1);
|
180 | }
|
181 | return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
|
182 | s4() + '-' + s4() + s4() + s4();
|
183 | } |
\ | No newline at end of file |