|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/* globals describe it */ |
| 4 | +const should = require("should"); |
| 5 | +const path = require("path"); |
| 6 | +const fs = require("fs"); |
| 7 | +const spawn = require("child_process").spawn; |
| 8 | + |
| 9 | +function loadOptsFile(optsPath) { |
| 10 | + // Options file parser from Mocha |
| 11 | + // https://github.com/mochajs/mocha/blob/2bb2b9fa35818db7a02e5068364b0c417436b1af/bin/options.js#L25-L31 |
| 12 | + return fs.readFileSync(optsPath, 'utf8') |
| 13 | + .replace(/\\\s/g, '%20') |
| 14 | + .split(/\s/) |
| 15 | + .filter(Boolean) |
| 16 | + .map(function(value) { |
| 17 | + return value.replace(/%20/g, ' '); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function getTestSpecificArguments(testDirectory) { |
| 22 | + try { |
| 23 | + return loadOptsFile(path.join(testDirectory, "test.opts")); |
| 24 | + } catch(e) { |
| 25 | + return []; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function convertToArrayOfLines(outputArray) { |
| 30 | + if(outputArray.length === 0) return outputArray; |
| 31 | + return outputArray.join('').split('\n'); |
| 32 | +} |
| 33 | + |
| 34 | +const casesPath = path.join(__dirname, "binCases"); |
| 35 | +const defaultArgs = loadOptsFile(path.join(casesPath, "test.opts")); |
| 36 | + |
| 37 | +describe("BinTestCases", function() { |
| 38 | + const categoryDirectories = fs.readdirSync(casesPath).filter((folder) => { |
| 39 | + return fs.statSync(path.join(casesPath, folder)).isDirectory() |
| 40 | + }); |
| 41 | + |
| 42 | + const categories = categoryDirectories.map(function(categoryDirectory) { |
| 43 | + return { |
| 44 | + name: categoryDirectory, |
| 45 | + tests: fs.readdirSync(path.join(casesPath, categoryDirectory)) |
| 46 | + }; |
| 47 | + }); |
| 48 | + |
| 49 | + categories.forEach(function(category) { |
| 50 | + describe(category.name, function() { |
| 51 | + |
| 52 | + category.tests.forEach(function(testName) { |
| 53 | + const testDirectory = path.join(casesPath, category.name, testName); |
| 54 | + const testArgs = defaultArgs.concat(getTestSpecificArguments(testDirectory)); |
| 55 | + const testAssertions = require(path.join(testDirectory, "test.js")); |
| 56 | + const outputPath = path.join(path.resolve(casesPath, "../js/bin"), category.name, testName); |
| 57 | + |
| 58 | + const cmd = `${path.resolve(process.cwd(), "bin/webpack.js")}`; |
| 59 | + const args = testArgs.concat(["--output-path", `${outputPath}`]); |
| 60 | + const opts = { |
| 61 | + cwd: path.resolve("./", testDirectory) |
| 62 | + }; |
| 63 | + |
| 64 | + const env = { |
| 65 | + stdout: [], |
| 66 | + stderr: [], |
| 67 | + error: [] |
| 68 | + }; |
| 69 | + |
| 70 | + describe(testName, function() { |
| 71 | + before(function(done) { |
| 72 | + this.timeout(20000); |
| 73 | + |
| 74 | + const child = spawn(process.execPath, [cmd].concat(args), opts); |
| 75 | + |
| 76 | + child.on("close", function(code) { |
| 77 | + env.code = code; |
| 78 | + done(); |
| 79 | + }); |
| 80 | + |
| 81 | + child.on("error", function(error) { |
| 82 | + env.error.push(error); |
| 83 | + }); |
| 84 | + |
| 85 | + child.stdout.on("data", (data) => { |
| 86 | + env.stdout.push(data); |
| 87 | + }); |
| 88 | + |
| 89 | + child.stderr.on("data", (data) => { |
| 90 | + env.stderr.push(data); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should not cause any errors", function() { |
| 95 | + should(env.error).be.empty(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should run successfully", function() { |
| 99 | + const stdout = convertToArrayOfLines(env.stdout); |
| 100 | + const stderr = convertToArrayOfLines(env.stderr); |
| 101 | + testAssertions(env.code, stdout, stderr); |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments