Skip to content

Commit 7756882

Browse files
devversionjosephperrott
authored andcommitted
refactor(schematics): run tests for template files (#12562)
1 parent f8b70cd commit 7756882

File tree

19 files changed

+143
-92
lines changed

19 files changed

+143
-92
lines changed

src/lib/schematics/BUILD.bazel

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ filegroup(
1212
ts_library(
1313
name = "schematics",
1414
module_name = "@angular/material/schematics",
15-
srcs = glob(["**/*.ts"], exclude=["**/*.spec.ts", "update/test-cases/**/*.ts", "**/files/**/*"]),
15+
srcs = glob(["**/*.ts"], exclude=[
16+
"**/*.spec.ts",
17+
"**/files/**/*",
18+
"test-setup/**/*",
19+
"update/test-cases/**/*",
20+
]),
1621
tsconfig = ":tsconfig.json",
1722
)
1823

@@ -30,12 +35,11 @@ jasmine_node_test(
3035
name = "unit_tests",
3136
srcs = [":schematics_test_sources"],
3237
data = [":schematics_assets", ":schematics_test_cases"],
33-
deps = [":copy-collection-file", ":copy-migration-file"],
3438
)
3539

3640
ts_library(
3741
name = "schematics_test_sources",
38-
srcs = glob(["**/*.spec.ts"], exclude=["**/files/**/*"]),
42+
srcs = glob(["**/*.spec.ts", "test-setup/**/*.ts"], exclude=["**/files/**/*"]),
3943
deps = [":schematics"],
4044
tsconfig = ":tsconfig.json",
4145
testonly = True,
@@ -46,23 +50,3 @@ filegroup(
4650
srcs = glob(["update/test-cases/**/*_input.ts", "update/test-cases/**/*_expected_output.ts"]),
4751
testonly = True,
4852
)
49-
50-
# Workaround for https://github.com/bazelbuild/rules_typescript/issues/154
51-
genrule(
52-
name = "copy-collection-file",
53-
srcs = ["collection.json"],
54-
outs = ["test-collection.json"],
55-
cmd = "cp $< $@",
56-
output_to_bindir = True,
57-
testonly = True,
58-
)
59-
60-
# Workaround for https://github.com/bazelbuild/rules_typescript/issues/154
61-
genrule(
62-
name = "copy-migration-file",
63-
srcs = ["migration.json"],
64-
outs = ["test-migration.json"],
65-
cmd = "cp $< $@",
66-
output_to_bindir = True,
67-
testonly = True,
68-
)

src/lib/schematics/address-form/schema.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"project": {
1414
"type": "string",
1515
"description": "The name of the project.",
16-
"visible": false
16+
"$default": {
17+
"$source": "projectName"
18+
}
1719
},
1820
"name": {
1921
"type": "string",
@@ -88,6 +90,12 @@
8890
"type": "boolean",
8991
"default": false,
9092
"description": "Specifies if declaring module exports the component."
93+
},
94+
"entryComponent": {
95+
"type": "boolean",
96+
"default": false,
97+
"description": "Specifies if the component is an entry component of declaring module."
9198
}
92-
}
99+
},
100+
"required": []
93101
}

src/lib/schematics/collection.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,43 @@
66
"ng-add": {
77
"description": "Adds Angular Material to the application without affecting any templates",
88
"factory": "./install",
9+
"schema": "./install/schema.json",
910
"aliases": ["material-shell"]
1011
},
1112
// Create a dashboard component
1213
"dashboard": {
1314
"description": "Create a card-based dashboard component",
1415
"factory": "./dashboard/index",
16+
"schema": "./dashboard/schema.json",
1517
"aliases": ["material-dashboard"]
1618
},
1719
// Creates a table component
1820
"table": {
1921
"description": "Create a component that displays data with a data-table",
2022
"factory": "./table/index",
23+
"schema": "./table/schema.json",
2124
"aliases": ["material-table"]
2225
},
2326
// Creates toolbar and navigation components
2427
"nav": {
2528
"description": "Create a component with a responsive sidenav for navigation",
2629
"factory": "./nav/index",
30+
"schema": "./nav/schema.json",
2731
// TODO(devversion): re-add `materialNav` alias if we have the latest schematics version
2832
// that includes https://github.com/angular/angular-cli/pull/11390
2933
"aliases": [ "material-nav"]
3034
},
3135
// Create a file tree component
3236
"tree": {
3337
"description": "Create a file tree component.",
34-
"factory": "./tree/index"
38+
"factory": "./tree/index",
39+
"schema": "./tree/schema.json"
3540
},
3641
// Creates a address form component
3742
"addressForm": {
3843
"description": "Create a component with a address form",
3944
"factory": "./address-form/index",
45+
"schema": "./address-form/schema.json",
4046
"aliases": ["address-form"]
4147
}
4248
}

src/lib/schematics/dashboard/index.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
2-
import {createTestApp, collectionPath} from '../utils/testing';
2+
import {collectionPath, createTestApp} from '../test-setup/test-app';
33
import {getFileContent} from '@schematics/angular/utility/test';
44
import {Schema} from './schema';
55

@@ -18,10 +18,7 @@ describe('material-dashboard-schematic', () => {
1818
runner = new SchematicTestRunner('schematics', collectionPath);
1919
});
2020

21-
// TODO(devversion): Temporarily disabled because @angular-devkit/schematics is not able to
22-
// find the template files for the schematic. As soon as we find a way to properly reference
23-
// those files, we can re-enable this test.
24-
xit('should create dashboard files and add them to module', () => {
21+
it('should create dashboard files and add them to module', () => {
2522
const tree = runner.runSchematic('dashboard', { ...options }, createTestApp());
2623
const files = tree.files;
2724

src/lib/schematics/dashboard/schema.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"project": {
1414
"type": "string",
1515
"description": "The name of the project.",
16-
"visible": false,
1716
"$default": {
1817
"$source": "projectName"
1918
}
@@ -91,6 +90,11 @@
9190
"type": "boolean",
9291
"default": false,
9392
"description": "Specifies if declaring module exports the component."
93+
},
94+
"entryComponent": {
95+
"type": "boolean",
96+
"default": false,
97+
"description": "Specifies if the component is an entry component of declaring module."
9498
}
9599
},
96100
"required": []

src/lib/schematics/install/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Tree} from '@angular-devkit/schematics';
22
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
33
import {getProjectFromWorkspace} from '../utils/get-project';
44
import {getFileContent} from '@schematics/angular/utility/test';
5-
import {collectionPath, createTestApp} from '../utils/testing';
5+
import {collectionPath, createTestApp} from '../test-setup/test-app';
66
import {getWorkspace} from '@schematics/angular/utility/config';
77
import {getIndexHtmlPath} from '../utils/ast';
88
import {normalize} from '@angular-devkit/core';

src/lib/schematics/nav/index.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
22
import {Schema} from './schema';
33
import {getFileContent} from '@schematics/angular/utility/test';
4-
import {collectionPath, createTestApp} from '../utils/testing';
4+
import {collectionPath, createTestApp} from '../test-setup/test-app';
55

66
describe('material-nav-schematic', () => {
77
let runner: SchematicTestRunner;
@@ -19,10 +19,7 @@ describe('material-nav-schematic', () => {
1919
runner = new SchematicTestRunner('schematics', collectionPath);
2020
});
2121

22-
// TODO(devversion): Temporarily disabled because @angular-devkit/schematics is not able to
23-
// find the template files for the schematic. As soon as we find a way to properly reference
24-
// those files, we can re-enable this test.
25-
xit('should create nav files and add them to module', () => {
22+
it('should create nav files and add them to module', () => {
2623
const tree = runner.runSchematic('nav', { ...options }, createTestApp());
2724
const files = tree.files;
2825

src/lib/schematics/nav/schema.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"project": {
1414
"type": "string",
1515
"description": "The name of the project.",
16-
"visible": false,
1716
"$default": {
1817
"$source": "projectName"
1918
}
@@ -91,6 +90,11 @@
9190
"type": "boolean",
9291
"default": false,
9392
"description": "Specifies if declaring module exports the component."
93+
},
94+
"entryComponent": {
95+
"type": "boolean",
96+
"default": false,
97+
"description": "Specifies if the component is an entry component of declaring module."
9498
}
9599
},
96100
"required": []

src/lib/schematics/table/index.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
22
import {Schema} from './schema';
33
import {getFileContent} from '@schematics/angular/utility/test';
4-
import {collectionPath, createTestApp} from '../utils/testing';
4+
import {collectionPath, createTestApp} from '../test-setup/test-app';
55

66
describe('material-table-schematic', () => {
77
let runner: SchematicTestRunner;
@@ -19,10 +19,7 @@ describe('material-table-schematic', () => {
1919
runner = new SchematicTestRunner('schematics', collectionPath);
2020
});
2121

22-
// TODO(devversion): Temporarily disabled because @angular-devkit/schematics is not able to
23-
// find the template files for the schematic. As soon as we find a way to properly reference
24-
// those files, we can re-enable this test.
25-
xit('should create table files and add them to module', () => {
22+
it('should create table files and add them to module', () => {
2623
const tree = runner.runSchematic('table', { ...options }, createTestApp());
2724
const files = tree.files;
2825

src/lib/schematics/table/schema.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"project": {
1414
"type": "string",
1515
"description": "The name of the project.",
16-
"visible": false,
1716
"$default": {
1817
"$source": "projectName"
1918
}
@@ -91,6 +90,11 @@
9190
"type": "boolean",
9291
"default": false,
9392
"description": "Specifies if declaring module exports the component."
93+
},
94+
"entryComponent": {
95+
"type": "boolean",
96+
"default": false,
97+
"description": "Specifies if the component is an entry component of declaring module."
9498
}
9599
},
96100
"required": []
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/*
10+
* NOTE: This file will run before the actual tests start inside of Bazel.
11+
*
12+
* It automatically runs before all spec files because the spec files are blocked
13+
* until Jasmine runs the `describe` blocks.
14+
*
15+
* We copy all needed files into the proper Bazel bin output in order to be able to test
16+
* the schematics. Workaround for: https://github.com/bazelbuild/rules_typescript/issues/154
17+
*/
18+
19+
import {sync as globSync} from 'glob';
20+
import {dirname, join} from 'path';
21+
import {copySync} from 'fs-extra';
22+
23+
// Adding the test case files to the data of the `jasmine_node_test` Bazel rule does not mean
24+
// that the files are being copied over to the Bazel bin output. Bazel just patches the NodeJS
25+
// resolve function and maps the module paths to the original file location. Since we want to copy
26+
// the files to the bazel test directory because TSLint and the schematic test runner expect a real
27+
// file system, we need to resolve the original file path through a Bazel mapped file.
28+
const sourceDirectory = dirname(
29+
require.resolve('angular_material/src/lib/schematics/collection.json'));
30+
31+
const bazelBinDir = join(__dirname, '../');
32+
33+
// Copy all schema files to the bazel bin directory.
34+
globSync('**/schema.json', {cwd: sourceDirectory})
35+
.forEach(file => copySync(join(sourceDirectory, file), join(bazelBinDir, file)));
36+
37+
// Copy all template files to the bazel bin directory.
38+
globSync('**/files/**/*', {cwd: sourceDirectory})
39+
.forEach(file => copySync(join(sourceDirectory, file), join(bazelBinDir, file)));
40+
41+
// Copy the collection.json and migration.json file to the bazel bin directory.
42+
globSync('+(collection|migration).json', {cwd: sourceDirectory})
43+
.forEach(file => copySync(join(sourceDirectory, file), join(bazelBinDir, file)));

src/lib/schematics/utils/testing.ts renamed to src/lib/schematics/test-setup/post-scheduled-tasks.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,10 @@
77
*/
88

99
import {EngineHost, TaskScheduler} from '@angular-devkit/schematics';
10-
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
11-
import {join} from 'path';
10+
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
1211
import {from as observableFrom, Observable} from 'rxjs';
1312
import {concatMap, filter, last} from 'rxjs/operators';
1413

15-
/** Path to the test collection file for the Material schematics */
16-
export const collectionPath = join(__dirname, '..', 'test-collection.json');
17-
18-
/** Path to the test migration file for the Material update schematics */
19-
export const migrationCollection = join(__dirname, '..', 'test-migration.json');
20-
21-
/** Create a base app used for testing. */
22-
export function createTestApp(): UnitTestTree {
23-
const baseRunner = new SchematicTestRunner('material-schematics', collectionPath);
24-
25-
const workspaceTree = baseRunner.runExternalSchematic('@schematics/angular', 'workspace', {
26-
name: 'workspace',
27-
version: '6.0.0',
28-
newProjectRoot: 'projects',
29-
});
30-
31-
return baseRunner.runExternalSchematic('@schematics/angular', 'application', {
32-
name: 'material',
33-
inlineStyle: false,
34-
inlineTemplate: false,
35-
routing: false,
36-
style: 'scss',
37-
skipTests: false,
38-
}, workspaceTree);
39-
}
40-
4114
/**
4215
* Due to the fact that the Angular devkit does not support running scheduled tasks from a
4316
* schematic that has been launched through the TestRunner, we need to manually find the task
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
10+
import {join} from 'path';
11+
12+
/** Path to the collection file for the Material schematics */
13+
export const collectionPath = join(__dirname, '..', 'collection.json');
14+
15+
/** Path to the migration file for the Material update schematics */
16+
export const migrationCollection = join(__dirname, '..', 'migration.json');
17+
18+
/** Create a base app used for testing. */
19+
export function createTestApp(): UnitTestTree {
20+
const baseRunner = new SchematicTestRunner('material-schematics', collectionPath);
21+
22+
const workspaceTree = baseRunner.runExternalSchematic('@schematics/angular', 'workspace', {
23+
name: 'workspace',
24+
version: '6.0.0',
25+
newProjectRoot: 'projects',
26+
});
27+
28+
return baseRunner.runExternalSchematic('@schematics/angular', 'application', {
29+
name: 'material',
30+
inlineStyle: false,
31+
inlineTemplate: false,
32+
routing: false,
33+
style: 'scss',
34+
skipTests: false,
35+
}, workspaceTree);
36+
}

src/lib/schematics/tree/schema.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"project": {
1414
"type": "string",
1515
"description": "The name of the project.",
16-
"visible": false
16+
"$default": {
17+
"$source": "projectName"
18+
}
1719
},
1820
"name": {
1921
"type": "string",
@@ -88,6 +90,12 @@
8890
"type": "boolean",
8991
"default": false,
9092
"description": "Specifies if declaring module exports the component."
93+
},
94+
"entryComponent": {
95+
"type": "boolean",
96+
"default": false,
97+
"description": "Specifies if the component is an entry component of declaring module."
9198
}
92-
}
99+
},
100+
"required": []
93101
}

0 commit comments

Comments
 (0)