]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/init/npm-utils.js
bump version to 8.4.0-3
[pve-eslint.git] / eslint / tests / lib / init / npm-utils.js
1 /**
2 * @fileoverview Tests for rule fixer.
3 * @author Ian VanSchooten
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const
12 assert = require("chai").assert,
13 spawn = require("cross-spawn"),
14 sinon = require("sinon"),
15 npmUtils = require("../../../lib/init/npm-utils"),
16 log = require("../../../lib/shared/logging"),
17 { defineInMemoryFs } = require("../../_utils");
18
19 const proxyquire = require("proxyquire").noCallThru().noPreserveCache();
20
21 //------------------------------------------------------------------------------
22 // Helpers
23 //------------------------------------------------------------------------------
24
25 /**
26 * Import `npm-utils` with the in-memory file system.
27 * @param {Object} files The file definitions.
28 * @returns {Object} `npm-utils`.
29 */
30 function requireNpmUtilsWithInMemoryFileSystem(files) {
31 const fs = defineInMemoryFs({ files });
32
33 return proxyquire("../../../lib/init/npm-utils", { fs });
34 }
35
36 //------------------------------------------------------------------------------
37 // Tests
38 //------------------------------------------------------------------------------
39
40 describe("npmUtils", () => {
41 afterEach(() => {
42 sinon.verifyAndRestore();
43 });
44
45 describe("checkDevDeps()", () => {
46 let installStatus;
47
48 before(() => {
49 installStatus = npmUtils.checkDevDeps(["debug", "mocha", "notarealpackage", "jshint"]);
50 });
51
52 it("should not find a direct dependency of the project", () => {
53 assert.isFalse(installStatus.debug);
54 });
55
56 it("should find a dev dependency of the project", () => {
57 assert.isTrue(installStatus.mocha);
58 });
59
60 it("should not find non-dependencies", () => {
61 assert.isFalse(installStatus.notarealpackage);
62 });
63
64 it("should not find nested dependencies", () => {
65 assert.isFalse(installStatus.jshint);
66 });
67
68 it("should return false for a single, non-existent package", () => {
69 installStatus = npmUtils.checkDevDeps(["notarealpackage"]);
70 assert.isFalse(installStatus.notarealpackage);
71 });
72
73 it("should handle missing devDependencies key", () => {
74 const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
75 "package.json": JSON.stringify({ private: true, dependencies: {} })
76 });
77
78 // Should not throw.
79 stubbedNpmUtils.checkDevDeps(["some-package"]);
80 });
81
82 it("should throw with message when parsing invalid package.json", () => {
83 const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
84 "package.json": "{ \"not: \"valid json\" }"
85 });
86
87 assert.throws(() => {
88 try {
89 stubbedNpmUtils.checkDevDeps(["some-package"]);
90 } catch (error) {
91 assert.strictEqual(error.messageTemplate, "failed-to-read-json");
92 throw error;
93 }
94 }, "SyntaxError: Unexpected token v");
95 });
96 });
97
98 describe("checkDeps()", () => {
99 let installStatus;
100
101 before(() => {
102 installStatus = npmUtils.checkDeps(["debug", "mocha", "notarealpackage", "jshint"]);
103 });
104
105 it("should find a direct dependency of the project", () => {
106 assert.isTrue(installStatus.debug);
107 });
108
109 it("should not find a dev dependency of the project", () => {
110 assert.isFalse(installStatus.mocha);
111 });
112
113 it("should not find non-dependencies", () => {
114 assert.isFalse(installStatus.notarealpackage);
115 });
116
117 it("should not find nested dependencies", () => {
118 assert.isFalse(installStatus.jshint);
119 });
120
121 it("should return false for a single, non-existent package", () => {
122 installStatus = npmUtils.checkDeps(["notarealpackage"]);
123 assert.isFalse(installStatus.notarealpackage);
124 });
125
126 it("should throw if no package.json can be found", () => {
127 assert.throws(() => {
128 installStatus = npmUtils.checkDeps(["notarealpackage"], "/fakepath");
129 }, "Could not find a package.json file");
130 });
131
132 it("should handle missing dependencies key", () => {
133 const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
134 "package.json": JSON.stringify({ private: true, devDependencies: {} })
135 });
136
137 // Should not throw.
138 stubbedNpmUtils.checkDeps(["some-package"]);
139 });
140
141 it("should throw with message when parsing invalid package.json", () => {
142 const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
143 "package.json": "{ \"not: \"valid json\" }"
144 });
145
146 assert.throws(() => {
147 try {
148 stubbedNpmUtils.checkDeps(["some-package"]);
149 } catch (error) {
150 assert.strictEqual(error.messageTemplate, "failed-to-read-json");
151 throw error;
152 }
153 }, "SyntaxError: Unexpected token v");
154 });
155 });
156
157 describe("checkPackageJson()", () => {
158 it("should return true if package.json exists", () => {
159 const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({
160 "package.json": "{ \"file\": \"contents\" }"
161 });
162
163 assert.strictEqual(stubbedNpmUtils.checkPackageJson(), true);
164 });
165
166 it("should return false if package.json does not exist", () => {
167 const stubbedNpmUtils = requireNpmUtilsWithInMemoryFileSystem({});
168
169 assert.strictEqual(stubbedNpmUtils.checkPackageJson(), false);
170 });
171 });
172
173 describe("installSyncSaveDev()", () => {
174 it("should invoke npm to install a single desired package", () => {
175 const stub = sinon.stub(spawn, "sync").returns({ stdout: "" });
176
177 npmUtils.installSyncSaveDev("desired-package");
178 assert(stub.calledOnce);
179 assert.strictEqual(stub.firstCall.args[0], "npm");
180 assert.deepStrictEqual(stub.firstCall.args[1], ["i", "--save-dev", "desired-package"]);
181 stub.restore();
182 });
183
184 it("should accept an array of packages to install", () => {
185 const stub = sinon.stub(spawn, "sync").returns({ stdout: "" });
186
187 npmUtils.installSyncSaveDev(["first-package", "second-package"]);
188 assert(stub.calledOnce);
189 assert.strictEqual(stub.firstCall.args[0], "npm");
190 assert.deepStrictEqual(stub.firstCall.args[1], ["i", "--save-dev", "first-package", "second-package"]);
191 stub.restore();
192 });
193
194 it("should log an error message if npm throws ENOENT error", () => {
195 const logErrorStub = sinon.stub(log, "error");
196 const npmUtilsStub = sinon.stub(spawn, "sync").returns({ error: { code: "ENOENT" } });
197
198 npmUtils.installSyncSaveDev("some-package");
199
200 assert(logErrorStub.calledOnce);
201
202 logErrorStub.restore();
203 npmUtilsStub.restore();
204 });
205 });
206
207 describe("fetchPeerDependencies()", () => {
208 it("should execute 'npm show --json <packageName> peerDependencies' command", () => {
209 const stub = sinon.stub(spawn, "sync").returns({ stdout: "" });
210
211 npmUtils.fetchPeerDependencies("desired-package");
212 assert(stub.calledOnce);
213 assert.strictEqual(stub.firstCall.args[0], "npm");
214 assert.deepStrictEqual(stub.firstCall.args[1], ["show", "--json", "desired-package", "peerDependencies"]);
215 stub.restore();
216 });
217
218 it("should return null if npm throws ENOENT error", () => {
219 const stub = sinon.stub(spawn, "sync").returns({ error: { code: "ENOENT" } });
220
221 const peerDependencies = npmUtils.fetchPeerDependencies("desired-package");
222
223 assert.isNull(peerDependencies);
224
225 stub.restore();
226 });
227 });
228 });