mirror of https://github.com/webpack/webpack.git
				
				
				
			Merge pull request #4054 from shubheksha/refactor-test-WatchTestCases
refactor(ES6): WatchTestCases.test.js
This commit is contained in:
		
						commit
						c9bef6b965
					
				| 
						 | 
				
			
			@ -1,21 +1,23 @@
 | 
			
		|||
var should = require("should");
 | 
			
		||||
var path = require("path");
 | 
			
		||||
var fs = require("fs");
 | 
			
		||||
var vm = require("vm");
 | 
			
		||||
var Test = require("mocha/lib/test");
 | 
			
		||||
var checkArrayExpectation = require("./checkArrayExpectation");
 | 
			
		||||
"use strict";
 | 
			
		||||
 | 
			
		||||
var Stats = require("../lib/Stats");
 | 
			
		||||
var webpack = require("../lib/webpack");
 | 
			
		||||
const should = require("should");
 | 
			
		||||
const path = require("path");
 | 
			
		||||
const fs = require("fs");
 | 
			
		||||
const vm = require("vm");
 | 
			
		||||
const Test = require("mocha/lib/test");
 | 
			
		||||
const checkArrayExpectation = require("./checkArrayExpectation");
 | 
			
		||||
 | 
			
		||||
const Stats = require("../lib/Stats");
 | 
			
		||||
const webpack = require("../lib/webpack");
 | 
			
		||||
 | 
			
		||||
function copyDiff(src, dest) {
 | 
			
		||||
	if(!fs.existsSync(dest))
 | 
			
		||||
		fs.mkdirSync(dest);
 | 
			
		||||
	var files = fs.readdirSync(src);
 | 
			
		||||
	files.forEach(function(filename) {
 | 
			
		||||
		var srcFile = path.join(src, filename);
 | 
			
		||||
		var destFile = path.join(dest, filename);
 | 
			
		||||
		var directory = fs.statSync(srcFile).isDirectory();
 | 
			
		||||
	const files = fs.readdirSync(src);
 | 
			
		||||
	files.forEach((filename) => {
 | 
			
		||||
		const srcFile = path.join(src, filename);
 | 
			
		||||
		const destFile = path.join(dest, filename);
 | 
			
		||||
		const directory = fs.statSync(srcFile).isDirectory();
 | 
			
		||||
		if(directory) {
 | 
			
		||||
			copyDiff(srcFile, destFile);
 | 
			
		||||
		} else {
 | 
			
		||||
| 
						 | 
				
			
			@ -27,10 +29,10 @@ function copyDiff(src, dest) {
 | 
			
		|||
function remove(src) {
 | 
			
		||||
	if(!fs.existsSync(src))
 | 
			
		||||
		return;
 | 
			
		||||
	var files = fs.readdirSync(src);
 | 
			
		||||
	files.forEach(function(filename) {
 | 
			
		||||
		var srcFile = path.join(src, filename);
 | 
			
		||||
		var directory = fs.statSync(srcFile).isDirectory();
 | 
			
		||||
	const files = fs.readdirSync(src);
 | 
			
		||||
	files.forEach((filename) => {
 | 
			
		||||
		const srcFile = path.join(src, filename);
 | 
			
		||||
		const directory = fs.statSync(srcFile).isDirectory();
 | 
			
		||||
		if(directory) {
 | 
			
		||||
			remove(srcFile);
 | 
			
		||||
		} else {
 | 
			
		||||
| 
						 | 
				
			
			@ -39,57 +41,53 @@ function remove(src) {
 | 
			
		|||
	});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
describe("WatchTestCases", function() {
 | 
			
		||||
	var casesPath = path.join(__dirname, "watchCases");
 | 
			
		||||
	var categories = fs.readdirSync(casesPath);
 | 
			
		||||
describe("WatchTestCases", () => {
 | 
			
		||||
	const casesPath = path.join(__dirname, "watchCases");
 | 
			
		||||
	let categories = fs.readdirSync(casesPath);
 | 
			
		||||
 | 
			
		||||
	categories = categories.map(function(cat) {
 | 
			
		||||
	categories = categories.map((cat) => {
 | 
			
		||||
		return {
 | 
			
		||||
			name: cat,
 | 
			
		||||
			tests: fs.readdirSync(path.join(casesPath, cat)).filter(function(folder) {
 | 
			
		||||
				return folder.indexOf("_") < 0;
 | 
			
		||||
			}).sort()
 | 
			
		||||
			tests: fs.readdirSync(path.join(casesPath, cat)).filter((folder) => folder.indexOf("_") < 0).sort()
 | 
			
		||||
		};
 | 
			
		||||
	});
 | 
			
		||||
	before(function() {
 | 
			
		||||
		var dest = path.join(__dirname, "js");
 | 
			
		||||
	before(() => {
 | 
			
		||||
		let dest = path.join(__dirname, "js");
 | 
			
		||||
		if(!fs.existsSync(dest))
 | 
			
		||||
			fs.mkdirSync(dest);
 | 
			
		||||
		dest = path.join(__dirname, "js", "watch-src");
 | 
			
		||||
		if(!fs.existsSync(dest))
 | 
			
		||||
			fs.mkdirSync(dest);
 | 
			
		||||
	});
 | 
			
		||||
	categories.forEach(function(category) {
 | 
			
		||||
		before(function() {
 | 
			
		||||
			var dest = path.join(__dirname, "js", "watch-src", category.name);
 | 
			
		||||
	categories.forEach((category) => {
 | 
			
		||||
		before(() => {
 | 
			
		||||
			const dest = path.join(__dirname, "js", "watch-src", category.name);
 | 
			
		||||
			if(!fs.existsSync(dest))
 | 
			
		||||
				fs.mkdirSync(dest);
 | 
			
		||||
		})
 | 
			
		||||
		describe(category.name, function() {
 | 
			
		||||
			category.tests.forEach(function(testName) {
 | 
			
		||||
				describe(testName, function() {
 | 
			
		||||
					var tempDirectory = path.join(__dirname, "js", "watch-src", category.name, testName);
 | 
			
		||||
					var testDirectory = path.join(casesPath, category.name, testName);
 | 
			
		||||
					var runs = fs.readdirSync(testDirectory).sort().filter(function(name) {
 | 
			
		||||
		describe(category.name, () => {
 | 
			
		||||
			category.tests.forEach((testName) => {
 | 
			
		||||
				describe(testName, () => {
 | 
			
		||||
					const tempDirectory = path.join(__dirname, "js", "watch-src", category.name, testName);
 | 
			
		||||
					const testDirectory = path.join(casesPath, category.name, testName);
 | 
			
		||||
					const runs = fs.readdirSync(testDirectory).sort().filter((name) => {
 | 
			
		||||
						return fs.statSync(path.join(testDirectory, name)).isDirectory();
 | 
			
		||||
					}).map(function(name) {
 | 
			
		||||
					}).map((name) => {
 | 
			
		||||
						return {
 | 
			
		||||
							name: name,
 | 
			
		||||
							suite: describe(name, function() {})
 | 
			
		||||
						}
 | 
			
		||||
					});
 | 
			
		||||
					before(function() {
 | 
			
		||||
						remove(tempDirectory);
 | 
			
		||||
							suite: describe(name, () => {})
 | 
			
		||||
						};
 | 
			
		||||
					});
 | 
			
		||||
					before(() => remove(tempDirectory));
 | 
			
		||||
					it("should compile", function(done) {
 | 
			
		||||
						this.timeout(30000);
 | 
			
		||||
						var outputDirectory = path.join(__dirname, "js", "watch", category.name, testName);
 | 
			
		||||
						const outputDirectory = path.join(__dirname, "js", "watch", category.name, testName);
 | 
			
		||||
 | 
			
		||||
						var options = {};
 | 
			
		||||
						var configPath = path.join(testDirectory, "webpack.config.js");
 | 
			
		||||
						let options = {};
 | 
			
		||||
						const configPath = path.join(testDirectory, "webpack.config.js");
 | 
			
		||||
						if(fs.existsSync(configPath))
 | 
			
		||||
							options = require(configPath);
 | 
			
		||||
						var applyConfig = function(options) {
 | 
			
		||||
						const applyConfig = (options) => {
 | 
			
		||||
							if(!options.context) options.context = tempDirectory;
 | 
			
		||||
							if(!options.entry) options.entry = "./index.js";
 | 
			
		||||
							if(!options.target) options.target = "async-node";
 | 
			
		||||
| 
						 | 
				
			
			@ -97,21 +95,21 @@ describe("WatchTestCases", function() {
 | 
			
		|||
							if(!options.output.path) options.output.path = outputDirectory;
 | 
			
		||||
							if(typeof options.output.pathinfo === "undefined") options.output.pathinfo = true;
 | 
			
		||||
							if(!options.output.filename) options.output.filename = "bundle.js";
 | 
			
		||||
						}
 | 
			
		||||
						};
 | 
			
		||||
						if(Array.isArray(options)) {
 | 
			
		||||
							options.forEach(applyConfig)
 | 
			
		||||
							options.forEach(applyConfig);
 | 
			
		||||
						} else {
 | 
			
		||||
							applyConfig(options)
 | 
			
		||||
							applyConfig(options);
 | 
			
		||||
						}
 | 
			
		||||
 | 
			
		||||
						var state = {};
 | 
			
		||||
						var runIdx = 0;
 | 
			
		||||
						var run = runs[runIdx];
 | 
			
		||||
						var lastHash = "";
 | 
			
		||||
						const state = {};
 | 
			
		||||
						let runIdx = 0;
 | 
			
		||||
						let run = runs[runIdx];
 | 
			
		||||
						let lastHash = "";
 | 
			
		||||
						copyDiff(path.join(testDirectory, run.name), tempDirectory);
 | 
			
		||||
 | 
			
		||||
						var compiler = webpack(options);
 | 
			
		||||
						var watching = compiler.watch({}, function(err, stats) {
 | 
			
		||||
						const compiler = webpack(options);
 | 
			
		||||
						const watching = compiler.watch({}, (err, stats) => {
 | 
			
		||||
							if(stats.hash === lastHash)
 | 
			
		||||
								return;
 | 
			
		||||
							lastHash = stats.hash;
 | 
			
		||||
| 
						 | 
				
			
			@ -119,18 +117,18 @@ describe("WatchTestCases", function() {
 | 
			
		|||
								return done(new Error("Compilation changed but no change was issued " + lastHash + " != " + stats.hash + " (run " + runIdx + ")"));
 | 
			
		||||
							run.done = true;
 | 
			
		||||
							if(err) return done(err);
 | 
			
		||||
							var statOptions = Stats.presetToOptions("verbose");
 | 
			
		||||
							const statOptions = Stats.presetToOptions("verbose");
 | 
			
		||||
							statOptions.colors = false;
 | 
			
		||||
							fs.writeFileSync(path.join(outputDirectory, "stats.txt"), stats.toString(statOptions), "utf-8");
 | 
			
		||||
							var jsonStats = stats.toJson({
 | 
			
		||||
							const jsonStats = stats.toJson({
 | 
			
		||||
								errorDetails: true
 | 
			
		||||
							});
 | 
			
		||||
							if(checkArrayExpectation(testDirectory, jsonStats, "error", "Error", done)) return;
 | 
			
		||||
							if(checkArrayExpectation(testDirectory, jsonStats, "warning", "Warning", done)) return;
 | 
			
		||||
							var exportedTests = 0;
 | 
			
		||||
							let exportedTests = 0;
 | 
			
		||||
 | 
			
		||||
							function _it(title, fn) {
 | 
			
		||||
								var test = new Test(title, fn);
 | 
			
		||||
								const test = new Test(title, fn);
 | 
			
		||||
								run.suite.addTest(test);
 | 
			
		||||
								exportedTests++;
 | 
			
		||||
								return test;
 | 
			
		||||
| 
						 | 
				
			
			@ -138,30 +136,31 @@ describe("WatchTestCases", function() {
 | 
			
		|||
 | 
			
		||||
							function _require(currentDirectory, module) {
 | 
			
		||||
								if(Array.isArray(module) || /^\.\.?\//.test(module)) {
 | 
			
		||||
									var fn;
 | 
			
		||||
									var content;
 | 
			
		||||
									let fn;
 | 
			
		||||
									let content;
 | 
			
		||||
									let p;
 | 
			
		||||
									if(Array.isArray(module)) {
 | 
			
		||||
										var p = path.join(currentDirectory, module[0]);
 | 
			
		||||
										content = module.map(function(p) {
 | 
			
		||||
											var p = path.join(currentDirectory, p);
 | 
			
		||||
										p = path.join(currentDirectory, module[0]);
 | 
			
		||||
										content = module.map((arg) => {
 | 
			
		||||
											p = path.join(currentDirectory, arg);
 | 
			
		||||
											return fs.readFileSync(p, "utf-8");
 | 
			
		||||
										}).join("\n");
 | 
			
		||||
									} else {
 | 
			
		||||
										var p = path.join(currentDirectory, module);
 | 
			
		||||
										p = path.join(currentDirectory, module);
 | 
			
		||||
										content = fs.readFileSync(p, "utf-8");
 | 
			
		||||
									}
 | 
			
		||||
									fn = vm.runInThisContext("(function(require, module, exports, __dirname, __filename, it, WATCH_STEP, STATS_JSON, STATE) {" + content + "\n})", p);
 | 
			
		||||
									var module = {
 | 
			
		||||
									const m = {
 | 
			
		||||
										exports: {}
 | 
			
		||||
									};
 | 
			
		||||
									fn.call(module.exports, _require.bind(null, path.dirname(p)), module, module.exports, path.dirname(p), p, _it, run.name, jsonStats, state);
 | 
			
		||||
									fn.call(m.exports, _require.bind(null, path.dirname(p)), m, m.exports, path.dirname(p), p, _it, run.name, jsonStats, state);
 | 
			
		||||
									return module.exports;
 | 
			
		||||
								} else if(testConfig.modules && module in testConfig.modules) {
 | 
			
		||||
									return testConfig.modules[module];
 | 
			
		||||
								} else return require(module);
 | 
			
		||||
							}
 | 
			
		||||
 | 
			
		||||
							var testConfig = {};
 | 
			
		||||
							let testConfig = {};
 | 
			
		||||
							try {
 | 
			
		||||
								// try to load a test file
 | 
			
		||||
								testConfig = require(path.join(testDirectory, "test.config.js"));
 | 
			
		||||
| 
						 | 
				
			
			@ -174,7 +173,7 @@ describe("WatchTestCases", function() {
 | 
			
		|||
							runIdx++;
 | 
			
		||||
							if(runIdx < runs.length) {
 | 
			
		||||
								run = runs[runIdx];
 | 
			
		||||
								setTimeout(function() {
 | 
			
		||||
								setTimeout(() => {
 | 
			
		||||
									copyDiff(path.join(testDirectory, run.name), tempDirectory);
 | 
			
		||||
								}, 50);
 | 
			
		||||
							} else {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue