|
3 | 3 | */ |
4 | 4 | import path from 'path'; |
5 | 5 |
|
6 | | -import { expect } from 'chai'; |
7 | | -import { CLIEngine } from 'eslint'; |
| 6 | +import { expect, use } from 'chai'; |
| 7 | +import chaiAsPromised from 'chai-as-promised'; |
| 8 | +import { CLIEngine, ESLint as ESLintClass } from 'eslint'; |
8 | 9 | import eslintPkg from 'eslint/package.json'; |
9 | 10 | import semver from 'semver'; |
| 11 | +import * as importPlugin from '../../src/index'; |
| 12 | + |
| 13 | +use(chaiAsPromised); |
| 14 | + |
| 15 | +const ESLint = ESLintClass || class { |
| 16 | + constructor(options) { |
| 17 | + options = Object.assign({},options); |
| 18 | + const overrideConfig = options.overrideConfig; |
| 19 | + delete options.overrideConfig; |
| 20 | + const overrideConfigFile = options.overrideConfigFile; |
| 21 | + delete options.overrideConfigFile; |
| 22 | + const pluginsMap = options.plugins; |
| 23 | + delete options.plugins; |
| 24 | + this.engine = new CLIEngine(Object.assign(options, overrideConfig, overrideConfigFile ? { configFile:overrideConfigFile }: {})); |
| 25 | + |
| 26 | + for (const [name, plugin] of Object.entries(pluginsMap || {})) { |
| 27 | + this.engine.addPlugin(name, plugin); |
| 28 | + } |
| 29 | + } |
| 30 | + lintFiles(params) { |
| 31 | + const result = this.engine.executeOnFiles(params); |
| 32 | + return Promise.resolve(result.results); |
| 33 | + } |
| 34 | +}; |
10 | 35 |
|
11 | 36 | describe('CLI regression tests', function () { |
12 | 37 | describe('issue #210', function () { |
13 | | - let cli; |
| 38 | + let eslint; |
14 | 39 | before(function () { |
15 | | - cli = new CLIEngine({ |
| 40 | + eslint = new ESLint({ |
16 | 41 | useEslintrc: false, |
17 | | - configFile: './tests/files/issue210.config.js', |
| 42 | + overrideConfigFile: './tests/files/issue210.config.js', |
18 | 43 | rulePaths: ['./src/rules'], |
19 | | - rules: { |
20 | | - 'named': 2, |
| 44 | + overrideConfig: { |
| 45 | + rules: { |
| 46 | + 'named': 2, |
| 47 | + }, |
21 | 48 | }, |
| 49 | + plugins: { 'eslint-plugin-import': importPlugin }, |
22 | 50 | }); |
23 | 51 | }); |
24 | 52 | it("doesn't throw an error on gratuitous, erroneous self-reference", function () { |
25 | | - expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw(); |
| 53 | + return expect(eslint.lintFiles(['./tests/files/issue210.js'])).not.to.rejected; |
26 | 54 | }); |
27 | 55 | }); |
28 | 56 |
|
29 | 57 | describe('issue #1645', function () { |
30 | | - let cli; |
| 58 | + let eslint; |
31 | 59 | beforeEach(function () { |
32 | 60 | if (semver.satisfies(eslintPkg.version, '< 6')) { |
33 | 61 | this.skip(); |
34 | 62 | } else { |
35 | | - cli = new CLIEngine({ |
| 63 | + eslint = new ESLint({ |
36 | 64 | useEslintrc: false, |
37 | | - configFile: './tests/files/just-json-files/.eslintrc.json', |
| 65 | + overrideConfigFile: './tests/files/just-json-files/.eslintrc.json', |
38 | 66 | rulePaths: ['./src/rules'], |
39 | 67 | ignore: false, |
| 68 | + plugins: { 'eslint-plugin-import': importPlugin }, |
40 | 69 | }); |
41 | 70 | } |
42 | 71 | }); |
43 | 72 |
|
44 | 73 | it('throws an error on invalid JSON', () => { |
45 | 74 | const invalidJSON = './tests/files/just-json-files/invalid.json'; |
46 | | - const results = cli.executeOnFiles([invalidJSON]); |
47 | | - expect(results).to.eql({ |
48 | | - results: [ |
49 | | - { |
50 | | - filePath: path.resolve(invalidJSON), |
51 | | - messages: [ |
52 | | - { |
53 | | - column: 2, |
54 | | - endColumn: 3, |
55 | | - endLine: 1, |
56 | | - line: 1, |
57 | | - message: 'Expected a JSON object, array or literal.', |
58 | | - nodeType: results.results[0].messages[0].nodeType, // we don't care about this one |
59 | | - ruleId: 'json/*', |
60 | | - severity: 2, |
61 | | - source: results.results[0].messages[0].source, // NewLine-characters might differ depending on git-settings |
62 | | - }, |
63 | | - ], |
64 | | - errorCount: 1, |
65 | | - ...(semver.satisfies(eslintPkg.version, '>= 7.32') && { |
66 | | - fatalErrorCount: 0, |
67 | | - }), |
68 | | - warningCount: 0, |
69 | | - fixableErrorCount: 0, |
70 | | - fixableWarningCount: 0, |
71 | | - source: results.results[0].source, // NewLine-characters might differ depending on git-settings |
72 | | - }, |
73 | | - ], |
74 | | - ...(semver.satisfies(eslintPkg.version, '>= 7.32') && { |
75 | | - fatalErrorCount: 0, |
76 | | - }), |
77 | | - errorCount: 1, |
78 | | - warningCount: 0, |
79 | | - fixableErrorCount: 0, |
80 | | - fixableWarningCount: 0, |
81 | | - usedDeprecatedRules: results.usedDeprecatedRules, // we don't care about this one |
| 75 | + return eslint.lintFiles([invalidJSON]).then(results => { |
| 76 | + expect(results).to.eql( |
| 77 | + [ |
| 78 | + { |
| 79 | + filePath: path.resolve(invalidJSON), |
| 80 | + messages: [ |
| 81 | + { |
| 82 | + column: 2, |
| 83 | + endColumn: 3, |
| 84 | + endLine: 1, |
| 85 | + line: 1, |
| 86 | + message: 'Expected a JSON object, array or literal.', |
| 87 | + nodeType: results[0].messages[0].nodeType, // we don't care about this one |
| 88 | + ruleId: 'json/*', |
| 89 | + severity: 2, |
| 90 | + source: results[0].messages[0].source, // NewLine-characters might differ depending on git-settings |
| 91 | + }, |
| 92 | + ], |
| 93 | + errorCount: 1, |
| 94 | + ...(semver.satisfies(eslintPkg.version, '>= 7.32 || ^8.0.0-0') && { |
| 95 | + fatalErrorCount: 0, |
| 96 | + }), |
| 97 | + warningCount: 0, |
| 98 | + fixableErrorCount: 0, |
| 99 | + fixableWarningCount: 0, |
| 100 | + source: results[0].source, // NewLine-characters might differ depending on git-settings |
| 101 | + ...(semver.satisfies(eslintPkg.version, '>= 7.0.0') && { |
| 102 | + usedDeprecatedRules: results[0].usedDeprecatedRules, // we don't care about this one |
| 103 | + }), |
| 104 | + }, |
| 105 | + ], |
| 106 | + ); |
82 | 107 | }); |
83 | 108 | }); |
84 | 109 | }); |
|
0 commit comments