Skip to content

Commit

Permalink
Add test to make sure configHash option is working
Browse files Browse the repository at this point in the history
  • Loading branch information
mzgoddard committed Sep 11, 2016
1 parent d626073 commit 2ffc405
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/fixtures/hard-source-confighash-dir/config-hash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-confighash-dir/fib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(n) {
return n + (n > 0 ? n - 2 : 0);
};
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-confighash-dir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var fib = require('./fib');

console.log(fib(3));
24 changes: 24 additions & 0 deletions tests/fixtures/hard-source-confighash-dir/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var fs = require('fs');

var HardSourceWebpackPlugin = require('../../..');

module.exports = {
context: __dirname,
entry: './index.js',
output: {
path: __dirname + '/tmp',
filename: 'main.js',
},
plugins: [
new HardSourceWebpackPlugin({
cacheDirectory: 'cache/[confighash]',
recordsPath: 'cache/[confighash]/records.json',
configHash: function(config) {
return fs.readFileSync(__dirname + '/config-hash', 'utf8');
},
environmentPaths: {
root: __dirname + '/../../..',
},
}),
],
};
1 change: 1 addition & 0 deletions tests/fixtures/hard-source-confighash/config-hash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-confighash/fib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(n) {
return n + (n > 0 ? n - 2 : 0);
};
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-confighash/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var fib = require('./fib');

console.log(fib(3));
24 changes: 24 additions & 0 deletions tests/fixtures/hard-source-confighash/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var fs = require('fs');

var HardSourceWebpackPlugin = require('../../..');

module.exports = {
context: __dirname,
entry: './index.js',
output: {
path: __dirname + '/tmp',
filename: 'main.js',
},
plugins: [
new HardSourceWebpackPlugin({
cacheDirectory: 'cache',
recordsPath: 'cache/records.json',
configHash: function(config) {
return fs.readFileSync(__dirname + '/config-hash', 'utf8');
},
environmentPaths: {
root: __dirname + '/../../..',
},
}),
],
};
51 changes: 51 additions & 0 deletions tests/hard-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var fs = require('fs');

var expect = require('chai').expect;

var itCompilesChange = require('./util').itCompilesChange;

describe('hard-source features', function() {

itCompilesChange('hard-source-confighash', {
'config-hash': 'a',
'fib.js': [
'module.exports = function(n) {',
' return n + (n > 0 ? n - 1 : 0);',
'};',
].join('\n'),
'fib/index.js': null,
}, {
'config-hash': 'b',
'fib.js': null,
'fib/index.js': [
'module.exports = function(n) {',
' return n + (n > 0 ? n - 2 : 0);',
'};',
].join('\n'),
}, function(output) {
expect(fs.readdirSync(__dirname + '/fixtures/hard-source-confighash/tmp/cache'))
.to.have.length(6);
});

itCompilesChange('hard-source-confighash-dir', {
'config-hash': 'a',
'fib.js': [
'module.exports = function(n) {',
' return n + (n > 0 ? n - 1 : 0);',
'};',
].join('\n'),
'fib/index.js': null,
}, {
'config-hash': 'b',
'fib.js': null,
'fib/index.js': [
'module.exports = function(n) {',
' return n + (n > 0 ? n - 2 : 0);',
'};',
].join('\n'),
}, function(output) {
expect(fs.readdirSync(__dirname + '/fixtures/hard-source-confighash-dir/tmp/cache'))
.to.have.length(2);
});

});
22 changes: 20 additions & 2 deletions tests/util/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var fs = require('fs');
var path = require('path');
var vm = require('vm');

var expect = require('chai').expect;
var MemoryFS = require('memory-fs');
Expand All @@ -8,9 +9,26 @@ var Promise = require('bluebird');
var rimraf = require('rimraf');
var webpack = require('webpack');

function wrapModule(code) {
return '(function(exports, require, module, __filename, __dirname) {' +
code +
'})';
}

function callModule(fn, filename) {
var module = {exports: {}};
fn(module.exports, Object.assign(function(modulename) {
if (/\W/.test(modulename[0])) {
return require(path.join(path.dirname(filename), modulename));
}
return require(modulename);
}, require), module, filename, path.dirname(filename));
return module.exports;
}

exports.compile = function(fixturePath) {
var configPath = path.join(__dirname, '..', 'fixtures', fixturePath, 'webpack.config');
var compiler = webpack(require(configPath));
var configPath = path.join(__dirname, '..', 'fixtures', fixturePath, 'webpack.config.js');
var compiler = webpack(callModule(vm.runInThisContext(wrapModule(fs.readFileSync(configPath, 'utf8')), {filename: configPath}), configPath));
var outputfs = compiler.outputFileSystem = new MemoryFS();
var readdir = Promise.promisify(outputfs.readdir, {context: outputfs});
var readFile = Promise.promisify(outputfs.readFile, {context: outputfs});
Expand Down

0 comments on commit 2ffc405

Please sign in to comment.