Skip to content

Commit 09172b9

Browse files
committed
🐛 (tasks) fix frontend task run on empty test generation
1 parent 551df88 commit 09172b9

File tree

3 files changed

+73
-49
lines changed

3 files changed

+73
-49
lines changed

.tasks/.tests/benchmarks/compute-benchmarks.task.mjs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import colors from 'ansi-colors'
22
import childProcess from 'child_process'
33
import log from 'fancy-log'
4-
import {
5-
existsSync,
6-
mkdirSync,
7-
writeFileSync
8-
} from 'fs'
94
import {
105
basename,
116
dirname,
@@ -17,13 +12,14 @@ import {
1712
nodeModulesDirectory,
1813
packageName,
1914
packageSourcesDirectory as sourcesDir,
20-
packageTestsBenchmarksDirectory as benchesDir
15+
packageTestsBenchmarksDirectory as benchesDir,
16+
createDirectoryIfNotExist,
17+
createFile
2118
} from '../../_utils.mjs'
2219
import { sourcesFiles } from '../../configs/compute-benchmarks.conf.mjs'
2320

2421
const {
2522
red,
26-
green,
2723
yellow
2824
} = colors
2925

@@ -32,10 +28,7 @@ const {
3228
*/
3329
const computeBenchmarksTask = ( done ) => {
3430

35-
if ( !existsSync( benchesDir ) ) {
36-
log( 'Creating', green( benchesDir ) )
37-
mkdirSync( benchesDir, { recursive: true } )
38-
}
31+
createDirectoryIfNotExist( benchesDir )
3932

4033
const benchRootImports = []
4134
for ( let sourceFile of sourcesFiles ) {
@@ -156,13 +149,8 @@ const computeBenchmarksTask = ( done ) => {
156149
exports: suitesToExports
157150
} )
158151

159-
if ( !existsSync( benchDirPath ) ) {
160-
log( 'Creating', green( benchDirPath ) )
161-
mkdirSync( benchDirPath, { recursive: true } )
162-
}
163-
164-
log( 'Creating', green( benchFilePath ) )
165-
writeFileSync( benchFilePath, template )
152+
createDirectoryIfNotExist( benchDirPath )
153+
createFile( benchFilePath, template )
166154

167155
} catch ( error ) {
168156

@@ -185,6 +173,16 @@ const computeBenchmarksTask = ( done ) => {
185173

186174
}
187175

176+
// Use a fallback in case no benches were found at all
177+
if ( benchRootImports.length === 0 ) {
178+
log( 'Warning ', yellow( 'No usable exports found, generate default file to avoid frontend breakage.' ) )
179+
const defaultBenchesDir = join( benchesDir, 'default' )
180+
const defaultBenchesPath = join( defaultBenchesDir, 'default.bench.js' )
181+
182+
createDirectoryIfNotExist( defaultBenchesDir )
183+
createFile( defaultBenchesPath, '// Avoid web test runner crash on empty benches' )
184+
}
185+
188186
const benchesTemplate = '' +
189187
`${ templateImports }` + '\n' +
190188
'const suites = [' + '\n' +
@@ -196,9 +194,7 @@ const computeBenchmarksTask = ( done ) => {
196194
`}` + '\n'
197195

198196
const benchesFilePath = join( benchesDir, `${ packageName }.benchmarks.js` )
199-
200-
log( 'Creating', green( benchesFilePath ) )
201-
writeFileSync( benchesFilePath, benchesTemplate )
197+
createFile( benchesFilePath, benchesTemplate )
202198

203199
done()
204200

.tasks/.tests/unit-tests/compute-unit-tests.task.mjs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import colors from 'ansi-colors'
22
import childProcess from 'child_process'
33
import log from 'fancy-log'
4-
import {
5-
existsSync,
6-
mkdirSync,
7-
writeFileSync
8-
} from 'fs'
94
import { isNotEmptyArray } from 'itee-validators'
105
import {
116
basename,
@@ -15,18 +10,19 @@ import {
1510
relative
1611
} from 'path'
1712
import {
13+
createDirectoryIfNotExist,
14+
createFile,
15+
getPrettyPackageName,
1816
Indenter,
1917
nodeModulesDirectory,
2018
packageName,
2119
packageSourcesDirectory as sourcesDir,
22-
packageTestsUnitsDirectory as unitsDir,
23-
getPrettyPackageName
20+
packageTestsUnitsDirectory as unitsDir
2421
} from '../../_utils.mjs'
2522
import { sourcesFiles } from '../../configs/compute-unit-tests.conf.mjs'
2623

2724
const {
2825
red,
29-
green,
3026
yellow
3127
} = colors
3228

@@ -35,10 +31,7 @@ const {
3531
*/
3632
const computeUnitTestsTask = ( done ) => {
3733

38-
if ( !existsSync( unitsDir ) ) {
39-
log( 'Creating', green( unitsDir ) )
40-
mkdirSync( unitsDir, { recursive: true } )
41-
}
34+
createDirectoryIfNotExist( unitsDir )
4235

4336
const unitsImportMap = []
4437
for ( let sourceFile of sourcesFiles ) {
@@ -484,13 +477,8 @@ const computeUnitTestsTask = ( done ) => {
484477
path: importUnitFilePath.replace( /\\/g, '/' )
485478
} )
486479

487-
if ( !existsSync( unitDirPath ) ) {
488-
log( 'Creating', green( unitDirPath ) )
489-
mkdirSync( unitDirPath, { recursive: true } )
490-
}
491-
492-
log( 'Creating', green( unitFilePath ) )
493-
writeFileSync( unitFilePath, template )
480+
createDirectoryIfNotExist( unitDirPath )
481+
createFile( unitFilePath, template )
494482

495483
} catch ( error ) {
496484

@@ -514,17 +502,20 @@ const computeUnitTestsTask = ( done ) => {
514502

515503
} else {
516504

517-
log( yellow( 'No tests were generated. Create fallback global root import file.' ) )
505+
log( 'Warning ', yellow( 'No tests were generated. Create fallback global root import file.' ) )
506+
const defaultUnitsDir = join( unitsDir, 'default' )
507+
const defaultUnitsPath = join( defaultUnitsDir, 'default.unit.mjs' )
508+
509+
createDirectoryIfNotExist( defaultUnitsDir )
510+
createFile( defaultUnitsPath, '// Avoid web test runner crash on empty benches' )
518511

519-
const prettyPackageName = getPrettyPackageName('#')
520-
unitsTemplate = `describe( '${prettyPackageName}', () => {} )` + '\n'
512+
const prettyPackageName = getPrettyPackageName( '#' )
513+
unitsTemplate = `describe( '${ prettyPackageName }', () => {} )` + '\n'
521514

522515
}
523516

524517
const unitsFilePath = join( unitsDir, `${ packageName }.units.mjs` )
525-
526-
log( 'Creating', green( unitsFilePath ) )
527-
writeFileSync( unitsFilePath, unitsTemplate )
518+
createFile( unitsFilePath, unitsTemplate )
528519

529520
done()
530521

.tasks/_utils.mjs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import colors from 'ansi-colors'
22
import childProcess from 'child_process'
33
import log from 'fancy-log'
4-
import { readFileSync } from 'fs'
4+
import {
5+
existsSync,
6+
mkdirSync,
7+
readFileSync,
8+
writeFileSync
9+
} from 'fs'
510
import {
611
dirname,
712
join
813
} from 'path'
914
import { fileURLToPath } from 'url'
1015

11-
const red = colors.red
12-
const yellow = colors.yellow
16+
const {
17+
red,
18+
green,
19+
yellow
20+
} = colors
21+
22+
///
1323

1424
function getDirname() {
1525

@@ -52,6 +62,8 @@ const packageTestsUnitsDirectory = join( packageTestsDirectory, 'units' )
5262
const packageDocsDirectory = join( packageRootDirectory, 'docs' )
5363
const packageTutorialsDirectory = join( packageRootDirectory, 'tutorials' )
5464

65+
///
66+
5567
function getPackageJson() {
5668

5769
const packageData = readFileSync( packageJsonPath )
@@ -124,6 +136,26 @@ function getPrettyNpmVersion() {
124136

125137
}
126138

139+
///
140+
141+
function createDirectoryIfNotExist( directoryPath ) {
142+
143+
if ( !existsSync( directoryPath ) ) {
144+
log( 'Creating', green( directoryPath ) )
145+
mkdirSync( directoryPath, { recursive: true } )
146+
}
147+
148+
}
149+
150+
function createFile( filePath, fileContent ) {
151+
152+
log( 'Creating', green( filePath ) )
153+
writeFileSync( filePath, fileContent )
154+
155+
}
156+
157+
///
158+
127159
function IndenterFactory( indentationChar = '\t', indentationLevel = 5 ) {
128160

129161
const indentationLevels = {}
@@ -163,6 +195,8 @@ class Indenter {
163195

164196
}
165197

198+
///
199+
166200
export {
167201
packageRootDirectory,
168202
packageJsonPath,
@@ -188,5 +222,8 @@ export {
188222
getPrettyNodeVersion,
189223
getPrettyNpmVersion,
190224

225+
createDirectoryIfNotExist,
226+
createFile,
227+
191228
IndenterFactory as Indenter
192229
}

0 commit comments

Comments
 (0)