]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/init/npm-utils.js
2 * @fileoverview Tests for rule fixer.
3 * @author Ian VanSchooten
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
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");
19 const proxyquire
= require("proxyquire").noCallThru().noPreserveCache();
21 //------------------------------------------------------------------------------
23 //------------------------------------------------------------------------------
26 * Import `npm-utils` with the in-memory file system.
27 * @param {Object} files The file definitions.
28 * @returns {Object} `npm-utils`.
30 function requireNpmUtilsWithInMemoryFileSystem(files
) {
31 const fs
= defineInMemoryFs({ files
});
33 return proxyquire("../../../lib/init/npm-utils", { fs
});
36 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
40 describe("npmUtils", () => {
42 sinon
.verifyAndRestore();
45 describe("checkDevDeps()", () => {
49 installStatus
= npmUtils
.checkDevDeps(["debug", "mocha", "notarealpackage", "jshint"]);
52 it("should not find a direct dependency of the project", () => {
53 assert
.isFalse(installStatus
.debug
);
56 it("should find a dev dependency of the project", () => {
57 assert
.isTrue(installStatus
.mocha
);
60 it("should not find non-dependencies", () => {
61 assert
.isFalse(installStatus
.notarealpackage
);
64 it("should not find nested dependencies", () => {
65 assert
.isFalse(installStatus
.jshint
);
68 it("should return false for a single, non-existent package", () => {
69 installStatus
= npmUtils
.checkDevDeps(["notarealpackage"]);
70 assert
.isFalse(installStatus
.notarealpackage
);
73 it("should handle missing devDependencies key", () => {
74 const stubbedNpmUtils
= requireNpmUtilsWithInMemoryFileSystem({
75 "package.json": JSON
.stringify({ private: true, dependencies
: {} })
79 stubbedNpmUtils
.checkDevDeps(["some-package"]);
82 it("should throw with message when parsing invalid package.json", () => {
83 const stubbedNpmUtils
= requireNpmUtilsWithInMemoryFileSystem({
84 "package.json": "{ \"not: \"valid json\" }"
89 stubbedNpmUtils
.checkDevDeps(["some-package"]);
91 assert
.strictEqual(error
.messageTemplate
, "failed-to-read-json");
94 }, "SyntaxError: Unexpected token v");
98 describe("checkDeps()", () => {
102 installStatus
= npmUtils
.checkDeps(["debug", "mocha", "notarealpackage", "jshint"]);
105 it("should find a direct dependency of the project", () => {
106 assert
.isTrue(installStatus
.debug
);
109 it("should not find a dev dependency of the project", () => {
110 assert
.isFalse(installStatus
.mocha
);
113 it("should not find non-dependencies", () => {
114 assert
.isFalse(installStatus
.notarealpackage
);
117 it("should not find nested dependencies", () => {
118 assert
.isFalse(installStatus
.jshint
);
121 it("should return false for a single, non-existent package", () => {
122 installStatus
= npmUtils
.checkDeps(["notarealpackage"]);
123 assert
.isFalse(installStatus
.notarealpackage
);
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");
132 it("should handle missing dependencies key", () => {
133 const stubbedNpmUtils
= requireNpmUtilsWithInMemoryFileSystem({
134 "package.json": JSON
.stringify({ private: true, devDependencies
: {} })
138 stubbedNpmUtils
.checkDeps(["some-package"]);
141 it("should throw with message when parsing invalid package.json", () => {
142 const stubbedNpmUtils
= requireNpmUtilsWithInMemoryFileSystem({
143 "package.json": "{ \"not: \"valid json\" }"
146 assert
.throws(() => {
148 stubbedNpmUtils
.checkDeps(["some-package"]);
150 assert
.strictEqual(error
.messageTemplate
, "failed-to-read-json");
153 }, "SyntaxError: Unexpected token v");
157 describe("checkPackageJson()", () => {
158 it("should return true if package.json exists", () => {
159 const stubbedNpmUtils
= requireNpmUtilsWithInMemoryFileSystem({
160 "package.json": "{ \"file\": \"contents\" }"
163 assert
.strictEqual(stubbedNpmUtils
.checkPackageJson(), true);
166 it("should return false if package.json does not exist", () => {
167 const stubbedNpmUtils
= requireNpmUtilsWithInMemoryFileSystem({});
169 assert
.strictEqual(stubbedNpmUtils
.checkPackageJson(), false);
173 describe("installSyncSaveDev()", () => {
174 it("should invoke npm to install a single desired package", () => {
175 const stub
= sinon
.stub(spawn
, "sync").returns({ stdout
: "" });
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"]);
184 it("should accept an array of packages to install", () => {
185 const stub
= sinon
.stub(spawn
, "sync").returns({ stdout
: "" });
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"]);
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" } });
198 npmUtils
.installSyncSaveDev("some-package");
200 assert(logErrorStub
.calledOnce
);
202 logErrorStub
.restore();
203 npmUtilsStub
.restore();
207 describe("fetchPeerDependencies()", () => {
208 it("should execute 'npm show --json <packageName> peerDependencies' command", () => {
209 const stub
= sinon
.stub(spawn
, "sync").returns({ stdout
: "" });
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"]);
218 it("should return null if npm throws ENOENT error", () => {
219 const stub
= sinon
.stub(spawn
, "sync").returns({ error
: { code
: "ENOENT" } });
221 const peerDependencies
= npmUtils
.fetchPeerDependencies("desired-package");
223 assert
.isNull(peerDependencies
);