Skip to content

Commit ba21b94

Browse files
committed
Check if tests needed before build
1 parent 2e6d95a commit ba21b94

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

.github/workflows/test-changed-auth.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ jobs:
2727
run: |
2828
cp config/ci.config.json config/project.json
2929
yarn
30+
- name: Check if tests needed
31+
run: yarn ts-node-script scripts/ci-test/has_changed.ts
32+
id: check-tests-needed
3033
- name: build
34+
if: ${{steps.check-tests-needed.outputs.NEEDS_TESTS}}
3135
run: yarn build
3236
- name: Run tests on changed packages
37+
if: ${{steps.check-tests-needed.outputs.NEEDS_TESTS}}
3338
run: xvfb-run yarn test:changed auth

scripts/ci-test/has_changed.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
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+
import { resolve } from 'path';
19+
import { filterTasks, getTestTasks, TestReason } from './tasks';
20+
import { argv } from 'yargs';
21+
import { testConfig } from './testConfig';
22+
import chalk from 'chalk';
23+
const root = resolve(__dirname, '../..');
24+
25+
const inputTestConfigName = argv._[0].toString();
26+
const config = testConfig[inputTestConfigName]!;
27+
28+
async function checkIfTestsNeeded() {
29+
try {
30+
const testTasks = filterTasks(await getTestTasks(), config);
31+
if (testTasks.length > 0) {
32+
for (const task of testTasks) {
33+
if (task.reason === TestReason.Changed) {
34+
console.log(
35+
chalk`{yellow ${task.pkgName} (contains modified files)}`
36+
);
37+
} else if (task.reason === TestReason.Dependent) {
38+
console.log(
39+
chalk`{yellow ${task.pkgName} (depends on modified files)}`
40+
);
41+
} else {
42+
console.log(chalk`{yellow ${task.pkgName} (running all tests)}`);
43+
}
44+
}
45+
console.log(`::set-output name=NEEDS_TESTS::true`);
46+
} else {
47+
console.log(`::set-output name=NEEDS_TESTS::false`);
48+
}
49+
process.exit(0);
50+
} catch (e) {
51+
console.error(e);
52+
process.exit(1);
53+
}
54+
}
55+
56+
checkIfTestsNeeded();

0 commit comments

Comments
 (0)