Skip to content

Improve logic for detecting what PR tests to run #2615

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 4 commits into from
Feb 12, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 65 additions & 11 deletions scripts/run_changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

const { resolve } = require('path');
const { spawn } = require('child-process-promise');
const { spawn, exec } = require('child-process-promise');
const chalk = require('chalk');
const simpleGit = require('simple-git/promise');

Expand Down Expand Up @@ -52,35 +52,82 @@ const alwaysRunTestPaths = [
'integration/webpack'
];

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

/**
* Identify modified packages that require tests.
*/
async function getChangedPackages() {
const packageInfo = JSON.parse(
(await exec('npx lerna ls --json', { cwd: root })).stdout
);
const depGraph = JSON.parse(
(await exec('npx lerna ls --graph', { cwd: root })).stdout
);
const diff = await git.diff(['--name-only', 'origin/master...HEAD']);
const changedFiles = diff.split('\n');
const changedPackages = {};
for (const filename of changedFiles) {
// Files that trigger full test suite.
if (fullTestTriggerFiles.includes(filename)) {
console.log(
chalk`{blue Running all tests because ${filename} was modified.}`
);
return { testAll: true };
}
// Files outside a package dir that should trigger its tests.
if (specialPaths[filename]) {
for (const targetPackage of specialPaths[filename]) {
changedPackages[targetPackage] = 'dependency';
}
}
// Check for changed files inside package dirs.
const match = filename.match('^(packages/[a-zA-Z0-9-]+)/.*');
if (match && match[1]) {
const pkg = require(resolve(root, match[1], 'package.json'));
if (pkg && pkg.scripts.test) {
changedPackages[match[1]] = true;
const changedPackage = require(resolve(root, match[1], 'package.json'));
if (changedPackage && changedPackage.scripts.test) {
// Add the package itself.
changedPackages[match[1]] = 'direct';
// Add packages that depend on it.
for (const package in depGraph) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we run tests for depending packages even if changedPackage.scripts.test evaluates to false? something like:

if (changedPackage) {
  if (changedPackage.scripts.test) {
      changedPackages[match[1]] = 'direct';
  }

  for (const package in depGraph) {
  ...
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fixed.

if (depGraph[package].includes(changedPackage.name)) {
const depData = packageInfo.find(item => item.name === package);
if (depData) {
const depPkgJson = require(resolve(
depData.location,
'package.json'
));
if (depPkgJson && depPkgJson.scripts.test) {
const depPath = depData.location.replace(`${root}/`, '');
if (!changedPackages[depPath]) {
changedPackages[depPath] = 'dependency';
}
}
}
}
}
}
}
}
if (Object.keys(changedPackages).length > 0) {
return { testAll: false, packageDirs: Object.keys(changedPackages) };
return {
testAll: false,
changedPackages
};
} else {
console.log(
chalk`{green No changes detected in any package. Skipping all package-specific tests.}`
);
return { testAll: false, packageDirs: [] };
return { testAll: false };
}
}

Expand All @@ -103,21 +150,28 @@ async function runTests(pathList) {

async function main() {
try {
const { testAll, packageDirs = [] } = await getChangedPackages();
const {
testAll,
changedPackages = {}
} = await getChangedPackages();
if (testAll) {
await spawn('yarn', ['test'], {
stdio: 'inherit'
});
} else {
console.log(chalk`{blue Running tests in:}`);
for (const filename of alwaysRunTestPaths) {
for (const filename of alwaysRunTestPaths) { // array
console.log(chalk`{green ${filename} (always runs)}`);
}
for (const filename of packageDirs) {
console.log(chalk`{yellow ${filename} (contains modified files)}`);
for (const filename in changedPackages) { // obj
if (changedPackages[filename] === 'direct') {
console.log(chalk`{yellow ${filename} (contains modified files)}`);
} else {
console.log(chalk`{yellow ${filename} (depends on modified files)}`);
}
}
await runTests(alwaysRunTestPaths);
await runTests(packageDirs);
await runTests(Object.keys(changedPackages));
}
} catch (e) {
console.error(e);
Expand Down