|
| 1 | +var fs = require("fs"); |
| 2 | +var vm = require("vm"); |
| 3 | +var path = require("path"); |
| 4 | +var webpack = require("webpack"); |
| 5 | +var should = require("should"); |
| 6 | +var ExtractTextPlugin = require("../"); |
| 7 | + |
| 8 | +var cases = fs.readdirSync(path.join(__dirname, "cases")); |
| 9 | + |
| 10 | +describe("TestCases", function() { |
| 11 | + cases.forEach(function(testCase) { |
| 12 | + it(testCase, function(done) { |
| 13 | + var testDirectory = path.join(__dirname, "cases", testCase); |
| 14 | + var outputDirectory = path.join(__dirname, "js", testCase); |
| 15 | + var options = { entry: { test: "./index.js" } }; |
| 16 | + var configFile = path.join(testDirectory, "webpack.config.js"); |
| 17 | + if(fs.existsSync(configFile)) |
| 18 | + options = require(configFile); |
| 19 | + options.context = testDirectory; |
| 20 | + if(!options.module) options.module = {}; |
| 21 | + if(!options.module.loaders) options.module.loaders = [ |
| 22 | + { test: /\.txt$/, loader: ExtractTextPlugin.extract("raw-loader") } |
| 23 | + ]; |
| 24 | + if(!options.output) options.output = { filename: "[name].js" }; |
| 25 | + if(!options.output.path) options.output.path = outputDirectory; |
| 26 | + webpack(options, function(err, stats) { |
| 27 | + if(err) return done(err); |
| 28 | + if(stats.hasErrors()) return done(new Error(stats.toString())); |
| 29 | + var testFile = path.join(outputDirectory, "test.js"); |
| 30 | + if(fs.existsSync(testFile)) |
| 31 | + require(testFile)(suite); |
| 32 | + var expectedDirectory = path.join(testDirectory, "expected"); |
| 33 | + fs.readdirSync(expectedDirectory).forEach(function(file) { |
| 34 | + var filePath = path.join(expectedDirectory, file); |
| 35 | + var actualPath = path.join(outputDirectory, file); |
| 36 | + readFileOrEmpty(actualPath).should.be.eql( |
| 37 | + readFileOrEmpty(filePath), |
| 38 | + file + " should be correct"); |
| 39 | + }); |
| 40 | + done(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +function readFileOrEmpty(path) { |
| 47 | + try { |
| 48 | + return fs.readFileSync(path, "utf-8"); |
| 49 | + } catch(e) { |
| 50 | + return ""; |
| 51 | + } |
| 52 | +} |
0 commit comments