4
4
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
5
5
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
6
6
import { assert } from 'chai' ;
7
- import { red , green , gray } from 'colorette' ;
7
+ import { gray , green , red } from 'colorette' ;
8
8
import { diffLines } from 'diff' ;
9
- import { existsSync , readdirSync , readFileSync } from 'fs' ;
9
+ import { Dirent , promises as fs } from 'fs' ;
10
10
import mockFs from 'mock-fs' ;
11
11
import type { DirectoryItems } from 'mock-fs/lib/filesystem' ;
12
12
import { join } from 'path' ;
13
- // eslint-disable-next-line @typescript-eslint/no-shadow
14
- import { URL } from 'url' ;
13
+ import { URL } from 'node:url' ;
15
14
import { v4 as uuid } from 'uuid' ;
16
15
import { Configuration } from '../src/cli/configuration.js' ;
17
16
import { $Generator } from '../src/cli/generator.js' ;
18
17
import { Endpoint , EndpointConfiguration } from '../src/cli/shared.js' ;
18
+ import { exists } from '../src/cli/file-system-helper.js' ;
19
19
20
20
const __dirname = process . cwd ( ) ;
21
21
22
22
describe ( 'generator' , function ( ) {
23
23
24
- function readAllContent ( path : string ) : Map < string , string > {
24
+ async function readAllContent ( path : string ) : Promise < Map < string , string > > {
25
25
const result = new Map < string , string > ( ) ;
26
- for ( const dirent of readdirSync ( path , { encoding : 'utf8' , withFileTypes : true } ) ) {
26
+ for ( const dirent of await fs . readdir ( path , { encoding : 'utf8' , withFileTypes : true } ) ) {
27
27
const entryName = dirent . name ;
28
28
const completePath = join ( path , entryName ) ;
29
29
if ( dirent . isFile ( ) ) {
30
- result . set ( entryName , readFileSync ( completePath , 'utf8' ) ) ;
30
+ result . set ( entryName , await fs . readFile ( completePath , 'utf8' ) ) ;
31
31
} else {
32
- const content = readAllContent ( completePath ) ;
32
+ const content = await readAllContent ( completePath ) ;
33
33
for ( const [ key , value ] of content ) {
34
34
result . set ( join ( entryName , key ) , value ) ;
35
35
}
@@ -65,12 +65,14 @@ describe('generator', function () {
65
65
}
66
66
67
67
const dataPath = join ( __dirname , 'tests' , 'data' ) ;
68
- for ( const dirent of
69
- readdirSync ( dataPath , { encoding : 'utf8' , withFileTypes : true } )
70
- . filter ( ( x ) => x . isDirectory ( ) )
71
- ) {
68
+ let directoryEntries : Dirent [ ] = [ ] ;
69
+ before ( async function ( ) {
70
+ directoryEntries = ( await fs . readdir ( dataPath , { encoding : 'utf8' , withFileTypes : true } ) )
71
+ . filter ( ( x ) => x . isDirectory ( ) ) ;
72
+ } ) ;
73
+ for ( const dirent of directoryEntries ) {
72
74
const dirName = dirent . name ;
73
- it ( `works for ${ dirName } ` , async function ( ) {
75
+ it . only ( `works for ${ dirName } ` , async function ( ) {
74
76
let generator : $Generator | null = null ;
75
77
try {
76
78
// arrange
@@ -84,36 +86,36 @@ describe('generator', function () {
84
86
let configuredEndpoints : EndpointConfiguration [ ] | undefined = undefined ;
85
87
let hasConfiguredEndpoints = false ;
86
88
const args = [ '--outputDir' , baseOutputPath ] ;
87
- if ( existsSync ( configFilePath ) ) {
89
+ if ( await exists ( configFilePath ) ) {
88
90
args . unshift ( '--config' , configFilePath ) ;
89
91
mockFsConfig [ configFilePath ] = mockFs . load ( configFilePath ) ;
90
- // eslint-disable-next-line @typescript-eslint/no-var-requires
91
- configuredEndpoints = ( await import ( configFilePath ) ) . endpoints ;
92
+ configuredEndpoints = ( await import ( `file:///${ configFilePath } ` ) ) . endpoints ;
92
93
hasConfiguredEndpoints = Array . isArray ( configuredEndpoints ) && configuredEndpoints . length > 0 ;
93
94
}
94
95
if ( ! hasConfiguredEndpoints ) {
95
96
args . push ( '--endpoint' , 'https://api.example.com' ) ;
96
97
}
97
- const expected = readAllContent ( join ( caseDir , 'expected' ) ) ;
98
+ const expected = await readAllContent ( join ( caseDir , 'expected' ) ) ;
98
99
mockFs ( mockFsConfig , { createCwd : true } ) ;
99
100
const configuration = await Configuration . createFromCLIArgs ( args ) ;
100
101
generator = new $Generator ( ) ;
101
102
102
103
for ( const ep of configuration . endpoints ) {
103
104
let epInput = join ( inputDir , new URL ( ep . url ) . hostname ) ;
104
- epInput = mockFs . bypass ( ( ) => existsSync ( epInput ) ) ? epInput : inputDir ;
105
+ epInput = await exists ( epInput ) ? epInput : inputDir ;
105
106
106
- const edmxXml = mockFs . bypass ( ( ) => readFileSync ( join ( epInput , 'metadata.xml' ) , 'utf8' ) ) ;
107
+ const edmxXml = await fs . readFile ( join ( epInput , 'metadata.xml' ) , 'utf8' ) ;
108
+ console . log ( '[test] edmxXml' , edmxXml ) ;
107
109
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
108
- const endpoints : Endpoint [ ] = mockFs . bypass ( ( ) => JSON . parse ( readFileSync ( join ( epInput , 'endpoints.json' ) , 'utf8' ) ) . value ) ;
110
+ const endpoints : Endpoint [ ] = JSON . parse ( await fs . readFile ( join ( epInput , 'endpoints.json' ) , 'utf8' ) ) . value ;
109
111
110
112
// act
111
- generator . generateEndpointsFile ( endpoints , ep ) ;
112
- generator . generateEdm ( edmxXml , endpoints , ep ) ;
113
+ await generator . generateEndpointsFile ( endpoints , ep ) ;
114
+ await generator . generateEdm ( edmxXml , endpoints , ep ) ;
113
115
}
114
116
115
117
// assert
116
- const actual = readAllContent ( baseOutputPath ) ;
118
+ const actual = await readAllContent ( baseOutputPath ) ;
117
119
assertContent ( actual , expected ) ;
118
120
} catch ( e ) {
119
121
assert . fail ( ( e as Error ) . message ) ;
0 commit comments