Skip to content

Commit 394336c

Browse files
authored
refactor: fix schematics not respecting new -module.ts suffix (#31299)
Angular CLI generates `-module.ts` suffix when using `--standalone: false`. Our schematic tests generate the same, and fail currently because some files weren't picked up properly by the rule. This commit fixes this, and adds supports for the new suffix in a naive way until angular/angular-cli#30471 is available.
1 parent 951f37e commit 394336c

File tree

8 files changed

+54
-46
lines changed

8 files changed

+54
-46
lines changed

src/cdk/schematics/ng-generate/drag-drop/index.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('CDK drag-drop schematic', () => {
1212
// updating the other tests as well because `createTestApp` is responsible for creating
1313
// the project.
1414
project: 'material',
15+
module: 'app-module.ts',
1516
};
1617

1718
beforeEach(() => {
@@ -21,7 +22,7 @@ describe('CDK drag-drop schematic', () => {
2122
it('should create drag-drop files and add them to module', async () => {
2223
const app = await createTestApp(runner, {standalone: false});
2324
const tree = await runner.runSchematic('drag-drop', baseOptions, app);
24-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
25+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
2526
const files = tree.files;
2627

2728
expect(files).toContain('/projects/material/src/app/foo/foo.component.css');
@@ -36,7 +37,7 @@ describe('CDK drag-drop schematic', () => {
3637
it('should add drag-drop module', async () => {
3738
const app = await createTestApp(runner, {standalone: false});
3839
const tree = await runner.runSchematic('drag-drop', baseOptions, app);
39-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
40+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
4041

4142
expect(moduleContent).toContain('DragDropModule');
4243
});
@@ -45,7 +46,7 @@ describe('CDK drag-drop schematic', () => {
4546
it('should generate a standalone component', async () => {
4647
const app = await createTestApp(runner, {standalone: false});
4748
const tree = await runner.runSchematic('drag-drop', {...baseOptions, standalone: true}, app);
48-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
49+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
4950
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
5051

5152
expect(module).not.toContain('DragDropModule');
@@ -58,7 +59,7 @@ describe('CDK drag-drop schematic', () => {
5859
it('should generate a component with no imports and standalone false', async () => {
5960
const app = await createTestApp(runner, {standalone: false});
6061
const tree = await runner.runSchematic('drag-drop', {...baseOptions, standalone: false}, app);
61-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
62+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
6263
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
6364

6465
expect(module).toContain('DragDropModule');
@@ -74,7 +75,7 @@ describe('CDK drag-drop schematic', () => {
7475
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
7576
const test = getFileContent(tree, '/projects/material/src/app/foo/foo.component.spec.ts');
7677

77-
expect(tree.exists('/projects/material/src/app/app.module.ts')).toBe(false);
78+
expect(tree.exists('/projects/material/src/app/app-module.ts')).toBe(false);
7879

7980
expect(component).toContain('imports: [');
8081

src/cdk/schematics/utils/build-component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ export function buildComponent(
194194
options.standalone = await isStandaloneSchematic(host, options);
195195

196196
if (!options.standalone) {
197-
options.module = findModuleFromOptions(host, options);
197+
// TODO: Remove ext option when the Angular CLI looks for both candidate locations.
198+
options.module = findModuleFromOptions(host, {...options, moduleExt: 'module.ts'});
198199
}
199200

200201
const parsedPath = parseName(options.path!, options.name);

src/material/schematics/ng-generate/address-form/index.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('Material address-form schematic', () => {
99
const baseOptions: Schema = {
1010
name: 'foo',
1111
project: 'material',
12+
module: 'app-module.ts',
1213
};
1314

1415
beforeEach(() => {
@@ -25,15 +26,15 @@ describe('Material address-form schematic', () => {
2526
expect(files).toContain('/projects/material/src/app/foo/foo.component.spec.ts');
2627
expect(files).toContain('/projects/material/src/app/foo/foo.component.ts');
2728

28-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
29+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
2930
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
3031
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
3132
});
3233

3334
it('should add address-form imports to module', async () => {
3435
const app = await createTestApp(runner, {standalone: false});
3536
const tree = await runner.runSchematic('address-form', baseOptions, app);
36-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
37+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
3738

3839
expect(moduleContent).toContain('MatInputModule');
3940
expect(moduleContent).toContain('MatButtonModule');
@@ -58,7 +59,7 @@ describe('Material address-form schematic', () => {
5859
{...baseOptions, standalone: true},
5960
app,
6061
);
61-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
62+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
6263
const content = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
6364
const requiredModules = [
6465
'MatInputModule',
@@ -84,7 +85,7 @@ describe('Material address-form schematic', () => {
8485
{...baseOptions, standalone: false},
8586
app,
8687
);
87-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
88+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
8889
const content = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
8990
const requiredModules = [
9091
'MatInputModule',
@@ -108,7 +109,7 @@ describe('Material address-form schematic', () => {
108109
const tree = await runner.runSchematic('address-form', baseOptions, app);
109110
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
110111

111-
expect(tree.exists('/projects/material/src/app/app.module.ts')).toBe(false);
112+
expect(tree.exists('/projects/material/src/app/app-module.ts')).toBe(false);
112113
expect(component).toContain('imports: [');
113114
});
114115
});

src/material/schematics/ng-generate/dashboard/index.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('material-dashboard-schematic', () => {
99
const baseOptions: Schema = {
1010
name: 'foo',
1111
project: 'material',
12+
module: './app-module.ts',
1213
};
1314

1415
beforeEach(() => {
@@ -25,15 +26,15 @@ describe('material-dashboard-schematic', () => {
2526
expect(files).toContain('/projects/material/src/app/foo/foo.component.spec.ts');
2627
expect(files).toContain('/projects/material/src/app/foo/foo.component.ts');
2728

28-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
29+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
2930
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
3031
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
3132
});
3233

3334
it('should add dashboard imports to module', async () => {
3435
const app = await createTestApp(runner, {standalone: false});
3536
const tree = await runner.runSchematic('dashboard', baseOptions, app);
36-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
37+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
3738

3839
expect(moduleContent).toContain('MatGridListModule');
3940
expect(moduleContent).toContain('MatCardModule');
@@ -62,7 +63,7 @@ describe('material-dashboard-schematic', () => {
6263
it('should generate a standalone component', async () => {
6364
const app = await createTestApp(runner, {standalone: false});
6465
const tree = await runner.runSchematic('dashboard', {...baseOptions, standalone: true}, app);
65-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
66+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
6667
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
6768
const requiredModules = [
6869
'MatButtonModule',
@@ -84,7 +85,7 @@ describe('material-dashboard-schematic', () => {
8485
it('should generate a component with no imports and standalone false', async () => {
8586
const app = await createTestApp(runner, {standalone: false});
8687
const tree = await runner.runSchematic('dashboard', {...baseOptions, standalone: false}, app);
87-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
88+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
8889
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
8990
const requiredModules = [
9091
'MatButtonModule',
@@ -113,7 +114,7 @@ describe('material-dashboard-schematic', () => {
113114
'/projects/material/src/app/foo/foo.component.ts',
114115
);
115116

116-
expect(tree.exists('/projects/material/src/app/app.module.ts')).toBe(false);
117+
expect(tree.exists('/projects/material/src/app/app-module.ts')).toBe(false);
117118
expect(componentContent).toContain('imports: [');
118119
});
119120
});

src/material/schematics/ng-generate/navigation/index.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ describe('material-navigation-schematic', () => {
1010
const baseOptions: Schema = {
1111
name: 'foo',
1212
project: 'material',
13+
module: './app-module.ts',
1314
};
1415

1516
beforeEach(() => {
1617
runner = new SchematicTestRunner('schematics', COLLECTION_PATH);
1718
});
1819

1920
function expectNavigationSchematicModuleImports(tree: UnitTestTree) {
20-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
21+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
2122
expect(moduleContent).toMatch(/MatToolbarModule,\s+/);
2223
expect(moduleContent).toMatch(/MatButtonModule,\s+/);
2324
expect(moduleContent).toMatch(/MatSidenavModule,\s+/);
@@ -44,7 +45,7 @@ describe('material-navigation-schematic', () => {
4445
expect(files).toContain('/projects/material/src/app/foo/foo.component.spec.ts');
4546
expect(files).toContain('/projects/material/src/app/foo/foo.component.ts');
4647

47-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
48+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
4849
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
4950
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
5051
});
@@ -73,7 +74,7 @@ describe('material-navigation-schematic', () => {
7374
it('should generate a standalone component', async () => {
7475
const app = await createTestApp(runner, {standalone: false});
7576
const tree = await runner.runSchematic('navigation', {...baseOptions, standalone: true}, app);
76-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
77+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
7778
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
7879
const requiredModules = [
7980
'MatToolbarModule',
@@ -99,7 +100,7 @@ describe('material-navigation-schematic', () => {
99100
{...baseOptions, standalone: false},
100101
app,
101102
);
102-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
103+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
103104
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
104105
const requiredModules = [
105106
'MatToolbarModule',
@@ -128,7 +129,7 @@ describe('material-navigation-schematic', () => {
128129
'/projects/material/src/app/foo/foo.component.ts',
129130
);
130131

131-
expect(tree.exists('/projects/material/src/app/app.module.ts')).toBe(false);
132+
expect(tree.exists('/projects/material/src/app/app-module.ts')).toBe(false);
132133
expect(componentContent).toContain('imports: [');
133134
});
134135
});

src/material/schematics/ng-generate/table/index.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('material-table-schematic', () => {
99
const baseOptions: Schema = {
1010
name: 'foo',
1111
project: 'material',
12+
module: './app-module.ts',
1213
};
1314

1415
beforeEach(() => {
@@ -26,7 +27,7 @@ describe('material-table-schematic', () => {
2627
expect(files).toContain('/projects/material/src/app/foo/foo.component.ts');
2728
expect(files).toContain('/projects/material/src/app/foo/foo-datasource.ts');
2829

29-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
30+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
3031
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
3132
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
3233

@@ -49,7 +50,7 @@ describe('material-table-schematic', () => {
4950
it('should add table imports to module', async () => {
5051
const app = await createTestApp(runner, {standalone: false});
5152
const tree = await runner.runSchematic('table', baseOptions, app);
52-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
53+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
5354

5455
expect(moduleContent).toContain('MatTableModule');
5556
expect(moduleContent).toContain('MatPaginatorModule');
@@ -74,7 +75,7 @@ describe('material-table-schematic', () => {
7475
it('should generate a standalone component', async () => {
7576
const app = await createTestApp(runner, {standalone: false});
7677
const tree = await runner.runSchematic('table', {...baseOptions, standalone: true}, app);
77-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
78+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
7879
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
7980
const requiredModules = ['MatTableModule', 'MatPaginatorModule', 'MatSortModule'];
8081

@@ -90,7 +91,7 @@ describe('material-table-schematic', () => {
9091
it('should generate a component with no imports and standalone false', async () => {
9192
const app = await createTestApp(runner, {standalone: false});
9293
const tree = await runner.runSchematic('table', {...baseOptions, standalone: false}, app);
93-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
94+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
9495
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
9596
const requiredModules = ['MatTableModule', 'MatPaginatorModule', 'MatSortModule'];
9697

@@ -113,7 +114,7 @@ describe('material-table-schematic', () => {
113114
'/projects/material/src/app/foo/foo.component.ts',
114115
);
115116

116-
expect(tree.exists('/projects/material/src/app/app.module.ts')).toBe(false);
117+
expect(tree.exists('/projects/material/src/app/app-module.ts')).toBe(false);
117118
expect(componentContent).toContain('imports: [');
118119
});
119120
});

src/material/schematics/ng-generate/tree/index.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('Material tree schematic', () => {
99
const baseOptions: Schema = {
1010
name: 'foo',
1111
project: 'material',
12+
module: './app-module.ts',
1213
};
1314

1415
beforeEach(() => {
@@ -25,15 +26,15 @@ describe('Material tree schematic', () => {
2526
expect(files).toContain('/projects/material/src/app/foo/foo.component.spec.ts');
2627
expect(files).toContain('/projects/material/src/app/foo/foo.component.ts');
2728

28-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
29+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
2930
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo.component'/);
3031
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooComponent\r?\n/m);
3132
});
3233

3334
it('should add tree imports to module', async () => {
3435
const app = await createTestApp(runner, {standalone: false});
3536
const tree = await runner.runSchematic('tree', baseOptions, app);
36-
const moduleContent = getFileContent(tree, '/projects/material/src/app/app.module.ts');
37+
const moduleContent = getFileContent(tree, '/projects/material/src/app/app-module.ts');
3738

3839
expect(moduleContent).toContain('MatTreeModule');
3940
expect(moduleContent).toContain('MatIconModule');
@@ -52,7 +53,7 @@ describe('Material tree schematic', () => {
5253
it('should generate a standalone component', async () => {
5354
const app = await createTestApp(runner, {standalone: false});
5455
const tree = await runner.runSchematic('tree', {...baseOptions, standalone: true}, app);
55-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
56+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
5657
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
5758
const requiredModules = ['MatTreeModule', 'MatButtonModule', 'MatIconModule'];
5859

@@ -68,7 +69,7 @@ describe('Material tree schematic', () => {
6869
it('should generate a component with no imports and standalone false', async () => {
6970
const app = await createTestApp(runner, {standalone: false});
7071
const tree = await runner.runSchematic('tree', {...baseOptions, standalone: false}, app);
71-
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
72+
const module = getFileContent(tree, '/projects/material/src/app/app-module.ts');
7273
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
7374
const requiredModules = ['MatTreeModule', 'MatButtonModule', 'MatIconModule'];
7475

@@ -91,7 +92,7 @@ describe('Material tree schematic', () => {
9192
'/projects/material/src/app/foo/foo.component.ts',
9293
);
9394

94-
expect(tree.exists('/projects/material/src/app/app.module.ts')).toBe(false);
95+
expect(tree.exists('/projects/material/src/app/app-module.ts')).toBe(false);
9596
expect(componentContent).toContain('imports: [');
9697
});
9798
});

0 commit comments

Comments
 (0)