|
| 1 | +const { spawnSync } = require("child_process"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | +const mainVersion = Number(process.version.replace("v","").split(".")[0]) |
| 5 | + |
| 6 | +// Loop through all the folders and run `npm test` |
| 7 | + |
| 8 | +const blocklist = ["validateModuleExportsMatchCommonJS", "node_modules"]; |
| 9 | +const filesInTest = fs.readdirSync(__dirname); |
| 10 | +const tests = filesInTest |
| 11 | + .filter((f) => fs.statSync(path.join(__dirname, f)).isDirectory()) |
| 12 | + .filter((f) => !blocklist.includes(f)); |
| 13 | + |
| 14 | +// Support setting up the test node modules |
| 15 | +if (!filesInTest.includes("node_modules")) { |
| 16 | + console.log("Installing Deps..."); |
| 17 | + spawnSync("npm", ["install"], { cwd: __dirname }); |
| 18 | + console.log("Installed"); |
| 19 | +} |
| 20 | + |
| 21 | +const chalk = require("chalk").default; |
| 22 | +for (const test of tests) { |
| 23 | + console.log("---> " + chalk.bold(test)); |
| 24 | + |
| 25 | + const pgkJSON = require(path.join(__dirname, test, "package.json")); |
| 26 | + |
| 27 | + // Allow skipping things which need a minimum of node 14 (es modules) |
| 28 | + if (pgkJSON.engines && pgkJSON.engines.node) { |
| 29 | + const minVersion = Number(pgkJSON.engines.node) |
| 30 | + if (minVersion > mainVersion) { |
| 31 | + console.log("Skipping") |
| 32 | + continue |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // The webpack 5 tests have unique deps |
| 37 | + if (pgkJSON.dependencies || pgkJSON.devDependencies) { |
| 38 | + const nodeModsInstalled = fs.existsSync(path.join(__dirname, test, "node_modules")); |
| 39 | + if (!nodeModsInstalled) { |
| 40 | + spawnSync("npm", ["install"], { cwd: path.join(__dirname, test) }); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Run the test command |
| 45 | + const results = spawnSync("npm", ["test"], { cwd: path.join(__dirname, test) }); |
| 46 | + console.log(results.stdout.toString()) |
| 47 | + if (results.status) { |
| 48 | + console.log(chalk.bold.red("Error running test: ") + chalk.bold(test)) |
| 49 | + console.log(results.stderr.toString()) |
| 50 | + console.log(chalk.bold.red("^^^ Error running test: ") + chalk.bold(test)) |
| 51 | + process.exitCode = results.status |
| 52 | + } |
| 53 | +} |
| 54 | + |
0 commit comments