-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: add test cases for update schematics #12473
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
jelbourn
merged 1 commit into
angular:master
from
devversion:build/schematics-update-tests-tslint
Aug 2, 2018
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,76 @@ | ||
import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core'; | ||
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing'; | ||
import {SchematicTestRunner} from '@angular-devkit/schematics/testing'; | ||
import {readFileSync} from 'fs'; | ||
import {createTestApp, migrationCollection, runPostScheduledTasks} from '../../utils/testing'; | ||
|
||
describe('test cases', () => { | ||
|
||
// Module name suffix for data files of the `jasmine_node_test` Bazel rule. | ||
const bazelModuleSuffix = 'angular_material/src/lib/schematics/update/test-cases'; | ||
|
||
/** | ||
* Name of test cases that will be used to verify that update schematics properly update | ||
* a developers application. | ||
*/ | ||
const testCases = [ | ||
'v5/ts-class-names' | ||
]; | ||
|
||
// Iterates through every test case directory and generates a jasmine test block that will | ||
// verify that the update schematics properly update the test input to the expected output. | ||
testCases.forEach(testCaseName => { | ||
|
||
// Adding the test case files to the data of the `jasmine_node_test` Bazel rule does not mean | ||
// that the files are being copied over to the Bazel bin output. Bazel just patches the NodeJS | ||
// resolve function and maps the module paths to the original file location. Since we | ||
// need to load the content of those test cases, we need to resolve the original file path. | ||
const inputPath = require.resolve(`${bazelModuleSuffix}/${testCaseName}_input.ts`); | ||
const expectedOutputPath = require | ||
.resolve(`${bazelModuleSuffix}/${testCaseName}_expected_output.ts`); | ||
|
||
it(`should apply update schematics to test case: ${testCaseName}`, () => { | ||
const runner = new SchematicTestRunner('schematics', migrationCollection); | ||
|
||
runner.runSchematic('migration-01', {}, createTestAppWithTestCase(inputPath)); | ||
|
||
// Run the scheduled TSLint fix task from the update schematic. This task is responsible for | ||
// identifying outdated code parts and performs the fixes. Since tasks won't run automatically | ||
// within a `SchematicTestRunner`, we manually need to run the scheduled task. | ||
return runPostScheduledTasks(runner, 'tslint-fix').toPromise().then(() => { | ||
expect(readFileContent('projects/material/src/main.ts')) | ||
.toBe(readFileContent(expectedOutputPath)); | ||
}); | ||
}); | ||
}); | ||
|
||
/** Reads the UTF8 content of the specified file. Normalizes the path and ensures that */ | ||
function readFileContent(filePath: string): string { | ||
return readFileSync(filePath, 'utf8'); | ||
} | ||
|
||
/** | ||
* Creates a test app schematic tree that includes the specified test case as TypeScript | ||
* entry point file. Also writes the app tree to a real file system location in order to be | ||
* able to test the tslint fix rules. | ||
*/ | ||
function createTestAppWithTestCase(testCaseInputPath: string) { | ||
const tempFileSystemHost = new TempScopedNodeJsSyncHost(); | ||
const appTree = createTestApp(); | ||
|
||
appTree.overwrite('/projects/material/src/main.ts', readFileContent(testCaseInputPath)); | ||
|
||
// Since the TSLint fix task expects all files to be present on the real file system, we | ||
// map every file in the app tree to a temporary location on the file system. | ||
appTree.files.map(f => normalize(f)).forEach(f => { | ||
tempFileSystemHost.sync.write(f, virtualFs.stringToFileBuffer(appTree.readContent(f))); | ||
}); | ||
|
||
// Switch to the new temporary directory because otherwise TSLint cannot read the files. | ||
process.chdir(getSystemPath(tempFileSystemHost.root)); | ||
|
||
return appTree; | ||
} | ||
}); | ||
|
||
|
14 changes: 14 additions & 0 deletions
14
src/lib/schematics/update/test-cases/v5/ts-class-names_expected_output.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,14 @@ | ||
import {CdkConnectedOverlay, CdkOverlayOrigin} from '@angular/cdk/overlay'; | ||
import {CdkObserveContent} from '@angular/cdk/observers'; | ||
import {CdkTrapFocus} from '@angular/cdk/a11y'; | ||
import {FloatLabelType, LabelOptions, MAT_LABEL_GLOBAL_OPTIONS} from '@angular/material'; | ||
|
||
const a = new CdkConnectedOverlay(); | ||
const b = new CdkOverlayOrigin(); | ||
const c = new CdkObserveContent(); | ||
const d = new CdkTrapFocus(); | ||
|
||
const e: FloatLabelType = 'test'; | ||
const f: LabelOptions = 'opt2'; | ||
|
||
const g = {provide: MAT_LABEL_GLOBAL_OPTIONS, useValue: 'test-options'}; |
14 changes: 14 additions & 0 deletions
14
src/lib/schematics/update/test-cases/v5/ts-class-names_input.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,14 @@ | ||
import {ConnectedOverlayDirective, OverlayOrigin} from '@angular/cdk/overlay'; | ||
import {ObserveContent} from '@angular/cdk/observers'; | ||
import {FocusTrapDirective} from '@angular/cdk/a11y'; | ||
import {FloatPlaceholderType, PlaceholderOptions, MAT_PLACEHOLDER_GLOBAL_OPTIONS} from '@angular/material'; | ||
|
||
const a = new ConnectedOverlayDirective(); | ||
const b = new OverlayOrigin(); | ||
const c = new ObserveContent(); | ||
const d = new FocusTrapDirective(); | ||
|
||
const e: FloatPlaceholderType = 'test'; | ||
const f: PlaceholderOptions = 'opt2'; | ||
|
||
const g = {provide: MAT_PLACEHOLDER_GLOBAL_OPTIONS, useValue: 'test-options'}; |
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
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
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.
Actually, I need to find a way to properly exclude the test cases here. Will work on that