Skip to content

Commit 5963d14

Browse files
devversionmmalerba
authored andcommitted
build(schematics): test schematics properly (#12410)
* build(schematics): test schematics properly * Previously, the spec files that have been pushed to the repository, didn't run at all, or were outdated. This PR updates the tests and also enables most of them (as much as possible). * Sets up the Bazel test rules (with a few needed workarounds) in order to test the schematics * Removed devkit utils that can be used from `@schematics/angular` which is **always present** (we can assume that) * Moved data `.json` files into TypeScript files (in order to workaround a bazel typescript issue; and to allow separation for v6 and v7 data at some point) * Updates and fixes schematics to properly work within the latest Angular CLI version * Address feedback * Remove unnecessary diff
1 parent 1f53841 commit 5963d14

File tree

71 files changed

+1408
-2379
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1408
-2379
lines changed

package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"zone.js": "^0.8.26"
4343
},
4444
"devDependencies": {
45-
"@angular-devkit/core": "^0.5.12",
46-
"@angular-devkit/schematics": "^0.5.12",
45+
"@angular-devkit/core": "^0.7.1",
46+
"@angular-devkit/schematics": "^0.7.1",
4747
"@angular/bazel": "6.0.0",
4848
"@angular/compiler-cli": "6.0.0",
4949
"@angular/http": "6.0.0",
@@ -53,7 +53,7 @@
5353
"@angular/upgrade": "6.0.0",
5454
"@bazel/ibazel": "0.3.1",
5555
"@google-cloud/storage": "^1.1.1",
56-
"@schematics/angular": "^0.5.12",
56+
"@schematics/angular": "^0.7.1",
5757
"@types/chalk": "^0.4.31",
5858
"@types/fs-extra": "^4.0.3",
5959
"@types/glob": "^5.0.33",

src/lib/schematics/BUILD.bazel

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package(default_visibility = ["//visibility:public"])
22

33
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
4-
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")
4+
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package", "jasmine_node_test")
55

6+
filegroup(
7+
name = "schematics_assets",
8+
srcs = glob(["**/files/**/*"]) + ["README.md", "collection.json", "migration.json"],
9+
)
610

711
ts_library(
812
name = "schematics",
@@ -14,8 +18,43 @@ ts_library(
1418
# This package is intended to be combined into the main @angular/material package as a dep.
1519
npm_package(
1620
name = "npm_package",
17-
srcs = [
18-
":collection.json",
19-
] + glob(["**/files/**/*", "**/data/**/*", "**/schema.json", "**/migration.json"]),
21+
srcs = [":schematics_assets"],
2022
deps = [":schematics"],
2123
)
24+
25+
### Testing rules
26+
27+
jasmine_node_test(
28+
name = "unit_tests",
29+
srcs = [":schematics_test_sources"],
30+
data = [":schematics_assets"],
31+
deps = [":copy-collection-file", ":copy-migration-file"],
32+
)
33+
34+
ts_library(
35+
name = "schematics_test_sources",
36+
srcs = glob(["**/*.spec.ts"], exclude=["**/files/**/*"]),
37+
deps = [":schematics"],
38+
tsconfig = ":tsconfig.json",
39+
testonly = True,
40+
)
41+
42+
# Workaround for https://github.com/bazelbuild/rules_typescript/issues/154
43+
genrule(
44+
name = "copy-collection-file",
45+
srcs = ["collection.json"],
46+
outs = ["test-collection.json"],
47+
cmd = "cp $< $@",
48+
output_to_bindir = True,
49+
testonly = True,
50+
)
51+
52+
# Workaround for https://github.com/bazelbuild/rules_typescript/issues/154
53+
genrule(
54+
name = "copy-migration-file",
55+
srcs = ["migration.json"],
56+
outs = ["test-migration.json"],
57+
cmd = "cp $< $@",
58+
output_to_bindir = True,
59+
testonly = True,
60+
)

src/lib/schematics/address-form/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88

99
import {chain, Rule, noop, Tree} from '@angular-devkit/schematics';
10+
import {buildComponent} from '../utils/build-component';
1011
import {Schema} from './schema';
1112
import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
12-
import {buildComponent} from '../utils/devkit-utils/component';
1313

1414
/**
1515
* Scaffolds a new table component.

src/lib/schematics/collection.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,37 @@
66
"ng-add": {
77
"description": "Adds Angular Material to the application without affecting any templates",
88
"factory": "./install",
9-
"schema": "./install/schema.json",
109
"aliases": ["material-shell"]
1110
},
1211
// Create a dashboard component
1312
"dashboard": {
1413
"description": "Create a card-based dashboard component",
1514
"factory": "./dashboard/index",
16-
"schema": "./dashboard/schema.json",
1715
"aliases": ["material-dashboard"]
1816
},
1917
// Creates a table component
2018
"table": {
2119
"description": "Create a component that displays data with a data-table",
2220
"factory": "./table/index",
23-
"schema": "./table/schema.json",
2421
"aliases": ["material-table"]
2522
},
2623
// Creates toolbar and navigation components
2724
"nav": {
2825
"description": "Create a component with a responsive sidenav for navigation",
2926
"factory": "./nav/index",
30-
"schema": "./nav/schema.json",
31-
"aliases": [ "material-nav", "materialNav" ]
27+
// TODO(devversion): re-add `materialNav` alias if we have the latest schematics version
28+
// that includes https://github.com/angular/angular-cli/pull/11390
29+
"aliases": [ "material-nav"]
3230
},
3331
// Create a file tree component
3432
"tree": {
3533
"description": "Create a file tree component.",
36-
"factory": "./tree/index",
37-
"schema": "./tree/schema.json"
34+
"factory": "./tree/index"
3835
},
3936
// Creates a address form component
4037
"addressForm": {
4138
"description": "Create a component with a address form",
4239
"factory": "./address-form/index",
43-
"schema": "./address-form/schema.json",
4440
"aliases": ["address-form"]
4541
}
4642
}

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
11
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
2-
import {join} from 'path';
3-
import {createTestApp} from '../utils/testing';
2+
import {createTestApp, collectionPath} from '../utils/testing';
43
import {getFileContent} from '@schematics/angular/utility/test';
5-
6-
const collectionPath = join(__dirname, '../collection.json');
4+
import {Schema} from './schema';
75

86
describe('material-dashboard-schematic', () => {
97
let runner: SchematicTestRunner;
10-
const options = {
8+
const options: Schema = {
119
name: 'foo',
12-
path: 'app',
13-
sourceDir: 'src',
14-
inlineStyle: false,
15-
inlineTemplate: false,
10+
project: 'material',
1611
changeDetection: 'Default',
1712
styleext: 'css',
1813
spec: true,
19-
module: undefined,
2014
export: false,
21-
prefix: undefined,
22-
viewEncapsulation: undefined,
2315
};
2416

2517
beforeEach(() => {
2618
runner = new SchematicTestRunner('schematics', collectionPath);
2719
});
2820

29-
it('should create dashboard files and add them to module', () => {
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', () => {
3025
const tree = runner.runSchematic('dashboard', { ...options }, createTestApp());
3126
const files = tree.files;
3227

33-
expect(files).toContain('/src/app/foo/foo.component.css');
34-
expect(files).toContain('/src/app/foo/foo.component.html');
35-
expect(files).toContain('/src/app/foo/foo.component.spec.ts');
36-
expect(files).toContain('/src/app/foo/foo.component.ts');
28+
expect(files).toContain('/projects/material/src/app/foo/foo.component.css');
29+
expect(files).toContain('/projects/material/src/app/foo/foo.component.html');
30+
expect(files).toContain('/projects/material/src/app/foo/foo.component.spec.ts');
31+
expect(files).toContain('/projects/material/src/app/foo/foo.component.ts');
3732

38-
const moduleContent = getFileContent(tree, '/src/app/app.module.ts');
33+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
3934
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
4035
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
4136
});
4237

4338
it('should add dashboard imports to module', () => {
44-
const tree = runner.runSchematic('materialDashboard', { ...options }, createTestApp());
45-
const moduleContent = getFileContent(tree, '/src/app/app.module.ts');
39+
const tree = runner.runSchematic('dashboard', { ...options }, createTestApp());
40+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
4641

4742
expect(moduleContent).toContain('MatGridListModule');
4843
expect(moduleContent).toContain('MatCardModule');

src/lib/schematics/dashboard/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {chain, Rule, noop, Tree} from '@angular-devkit/schematics';
10-
import {Schema} from './schema';
9+
import {chain, noop, Rule, Tree} from '@angular-devkit/schematics';
1110
import {addModuleImportToModule, findModuleFromOptions} from '../utils/ast';
12-
import {buildComponent} from '../utils/devkit-utils/component';
11+
import {buildComponent} from '../utils/build-component';
12+
import {Schema} from './schema';
1313

1414
/**
1515
* Scaffolds a new dashboard component.

src/lib/schematics/install/custom-theme.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Project} from '../utils/devkit-utils/config';
10-
119
/** Create custom theme for the given application configuration. */
12-
export function createCustomTheme(project: Project) {
13-
const name = project.name || 'app';
10+
export function createCustomTheme(name: string = 'app') {
1411
return `
1512
// Custom Theming for Angular Material
1613
// For more information: https://material.angular.io/guide/theming
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import {Tree} from '@angular-devkit/schematics';
22
import {SchematicTestRunner} from '@angular-devkit/schematics/testing';
3-
import {join} from 'path';
3+
import {getProjectFromWorkspace} from '../utils/get-project';
44
import {getFileContent} from '@schematics/angular/utility/test';
5-
import {createTestApp} from '../utils/testing';
6-
import {getConfig, getAppFromConfig} from '@schematics/angular/utility/config';
5+
import {collectionPath, createTestApp} from '../utils/testing';
6+
import {getConfig, getAppFromConfig, getWorkspace} from '@schematics/angular/utility/config';
77
import {getIndexHtmlPath} from '../utils/ast';
88
import {normalize} from '@angular-devkit/core';
9-
import { getWorkspace, getProjectFromWorkspace } from '../utils/devkit-utils/config';
109

11-
const collectionPath = join(__dirname, '../collection.json');
12-
13-
describe('material-shell-schematic', () => {
10+
describe('material-install-schematic', () => {
1411
let runner: SchematicTestRunner;
1512
let appTree: Tree;
1613

@@ -20,47 +17,50 @@ describe('material-shell-schematic', () => {
2017
});
2118

2219
it('should update package.json', () => {
23-
const tree = runner.runSchematic('install', {}, appTree);
20+
const tree = runner.runSchematic('ng-add', {}, appTree);
2421
const packageJson = JSON.parse(getFileContent(tree, '/package.json'));
2522

2623
expect(packageJson.dependencies['@angular/material']).toBeDefined();
2724
expect(packageJson.dependencies['@angular/cdk']).toBeDefined();
2825
});
2926

3027
it('should add default theme', () => {
31-
const tree = runner.runSchematic('shell', {}, appTree);
32-
const config: any = getConfig(tree);
33-
config.apps.forEach(app => {
34-
expect(app.styles).toContain(
35-
'../node_modules/@angular/material/prebuilt-themes/indigo-pink.css');
36-
});
28+
const tree = runner.runSchematic('ng-add', {}, appTree);
29+
30+
const workspace = getWorkspace(tree);
31+
const project = getProjectFromWorkspace(workspace);
32+
33+
console.log(tree.files);
34+
35+
expect(project.architect!['build']).toBeTruthy();
36+
expect(project.architect!['build']['options']).toBeTruthy();
37+
expect(project.architect!['build']['options']['styles']).toContain(
38+
'./node_modules/@angular/material/prebuilt-themes/indigo-pink.css');
3739
});
3840

3941
it('should add custom theme', () => {
40-
const tree = runner.runSchematic('install', {
41-
theme: 'custom'
42-
}, appTree);
42+
const tree = runner.runSchematic('ng-add', {theme: 'custom'}, appTree);
4343

44-
const config = getConfig(tree);
45-
const app: any = getAppFromConfig(config, '0');
46-
const stylesPath = normalize(`/${app.root}/styles.scss`);
44+
const workspace = getWorkspace(tree);
45+
const project = getProjectFromWorkspace(workspace);
46+
const expectedStylesPath = normalize(`/${project.root}/src/styles.scss`);
4747

48-
const buffer: any = tree.read(stylesPath);
49-
const src = buffer.toString();
48+
const buffer = tree.read(expectedStylesPath);
49+
const src = buffer!.toString();
5050

5151
expect(src.indexOf(`@import '~@angular/material/theming';`)).toBeGreaterThan(-1);
5252
expect(src.indexOf(`$app-primary`)).toBeGreaterThan(-1);
5353
});
5454

5555
it('should add font links', () => {
56-
const tree = runner.runSchematic('install', {}, appTree);
57-
const config: any = getConfig(tree);
56+
const tree = runner.runSchematic('ng-add', {}, appTree);
5857
const workspace = getWorkspace(tree);
59-
const project = getProjectFromWorkspace(workspace, config.project.name);
58+
const project = getProjectFromWorkspace(workspace);
6059

6160
const indexPath = getIndexHtmlPath(project);
6261
const buffer: any = tree.read(indexPath);
6362
const indexSrc = buffer.toString();
63+
6464
expect(indexSrc.indexOf('fonts.googleapis.com')).toBeGreaterThan(-1);
6565
});
6666
});

src/lib/schematics/install/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import {
1616
} from '@angular-devkit/schematics';
1717
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
1818
import {addModuleImportToRootModule, getStylesPath} from '../utils/ast';
19-
import {InsertChange} from '../utils/devkit-utils/change';
20-
import {getProjectFromWorkspace, getWorkspace} from '../utils/devkit-utils/config';
19+
import {InsertChange} from '@schematics/angular/utility/change';
20+
import {getWorkspace} from '@schematics/angular/utility/config';
21+
import {getProjectFromWorkspace} from '../utils/get-project';
2122
import {addHeadLink} from '../utils/html';
2223
import {angularVersion, materialVersion} from '../utils/lib-versions';
2324
import {addPackageToPackageJson} from '../utils/package';

0 commit comments

Comments
 (0)