|
| 1 | +/** |
| 2 | + * @fileoverview A utility to test ecosystem plugin(s) against the local ESLint. |
| 3 | + * @author Josh Goldberg |
| 4 | + */ |
| 5 | + |
| 6 | +//----------------------------------------------------------------------------- |
| 7 | +// Requirements |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +import debug from "debug"; |
| 11 | +import spawn from "cross-spawn"; |
| 12 | +import fs from "node:fs/promises"; |
| 13 | +import path from "node:path"; |
| 14 | +import { styleText } from "node:util"; |
| 15 | + |
| 16 | +import { getPlugins } from "./data.mjs"; |
| 17 | + |
| 18 | +const log = debug("test:ecosystem"); |
| 19 | + |
| 20 | +/** |
| 21 | + * @typedef {import("./data").PluginSettings} PluginSettings |
| 22 | + */ |
| 23 | + |
| 24 | +//----------------------------------------------------------------------------- |
| 25 | +// Helpers |
| 26 | +//----------------------------------------------------------------------------- |
| 27 | + |
| 28 | +/** |
| 29 | + * Runs ecosystem tests for a single plugin. It will: |
| 30 | + * 1. Clone the plugin repository into a sandbox directory |
| 31 | + * 2. Check out the plugin's commit to test on |
| 32 | + * 3. Install the plugin's dependencies |
| 33 | + * 4. Link the local ESLint into the plugin |
| 34 | + * 5. Build, if the plugin defines a build script |
| 35 | + * 6. Run tests |
| 36 | + * This intentionally does not try/catch: any errors will be thrown. |
| 37 | + * |
| 38 | + * @param {string} pluginKey |
| 39 | + * @param {PluginSettings} pluginSettings |
| 40 | + */ |
| 41 | +async function runTests(pluginKey, pluginSettings) { |
| 42 | + const directory = path.join( |
| 43 | + SANDBOX_DIRECTORY, |
| 44 | + pluginKey |
| 45 | + .replaceAll(/[^a-z-]/g, " ") |
| 46 | + .trim() |
| 47 | + .replaceAll(" ", "-"), |
| 48 | + ); |
| 49 | + console.log(styleText("bold", `Testing ${pluginKey} in ${directory}`)); |
| 50 | + |
| 51 | + /** |
| 52 | + * Attempts to run a command in the plugin sandbox directory. |
| 53 | + * If it fails, any error stdout will be logged in red before a (re-)thrown error. |
| 54 | + * @param {string} command |
| 55 | + * @param {string[]} args |
| 56 | + */ |
| 57 | + const runCommand = ([command, ...args]) => { |
| 58 | + console.log( |
| 59 | + styleText("gray", `[${pluginKey}] ${[command, ...args].join(" ")}`), |
| 60 | + ); |
| 61 | + |
| 62 | + const result = spawn.sync(command, args, { |
| 63 | + cwd: directory, |
| 64 | + stdio: log.enabled ? "inherit" : undefined, |
| 65 | + }); |
| 66 | + |
| 67 | + if (result.status || result.error) { |
| 68 | + throw result.error ?? new Error(result.stderr.toString()); |
| 69 | + } |
| 70 | + |
| 71 | + return result; |
| 72 | + }; |
| 73 | + |
| 74 | + // 1. Clone the plugin repository into a sandbox directory |
| 75 | + await fs.mkdir(directory, { recursive: true }); |
| 76 | + runCommand([ |
| 77 | + "git", |
| 78 | + "clone", |
| 79 | + pluginSettings.repository, |
| 80 | + directory, |
| 81 | + "--depth", |
| 82 | + "1", |
| 83 | + ]); |
| 84 | + |
| 85 | + // 2. Check out the plugin's commit to test on |
| 86 | + runCommand(["git", "fetch", "origin", pluginSettings.commit]); |
| 87 | + runCommand(["git", "checkout", pluginSettings.commit]); |
| 88 | + |
| 89 | + // 3. Install the plugin's dependencies |
| 90 | + runCommand(["pwd"]); |
| 91 | + runCommand(pluginSettings.commands.install); |
| 92 | + |
| 93 | + // 4. Link the local ESLint into the plugin |
| 94 | + runCommand(["npm", "link", "eslint"]); |
| 95 | + |
| 96 | + // 5. Build, if the plugin defines a build script |
| 97 | + if (pluginSettings.commands.build) { |
| 98 | + runCommand(pluginSettings.commands.build); |
| 99 | + } |
| 100 | + |
| 101 | + // 6. Run the plugin's tests |
| 102 | + runCommand(pluginSettings.commands.test); |
| 103 | +} |
| 104 | + |
| 105 | +//----------------------------------------------------------------------------- |
| 106 | +// Main |
| 107 | +//----------------------------------------------------------------------------- |
| 108 | + |
| 109 | +const { pluginsSelected } = await getPlugins("test"); |
| 110 | + |
| 111 | +const SANDBOX_DIRECTORY = path.join(process.cwd(), "ecosystem"); |
| 112 | + |
| 113 | +console.log(`Clearing existing sandbox directory: ${SANDBOX_DIRECTORY}`); |
| 114 | +await fs.rm(SANDBOX_DIRECTORY, { |
| 115 | + force: true, |
| 116 | + maxRetries: 8, |
| 117 | + recursive: true, |
| 118 | +}); |
| 119 | +await fs.mkdir(SANDBOX_DIRECTORY, { recursive: true }); |
| 120 | +console.log(""); |
| 121 | + |
| 122 | +const errors = []; |
| 123 | + |
| 124 | +// For each plugin to test, we try to runTests, recording thrown exceptions in errors |
| 125 | +for (const [pluginKey, pluginSettings] of pluginsSelected) { |
| 126 | + try { |
| 127 | + await runTests(pluginKey, pluginSettings); |
| 128 | + console.log(`Passed: ${pluginKey}\n`); |
| 129 | + } catch (error) { |
| 130 | + errors.push({ error, pluginKey }); |
| 131 | + console.log(styleText("red", `Failed: ${pluginKey}\n`)); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// If we had any errors, report them and exit as failed |
| 136 | +if (errors.length) { |
| 137 | + console.error(styleText("red", "Errors occurred while testing plugins:\n")); |
| 138 | + for (const { error, pluginKey } of errors) { |
| 139 | + console.error( |
| 140 | + `${styleText(["bold", "red"], pluginKey)}: ${styleText("red", `${error.stack || error}`)}\n`, |
| 141 | + ); |
| 142 | + } |
| 143 | + process.exitCode = 1; |
| 144 | +} else { |
| 145 | + console.log(styleText("green", "All tests completed successfully.")); |
| 146 | +} |
0 commit comments