]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/init/config-file.js
2 * @fileoverview Tests for ConfigFile
3 * @author Nicholas C. Zakas
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const assert
= require("chai").assert
,
12 sinon
= require("sinon"),
13 path
= require("path"),
14 yaml
= require("js-yaml"),
15 espree
= require("espree"),
16 ConfigFile
= require("../../../lib/init/config-file"),
17 { CLIEngine
} = require("../../../lib/cli-engine");
19 const proxyquire
= require("proxyquire").noCallThru().noPreserveCache();
21 //------------------------------------------------------------------------------
23 //------------------------------------------------------------------------------
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.
31 function getFixturePath(filepath
) {
32 return path
.resolve(__dirname
, "../../fixtures/config-file", filepath
);
35 //------------------------------------------------------------------------------
37 //------------------------------------------------------------------------------
39 describe("ConfigFile", () => {
40 describe("write()", () => {
57 sinon
.verifyAndRestore();
61 ["JavaScript", "foo.js", espree
.parse
],
62 ["JSON", "bar.json", JSON
.parse
],
63 ["YAML", "foo.yaml", yaml
.load
],
64 ["YML", "foo.yml", yaml
.load
]
65 ].forEach(([fileType
, filename
, validate
]) => {
67 it(`should write a file through fs when a ${fileType} path is passed`, () => {
69 writeFileSync
: () => {}
72 sinon
.mock(fakeFS
).expects("writeFileSync").withExactArgs(
74 sinon
.match(value
=> !!validate(value
)),
78 const StubbedConfigFile
= proxyquire("../../../lib/init/config-file", {
82 StubbedConfigFile
.write(config
, filename
);
85 it("should include a newline character at EOF", () => {
87 writeFileSync
: () => {}
90 sinon
.mock(fakeFS
).expects("writeFileSync").withExactArgs(
92 sinon
.match(value
=> value
.endsWith("\n")),
96 const StubbedConfigFile
= proxyquire("../../../lib/init/config-file", {
100 StubbedConfigFile
.write(config
, filename
);
104 it("should make sure js config files match linting rules", () => {
106 writeFileSync
: () => {}
109 const singleQuoteConfig
= {
111 quotes
: [2, "single"]
115 sinon
.mock(fakeFS
).expects("writeFileSync").withExactArgs(
117 sinon
.match(value
=> !value
.includes("\"")),
121 const StubbedConfigFile
= proxyquire("../../../lib/init/config-file", {
125 StubbedConfigFile
.write(singleQuoteConfig
, "test-config.js");
128 it("should still write a js config file even if linting fails", () => {
130 writeFileSync
: () => {}
132 const fakeCLIEngine
= sinon
.mock().withExactArgs(sinon
.match({
138 Object
.defineProperties(fakeCLIEngine
.prototype, Object
.getOwnPropertyDescriptors(CLIEngine
.prototype));
139 sinon
.stub(fakeCLIEngine
.prototype, "executeOnText").throws();
141 sinon
.mock(fakeFS
).expects("writeFileSync").once();
143 const StubbedConfigFile
= proxyquire("../../../lib/init/config-file", {
145 "../cli-engine": { CLIEngine
: fakeCLIEngine
}
148 assert
.throws(() => {
149 StubbedConfigFile
.write(config
, "test-config.js");
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);