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