Skip to content

Commit c38c72c

Browse files
committed
Address feedback
1 parent b89428c commit c38c72c

File tree

7 files changed

+36
-27
lines changed

7 files changed

+36
-27
lines changed

src/lib/schematics/BUILD.bazel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ filegroup(
1111
ts_library(
1212
name = "schematics",
1313
module_name = "@angular/material/schematics",
14-
srcs = glob(["**/*.ts"], exclude=["**/*.spec.ts", "**/*.fixture.ts", "**/files/**/*"]),
14+
srcs = glob(["**/*.ts"], exclude=["**/*.spec.ts", "update/tests/**/*.ts", "**/files/**/*"]),
1515
tsconfig = ":tsconfig.json",
1616
)
1717

@@ -27,7 +27,7 @@ npm_package(
2727
jasmine_node_test(
2828
name = "unit_tests",
2929
srcs = [":schematics_test_sources"],
30-
data = [":schematics_assets", ":schematics_test_fixtures"],
30+
data = [":schematics_assets", ":schematics_test_cases"],
3131
deps = [":copy-collection-file", ":copy-migration-file"],
3232
)
3333

@@ -40,8 +40,8 @@ ts_library(
4040
)
4141

4242
filegroup(
43-
name = "schematics_test_fixtures",
44-
srcs = glob(["update/tests/**/*.fixture"]),
43+
name = "schematics_test_cases",
44+
srcs = glob(["update/tests/**/input.ts", "update/tests/**/expected_output.ts"]),
4545
testonly = True,
4646
)
4747

src/lib/schematics/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
]
1616
},
1717
"exclude": [
18+
"**/*.spec.ts",
19+
// Exclude template files that will be copied by the schematics. Those are not valid TS.
1820
"*/files/**/*",
19-
"**/*spec*"
21+
// Exclude test-case files inside of the update tests directory.
22+
"update/tests/**/*"
2023
]
2124
}

src/lib/schematics/update/tests/run-fixtures.spec.ts renamed to src/lib/schematics/update/tests/test-cases.spec.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@ import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core';
22
import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing';
33
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
44
import {readFileSync} from 'fs';
5-
import {join} from 'path';
5+
import {join, dirname} from 'path';
66
import {createTestApp, migrationCollection, runPostScheduledTasks} from '../../utils/testing';
77

8-
describe('update fixtures', () => {
8+
describe('test cases', () => {
99

1010
/**
11-
* Path to directories that contain a testing fixture and result fixture. These fixtures
12-
* will be used to verify the update schematic.
11+
* Path to directories that contain a test case for the update schematics. These test cases
12+
* will be used to verify that update schematics properly update developer's applications.
1313
*/
14-
const fixturesDirectories = [
14+
const testCaseDirs = [
1515
'angular_material/src/lib/schematics/update/tests/class-names',
1616
];
1717

18-
// Iterates through every fixture directory and generates a jasmine test block that will verify
19-
// that the update schematics properly update the test fixture to the expected fixture.
20-
fixturesDirectories.forEach(fixtureDirPath => {
18+
// Iterates through every test case directory and generates a jasmine test block that will
19+
// verify that the update schematics properly update the test input to the expected output.
20+
testCaseDirs.forEach(testCaseDir => {
2121

22-
// Adding the fixture files to the data of the `jasmine_node_test` Bazel rule does not mean
23-
// that the fixtures are being copied over to the bin output. Bazel just patches the NodeJS
24-
// resolve function in order to map the module paths to the original file location. Since we
25-
// need to load the content of those fixtures, we need to resolve the original file path.
26-
const testFixturePath = require.resolve(join(fixtureDirPath, 'test.fixture'));
27-
const expectedFixturePath = require.resolve(join(fixtureDirPath, 'expected.fixture'));
22+
// Adding the test case files to the data of the `jasmine_node_test` Bazel rule does not mean
23+
// that the files are being copied over to the Bazel bin output. Bazel just patches the NodeJS
24+
// resolve function and maps the module paths to the original file location. Since we
25+
// need to load the content of those test cases, we need to resolve the original file path.
26+
const inputPath = require.resolve(join(testCaseDir, 'input.ts'));
27+
const expectedOutputPath = require.resolve(join(testCaseDir, 'expected_output.ts'));
2828

29-
it(`should properly update fixture "${fixtureDirPath}" with the update schematic`, () => {
29+
it(`should apply update schematics to test case: ${dirname(testCaseDir)}`, () => {
3030
const runner = new SchematicTestRunner('schematics', migrationCollection);
3131

32-
runner.runSchematic('migration-01', {}, createTestAppWithFixture(testFixturePath));
32+
runner.runSchematic('migration-01', {}, createTestAppWithTestCase(inputPath));
3333

3434
// Run the scheduled TSLint fix task from the update schematic. This task is responsible for
3535
// identifying outdated code parts and performs the fixes. Since tasks won't run automatically
3636
// within a `SchematicTestRunner`, we manually need to run the scheduled task.
3737
return runPostScheduledTasks(runner, 'tslint-fix').toPromise().then(() => {
3838
expect(readFileContent('projects/material/src/main.ts'))
39-
.toBe(readFileContent(expectedFixturePath));
39+
.toBe(readFileContent(expectedOutputPath));
4040
});
4141
});
4242
});
@@ -47,14 +47,15 @@ describe('update fixtures', () => {
4747
}
4848

4949
/**
50-
* Creates a test app schematic tree that includes the specified fixture as TypeScript
51-
* entry point file.
50+
* Creates a test app schematic tree that includes the specified test case as TypeScript
51+
* entry point file. Also writes the app tree to a real file system location in order to be
52+
* able to test the tslint fix rules.
5253
*/
53-
function createTestAppWithFixture(fixturePath: string) {
54+
function createTestAppWithTestCase(testCaseInputPath: string) {
5455
const tempFileSystemHost = new TempScopedNodeJsSyncHost();
5556
const appTree = createTestApp();
5657

57-
appTree.overwrite('/projects/material/src/main.ts', readFileContent(fixturePath));
58+
appTree.overwrite('/projects/material/src/main.ts', readFileContent(testCaseInputPath));
5859

5960
// Since the TSLint fix task expects all files to be present on the real file system, we
6061
// map every file in the app tree to a temporary location on the file system.

src/lib/schematics/update/update.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ export function postUpdate(): Rule {
7474
' be automatically fixed.');
7575
}
7676

77-
/** Gets the first tsconfig path from possible locations based on the history of the CLI. */
77+
/**
78+
* Gets all tsconfig paths from a CLI project by reading the workspace configuration
79+
* and looking for common tsconfig locations.
80+
*/
7881
function getTsConfigPaths(tree: Tree): string[] {
7982
// Start with some tsconfig paths that are generally used.
8083
const tsconfigPaths = [

src/lib/schematics/utils/testing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export function createTestApp(): UnitTestTree {
4949
export function runPostScheduledTasks(runner: SchematicTestRunner, taskName: string)
5050
: Observable<void> {
5151

52+
// Workaround until there is a public API to run scheduled tasks in the @angular-devkit.
53+
// See: https://github.com/angular/angular-cli/issues/11739
5254
const host = runner.engine['_host'] as EngineHost<{}, {}>;
5355
const tasks = runner.engine['_taskSchedulers'] as TaskScheduler[];
5456

0 commit comments

Comments
 (0)