13
13
const assert = require ( "assert" ) ;
14
14
const util = require ( "util" ) ;
15
15
const fs = require ( "fs" ) ;
16
- const fsp = fs . promises ;
16
+ const fsp = require ( "fs/ promises" ) ;
17
17
const os = require ( "os" ) ;
18
18
const path = require ( "path" ) ;
19
19
const timers = require ( "node:timers/promises" ) ;
@@ -1057,6 +1057,61 @@ describe("ESLint", () => {
1057
1057
await assert . rejects ( async ( ) => await eslint . lintFiles ( [ "lib/cli.js" ] ) , / E x p e c t e d o b j e c t w i t h p a r s e \( \) o r p a r s e F o r E S L i n t \( \) m e t h o d / u) ;
1058
1058
} ) ;
1059
1059
1060
+ // https://github.com/eslint/eslint/issues/18407
1061
+ it ( "should work in case when `fsp.readFile()` returns an object that is not an instance of Promise from this realm" , async ( ) => {
1062
+
1063
+ /**
1064
+ * Promise wrapper
1065
+ */
1066
+ class PromiseLike {
1067
+ constructor ( promise ) {
1068
+ this . promise = promise ;
1069
+ }
1070
+ then ( ...args ) {
1071
+ return new PromiseLike ( this . promise . then ( ...args ) ) ;
1072
+ }
1073
+ catch ( ...args ) {
1074
+ return new PromiseLike ( this . promise . catch ( ...args ) ) ;
1075
+ }
1076
+ finally ( ...args ) {
1077
+ return new PromiseLike ( this . promise . finally ( ...args ) ) ;
1078
+ }
1079
+ }
1080
+
1081
+ const spy = sinon . spy (
1082
+ ( ...args ) => new PromiseLike ( fsp . readFile ( ...args ) )
1083
+ ) ;
1084
+
1085
+ const { ESLint : LocalESLint } = proxyquire ( "../../../lib/eslint/eslint" , {
1086
+ "fs/promises" : {
1087
+ readFile : spy ,
1088
+ "@noCallThru" : false // allows calling other methods of `fs/promises`
1089
+ }
1090
+ } ) ;
1091
+
1092
+ const testDir = "tests/fixtures/simple-valid-project" ;
1093
+ const expectedLintedFiles = [
1094
+ path . resolve ( testDir , "foo.js" ) ,
1095
+ path . resolve ( testDir , "src" , "foobar.js" )
1096
+ ] ;
1097
+
1098
+ eslint = new LocalESLint ( {
1099
+ cwd : originalDir ,
1100
+ overrideConfigFile : path . resolve ( testDir , "eslint.config.js" )
1101
+ } ) ;
1102
+
1103
+ const results = await eslint . lintFiles ( [ `${ testDir } /**/foo*.js` ] ) ;
1104
+
1105
+ assert . strictEqual ( results . length , expectedLintedFiles . length ) ;
1106
+
1107
+ expectedLintedFiles . forEach ( ( file , index ) => {
1108
+ assert ( spy . calledWith ( file ) , `Spy was not called with ${ file } ` ) ;
1109
+ assert . strictEqual ( results [ index ] . filePath , file ) ;
1110
+ assert . strictEqual ( results [ index ] . messages . length , 0 ) ;
1111
+ assert . strictEqual ( results [ index ] . suppressedMessages . length , 0 ) ;
1112
+ } ) ;
1113
+ } ) ;
1114
+
1060
1115
describe ( "Invalid inputs" , ( ) => {
1061
1116
1062
1117
[
@@ -5513,13 +5568,10 @@ describe("ESLint", () => {
5513
5568
} ) ;
5514
5569
5515
5570
it ( "should call fs.writeFile() for each result with output" , async ( ) => {
5516
- const fakeFS = {
5517
- writeFile : sinon . spy ( ( ) => Promise . resolve ( ) )
5518
- } ;
5519
- const spy = fakeFS . writeFile ;
5571
+ const spy = sinon . spy ( ( ) => Promise . resolve ( ) ) ;
5520
5572
const { ESLint : localESLint } = proxyquire ( "../../../lib/eslint/eslint" , {
5521
- fs : {
5522
- promises : fakeFS
5573
+ "fs/promises" : {
5574
+ writeFile : spy
5523
5575
}
5524
5576
} ) ;
5525
5577
@@ -5542,15 +5594,13 @@ describe("ESLint", () => {
5542
5594
} ) ;
5543
5595
5544
5596
it ( "should call fs.writeFile() for each result with output and not at all for a result without output" , async ( ) => {
5545
- const fakeFS = {
5546
- writeFile : sinon . spy ( ( ) => Promise . resolve ( ) )
5547
- } ;
5548
- const spy = fakeFS . writeFile ;
5597
+ const spy = sinon . spy ( ( ) => Promise . resolve ( ) ) ;
5549
5598
const { ESLint : localESLint } = proxyquire ( "../../../lib/eslint/eslint" , {
5550
- fs : {
5551
- promises : fakeFS
5599
+ "fs/promises" : {
5600
+ writeFile : spy
5552
5601
}
5553
5602
} ) ;
5603
+
5554
5604
const results = [
5555
5605
{
5556
5606
filePath : path . resolve ( "foo.js" ) ,
0 commit comments