-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular-devkit/build-angular): option to build and test only specified spec files #13423
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
58 changes: 58 additions & 0 deletions
58
packages/angular_devkit/build_angular/src/angular-cli-files/plugins/single-test-transform.ts
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,58 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google Inc. All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.io/license | ||
| */ | ||
| import { logging } from '@angular-devkit/core'; | ||
| import { getOptions } from 'loader-utils'; | ||
| import { extname, join } from 'path'; | ||
| import { loader } from 'webpack'; | ||
|
|
||
| export interface SingleTestTransformLoaderOptions { | ||
| files: string[]; // list of paths relative to main | ||
| logger: logging.Logger; | ||
| } | ||
|
|
||
| export const SingleTestTransformLoader = require.resolve(join(__dirname, 'single-test-transform')); | ||
|
|
||
| /** | ||
| * This loader transforms the default test file to only run tests | ||
| * for some specs instead of all specs. | ||
| * It works by replacing the known content of the auto-generated test file: | ||
| * const context = require.context('./', true, /\.spec\.ts$/); | ||
| * context.keys().map(context); | ||
| * with: | ||
| * const context = { keys: () => ({ map: (_a) => { } }) }; | ||
| * context.keys().map(context); | ||
| * So that it does nothing. | ||
| * Then it adds import statements for each file in the files options | ||
| * array to import them directly, and thus run the tests there. | ||
| */ | ||
| export default function loader(this: loader.LoaderContext, source: string) { | ||
| const options = getOptions(this) as SingleTestTransformLoaderOptions; | ||
| const lineSeparator = process.platform === 'win32' ? '\r\n' : '\n'; | ||
|
|
||
| const targettedImports = options.files | ||
| .map(path => `require('./${path.replace('.' + extname(path), '')}');`) | ||
| .join(lineSeparator); | ||
|
|
||
| // TODO: maybe a documented 'marker/comment' inside test.ts would be nicer? | ||
FDIM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const regex = /require\.context\(.*/; | ||
|
|
||
| // signal the user that expected content is not present | ||
| if (!regex.test(source)) { | ||
| const message = [ | ||
| `The 'include' option requires that the 'main' file for tests include the line below:`, | ||
| `const context = require.context('./', true, /\.spec\.ts$/);`, | ||
| `Arguments passed to require.context are not strict and can be changed`, | ||
| ]; | ||
| options.logger.error(message.join(lineSeparator)); | ||
| } | ||
|
|
||
| const mockedRequireContext = '{ keys: () => ({ map: (_a) => { } }) };' + lineSeparator; | ||
| source = source.replace(regex, mockedRequireContext + targettedImports); | ||
|
|
||
| return source; | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
packages/angular_devkit/build_angular/src/angular-cli-files/utilities/find-tests.ts
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,62 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google Inc. All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.io/license | ||
| */ | ||
| import { existsSync } from 'fs'; | ||
| import * as glob from 'glob'; | ||
| import { basename, dirname, extname, join } from 'path'; | ||
| import { isDirectory } from './is-directory'; | ||
|
|
||
| // go through all patterns and find unique list of files | ||
| export function findTests(patterns: string[], cwd: string, workspaceRoot: string): string[] { | ||
| return patterns.reduce( | ||
| (files, pattern) => { | ||
| const relativePathToMain = cwd.replace(workspaceRoot, '').substr(1); // remove leading slash | ||
| const tests = findMatchingTests(pattern, cwd, relativePathToMain); | ||
| tests.forEach(file => { | ||
| if (!files.includes(file)) { | ||
| files.push(file); | ||
| } | ||
| }); | ||
|
|
||
| return files; | ||
| }, | ||
| [] as string[], | ||
| ); | ||
| } | ||
|
|
||
| function findMatchingTests(pattern: string, cwd: string, relativePathToMain: string): string[] { | ||
| // normalize pattern, glob lib only accepts forward slashes | ||
| pattern = pattern.replace(/\\/g, '/'); | ||
| relativePathToMain = relativePathToMain.replace(/\\/g, '/'); | ||
|
|
||
| // remove relativePathToMain to support relative paths from root | ||
| // such paths are easy to get when running scripts via IDEs | ||
| if (pattern.startsWith(relativePathToMain + '/')) { | ||
| pattern = pattern.substr(relativePathToMain.length + 1); // +1 to include slash | ||
| } | ||
|
|
||
| // special logic when pattern does not look like a glob | ||
| if (!glob.hasMagic(pattern)) { | ||
| if (isDirectory(join(cwd, pattern))) { | ||
| pattern = `${pattern}/**/*.spec.@(ts|tsx)`; | ||
| } else { | ||
| // see if matching spec file exists | ||
| const extension = extname(pattern); | ||
| const matchingSpec = `${basename(pattern, extension)}.spec${extension}`; | ||
|
|
||
| if (existsSync(join(cwd, dirname(pattern), matchingSpec))) { | ||
| pattern = join(dirname(pattern), matchingSpec).replace(/\\/g, '/'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const files = glob.sync(pattern, { | ||
| cwd, | ||
| }); | ||
|
|
||
| return files; | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.