Skip to content

Commit ee57d26

Browse files
authored
Run tests only on changed packages on PR/push (#2565)
1 parent d9bf41c commit ee57d26

File tree

6 files changed

+130
-4
lines changed

6 files changed

+130
-4
lines changed

.github/workflows/cross-browser-test.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
name: Cross-Browser Test Flow
22

3-
on: push
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
types:
8+
- closed
49

510
jobs:
611
cross-browser-test:
712
name: Cross-Browser (Saucelabs) Tests
813
runs-on: ubuntu-latest
14+
if: github.event.pull_request.merged
915
env:
1016
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
1117
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}

.github/workflows/node-chrome-test.yml renamed to .github/workflows/test-all.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
name: Main Test Flow
1+
name: Run All Tests
22

3-
on: [push, pull_request]
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
types:
8+
- closed
9+
schedule:
10+
- cron: '0 6,18 * * *'
411

512
jobs:
613
test:

.github/workflows/test-changed.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Push/PR
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
name: Test Packages With Changed Files
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v1
12+
- name: Set up Node (10)
13+
uses: actions/setup-node@v1
14+
with:
15+
node-version: 10.x
16+
- name: Test setup and yarn install
17+
run: |
18+
cp config/ci.config.json config/project.json
19+
yarn
20+
- name: yarn build
21+
run: yarn build
22+
- name: Run tests on changed packages
23+
run: xvfb-run yarn test:changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"pretest:coverage": "mkdirp coverage",
3030
"ci:coverage": "lcov-result-merger 'packages/**/lcov.info' 'lcov-all.info'",
3131
"test:coverage": "lcov-result-merger 'packages/**/lcov.info' | coveralls",
32+
"test:changed": "node scripts/run_changed.js",
3233
"test:setup": "node tools/config.js",
3334
"test:saucelabs": "node scripts/run_saucelabs.js",
3435
"test:saucelabs:single": "karma start config/karma.saucelabs.js --single-run",

scripts/run_changed.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const { resolve } = require('path');
19+
const { spawn } = require('child-process-promise');
20+
const simpleGit = require('simple-git/promise');
21+
const root = resolve(__dirname, '..');
22+
const git = simpleGit(root);
23+
24+
/**
25+
* Runs tests on packages changed (in comparison to origin/master...HEAD)
26+
*/
27+
28+
async function runTestsOnChangedPackages() {
29+
const diff = await git.diff(['--name-only', 'origin/master...HEAD']);
30+
const changedFiles = diff.split('\n');
31+
const changedPackages = {};
32+
for (const filename of changedFiles) {
33+
const match = filename.match('^(packages/[a-zA-Z0-9-]+)/.*');
34+
if (match && match[1]) {
35+
const pkg = require(resolve(root, match[1], 'package.json'));
36+
if (pkg && pkg.scripts.test) {
37+
changedPackages[match[1]] = true;
38+
}
39+
}
40+
}
41+
if (changedPackages.size > 0) {
42+
await runTests(Object.keys(changedPackages));
43+
} else {
44+
console.log(
45+
'No changes detected in any package. Skipping all package-specific tests.'
46+
);
47+
}
48+
}
49+
50+
/**
51+
* Runs `yarn test` in all dirs in pathList.
52+
* @param {Array<string>} pathList
53+
*/
54+
async function runTests(pathList) {
55+
if (!pathList) return;
56+
for (const testPath of pathList) {
57+
try {
58+
await spawn('yarn', ['--cwd', testPath, 'test'], {
59+
stdio: 'inherit'
60+
});
61+
} catch (e) {
62+
throw new Error(`Error running tests in ${testPath}.`);
63+
}
64+
}
65+
}
66+
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+
async function main() {
80+
try {
81+
await runIntegrationTests();
82+
await runTestsOnChangedPackages();
83+
} catch (e) {
84+
console.error(e);
85+
process.exit(1);
86+
}
87+
}
88+
89+
main();

tools/gitHooks/license.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const root = resolve(__dirname, '../..');
1111
const git = simpleGit(root);
1212
const licenseHeader = `/**
1313
* @license
14-
* Copyright 2019 Google Inc.
14+
* Copyright 2020 Google Inc.
1515
*
1616
* Licensed under the Apache License, Version 2.0 (the "License");
1717
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)