11import { spawnSync } from 'node:child_process' ;
2- import { join , relative } from 'node:path' ;
2+ import os from 'node:os' ;
3+ import path from 'node:path' ;
34import { ESLint } from 'eslint' ;
4- import { CWD as PROJECT_CWD } from '../src/utils.js' ;
5+ import { CWD as PROJECT_CWD , slash } from '../src/utils.js' ;
56
6- const CWD = join ( PROJECT_CWD , '..' , '..' ) ;
7+ const CWD = path . join ( PROJECT_CWD , '..' , '..' ) ;
78
89function countErrors ( results : ESLint . LintResult [ ] ) : number {
910 return results . reduce < number > ( ( acc , curr : ESLint . LintResult & { fatalErrorCount : number } ) => {
@@ -18,7 +19,11 @@ ${results.map(result => result.messages.map(m => m.message)).join('\n\n')}
1819}
1920
2021function getFlatESLintOutput ( cwd : string ) : ESLint . LintResult [ ] {
21- const { stdout, stderr } = spawnSync ( 'eslint' , [ '--format' , 'json' , '.' ] , { cwd } ) ;
22+ const { stdout, stderr } = spawnSync ( 'eslint' , [ '--format' , 'json' , '.' ] , {
23+ cwd,
24+ // For Windows, otherwise `stdout` and `stderr` are `null`
25+ shell : os . platform ( ) === 'win32' ,
26+ } ) ;
2227
2328 return parseESLintOutput ( { stdout, stderr } ) ;
2429}
@@ -30,6 +35,8 @@ function getLegacyESLintOutput(cwd: string): ESLint.LintResult[] {
3035 {
3136 cwd,
3237 env : { ...process . env , ESLINT_USE_FLAT_CONFIG : 'false' } ,
38+ // For Windows, otherwise `stdout` and `stderr` are `null`
39+ shell : os . platform ( ) === 'win32' ,
3340 } ,
3441 ) ;
3542
@@ -46,10 +53,15 @@ function parseESLintOutput({
4653 const errorOutput = stderr
4754 . toString ( )
4855 . replace (
49- / \( n o d e : \d { 4 , 7 } \) \[ D E P 0 0 4 0 ] D e p r e c a t i o n W a r n i n g : T h e ` p u n y c o d e ` m o d u l e i s d e p r e c a t e d . P l e a s e u s e a u s e r l a n d a l t e r n a t i v e i n s t e a d ./ ,
56+ / \( n o d e : \d + \) \[ D E P 0 0 4 0 ] D e p r e c a t i o n W a r n i n g : T h e ` p u n y c o d e ` m o d u l e i s d e p r e c a t e d . P l e a s e u s e a u s e r l a n d a l t e r n a t i v e i n s t e a d ./ ,
57+ '' ,
58+ )
59+ . replace (
60+ / \( n o d e : \d + \) E S L i n t R C W a r n i n g : Y o u a r e u s i n g a n e s l i n t r c c o n f i g u r a t i o n f i l e , w h i c h i s d e p r e c a t e d a n d s u p p o r t w i l l b e r e m o v e d i n v 1 0 .0 .0 . P l e a s e m i g r a t e t o a n e s l i n t .c o n f i g .j s f i l e . S e e h t t p s : \/ \/ e s l i n t .o r g \/ d o c s \/ l a t e s t \/ u s e \/ c o n f i g u r e \/ m i g r a t i o n - g u i d e f o r d e t a i l s ./ ,
5061 '' ,
5162 )
5263 . replace ( '(Use `node --trace-deprecation ...` to show where the warning was created)' , '' )
64+ . replace ( '(Use `node --trace-warnings ...` to show where the warning was created)' , '' )
5365 . trimEnd ( ) ;
5466 if ( errorOutput ) {
5567 throw new Error ( errorOutput ) ;
@@ -63,60 +75,62 @@ function parseESLintOutput({
6375function normalizeResults ( results : ESLint . LintResult [ ] ) {
6476 return results
6577 . map ( result => ( {
66- filePath : relative ( CWD , result . filePath ) ,
78+ filePath : slash ( path . relative ( CWD , result . filePath ) ) ,
6779 messages : result . messages ,
6880 } ) )
6981 . filter ( result => result . messages . length > 0 ) ;
7082}
7183
7284describe ( 'Examples' , ( ) => {
7385 it ( 'should work programmatically' , ( ) => {
74- const cwd = join ( CWD , 'examples/ programmatic' ) ;
86+ const cwd = path . join ( CWD , 'examples' , ' programmatic') ;
7587 testESLintOutput ( cwd , 6 ) ;
7688 } ) ;
7789
7890 it ( 'should work on `.js` files' , ( ) => {
79- const cwd = join ( CWD , 'examples/ code-file' ) ;
91+ const cwd = path . join ( CWD , 'examples' , ' code-file') ;
8092 testESLintOutput ( cwd , 4 ) ;
8193 } ) ;
8294
8395 it ( 'should work with `graphql-config`' , ( ) => {
84- const cwd = join ( CWD , 'examples/ graphql-config' ) ;
96+ const cwd = path . join ( CWD , 'examples' , ' graphql-config') ;
8597 testESLintOutput ( cwd , 2 ) ;
8698 } ) ;
8799
88100 it ( 'should work with `eslint-plugin-prettier`' , ( ) => {
89- const cwd = join ( CWD , 'examples/ prettier' ) ;
101+ const cwd = path . join ( CWD , 'examples' , ' prettier') ;
90102 testESLintOutput ( cwd , 23 ) ;
91103 } ) ;
92104
93105 it ( 'should work in monorepo' , ( ) => {
94- const cwd = join ( CWD , 'examples/ monorepo' ) ;
106+ const cwd = path . join ( CWD , 'examples' , ' monorepo') ;
95107 testESLintOutput ( cwd , 11 ) ;
96108 } ) ;
97109
98110 it ( 'should work in svelte' , ( ) => {
99- const cwd = join ( CWD , 'examples/ svelte-code-file' ) ;
111+ const cwd = path . join ( CWD , 'examples' , ' svelte-code-file') ;
100112 testESLintOutput ( cwd , 2 ) ;
101113 } ) ;
102114
103115 it ( 'should work in vue' , ( ) => {
104- const cwd = join ( CWD , 'examples/ vue-code-file' ) ;
116+ const cwd = path . join ( CWD , 'examples' , ' vue-code-file') ;
105117 testESLintOutput ( cwd , 2 ) ;
106118 } ) ;
107119
108120 it ( 'should work in multiple projects' , ( ) => {
109- const cwd = join ( CWD , 'examples/ multiple-projects-graphql-config' ) ;
121+ const cwd = path . join ( CWD , 'examples' , ' multiple-projects-graphql-config') ;
110122 testESLintOutput ( cwd , 4 ) ;
111123 } ) ;
112124} ) ;
113125
114126function testESLintOutput ( cwd : string , errorCount : number ) : void {
115127 const flatResults = getFlatESLintOutput ( cwd ) ;
116- expect ( countErrors ( flatResults ) ) . toBe ( errorCount ) ;
117- expect ( normalizeResults ( flatResults ) ) . toMatchSnapshot ( ) ;
118-
119128 const results = getLegacyESLintOutput ( cwd ) ;
129+ // Windows has some offset for `range`, I think due \r\n handling
130+ if ( os . platform ( ) !== 'win32' ) {
131+ expect ( normalizeResults ( flatResults ) ) . toMatchSnapshot ( ) ;
132+ expect ( normalizeResults ( results ) ) . toMatchSnapshot ( ) ;
133+ }
134+ expect ( countErrors ( flatResults ) ) . toBe ( errorCount ) ;
120135 expect ( countErrors ( results ) ) . toBe ( errorCount ) ;
121- expect ( normalizeResults ( results ) ) . toMatchSnapshot ( ) ;
122136}
0 commit comments