Skip to content

Use lerna to run test-changed tests #3589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 7, 2020
79 changes: 37 additions & 42 deletions scripts/run_changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ const fullTestTriggerFiles = [
/**
* Always run tests in these paths.
*/
const alwaysRunTestPaths = [
const alwaysRunTestPackages = [
// These tests are very fast.
'integration/browserify',
'integration/firebase-typings',
'integration/typescript',
'integration/webpack'
'firebase-browserify-test',
'firebase-package-typings-test',
'firebase-typescript-test',
'firebase-webpack-test'
];

/**
* These files trigger tests in other dirs
*/
const specialPaths = {
'scripts/emulator-testing/emulators/firestore-emulator.ts': [
'packages/firestore'
'@firebase/firestore'
],
'scripts/emulator-testing/emulators/database-emulator.ts': [
'packages/database'
'@firebase/database'
],
'scripts/emulator-testing/emulators/emulator.ts': [
'packages/firestore',
'packages/database'
'@firebase/firestore',
'@firebase/database'
],
'scripts/emulator-testing/firestore-test-runner.ts': ['packages/firestore'],
'scripts/emulator-testing/database-test-runner.ts': ['packages/database'],
'packages/firestore': ['integration/firestore']
'scripts/emulator-testing/firestore-test-runner.ts': ['@firebase/firestore'],
'scripts/emulator-testing/database-test-runner.ts': ['@firebase/database'],
'packages/firestore': ['firebase-firestore-integration-test']
};

/**
Expand Down Expand Up @@ -101,7 +101,9 @@ async function getChangedPackages() {
);
for (const matchingSpecialPath of matchingSpecialPaths) {
for (const targetPackage of specialPaths[matchingSpecialPath]) {
changedPackages[targetPackage] = 'dependency';
if (!changedPackages[targetPackage]) {
changedPackages[targetPackage] = 'dependency';
}
}
}
// Check for changed files inside package dirs.
Expand All @@ -110,7 +112,7 @@ async function getChangedPackages() {
const changedPackage = require(resolve(root, match[1], 'package.json'));
if (changedPackage) {
// Add the package itself.
changedPackages[match[1]] = 'direct';
changedPackages[changedPackage.name] = 'direct';
// Add packages that depend on it.
for (const package in depGraph) {
if (depGraph[package].includes(changedPackage.name)) {
Expand All @@ -121,9 +123,8 @@ async function getChangedPackages() {
'package.json'
));
if (depPkgJson) {
const depPath = depData.location.replace(`${root}/`, '');
if (!changedPackages[depPath]) {
changedPackages[depPath] = 'dependency';
if (!changedPackages[depPkgJson.name]) {
changedPackages[depPkgJson.name] = 'dependency';
}
}
}
Expand All @@ -145,23 +146,6 @@ async function getChangedPackages() {
}
}

/**
* Runs `yarn test` in all dirs in pathList.
* @param {Array<string>} pathList
*/
async function runTests(pathList) {
if (!pathList) return;
for (const testPath of pathList) {
try {
await spawn('yarn', ['--cwd', testPath, testCommand], {
stdio: 'inherit'
});
} catch (e) {
throw new Error(`Error running "yarn ${testCommand}" in ${testPath}.`);
}
}
}

async function main() {
try {
const { testAll, changedPackages = {} } = await getChangedPackages();
Expand All @@ -171,22 +155,33 @@ async function main() {
});
} else {
console.log(chalk`{blue Running tests in:}`);
for (const filename of alwaysRunTestPaths) {
for (const packageName of alwaysRunTestPackages) {
// array
console.log(chalk`{green ${filename} (always runs)}`);
console.log(chalk`{green ${packageName} (always runs)}`);
}
for (const filename in changedPackages) {
for (const packageName in changedPackages) {
// obj
if (changedPackages[filename] === 'direct') {
console.log(chalk`{yellow ${filename} (contains modified files)}`);
if (changedPackages[packageName] === 'direct') {
console.log(chalk`{yellow ${packageName} (contains modified files)}`);
} else {
console.log(chalk`{yellow ${filename} (depends on modified files)}`);
console.log(
chalk`{yellow ${packageName} (depends on modified files)}`
);
}
}

changedPackages['packages/app'] = 'direct';
await runTests(alwaysRunTestPaths);
await runTests(Object.keys(changedPackages));
let lernaCmds = ['lerna', 'run', '--concurrency', '4', '--stream'];
const packagesToRun = alwaysRunTestPackages.concat(
Object.keys(changedPackages)
);
for (const packageToRun of packagesToRun) {
lernaCmds.push('--scope');
lernaCmds.push(packageToRun);
}
lernaCmds.push(testCommand);
await spawn('npx', lernaCmds, { stdio: 'inherit', cwd: root });
process.exit();
}
} catch (e) {
console.error(chalk`{red ${e}}`);
Expand Down