|
1 | | -import {getSystemPath, normalize, virtualFs} from '@angular-devkit/core'; |
| 1 | +import {getSystemPath, normalize} from '@angular-devkit/core'; |
2 | 2 | import {TempScopedNodeJsSyncHost} from '@angular-devkit/core/node/testing'; |
3 | | -import {SchematicTestRunner} from '@angular-devkit/schematics/testing'; |
| 3 | +import * as virtualFs from '@angular-devkit/core/src/virtual-fs/host'; |
4 | 4 | import {readFileSync} from 'fs'; |
5 | | -import {runPostScheduledTasks} from '../../test-setup/post-scheduled-tasks'; |
6 | | -import {createTestApp, migrationCollection} from '../../test-setup/test-app'; |
7 | | - |
8 | | -describe('test cases', () => { |
9 | | - |
10 | | - // Module name suffix for data files of the `jasmine_node_test` Bazel rule. |
11 | | - const bazelModuleSuffix = 'angular_material/src/lib/schematics/update/test-cases'; |
12 | | - |
13 | | - /** |
14 | | - * Name of test cases that will be used to verify that update schematics properly update |
15 | | - * a developers application. |
16 | | - */ |
17 | | - const testCases = [ |
18 | | - 'v5/attribute-selectors', |
19 | | - ]; |
20 | | - |
21 | | - // Iterates through every test case directory and generates a jasmine test block that will |
22 | | - // verify that the update schematics properly update the test input to the expected output. |
23 | | - testCases.forEach(testCaseName => { |
24 | | - |
25 | | - // Adding the test case files to the data of the `jasmine_node_test` Bazel rule does not mean |
26 | | - // that the files are being copied over to the Bazel bin output. Bazel just patches the NodeJS |
27 | | - // resolve function and maps the module paths to the original file location. Since we |
28 | | - // need to load the content of those test cases, we need to resolve the original file path. |
29 | | - const inputPath = require.resolve(`${bazelModuleSuffix}/${testCaseName}_input.ts`); |
30 | | - const expectedOutputPath = require |
31 | | - .resolve(`${bazelModuleSuffix}/${testCaseName}_expected_output.ts`); |
32 | | - |
33 | | - it(`should apply update schematics to test case: ${testCaseName}`, () => { |
34 | | - const runner = new SchematicTestRunner('schematics', migrationCollection); |
35 | | - |
36 | | - runner.runSchematic('migration-01', {}, createTestAppWithTestCase(inputPath)); |
37 | | - |
38 | | - // Run the scheduled TSLint fix task from the update schematic. This task is responsible for |
39 | | - // identifying outdated code parts and performs the fixes. Since tasks won't run automatically |
40 | | - // within a `SchematicTestRunner`, we manually need to run the scheduled task. |
41 | | - return runPostScheduledTasks(runner, 'tslint-fix').toPromise().then(() => { |
42 | | - expect(readFileContent('projects/material/src/main.ts')) |
43 | | - .toBe(readFileContent(expectedOutputPath)); |
44 | | - }); |
45 | | - }); |
| 5 | +import {createTestApp} from '../../test-setup/test-app'; |
| 6 | + |
| 7 | +/** Module name suffix for data files of the `jasmine_node_test` Bazel rule. */ |
| 8 | +const bazelModuleSuffix = 'angular_material/src/lib/schematics/update/test-cases'; |
| 9 | + |
| 10 | +/** Reads the UTF8 content of the specified file. Normalizes the path and ensures that */ |
| 11 | +export function readFileContent(filePath: string): string { |
| 12 | + return readFileSync(filePath, 'utf8'); |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Resolves the original file path of the specified file that has been to the `data` of the |
| 17 | + * jasmine_node_test Bazel rule. |
| 18 | + * |
| 19 | + * Adding the test case files to the data of the `jasmine_node_test` Bazel rule does not mean |
| 20 | + * that the files are being copied over to the Bazel bin output. Bazel just patches the NodeJS |
| 21 | + * resolve function and maps the module paths to the original file location. |
| 22 | + */ |
| 23 | +export function resolveBazelDataFile(filePath: string) { |
| 24 | + return require.resolve(`${bazelModuleSuffix}/${filePath}`); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Creates a test app schematic tree that includes the specified test case as TypeScript |
| 29 | + * entry point file. Also writes the app tree to a real file system location in order to be |
| 30 | + * able to test the tslint fix rules. |
| 31 | + */ |
| 32 | +export function createTestAppWithTestCase(testCaseInputPath: string) { |
| 33 | + const tempFileSystemHost = new TempScopedNodeJsSyncHost(); |
| 34 | + const appTree = createTestApp(); |
| 35 | + |
| 36 | + appTree.overwrite('/projects/material/src/main.ts', readFileContent(testCaseInputPath)); |
| 37 | + |
| 38 | + // Since the TSLint fix task expects all files to be present on the real file system, we |
| 39 | + // map every file in the app tree to a temporary location on the file system. |
| 40 | + appTree.files.map(f => normalize(f)).forEach(f => { |
| 41 | + tempFileSystemHost.sync.write(f, virtualFs.stringToFileBuffer(appTree.readContent(f))); |
46 | 42 | }); |
47 | 43 |
|
48 | | - /** Reads the UTF8 content of the specified file. Normalizes the path and ensures that */ |
49 | | - function readFileContent(filePath: string): string { |
50 | | - return readFileSync(filePath, 'utf8'); |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * Creates a test app schematic tree that includes the specified test case as TypeScript |
55 | | - * entry point file. Also writes the app tree to a real file system location in order to be |
56 | | - * able to test the tslint fix rules. |
57 | | - */ |
58 | | - function createTestAppWithTestCase(testCaseInputPath: string) { |
59 | | - const tempFileSystemHost = new TempScopedNodeJsSyncHost(); |
60 | | - const appTree = createTestApp(); |
61 | | - |
62 | | - appTree.overwrite('/projects/material/src/main.ts', readFileContent(testCaseInputPath)); |
63 | | - |
64 | | - // Since the TSLint fix task expects all files to be present on the real file system, we |
65 | | - // map every file in the app tree to a temporary location on the file system. |
66 | | - appTree.files.map(f => normalize(f)).forEach(f => { |
67 | | - tempFileSystemHost.sync.write(f, virtualFs.stringToFileBuffer(appTree.readContent(f))); |
68 | | - }); |
69 | | - |
70 | | - // Switch to the new temporary directory because otherwise TSLint cannot read the files. |
71 | | - process.chdir(getSystemPath(tempFileSystemHost.root)); |
72 | | - |
73 | | - return appTree; |
74 | | - } |
75 | | -}); |
76 | | - |
| 44 | + // Switch to the new temporary directory because otherwise TSLint cannot read the files. |
| 45 | + process.chdir(getSystemPath(tempFileSystemHost.root)); |
77 | 46 |
|
| 47 | + return appTree; |
| 48 | +} |
0 commit comments