1
- const fs = require ( 'fs' ) . promises ;
1
+ const fs = require ( 'fs' ) ;
2
2
const path = require ( 'path' ) ;
3
3
const _ = require ( 'lodash' ) ;
4
4
const { getUnitTests, checkForAPIKey} = require ( './api' ) ;
@@ -28,7 +28,8 @@ let functionList = {},
28
28
folderStructureTree = [ ] ,
29
29
testsGenerated = [ ] ,
30
30
errors = [ ] ,
31
- ignoreFolders = [ 'node_modules' , 'pythagora_tests' ]
31
+ ignoreFolders = [ 'node_modules' , 'pythagora_tests' ] ,
32
+ processExtensions = [ '.js' ]
32
33
;
33
34
34
35
async function processFile ( filePath ) {
@@ -142,16 +143,34 @@ async function createTests(filePath, prefix, funcToTest) {
142
143
) ;
143
144
spinner . start ( folderStructureTree , indexToPush ) ;
144
145
146
+ let testFilePath = path . join ( getTestFilePath ( filePath , rootPath ) , `/${ funcData . functionName } .test.js` ) ;
147
+ if ( fs . existsSync ( testFilePath ) ) {
148
+ await spinner . stop ( ) ;
149
+ folderStructureTree [ indexToPush ] . line = `${ green } ${ folderStructureTree [ indexToPush ] . line } ${ reset } ` ;
150
+ continue ;
151
+ }
152
+
145
153
let formattedData = await reformatDataForPythagoraAPI ( funcData , filePath , getTestFilePath ( filePath , rootPath ) ) ;
146
- let tests = await getUnitTests ( formattedData , ( content ) => {
154
+ let { tests, error } = await getUnitTests ( formattedData , ( content ) => {
147
155
scrollableContent . setContent ( content ) ;
148
156
scrollableContent . setScrollPerc ( 100 ) ;
149
157
screen . render ( ) ;
150
158
} ) ;
151
- let testPath = await saveTests ( filePath , funcData . functionName , tests ) ;
152
- testsGenerated . push ( testPath ) ;
153
- await spinner . stop ( ) ;
154
- folderStructureTree [ indexToPush ] . line = `${ green } ${ folderStructureTree [ indexToPush ] . line } ${ reset } ` ;
159
+
160
+ if ( tests ) {
161
+ let testPath = await saveTests ( filePath , funcData . functionName , tests ) ;
162
+ testsGenerated . push ( testPath ) ;
163
+ await spinner . stop ( ) ;
164
+ folderStructureTree [ indexToPush ] . line = `${ green } ${ folderStructureTree [ indexToPush ] . line } ${ reset } ` ;
165
+ } else {
166
+ errors . push ( {
167
+ file :filePath ,
168
+ function : funcData . functionName ,
169
+ error
170
+ } ) ;
171
+ await spinner . stop ( ) ;
172
+ folderStructureTree [ indexToPush ] . line = `${ red } ${ folderStructureTree [ indexToPush ] . line } ${ reset } ` ;
173
+ }
155
174
}
156
175
157
176
if ( foundFunctions . length > 0 ) {
@@ -168,24 +187,24 @@ async function saveTests(filePath, name, testData) {
168
187
let dir = getTestFilePath ( filePath , rootPath ) ;
169
188
170
189
if ( ! await checkDirectoryExists ( dir ) ) {
171
- await fs . mkdir ( dir , { recursive : true } ) ;
190
+ fs . mkdirSync ( dir , { recursive : true } ) ;
172
191
}
173
192
174
193
let testPath = path . join ( dir , `/${ name } .test.js` ) ;
175
- await fs . writeFile ( testPath , testData ) ;
194
+ fs . writeFileSync ( testPath , testData ) ;
176
195
return testPath ;
177
196
}
178
197
179
198
async function traverseDirectory ( directory , onlyCollectFunctionData , prefix = '' , funcName ) {
180
199
if ( await checkPathType ( directory ) === 'file' && ! onlyCollectFunctionData ) {
181
- if ( path . extname ( directory ) !== '.js' ) throw new Error ( 'File is not a javascript file ' ) ;
200
+ if ( ! processExtensions . includes ( path . extname ( directory ) ) ) throw new Error ( 'File extension is not supported ' ) ;
182
201
const newPrefix = `| ${ prefix } | ` ;
183
202
return await createTests ( directory , newPrefix , funcName ) ;
184
203
}
185
- const files = await fs . readdir ( directory ) ;
204
+ const files = fs . readdirSync ( directory ) ;
186
205
for ( const file of files ) {
187
206
const absolutePath = path . join ( directory , file ) ;
188
- const stat = await fs . stat ( absolutePath ) ;
207
+ const stat = fs . statSync ( absolutePath ) ;
189
208
const isLast = files . indexOf ( file ) === files . length - 1 ;
190
209
if ( stat . isDirectory ( ) ) {
191
210
if ( ignoreFolders . includes ( path . basename ( absolutePath ) ) || path . basename ( absolutePath ) . charAt ( 0 ) === '.' ) continue ;
@@ -197,7 +216,7 @@ async function traverseDirectory(directory, onlyCollectFunctionData, prefix = ''
197
216
const newPrefix = isLast ? `${ prefix } ` : `${ prefix } | ` ;
198
217
await traverseDirectory ( absolutePath , onlyCollectFunctionData , newPrefix , funcName ) ;
199
218
} else {
200
- if ( path . extname ( absolutePath ) !== '.js' ) continue ;
219
+ if ( ! processExtensions . includes ( path . extname ( absolutePath ) ) ) continue ;
201
220
if ( onlyCollectFunctionData ) {
202
221
if ( isPathInside ( path . dirname ( queriedPath ) , absolutePath ) ) {
203
222
updateFolderTree ( prefix , isLast , absolutePath ) ;
@@ -225,7 +244,6 @@ async function getFunctionsForExport(dirPath) {
225
244
}
226
245
227
246
async function generateTestsForDirectory ( pathToProcess , funcName ) {
228
-
229
247
checkForAPIKey ( ) ;
230
248
queriedPath = path . resolve ( pathToProcess ) ;
231
249
rootPath = process . cwd ( ) ;
@@ -237,7 +255,12 @@ async function generateTestsForDirectory(pathToProcess, funcName) {
237
255
238
256
screen . destroy ( ) ;
239
257
process . stdout . write ( '\x1B[2J\x1B[0f' ) ;
240
- if ( errors . length ) console . log ( 'Errors encountered while trying to generate unit tests:\n' , errors ) ;
258
+ if ( errors . length ) {
259
+ let errLogPath = `${ path . resolve ( PYTHAGORA_UNIT_DIR , 'errorLogs.log' ) } `
260
+ fs . writeFileSync ( errLogPath , JSON . stringify ( errors ) ) ;
261
+ console . error ( 'There were errors encountered while trying to generate unit tests.\n' ) ;
262
+ console . error ( `You can find logs here: ${ errLogPath } ` ) ;
263
+ }
241
264
if ( testsGenerated . length === 0 ) {
242
265
console . log ( `${ bold + red } No tests generated!${ reset } ` ) ;
243
266
} else {
0 commit comments