|
| 1 | +/** |
| 2 | + * @fileoverview Tests for FileContext class. |
| 3 | + * @author Nicholas C. Zakas |
| 4 | + */ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const assert = require("chai").assert; |
| 13 | +const { FileContext } = require("../../../lib/linter/file-context"); |
| 14 | + |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | +// Tests |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +describe("FileContext", () => { |
| 20 | + const mockSourceCode = {}; |
| 21 | + const defaultConfig = { |
| 22 | + cwd: "/path/to/project", |
| 23 | + filename: "test.js", |
| 24 | + physicalFilename: "/path/to/project/test.js", |
| 25 | + sourceCode: mockSourceCode, |
| 26 | + parserOptions: { ecmaVersion: 2021 }, |
| 27 | + parserPath: "/path/to/parser", |
| 28 | + languageOptions: { ecmaVersion: 2022 }, |
| 29 | + settings: { env: { es6: true } }, |
| 30 | + }; |
| 31 | + |
| 32 | + describe("constructor", () => { |
| 33 | + it("should create a frozen instance with all properties set", () => { |
| 34 | + const context = new FileContext(defaultConfig); |
| 35 | + |
| 36 | + assert.strictEqual(context.cwd, defaultConfig.cwd); |
| 37 | + assert.strictEqual(context.filename, defaultConfig.filename); |
| 38 | + assert.strictEqual( |
| 39 | + context.physicalFilename, |
| 40 | + defaultConfig.physicalFilename, |
| 41 | + ); |
| 42 | + assert.strictEqual(context.sourceCode, defaultConfig.sourceCode); |
| 43 | + assert.deepStrictEqual( |
| 44 | + context.parserOptions, |
| 45 | + defaultConfig.parserOptions, |
| 46 | + ); |
| 47 | + assert.strictEqual(context.parserPath, defaultConfig.parserPath); |
| 48 | + assert.deepStrictEqual( |
| 49 | + context.languageOptions, |
| 50 | + defaultConfig.languageOptions, |
| 51 | + ); |
| 52 | + assert.deepStrictEqual(context.settings, defaultConfig.settings); |
| 53 | + |
| 54 | + // Verify the instance is frozen |
| 55 | + assert.throws(() => { |
| 56 | + context.cwd = "changed"; |
| 57 | + }, TypeError); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should allow partial configuration", () => { |
| 61 | + const partialConfig = { |
| 62 | + cwd: "/path/to/project", |
| 63 | + filename: "test.js", |
| 64 | + physicalFilename: "/path/to/project/test.js", |
| 65 | + sourceCode: mockSourceCode, |
| 66 | + }; |
| 67 | + |
| 68 | + const context = new FileContext(partialConfig); |
| 69 | + |
| 70 | + assert.strictEqual(context.cwd, partialConfig.cwd); |
| 71 | + assert.strictEqual(context.filename, partialConfig.filename); |
| 72 | + assert.strictEqual( |
| 73 | + context.physicalFilename, |
| 74 | + partialConfig.physicalFilename, |
| 75 | + ); |
| 76 | + assert.strictEqual(context.sourceCode, partialConfig.sourceCode); |
| 77 | + assert.isUndefined(context.parserOptions); |
| 78 | + assert.isUndefined(context.parserPath); |
| 79 | + assert.isUndefined(context.languageOptions); |
| 80 | + assert.isUndefined(context.settings); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("deprecated methods", () => { |
| 85 | + let context; |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + context = new FileContext(defaultConfig); |
| 89 | + }); |
| 90 | + |
| 91 | + it("getCwd() should return the cwd property", () => { |
| 92 | + assert.strictEqual(context.getCwd(), context.cwd); |
| 93 | + assert.strictEqual(context.getCwd(), defaultConfig.cwd); |
| 94 | + }); |
| 95 | + |
| 96 | + it("getFilename() should return the filename property", () => { |
| 97 | + assert.strictEqual(context.getFilename(), context.filename); |
| 98 | + assert.strictEqual(context.getFilename(), defaultConfig.filename); |
| 99 | + }); |
| 100 | + |
| 101 | + it("getPhysicalFilename() should return the physicalFilename property", () => { |
| 102 | + assert.strictEqual( |
| 103 | + context.getPhysicalFilename(), |
| 104 | + context.physicalFilename, |
| 105 | + ); |
| 106 | + assert.strictEqual( |
| 107 | + context.getPhysicalFilename(), |
| 108 | + defaultConfig.physicalFilename, |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it("getSourceCode() should return the sourceCode property", () => { |
| 113 | + assert.strictEqual(context.getSourceCode(), context.sourceCode); |
| 114 | + assert.strictEqual( |
| 115 | + context.getSourceCode(), |
| 116 | + defaultConfig.sourceCode, |
| 117 | + ); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe("extend()", () => { |
| 122 | + let context; |
| 123 | + |
| 124 | + beforeEach(() => { |
| 125 | + context = new FileContext(defaultConfig); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should create a new object with the original as prototype", () => { |
| 129 | + const extension = { extraProperty: "extra" }; |
| 130 | + const extended = context.extend(extension); |
| 131 | + |
| 132 | + // Verify new properties |
| 133 | + assert.strictEqual(extended.extraProperty, "extra"); |
| 134 | + |
| 135 | + // Verify inherited properties |
| 136 | + assert.strictEqual(extended.cwd, context.cwd); |
| 137 | + assert.strictEqual(extended.filename, context.filename); |
| 138 | + assert.strictEqual( |
| 139 | + extended.physicalFilename, |
| 140 | + context.physicalFilename, |
| 141 | + ); |
| 142 | + assert.strictEqual(extended.sourceCode, context.sourceCode); |
| 143 | + assert.deepStrictEqual( |
| 144 | + extended.parserOptions, |
| 145 | + context.parserOptions, |
| 146 | + ); |
| 147 | + assert.strictEqual(extended.parserPath, context.parserPath); |
| 148 | + assert.deepStrictEqual( |
| 149 | + extended.languageOptions, |
| 150 | + context.languageOptions, |
| 151 | + ); |
| 152 | + assert.deepStrictEqual(extended.settings, context.settings); |
| 153 | + }); |
| 154 | + |
| 155 | + it("should freeze the extended object", () => { |
| 156 | + const extension = { extraProperty: "extra" }; |
| 157 | + const extended = context.extend(extension); |
| 158 | + |
| 159 | + // Verify the extended object is frozen |
| 160 | + assert.throws(() => { |
| 161 | + extended.cwd = "changed"; |
| 162 | + }, TypeError); |
| 163 | + |
| 164 | + assert.throws(() => { |
| 165 | + extended.extraProperty = "changed"; |
| 166 | + }, TypeError); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should throw an error when attempting to override existing properties", () => { |
| 170 | + const extension = { cwd: "newCwd" }; |
| 171 | + |
| 172 | + assert.throws(() => { |
| 173 | + context.extend(extension); |
| 174 | + }, TypeError); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments