]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for ConfigFile | |
3 | * @author Nicholas C. Zakas | |
4 | */ | |
5 | "use strict"; | |
6 | ||
7 | //------------------------------------------------------------------------------ | |
8 | // Requirements | |
9 | //------------------------------------------------------------------------------ | |
10 | ||
11 | const assert = require("chai").assert, | |
eb39fafa DC |
12 | sinon = require("sinon"), |
13 | path = require("path"), | |
eb39fafa DC |
14 | yaml = require("js-yaml"), |
15 | espree = require("espree"), | |
16 | ConfigFile = require("../../../lib/init/config-file"), | |
17 | { CLIEngine } = require("../../../lib/cli-engine"); | |
18 | ||
19 | const proxyquire = require("proxyquire").noCallThru().noPreserveCache(); | |
20 | ||
21 | //------------------------------------------------------------------------------ | |
22 | // Helpers | |
23 | //------------------------------------------------------------------------------ | |
24 | ||
25 | /** | |
26 | * Helper function get easily get a path in the fixtures directory. | |
27 | * @param {string} filepath The path to find in the fixtures directory. | |
28 | * @returns {string} Full path in the fixtures directory. | |
29 | * @private | |
30 | */ | |
31 | function getFixturePath(filepath) { | |
32 | return path.resolve(__dirname, "../../fixtures/config-file", filepath); | |
33 | } | |
34 | ||
35 | //------------------------------------------------------------------------------ | |
36 | // Tests | |
37 | //------------------------------------------------------------------------------ | |
38 | ||
39 | describe("ConfigFile", () => { | |
40 | describe("write()", () => { | |
41 | let config; | |
42 | ||
43 | beforeEach(() => { | |
44 | config = { | |
45 | env: { | |
46 | browser: true, | |
47 | node: true | |
48 | }, | |
49 | rules: { | |
50 | quotes: 2, | |
51 | semi: 1 | |
52 | } | |
53 | }; | |
54 | }); | |
55 | ||
56 | afterEach(() => { | |
57 | sinon.verifyAndRestore(); | |
58 | }); | |
59 | ||
6f036462 | 60 | [ |
eb39fafa DC |
61 | ["JavaScript", "foo.js", espree.parse], |
62 | ["JSON", "bar.json", JSON.parse], | |
63 | ["YAML", "foo.yaml", yaml.safeLoad], | |
64 | ["YML", "foo.yml", yaml.safeLoad] | |
6f036462 | 65 | ].forEach(([fileType, filename, validate]) => { |
eb39fafa DC |
66 | |
67 | it(`should write a file through fs when a ${fileType} path is passed`, () => { | |
6f036462 TL |
68 | const fakeFS = { |
69 | writeFileSync: () => {} | |
70 | }; | |
eb39fafa DC |
71 | |
72 | sinon.mock(fakeFS).expects("writeFileSync").withExactArgs( | |
73 | filename, | |
74 | sinon.match(value => !!validate(value)), | |
75 | "utf8" | |
76 | ); | |
77 | ||
78 | const StubbedConfigFile = proxyquire("../../../lib/init/config-file", { | |
79 | fs: fakeFS | |
80 | }); | |
81 | ||
82 | StubbedConfigFile.write(config, filename); | |
83 | }); | |
84 | ||
85 | it("should include a newline character at EOF", () => { | |
6f036462 TL |
86 | const fakeFS = { |
87 | writeFileSync: () => {} | |
88 | }; | |
eb39fafa DC |
89 | |
90 | sinon.mock(fakeFS).expects("writeFileSync").withExactArgs( | |
91 | filename, | |
92 | sinon.match(value => value.endsWith("\n")), | |
93 | "utf8" | |
94 | ); | |
95 | ||
96 | const StubbedConfigFile = proxyquire("../../../lib/init/config-file", { | |
97 | fs: fakeFS | |
98 | }); | |
99 | ||
100 | StubbedConfigFile.write(config, filename); | |
101 | }); | |
102 | }); | |
103 | ||
104 | it("should make sure js config files match linting rules", () => { | |
6f036462 TL |
105 | const fakeFS = { |
106 | writeFileSync: () => {} | |
107 | }; | |
eb39fafa DC |
108 | |
109 | const singleQuoteConfig = { | |
110 | rules: { | |
111 | quotes: [2, "single"] | |
112 | } | |
113 | }; | |
114 | ||
115 | sinon.mock(fakeFS).expects("writeFileSync").withExactArgs( | |
116 | "test-config.js", | |
117 | sinon.match(value => !value.includes("\"")), | |
118 | "utf8" | |
119 | ); | |
120 | ||
121 | const StubbedConfigFile = proxyquire("../../../lib/init/config-file", { | |
122 | fs: fakeFS | |
123 | }); | |
124 | ||
125 | StubbedConfigFile.write(singleQuoteConfig, "test-config.js"); | |
126 | }); | |
127 | ||
128 | it("should still write a js config file even if linting fails", () => { | |
6f036462 TL |
129 | const fakeFS = { |
130 | writeFileSync: () => {} | |
131 | }; | |
eb39fafa DC |
132 | const fakeCLIEngine = sinon.mock().withExactArgs(sinon.match({ |
133 | baseConfig: config, | |
134 | fix: true, | |
135 | useEslintrc: false | |
136 | })); | |
137 | ||
138 | Object.defineProperties(fakeCLIEngine.prototype, Object.getOwnPropertyDescriptors(CLIEngine.prototype)); | |
139 | sinon.stub(fakeCLIEngine.prototype, "executeOnText").throws(); | |
140 | ||
141 | sinon.mock(fakeFS).expects("writeFileSync").once(); | |
142 | ||
143 | const StubbedConfigFile = proxyquire("../../../lib/init/config-file", { | |
144 | fs: fakeFS, | |
145 | "../cli-engine": { CLIEngine: fakeCLIEngine } | |
146 | }); | |
147 | ||
148 | assert.throws(() => { | |
149 | StubbedConfigFile.write(config, "test-config.js"); | |
150 | }); | |
151 | }); | |
152 | ||
153 | it("should throw error if file extension is not valid", () => { | |
154 | assert.throws(() => { | |
155 | ConfigFile.write({}, getFixturePath("yaml/.eslintrc.class")); | |
156 | }, /write to unknown file type/u); | |
157 | }); | |
158 | }); | |
159 | }); |