]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/init/source-code-utils.js
2 * @fileoverview Tests for source-code-util.
3 * @author Ian VanSchooten
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const path
= require("path"),
15 assert
= require("chai").assert
,
16 sinon
= require("sinon"),
17 sh
= require("shelljs"),
18 { SourceCode
} = require("../../../lib/source-code");
20 const proxyquire
= require("proxyquire").noCallThru().noPreserveCache();
21 const originalDir
= process
.cwd();
23 //------------------------------------------------------------------------------
25 //------------------------------------------------------------------------------
27 describe("SourceCodeUtil", () => {
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.
38 function getFixturePath(...args
) {
39 let filepath
= path
.join(fixtureDir
, ...args
);
42 filepath
= fs
.realpathSync(filepath
);
53 const requireStubs
= {
57 // copy into clean area so as not to get "infected" by this project's .eslintrc files
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
);
66 getSourceCodeOfFiles
= proxyquire("../../../lib/init/source-code-utils", requireStubs
).getSourceCodeOfFiles
;
70 log
.info
.resetHistory();
71 log
.error
.resetHistory();
75 sh
.rm("-r", fixtureDir
);
78 describe("getSourceCodeOfFiles()", () => {
80 it("should handle single string filename arguments", () => {
81 const filename
= getFixturePath("foo.js");
82 const sourceCode
= getSourceCodeOfFiles(filename
, { cwd
: fixtureDir
});
84 assert
.isObject(sourceCode
);
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
});
92 assert
.isObject(sourceCode
);
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
});
100 assert
.isObject(sourceCode
);
101 assert
.property(sourceCode
, filename
);
104 it("should accept a relative filename", () => {
105 const filename
= "foo.js";
106 const sourceCode
= getSourceCodeOfFiles(filename
, { cwd
: fixtureDir
});
108 assert
.isObject(sourceCode
);
109 assert
.property(sourceCode
, getFixturePath(filename
));
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") });
116 assert
.isObject(sourceCode
);
117 assert
.property(sourceCode
, getFixturePath("foo.js"));
120 it("should accept a callback", () => {
121 const filename
= getFixturePath("foo.js");
122 const spy
= sinon
.spy();
124 process
.chdir(fixtureDir
);
125 getSourceCodeOfFiles(filename
, {}, spy
);
126 process
.chdir(originalDir
);
127 assert(spy
.calledOnce
);
130 it("should call the callback with total number of files being processed", () => {
131 const filename
= getFixturePath("foo.js");
132 const spy
= sinon
.spy();
134 process
.chdir(fixtureDir
);
135 getSourceCodeOfFiles(filename
, {}, spy
);
136 process
.chdir(originalDir
);
137 assert
.strictEqual(spy
.firstCall
.args
[0], 1);
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
});
145 assert
.property(sourceCode
, fooFilename
);
146 assert
.property(sourceCode
, barFilename
);
149 it("should should not include non-existent filenames in results", () => {
150 const filename
= getFixturePath("missing.js");
152 assert
.throws(() => {
153 getSourceCodeOfFiles(filename
, { cwd
: fixtureDir
});
154 }, `No files matching '${filename}' were found.`);
157 it("should throw for files with parsing errors", () => {
158 const filename
= getFixturePath("parse-error", "parse-error.js");
161 getSourceCodeOfFiles(filename
, { cwd
: fixtureDir
});
162 }, /Parsing error: Unexpected token ;/u);
166 it("should obtain the sourceCode of a file", () => {
167 const filename
= getFixturePath("foo.js");
168 const sourceCode
= getSourceCodeOfFiles(filename
, { cwd
: fixtureDir
});
170 assert
.isObject(sourceCode
);
171 assert
.instanceOf(sourceCode
[filename
], SourceCode
);
174 it("should obtain the sourceCode of JSX files", () => {
175 const filename
= getFixturePath("jsx", "foo.jsx");
184 const sourceCode
= getSourceCodeOfFiles(filename
, options
);
186 assert
.isObject(sourceCode
);
187 assert
.instanceOf(sourceCode
[filename
], SourceCode
);
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
});
196 assert
.property(sourceCode
, unignoredFilename
);
197 assert
.notProperty(sourceCode
, ignoredFilename
);
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
});
206 assert
.strictEqual(Object
.keys(sourceCode
).length
, 2);
207 assert
.instanceOf(sourceCode
[fooFile
], SourceCode
);
208 assert
.instanceOf(sourceCode
[barFile
], SourceCode
);
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
);
217 assert
.strictEqual(Object
.keys(sourceCode
).length
, 1);
218 assert
.instanceOf(sourceCode
[abcFile
], SourceCode
);
221 it("should execute the callback function, if provided", () => {
222 const callback
= sinon
.spy();
223 const filename
= getFixturePath("foo.js");
225 getSourceCodeOfFiles(filename
, { cwd
: fixtureDir
}, callback
);
226 assert(callback
.calledOnce
);
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");
234 getSourceCodeOfFiles([fooFilename
, barFilename
], { cwd
: fixtureDir
}, callback
);
235 assert
.strictEqual(callback
.callCount
, 2);
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");
244 getSourceCodeOfFiles([firstFn
, secondFn
, thirdFn
], { cwd
: fixtureDir
}, callback
);
245 assert(callback
.calledWith(3));