/
config-file.js
159 lines (130 loc) · 5.02 KB
/
config-file.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
/**
* @fileoverview Tests for ConfigFile
* @author Nicholas C. Zakas
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert,
sinon = require("sinon"),
path = require("path"),
yaml = require("js-yaml"),
espree = require("espree"),
ConfigFile = require("../../../lib/init/config-file"),
{ CLIEngine } = require("../../../lib/cli-engine");
const proxyquire = require("proxyquire").noCallThru().noPreserveCache();
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Helper function get easily get a path in the fixtures directory.
* @param {string} filepath The path to find in the fixtures directory.
* @returns {string} Full path in the fixtures directory.
* @private
*/
function getFixturePath(filepath) {
return path.resolve(__dirname, "../../fixtures/config-file", filepath);
}
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("ConfigFile", () => {
describe("write()", () => {
let config;
beforeEach(() => {
config = {
env: {
browser: true,
node: true
},
rules: {
quotes: 2,
semi: 1
}
};
});
afterEach(() => {
sinon.verifyAndRestore();
});
[
["JavaScript", "foo.js", espree.parse],
["JSON", "bar.json", JSON.parse],
["YAML", "foo.yaml", yaml.safeLoad],
["YML", "foo.yml", yaml.safeLoad]
].forEach(([fileType, filename, validate]) => {
it(`should write a file through fs when a ${fileType} path is passed`, () => {
const fakeFS = {
writeFileSync: () => {}
};
sinon.mock(fakeFS).expects("writeFileSync").withExactArgs(
filename,
sinon.match(value => !!validate(value)),
"utf8"
);
const StubbedConfigFile = proxyquire("../../../lib/init/config-file", {
fs: fakeFS
});
StubbedConfigFile.write(config, filename);
});
it("should include a newline character at EOF", () => {
const fakeFS = {
writeFileSync: () => {}
};
sinon.mock(fakeFS).expects("writeFileSync").withExactArgs(
filename,
sinon.match(value => value.endsWith("\n")),
"utf8"
);
const StubbedConfigFile = proxyquire("../../../lib/init/config-file", {
fs: fakeFS
});
StubbedConfigFile.write(config, filename);
});
});
it("should make sure js config files match linting rules", () => {
const fakeFS = {
writeFileSync: () => {}
};
const singleQuoteConfig = {
rules: {
quotes: [2, "single"]
}
};
sinon.mock(fakeFS).expects("writeFileSync").withExactArgs(
"test-config.js",
sinon.match(value => !value.includes("\"")),
"utf8"
);
const StubbedConfigFile = proxyquire("../../../lib/init/config-file", {
fs: fakeFS
});
StubbedConfigFile.write(singleQuoteConfig, "test-config.js");
});
it("should still write a js config file even if linting fails", () => {
const fakeFS = {
writeFileSync: () => {}
};
const fakeCLIEngine = sinon.mock().withExactArgs(sinon.match({
baseConfig: config,
fix: true,
useEslintrc: false
}));
Object.defineProperties(fakeCLIEngine.prototype, Object.getOwnPropertyDescriptors(CLIEngine.prototype));
sinon.stub(fakeCLIEngine.prototype, "executeOnText").throws();
sinon.mock(fakeFS).expects("writeFileSync").once();
const StubbedConfigFile = proxyquire("../../../lib/init/config-file", {
fs: fakeFS,
"../cli-engine": { CLIEngine: fakeCLIEngine }
});
assert.throws(() => {
StubbedConfigFile.write(config, "test-config.js");
});
});
it("should throw error if file extension is not valid", () => {
assert.throws(() => {
ConfigFile.write({}, getFixturePath("yaml/.eslintrc.class"));
}, /write to unknown file type/u);
});
});
});