|
| 1 | +import childProcess from "node:child_process"; |
| 2 | +import GlobPattern from "#core/glob/pattern"; |
| 3 | +import Command from "#lib/command"; |
| 4 | + |
| 5 | +export default class extends Command { |
| 6 | + |
| 7 | + // static |
| 8 | + static cli () { |
| 9 | + return { |
| 10 | + "options": { |
| 11 | + "only": { |
| 12 | + "short": "o", |
| 13 | + "description": 'Run "only" tests.', |
| 14 | + "default": false, |
| 15 | + "schema": { "type": "boolean" }, |
| 16 | + }, |
| 17 | + "subpackages": { |
| 18 | + "description": "do not test subpackages", |
| 19 | + "default": true, |
| 20 | + "schema": { "type": "boolean" }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + "arguments": { |
| 24 | + "pattern": { |
| 25 | + "description": "Test name glob pattern.", |
| 26 | + "schema": { "type": "array", "items": { "type": "string", "format": "glob-pattern" } }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + // public |
| 33 | + async run () { |
| 34 | + const pkg = this._findGitPackage(); |
| 35 | + if ( !pkg ) return result( [ 500, "Unable to find root package" ] ); |
| 36 | + |
| 37 | + const include = [], |
| 38 | + exclude = []; |
| 39 | + |
| 40 | + if ( process.cli.arguments.pattern ) { |
| 41 | + for ( let pattern of process.cli.arguments.pattern ) { |
| 42 | + pattern = new GlobPattern( pattern ); |
| 43 | + |
| 44 | + if ( pattern.isNegated ) { |
| 45 | + exclude.push( pattern.regExp ); |
| 46 | + } |
| 47 | + else { |
| 48 | + include.push( pattern.regExp ); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const args = [ "--test" ]; |
| 54 | + |
| 55 | + if ( process.cli.options.only ) { |
| 56 | + args.push( "--test-only" ); |
| 57 | + } |
| 58 | + |
| 59 | + if ( include.length ) { |
| 60 | + for ( const pattern of include ) { |
| 61 | + args.push( "--test-name-pattern", pattern ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if ( exclude.length ) { |
| 66 | + for ( const pattern of exclude ) { |
| 67 | + args.push( "--test-skip-pattern", pattern ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + args.push( "tests/**/*.test.js" ); |
| 72 | + |
| 73 | + const packages = [ |
| 74 | + { |
| 75 | + "package": pkg, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + if ( process.cli.options.subpackages ) { |
| 80 | + packages.push( ...pkg.subPackages.map( pkg => { |
| 81 | + return { |
| 82 | + "package": pkg, |
| 83 | + }; |
| 84 | + } ) ); |
| 85 | + } |
| 86 | + |
| 87 | + for ( const test of packages ) { |
| 88 | + console.log( "Test package:", test.package.name ); |
| 89 | + |
| 90 | + const res = childProcess.spawnSync( "node", args, { |
| 91 | + "cwd": test.package.root, |
| 92 | + "stdio": "inherit", |
| 93 | + } ); |
| 94 | + |
| 95 | + test.res = res.status |
| 96 | + ? result( [ 500, "Tests failed" ] ) |
| 97 | + : result( 200 ); |
| 98 | + |
| 99 | + console.log(); |
| 100 | + } |
| 101 | + |
| 102 | + var error; |
| 103 | + |
| 104 | + console.log( "Tests results:" ); |
| 105 | + |
| 106 | + for ( const test of packages ) { |
| 107 | + if ( !test.res.ok ) error = test.res; |
| 108 | + |
| 109 | + console.log( `${ test.package.name } - ${ test.res }` ); |
| 110 | + } |
| 111 | + |
| 112 | + if ( error ) { |
| 113 | + return error; |
| 114 | + } |
| 115 | + else { |
| 116 | + return result( 200 ); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments