|
| 1 | +import * as fsPromises from "node:fs/promises"; |
| 2 | +import { analyzeModule } from "../../check-modules/module-analyzer.ts"; |
| 3 | +import { strict as assert } from "node:assert"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { test } from "node:test"; |
| 6 | +import { tmpdir } from "node:os"; |
| 7 | + |
| 8 | +async function createTempModule() { |
| 9 | + const root = await fsPromises.mkdtemp(join(tmpdir(), "module-analyzer-test-")); |
| 10 | + await fsPromises.mkdir(join(root, ".github"), { recursive: true }); |
| 11 | + |
| 12 | + await fsPromises.writeFile( |
| 13 | + join(root, "README.md"), |
| 14 | + [ |
| 15 | + "# MMM-Remote-Control", |
| 16 | + "", |
| 17 | + "## Installation", |
| 18 | + "git clone https://github.com/Jopyth/MMM-Remote-Control", |
| 19 | + "", |
| 20 | + "## Update", |
| 21 | + "Update steps", |
| 22 | + "", |
| 23 | + "## Config", |
| 24 | + "{", |
| 25 | + " module: \"MMM-Remote-Control\",", |
| 26 | + " config: {", |
| 27 | + " foo: true", |
| 28 | + " },", |
| 29 | + "},", |
| 30 | + "" |
| 31 | + ].join("\n") |
| 32 | + ); |
| 33 | + |
| 34 | + await fsPromises.writeFile( |
| 35 | + join(root, "CHANGELOG.md"), |
| 36 | + "XMLHttpRequest\nnpm run\ngit checkout\n" |
| 37 | + ); |
| 38 | + await fsPromises.writeFile(join(root, "CODE_OF_CONDUCT.md"), "code of conduct"); |
| 39 | + await fsPromises.writeFile(join(root, "LICENSE.md"), "license"); |
| 40 | + await fsPromises.writeFile(join(root, "eslint.config.mjs"), "export default [];\n"); |
| 41 | + await fsPromises.writeFile( |
| 42 | + join(root, "package.json"), |
| 43 | + JSON.stringify( |
| 44 | + { |
| 45 | + devDependencies: { eslint: "^10.0.0" }, |
| 46 | + scripts: { lint: "eslint" } |
| 47 | + }, |
| 48 | + null, |
| 49 | + 2 |
| 50 | + ) |
| 51 | + ); |
| 52 | + await fsPromises.writeFile( |
| 53 | + join(root, "package-lock.json"), |
| 54 | + `${JSON.stringify( |
| 55 | + { |
| 56 | + name: "x", |
| 57 | + note: "jshint", |
| 58 | + lockfileVersion: 2, |
| 59 | + scripts: { test: "npm run lint" } |
| 60 | + }, |
| 61 | + null, |
| 62 | + 2 |
| 63 | + )}\n` |
| 64 | + ); |
| 65 | + await fsPromises.writeFile(join(root, ".github", "dependabot.yaml"), "version: 2\nupdates: []\n"); |
| 66 | + |
| 67 | + return root; |
| 68 | +} |
| 69 | + |
| 70 | +test("analyzer keeps .github files and applies only lockfile-specific package-lock rules", async () => { |
| 71 | + const moduleRoot = await createTempModule(); |
| 72 | + const files = [ |
| 73 | + join(moduleRoot, "README.md"), |
| 74 | + join(moduleRoot, "CHANGELOG.md"), |
| 75 | + join(moduleRoot, "CODE_OF_CONDUCT.md"), |
| 76 | + join(moduleRoot, "LICENSE.md"), |
| 77 | + join(moduleRoot, "eslint.config.mjs"), |
| 78 | + join(moduleRoot, "package.json"), |
| 79 | + join(moduleRoot, "package-lock.json"), |
| 80 | + join(moduleRoot, ".github", "dependabot.yaml") |
| 81 | + ]; |
| 82 | + |
| 83 | + const result = await analyzeModule( |
| 84 | + moduleRoot, |
| 85 | + "MMM-Remote-Control", |
| 86 | + "https://github.com/Jopyth/MMM-Remote-Control", |
| 87 | + files |
| 88 | + ); |
| 89 | + |
| 90 | + assert.equal( |
| 91 | + result.issues.some(issue => issue.includes("There is no dependabot configuration file")), |
| 92 | + false |
| 93 | + ); |
| 94 | + assert.equal( |
| 95 | + result.issues.some(issue => issue.includes("in file `CHANGELOG.md`")), |
| 96 | + false |
| 97 | + ); |
| 98 | + assert.equal( |
| 99 | + result.issues.some( |
| 100 | + issue => issue.includes("Found `jshint`") && issue.includes("in file `package-lock.json`") |
| 101 | + ), |
| 102 | + false |
| 103 | + ); |
| 104 | + assert.equal( |
| 105 | + result.issues.some( |
| 106 | + issue => issue.includes("Found `\"lockfileVersion\": 2`") && issue.includes("in file `package-lock.json`") |
| 107 | + ), |
| 108 | + true |
| 109 | + ); |
| 110 | + |
| 111 | + assert.ok(Array.isArray(result.issues)); |
| 112 | +}); |
0 commit comments