-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: add a script to flag missing unit tests in MDC packages #20471
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import {readdirSync, readFileSync} from 'fs'; | ||
import {join, basename} from 'path'; | ||
import {sync as glob} from 'glob'; | ||
import chalk from 'chalk'; | ||
import * as ts from 'typescript'; | ||
|
||
const srcDirectory = join(__dirname, '../src'); | ||
const materialDirectories = readdirSync(join(srcDirectory, 'material')); | ||
|
||
// Goes through all the unit tests and flags the ones that don't exist in the MDC components. | ||
readdirSync(join(srcDirectory, 'material-experimental'), {withFileTypes: true}) | ||
.reduce((matches, entity) => { | ||
// Go through all the `material-experimental` directories and match them to ones in `material`. | ||
if (entity.isDirectory()) { | ||
const materialName = entity.name.replace(/^mdc-/, ''); | ||
|
||
if (materialDirectories.indexOf(materialName) > -1) { | ||
matches.set(materialName, entity.name); | ||
} | ||
} | ||
|
||
return matches; | ||
}, new Map<string, string>()) | ||
.forEach((mdcPackage, materialPackage) => { | ||
const mdcTestFiles = getUnitTestFiles(`material-experimental/${mdcPackage}`); | ||
|
||
// MDC entry points that don't have test files may not have been implemented yet. | ||
if (mdcTestFiles.length > 0) { | ||
// Filter out files that don't exist in the MDC package, allowing | ||
// us to ignore some files which may not need to be ported to MDC. | ||
const materialTestFiles = getUnitTestFiles(`material/${materialPackage}`).filter(path => { | ||
const fileName = basename(path); | ||
return mdcTestFiles.some(file => basename(file) === fileName); | ||
}); | ||
const materialTests = getTestNames(materialTestFiles); | ||
const mdcTests = getTestNames(mdcTestFiles); | ||
const missingTests = materialTests.filter(test => !mdcTests.includes(test)); | ||
|
||
if (missingTests.length > 0) { | ||
console.log(chalk.redBright(`\nMissing tests for ${mdcPackage}:`)); | ||
console.log(missingTests.join('\n')); | ||
} | ||
} | ||
}); | ||
|
||
/** | ||
* Gets all the names of all unit test files inside a | ||
* package name, excluding `testing` packages and e2e tests. | ||
*/ | ||
function getUnitTestFiles(name: string): string[] { | ||
return glob('{,!(testing)/**/}!(*.e2e).spec.ts', { | ||
absolute: true, | ||
cwd: join(srcDirectory, name) | ||
}); | ||
} | ||
|
||
/** Gets the name of all unit tests within a set of files. */ | ||
function getTestNames(files: string[]): string[] { | ||
const testNames: string[] = []; | ||
|
||
files.forEach(file => { | ||
const content = readFileSync(file, 'utf-8'); | ||
const sourceFile = ts.createSourceFile(file, content, ts.ScriptTarget.ES2015); | ||
|
||
sourceFile.forEachChild(function walk(node: ts.Node) { | ||
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && | ||
node.expression.text === 'it') { | ||
// Note that this is a little naive since it'll take the literal text of the test | ||
// name expression which could include things like string concatenation. It's fine | ||
// for the limited use cases of the script. | ||
testNames.push(node.arguments[0].getText(sourceFile) | ||
// Replace the quotes around the test name. | ||
.replace(/^['`]|['`]$/g, '') | ||
// Strip newlines followed by indentation. | ||
.replace(/\n\s+/g, ' ') | ||
// Strip escape characters. | ||
.replace(/\\/g, '') | ||
// Collapse concatenated strings. | ||
.replace(/['`]\s+\+\s+['`]/g, '')); | ||
} else { | ||
node.forEachChild(walk); | ||
} | ||
}); | ||
}); | ||
|
||
return testNames; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"outDir": "../dist/dev-infra-scripts", | ||
"target": "es2015", | ||
"lib": ["es2016"], | ||
"moduleResolution": "node", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was necessary, because vscode was flagging any imports to |
||
"types": ["node"], | ||
"strictNullChecks": true, | ||
"downlevelIteration": true | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this something manually to be run, or can we wire it up in CI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is to eventually run it on the CI, but I wanted to add all the missing tests first and it still needs a way to exclude some tests that are irrelevant for the MDC version.