]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/init/source-code-utils.js
bump version to 8.4.0-3
[pve-eslint.git] / eslint / tests / lib / init / source-code-utils.js
1 /**
2 * @fileoverview Tests for source-code-util.
3 * @author Ian VanSchooten
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const path = require("path"),
13 fs = require("fs"),
14 os = require("os"),
15 assert = require("chai").assert,
16 sinon = require("sinon"),
17 sh = require("shelljs"),
18 { SourceCode } = require("../../../lib/source-code");
19
20 const proxyquire = require("proxyquire").noCallThru().noPreserveCache();
21 const originalDir = process.cwd();
22
23 //------------------------------------------------------------------------------
24 // Tests
25 //------------------------------------------------------------------------------
26
27 describe("SourceCodeUtil", () => {
28
29 let fixtureDir,
30 getSourceCodeOfFiles;
31
32 /**
33 * Returns the path inside of the fixture directory.
34 * @param {...string} args file path segments.
35 * @returns {string} The path inside the fixture directory.
36 * @private
37 */
38 function getFixturePath(...args) {
39 let filepath = path.join(fixtureDir, ...args);
40
41 try {
42 filepath = fs.realpathSync(filepath);
43 return filepath;
44 } catch {
45 return filepath;
46 }
47 }
48
49 const log = {
50 info: sinon.spy(),
51 error: sinon.spy()
52 };
53 const requireStubs = {
54 "../logging": log
55 };
56
57 // copy into clean area so as not to get "infected" by this project's .eslintrc files
58 before(() => {
59 fixtureDir = `${os.tmpdir()}/eslint/fixtures/source-code-util`;
60 sh.mkdir("-p", fixtureDir);
61 sh.cp("-r", "./tests/fixtures/source-code-util/.", fixtureDir);
62 fixtureDir = fs.realpathSync(fixtureDir);
63 });
64
65 beforeEach(() => {
66 getSourceCodeOfFiles = proxyquire("../../../lib/init/source-code-utils", requireStubs).getSourceCodeOfFiles;
67 });
68
69 afterEach(() => {
70 log.info.resetHistory();
71 log.error.resetHistory();
72 });
73
74 after(() => {
75 sh.rm("-r", fixtureDir);
76 });
77
78 describe("getSourceCodeOfFiles()", () => {
79
80 it("should handle single string filename arguments", () => {
81 const filename = getFixturePath("foo.js");
82 const sourceCode = getSourceCodeOfFiles(filename, { cwd: fixtureDir });
83
84 assert.isObject(sourceCode);
85 });
86
87 it("should accept an array of string filenames", () => {
88 const fooFilename = getFixturePath("foo.js");
89 const barFilename = getFixturePath("bar.js");
90 const sourceCode = getSourceCodeOfFiles([fooFilename, barFilename], { cwd: fixtureDir });
91
92 assert.isObject(sourceCode);
93 });
94
95 it("should accept a glob argument", () => {
96 const glob = getFixturePath("*.js");
97 const filename = getFixturePath("foo.js");
98 const sourceCode = getSourceCodeOfFiles(glob, { cwd: fixtureDir });
99
100 assert.isObject(sourceCode);
101 assert.property(sourceCode, filename);
102 });
103
104 it("should accept a relative filename", () => {
105 const filename = "foo.js";
106 const sourceCode = getSourceCodeOfFiles(filename, { cwd: fixtureDir });
107
108 assert.isObject(sourceCode);
109 assert.property(sourceCode, getFixturePath(filename));
110 });
111
112 it("should accept a relative path to a file in a parent directory", () => {
113 const filename = "../foo.js";
114 const sourceCode = getSourceCodeOfFiles(filename, { cwd: getFixturePath("nested") });
115
116 assert.isObject(sourceCode);
117 assert.property(sourceCode, getFixturePath("foo.js"));
118 });
119
120 it("should accept a callback", () => {
121 const filename = getFixturePath("foo.js");
122 const spy = sinon.spy();
123
124 process.chdir(fixtureDir);
125 getSourceCodeOfFiles(filename, {}, spy);
126 process.chdir(originalDir);
127 assert(spy.calledOnce);
128 });
129
130 it("should call the callback with total number of files being processed", () => {
131 const filename = getFixturePath("foo.js");
132 const spy = sinon.spy();
133
134 process.chdir(fixtureDir);
135 getSourceCodeOfFiles(filename, {}, spy);
136 process.chdir(originalDir);
137 assert.strictEqual(spy.firstCall.args[0], 1);
138 });
139
140 it("should create an object with located filenames as keys", () => {
141 const fooFilename = getFixturePath("foo.js");
142 const barFilename = getFixturePath("bar.js");
143 const sourceCode = getSourceCodeOfFiles([fooFilename, barFilename], { cwd: fixtureDir });
144
145 assert.property(sourceCode, fooFilename);
146 assert.property(sourceCode, barFilename);
147 });
148
149 it("should should not include non-existent filenames in results", () => {
150 const filename = getFixturePath("missing.js");
151
152 assert.throws(() => {
153 getSourceCodeOfFiles(filename, { cwd: fixtureDir });
154 }, `No files matching '${filename}' were found.`);
155 });
156
157 it("should throw for files with parsing errors", () => {
158 const filename = getFixturePath("parse-error", "parse-error.js");
159
160 assert.throw(() => {
161 getSourceCodeOfFiles(filename, { cwd: fixtureDir });
162 }, /Parsing error: Unexpected token ;/u);
163
164 });
165
166 it("should obtain the sourceCode of a file", () => {
167 const filename = getFixturePath("foo.js");
168 const sourceCode = getSourceCodeOfFiles(filename, { cwd: fixtureDir });
169
170 assert.isObject(sourceCode);
171 assert.instanceOf(sourceCode[filename], SourceCode);
172 });
173
174 it("should obtain the sourceCode of JSX files", () => {
175 const filename = getFixturePath("jsx", "foo.jsx");
176 const options = {
177 cwd: fixtureDir,
178 parserOptions: {
179 ecmaFeatures: {
180 jsx: true
181 }
182 }
183 };
184 const sourceCode = getSourceCodeOfFiles(filename, options);
185
186 assert.isObject(sourceCode);
187 assert.instanceOf(sourceCode[filename], SourceCode);
188 });
189
190 it("should honor .eslintignore files by default", () => {
191 const glob = getFixturePath("*.js");
192 const unignoredFilename = getFixturePath("foo.js");
193 const ignoredFilename = getFixturePath("ignored.js");
194 const sourceCode = getSourceCodeOfFiles(glob, { cwd: fixtureDir });
195
196 assert.property(sourceCode, unignoredFilename);
197 assert.notProperty(sourceCode, ignoredFilename);
198 });
199
200 it("should obtain the sourceCode of all files in a specified folder", () => {
201 const folder = getFixturePath("nested");
202 const fooFile = getFixturePath("nested/foo.js");
203 const barFile = getFixturePath("nested/bar.js");
204 const sourceCode = getSourceCodeOfFiles(folder, { cwd: fixtureDir });
205
206 assert.strictEqual(Object.keys(sourceCode).length, 2);
207 assert.instanceOf(sourceCode[fooFile], SourceCode);
208 assert.instanceOf(sourceCode[barFile], SourceCode);
209 });
210
211 it("should accept cli options", () => {
212 const pattern = getFixturePath("ext");
213 const abcFile = getFixturePath("ext/foo.abc");
214 const cliOptions = { extensions: [".abc"], cwd: fixtureDir };
215 const sourceCode = getSourceCodeOfFiles(pattern, cliOptions);
216
217 assert.strictEqual(Object.keys(sourceCode).length, 1);
218 assert.instanceOf(sourceCode[abcFile], SourceCode);
219 });
220
221 it("should execute the callback function, if provided", () => {
222 const callback = sinon.spy();
223 const filename = getFixturePath("foo.js");
224
225 getSourceCodeOfFiles(filename, { cwd: fixtureDir }, callback);
226 assert(callback.calledOnce);
227 });
228
229 it("should execute callback function once per file", () => {
230 const callback = sinon.spy();
231 const fooFilename = getFixturePath("foo.js");
232 const barFilename = getFixturePath("bar.js");
233
234 getSourceCodeOfFiles([fooFilename, barFilename], { cwd: fixtureDir }, callback);
235 assert.strictEqual(callback.callCount, 2);
236 });
237
238 it("should call callback function with total number of files with sourceCode", () => {
239 const callback = sinon.spy();
240 const firstFn = getFixturePath("foo.js");
241 const secondFn = getFixturePath("bar.js");
242 const thirdFn = getFixturePath("nested/foo.js");
243
244 getSourceCodeOfFiles([firstFn, secondFn, thirdFn], { cwd: fixtureDir }, callback);
245 assert(callback.calledWith(3));
246 });
247
248 });
249
250 });