17
17
18
18
const { resolve } = require ( 'path' ) ;
19
19
const { spawn } = require ( 'child-process-promise' ) ;
20
+ const chalk = require ( 'chalk' ) ;
20
21
const simpleGit = require ( 'simple-git/promise' ) ;
22
+
21
23
const root = resolve ( __dirname , '..' ) ;
22
24
const git = simpleGit ( root ) ;
23
25
24
26
/**
25
- * Runs tests on packages changed (in comparison to origin/master...HEAD)
27
+ * Changes to these files warrant running all tests.
28
+ */
29
+ const fullTestTriggerFiles = [
30
+ // Global dependency changes.
31
+ 'package.json' ,
32
+ 'yarn.lock' ,
33
+ // Test/compile/lint configs.
34
+ 'config/karma.base.js' ,
35
+ 'config/.eslintrc.js' ,
36
+ 'config/mocha.browser.opts' ,
37
+ 'config/mocha.node.opts' ,
38
+ 'config/tsconfig.base.json' ,
39
+ 'config/webpack.test.js' ,
40
+ 'config/firestore.rules' ,
41
+ 'config/database.rules.json'
42
+ ] ;
43
+
44
+ /**
45
+ * Always run tests in these paths.
26
46
*/
47
+ const alwaysRunTestPaths = [
48
+ // These tests are very fast.
49
+ 'integration/browserify' ,
50
+ 'integration/firebase-typings' ,
51
+ 'integration/typescript' ,
52
+ 'integration/webpack'
53
+ ] ;
27
54
28
- async function runTestsOnChangedPackages ( ) {
55
+ /**
56
+ * Identify modified packages that require tests.
57
+ */
58
+ async function getChangedPackages ( ) {
29
59
const diff = await git . diff ( [ '--name-only' , 'origin/master...HEAD' ] ) ;
30
60
const changedFiles = diff . split ( '\n' ) ;
31
61
const changedPackages = { } ;
32
62
for ( const filename of changedFiles ) {
63
+ if ( fullTestTriggerFiles . includes ( filename ) ) {
64
+ console . log (
65
+ chalk `{blue Running all tests because ${ filename } was modified.}`
66
+ ) ;
67
+ return { testAll : true } ;
68
+ }
33
69
const match = filename . match ( '^(packages/[a-zA-Z0-9-]+)/.*' ) ;
34
70
if ( match && match [ 1 ] ) {
35
71
const pkg = require ( resolve ( root , match [ 1 ] , 'package.json' ) ) ;
@@ -39,11 +75,12 @@ async function runTestsOnChangedPackages() {
39
75
}
40
76
}
41
77
if ( Object . keys ( changedPackages ) . length > 0 ) {
42
- await runTests ( Object . keys ( changedPackages ) ) ;
78
+ return { testAll : false , packageDirs : Object . keys ( changedPackages ) } ;
43
79
} else {
44
80
console . log (
45
- ' No changes detected in any package. Skipping all package-specific tests.'
81
+ chalk `{green No changes detected in any package. Skipping all package-specific tests.}`
46
82
) ;
83
+ return { testAll : false , packageDirs : [ ] } ;
47
84
}
48
85
}
49
86
@@ -64,22 +101,24 @@ async function runTests(pathList) {
64
101
}
65
102
}
66
103
67
- /**
68
- * These are short, always run them.
69
- */
70
- async function runIntegrationTests ( ) {
71
- await runTests ( [
72
- 'integration/browserify' ,
73
- 'integration/firebase-typings' ,
74
- 'integration/typescript' ,
75
- 'integration/webpack'
76
- ] ) ;
77
- }
78
-
79
104
async function main ( ) {
80
105
try {
81
- await runIntegrationTests ( ) ;
82
- await runTestsOnChangedPackages ( ) ;
106
+ const { testAll, packageDirs = [ ] } = await getChangedPackages ( ) ;
107
+ if ( testAll ) {
108
+ await spawn ( 'yarn' , [ 'test' ] , {
109
+ stdio : 'inherit'
110
+ } ) ;
111
+ } else {
112
+ console . log ( chalk `{blue Running tests in:}` ) ;
113
+ for ( const filename of alwaysRunTestPaths ) {
114
+ console . log ( chalk `{green ${ filename } (always runs)}` ) ;
115
+ }
116
+ for ( const filename of packageDirs ) {
117
+ console . log ( chalk `{yellow ${ filename } (contains modified files)}` ) ;
118
+ }
119
+ await runTests ( alwaysRunTestPaths ) ;
120
+ await runTests ( packageDirs ) ;
121
+ }
83
122
} catch ( e ) {
84
123
console . error ( e ) ;
85
124
process . exit ( 1 ) ;
0 commit comments