| 
2 | 2 | 
 
  | 
3 | 3 | process.chdir(__dirname);  | 
4 | 4 | 
 
  | 
5 |  | -const { CLIEngine } = require('eslint');  | 
 | 5 | +const { ESLint } = require('eslint');  | 
6 | 6 | const assert = require('assert');  | 
7 | 7 | const fs = require('fs');  | 
8 | 8 | 
 
  | 
9 |  | -const cli = new CLIEngine({ reportUnusedDisableDirectives: true });  | 
 | 9 | +const linter = new ESLint({ reportUnusedDisableDirectives: "error" });  | 
10 | 10 | 
 
  | 
11 |  | -for (const name of fs.readdirSync('samples')) {  | 
12 |  | -	if (name[0] !== '.') {  | 
 | 11 | +async function run() {  | 
 | 12 | +	const tests = fs.readdirSync('samples').filter(name => name[0] !== '.');  | 
 | 13 | + | 
 | 14 | +	for (const name of tests) {  | 
13 | 15 | 		console.log(name);  | 
14 |  | -		if (process.platform === 'win32' && !fs.existsSync(`samples/${name}/preserve_line_endings`)) {  | 
15 |  | -			fs.writeFileSync(`samples/${name}/Input.svelte`, fs.readFileSync(`samples/${name}/Input.svelte`).toString().replace(/\r/g, ''));  | 
 | 16 | + | 
 | 17 | +		const path_input = `samples/${name}/Input.svelte`;  | 
 | 18 | +		const path_ple = `samples/${name}/preserve_line_endings`;  | 
 | 19 | +		const path_expected = `samples/${name}/expected.json`;  | 
 | 20 | +		const path_actual = `samples/${name}/actual.json`;  | 
 | 21 | + | 
 | 22 | +		if (process.platform === 'win32' && !exists(path_ple)) {  | 
 | 23 | +			const file = fs.readFileSync(path_input, "utf-8");  | 
 | 24 | +			fs.writeFileSync(path_input, file.replace(/\r/g, ''));  | 
16 | 25 | 		}  | 
17 |  | -		const actual_messages = cli.executeOnFiles([`samples/${name}/Input.svelte`]).results[0].messages;  | 
18 |  | -		fs.writeFileSync(`samples/${name}/actual.json`, JSON.stringify(actual_messages, null, '\t'));  | 
19 |  | -		const expected_messages = JSON.parse(fs.readFileSync(`samples/${name}/expected.json`).toString());  | 
20 |  | -		assert.equal(actual_messages.length, expected_messages.length);  | 
21 |  | -		assert.deepStrictEqual(actual_messages, actual_messages.map((message, i) => ({ ...message, ...expected_messages[i] })));  | 
 | 26 | + | 
 | 27 | +		const result = await linter.lintFiles(path_input);  | 
 | 28 | + | 
 | 29 | +		const actual = result[0] ? result[0].messages : [];  | 
 | 30 | +		const expected = JSON.parse(fs.readFileSync(path_expected, "utf-8"));  | 
 | 31 | + | 
 | 32 | +		fs.writeFileSync(path_actual, JSON.stringify(actual, null, '\t'));  | 
 | 33 | + | 
 | 34 | +		assert.equal(actual.length, expected.length);  | 
 | 35 | +		assert.deepStrictEqual(actual, actual.map((msg, i) => ({ ...msg, ...expected[i] })));  | 
22 | 36 | 		console.log('passed!\n');  | 
23 | 37 | 	}  | 
24 | 38 | }  | 
 | 39 | + | 
 | 40 | +function exists(path) {  | 
 | 41 | +	try {  | 
 | 42 | +		fs.accessSync(path);  | 
 | 43 | +		return true;  | 
 | 44 | +	} catch (err) {  | 
 | 45 | +		return false;  | 
 | 46 | +	}  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +run();  | 
0 commit comments