Skip to content

Commit 6ac41a2

Browse files
committed
firestore only test changed
1 parent fc83160 commit 6ac41a2

File tree

4 files changed

+168
-171
lines changed

4 files changed

+168
-171
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"ci:coverage": "lcov-result-merger 'packages/**/lcov.info' 'lcov-all.info'",
4040
"test:coverage": "lcov-result-merger 'packages/**/lcov.info' | coveralls",
4141
"test:changed": "node scripts/run_changed.js",
42+
"test:changed:firestore": "ts-node-script scripts/run_changed_firestore.ts",
4243
"test:setup": "node tools/config.js",
4344
"test:saucelabs": "node scripts/run_saucelabs.js",
4445
"docgen:js": "node scripts/docgen/generate-docs.js --api js",

scripts/run_changed_firestore.js

Lines changed: 0 additions & 170 deletions
This file was deleted.

scripts/run_changed_firestore.ts

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* @license
3+
* Copyright 2020 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 { spawn, exec } from 'child-process-promise';
20+
import chalk from 'chalk';
21+
import simpleGit from 'simple-git/promise';
22+
23+
interface TestTask {
24+
pkgName: string;
25+
reason: TestReason;
26+
}
27+
28+
interface TestReason {
29+
code: TestReasonCode;
30+
dependencies?: string[]; // populated when the code is Dependent
31+
}
32+
33+
enum TestReasonCode {
34+
Changed = 'changed',
35+
Dependent = 'dependent',
36+
Global = 'global'
37+
}
38+
39+
const root = resolve(__dirname, '..');
40+
const git = simpleGit(root);
41+
42+
// use test:ci command in CI
43+
const testCommand = !!process.env.CI ? 'test:ci' : 'test';
44+
45+
/**
46+
* Changes to these files warrant running all tests.
47+
*/
48+
const testTriggerFiles = [
49+
// Global dependency changes.
50+
'yarn.lock',
51+
// Test/compile configs.
52+
'config/karma.base.js',
53+
'config/mocha.browser.opts',
54+
'config/mocharc.node.js',
55+
'config/tsconfig.base.json',
56+
'config/webpack.test.js',
57+
'config/firestore.rules',
58+
'scripts/emulator-testing/emulators/firestore-emulator.ts',
59+
'scripts/emulator-testing/emulators/emulator.ts',
60+
'scripts/emulator-testing/firestore-test-runner.ts'
61+
];
62+
63+
/**
64+
* packages to test
65+
*/
66+
const firestorePackages = [
67+
'@firebase/firestore',
68+
'firebase-firestore-integration-test'
69+
];
70+
71+
function createTestTask(
72+
pkgName: string,
73+
reasonCode = TestReasonCode.Global
74+
): TestTask {
75+
return {
76+
pkgName,
77+
reason: {
78+
code: reasonCode
79+
}
80+
};
81+
}
82+
83+
/**
84+
* Identify modified packages that require tests.
85+
*/
86+
async function getTestTasks(): Promise<TestTask[]> {
87+
const packageInfo = JSON.parse(
88+
(await exec('npx lerna ls --json', { cwd: root })).stdout
89+
);
90+
const depGraph = JSON.parse(
91+
(await exec('npx lerna ls --graph', { cwd: root })).stdout
92+
);
93+
const diff = await git.diff(['--name-only', 'origin/master...HEAD']);
94+
const changedFiles = diff.split('\n');
95+
const testTasks: TestTask[] = [];
96+
for (const filename of changedFiles) {
97+
// Files that trigger all tests.
98+
if (testTriggerFiles.includes(filename)) {
99+
console.log(
100+
chalk`{blue Running Firestore tests because ${filename} was modified.}`
101+
);
102+
return firestorePackages.map(name => createTestTask(name));
103+
}
104+
105+
// Check for changed files inside package dirs.
106+
const match = filename.match('^(packages(-exp)?/[a-zA-Z0-9-]+)/.*');
107+
if (match && match[1]) {
108+
const { name: pkgName } = require(resolve(
109+
root,
110+
match[1],
111+
'package.json'
112+
));
113+
if (pkgName === 'firebase-firestore-integration-test') {
114+
testTasks.push(createTestTask(pkgName, TestReasonCode.Changed));
115+
} else if (pkgName === '@firebase/firestore') {
116+
return firestorePackages.map(name => createTestTask(name));
117+
} else {
118+
// check if any package Firestore depends on has changed
119+
const firestoreDependencies = depGraph['@firebase/firestore'];
120+
121+
if (firestoreDependencies.includes(pkgName)) {
122+
return firestorePackages.map(name => createTestTask(name));
123+
}
124+
}
125+
}
126+
}
127+
128+
if (testTasks.length === 0) {
129+
console.log(
130+
chalk`{green No changes detected that would trigger firestore tests. Skipping firestore tests.}`
131+
);
132+
}
133+
134+
return testTasks;
135+
}
136+
137+
async function main() {
138+
try {
139+
const tasks = await getTestTasks();
140+
141+
if (tasks.length === 0) {
142+
process.exit(0);
143+
}
144+
145+
const lernaCmds = ['lerna', 'run', '--concurrency', '4', '--stream'];
146+
console.log(chalk`{blue Running tests in:}`);
147+
for (const { pkgName, reason } of tasks) {
148+
if (reason.code === TestReasonCode.Changed) {
149+
console.log(chalk`{yellow ${pkgName} (contains modified files)}`);
150+
} else {
151+
console.log(chalk`{yellow ${pkgName} (depends on modified files)}`);
152+
}
153+
lernaCmds.push('--scope');
154+
lernaCmds.push(pkgName);
155+
}
156+
lernaCmds.push(testCommand);
157+
158+
await spawn('npx', lernaCmds, { stdio: 'inherit', cwd: root });
159+
process.exit();
160+
} catch (e) {
161+
console.error(chalk`{red ${e}}`);
162+
process.exit(1);
163+
}
164+
}
165+
166+
main();

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
resolved "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.10.4.tgz#168da1a36e90da68ae8d49c0f1b48c7c6249213a"
88
integrity sha512-vG6SvB6oYEhvgisZNFRmRCUkLz11c7rp+tbNTynGqc6mS1d5ATd/sGyV6W0KZZnXRKMTzZDRgQT3Ou9jhpAfUg==
99
dependencies:
10-
"@babel/highlight" "^7.10.4"
10+
"@babel/highlight" "^7.10.4"
1111

1212
"@babel/compat-data@^7.10.4":
1313
version "7.10.5"

0 commit comments

Comments
 (0)