Skip to content

Commit fd11f2b

Browse files
authored
fix(material/schematics): ensure test files are included in migration (#25757)
1 parent 319942d commit fd11f2b

24 files changed

+99
-63
lines changed

integration/mdc-migration/golden/src/app/components/autocomplete/autocomplete.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {ReactiveFormsModule} from '@angular/forms';
33
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
4-
import {MatLegacyAutocompleteModule as MatAutocompleteModule} from '@angular/material/legacy-autocomplete';
4+
import {MatAutocompleteModule} from '@angular/material/autocomplete';
55
import {AutocompleteComponent} from './autocomplete.component';
66

77
describe('AutocompleteComponent', () => {

integration/mdc-migration/golden/src/app/components/button/button.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyButtonModule as MatButtonModule} from '@angular/material/legacy-button';
3+
import {MatButtonModule} from '@angular/material/button';
44
import {ButtonComponent} from './button.component';
55

66
describe('ButtonComponent', () => {

integration/mdc-migration/golden/src/app/components/card/card.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyCardModule as MatCardModule} from '@angular/material/legacy-card';
3+
import {MatCardModule} from '@angular/material/card';
44
import {CardComponent} from './card.component';
55

66
describe('CardComponent', () => {

integration/mdc-migration/golden/src/app/components/checkbox/checkbox.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyCheckboxModule as MatCheckboxModule} from '@angular/material/legacy-checkbox';
3+
import {MatCheckboxModule} from '@angular/material/checkbox';
44
import {CheckboxComponent} from './checkbox.component';
55

66
describe('CheckboxComponent', () => {

integration/mdc-migration/golden/src/app/components/chips/chips.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyChipsModule as MatChipsModule} from '@angular/material/legacy-chips';
4-
import {MatLegacyFormFieldModule as MatFormFieldModule} from '@angular/material/legacy-form-field';
3+
import {MatChipsModule} from '@angular/material/chips';
4+
import {MatFormFieldModule} from '@angular/material/form-field';
55
import {MatIconModule} from '@angular/material/icon';
66

77
import {ChipsComponent} from './chips.component';

integration/mdc-migration/golden/src/app/components/form-field/form-field.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyFormFieldModule as MatFormFieldModule} from '@angular/material/legacy-form-field';
4-
import {MatLegacyInputModule as MatInputModule} from '@angular/material/legacy-input';
3+
import {MatFormFieldModule} from '@angular/material/form-field';
4+
import {MatInputModule} from '@angular/material/input';
55
import {FormFieldComponent} from './form-field.component';
66

77
describe('FormFieldComponent', () => {

integration/mdc-migration/golden/src/app/components/input/input.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyInputModule as MatInputModule} from '@angular/material/legacy-input';
3+
import {MatInputModule} from '@angular/material/input';
44
import {InputComponent} from './input.component';
55

66
describe('InputComponent', () => {

integration/mdc-migration/golden/src/app/components/list/list.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyListModule as MatListModule} from '@angular/material/legacy-list';
3+
import {MatListModule} from '@angular/material/list';
44
import {ListComponent} from './list.component';
55

66
describe('ListComponent', () => {

integration/mdc-migration/golden/src/app/components/menu/menu.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
33
import {MatIconModule} from '@angular/material/icon';
4-
import {MatLegacyMenuModule as MatMenuModule} from '@angular/material/legacy-menu';
4+
import {MatMenuModule} from '@angular/material/menu';
55

66
import {MenuComponent} from './menu.component';
77

integration/mdc-migration/golden/src/app/components/paginator/paginator.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
2-
import {MatLegacyPaginatorModule as MatPaginatorModule} from '@angular/material/legacy-paginator';
2+
import {MatPaginatorModule} from '@angular/material/paginator';
33
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
44
import {PaginatorComponent} from './paginator.component';
55

integration/mdc-migration/golden/src/app/components/progress-bar/progress-bar.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
2-
import {MatLegacyProgressBarModule as MatProgressBarModule} from '@angular/material/legacy-progress-bar';
2+
import {MatProgressBarModule} from '@angular/material/progress-bar';
33
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
44
import {ProgressBarComponent} from './progress-bar.component';
55

integration/mdc-migration/golden/src/app/components/progress-spinner/progress-spinner.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatProgressSpinnerModule} from '@angular/material/legacy-progress-spinner';
3+
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
44
import {ProgressSpinnerComponent} from './progress-spinner.component';
55

66
describe('ProgressSpinnerComponent', () => {

integration/mdc-migration/golden/src/app/components/radio/radio.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyRadioModule as MatRadioModule} from '@angular/material/legacy-radio';
3+
import {MatRadioModule} from '@angular/material/radio';
44
import {RadioComponent} from './radio.component';
55

66
describe('RadioComponent', () => {

integration/mdc-migration/golden/src/app/components/select/select.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacySelectModule as MatSelectModule} from '@angular/material/legacy-select';
3+
import {MatSelectModule} from '@angular/material/select';
44
import {SelectComponent} from './select.component';
55

66
describe('SelectComponent', () => {

integration/mdc-migration/golden/src/app/components/slide-toggle/slide-toggle.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatSlideToggleModule} from '@angular/material/legacy-slide-toggle';
3+
import {MatSlideToggleModule} from '@angular/material/slide-toggle';
44
import {SlideToggleComponent} from './slide-toggle.component';
55

66
describe('SlideToggleComponent', () => {

integration/mdc-migration/golden/src/app/components/slider/slider.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacySliderModule as MatSliderModule} from '@angular/material/legacy-slider';
3+
import {MatSliderModule} from '@angular/material/slider';
44
import {SliderComponent} from './slider.component';
55

66
describe('SliderComponent', () => {

integration/mdc-migration/golden/src/app/components/table/table.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatTableModule} from '@angular/material/legacy-table';
3+
import {MatTableModule} from '@angular/material/table';
44
import {TableComponent} from './table.component';
55

66
describe('TableComponent', () => {

integration/mdc-migration/golden/src/app/components/tabs/tabs.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatTabsModule} from '@angular/material/legacy-tabs';
3+
import {MatTabsModule} from '@angular/material/tabs';
44
import {TabsComponent} from './tabs.component';
55

66
describe('TabsComponent', () => {

integration/mdc-migration/golden/src/app/components/tooltip/tooltip.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ComponentFixture, TestBed} from '@angular/core/testing';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
3-
import {MatLegacyTooltipModule as MatTooltipModule} from '@angular/material/legacy-tooltip';
3+
import {MatTooltipModule} from '@angular/material/tooltip';
44
import {TooltipComponent} from './tooltip.component';
55

66
describe('TooltipComponent', () => {

integration/mdc-migration/migration-test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def migration_test(name, srcs, approve):
3131
# See: https://github.com/yarnpkg/yarn/issues/2165.
3232
# TODO(devversion): determine if a solution/workaround could live in the test runner.
3333
"yarn install --cache-folder .yarn_cache_folder/",
34-
"yarn ng generate @angular/material:mdc-migration --components all --tsconfig tsconfig.app.json",
34+
"yarn ng generate @angular/material:mdc-migration --components all",
3535
# TODO(amysorto): add back once MDC components are in @angular/material
3636
# "yarn test",
3737
" ".join([

src/cdk/schematics/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export * from './project-index-file';
1616
export * from './project-main-file';
1717
export * from './project-style-file';
1818
export * from './project-targets';
19+
export * from './project-tsconfig-paths';
1920
export * from './schematic-options';

src/material/schematics/ng-generate/mdc-migration/index.ts

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
*/
88

99
import {ComponentMigrator, MIGRATORS} from './rules';
10-
import {DevkitFileSystem, UpdateProject, findStylesheetFiles} from '@angular/cdk/schematics';
10+
import {
11+
DevkitFileSystem,
12+
UpdateProject,
13+
findStylesheetFiles,
14+
WorkspacePath,
15+
getWorkspaceConfigGracefully,
16+
getTargetTsconfigPath,
17+
} from '@angular/cdk/schematics';
1118
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
1219

1320
import {RuntimeCodeMigration} from './rules/ts-migration/runtime-migration';
1421
import {Schema} from './schema';
1522
import {TemplateMigration} from './rules/template-migration';
1623
import {ThemingStylesMigration} from './rules/theming-styles';
17-
import {dirname} from 'path';
1824

1925
/** Groups of components that must be migrated together. */
2026
const migrationGroups = [
@@ -55,48 +61,86 @@ function getComponentsToMigrate(requested: string[]): Set<string> {
5561
return componentsToMigrate;
5662
}
5763

58-
export default function (options: Schema): Rule {
59-
const componentsToMigrate = getComponentsToMigrate(options.components);
60-
const tsconfigPath = options.tsconfig;
61-
const migrationDir = options.directory ?? dirname(tsconfigPath);
62-
63-
console.log('Migrating:', [...componentsToMigrate]);
64-
console.log('Directory:', migrationDir);
64+
function runMigrations(
65+
context: SchematicContext,
66+
fileSystem: DevkitFileSystem,
67+
tsconfigPath: WorkspacePath,
68+
migrators: ComponentMigrator[],
69+
analyzedFiles: Set<WorkspacePath>,
70+
additionalStylesheetPaths: string[],
71+
): boolean {
72+
const program = UpdateProject.createProgramFromTsconfig(tsconfigPath, fileSystem);
73+
const project = new UpdateProject(context, program, fileSystem, analyzedFiles, context.logger);
74+
return !project.migrate(
75+
[ThemingStylesMigration, TemplateMigration, RuntimeCodeMigration],
76+
null,
77+
migrators,
78+
additionalStylesheetPaths,
79+
).hasFailures;
80+
}
6581

66-
const migrators: ComponentMigrator[] = [];
67-
for (let i = 0; i < MIGRATORS.length; i++) {
68-
if (componentsToMigrate.has(MIGRATORS[i].component)) {
69-
migrators.push(MIGRATORS[i]);
82+
export default function (options: Schema): Rule {
83+
return async (tree: Tree, context: SchematicContext) => {
84+
const logger = context.logger;
85+
const workspace = await getWorkspaceConfigGracefully(tree);
86+
if (workspace === null) {
87+
logger.error('Could not find workspace configuration file.');
88+
return;
7089
}
71-
}
7290

73-
return (tree: Tree, context: SchematicContext) => {
91+
const projectNames = workspace.projects.keys();
7492
const fileSystem = new DevkitFileSystem(tree);
75-
const program = UpdateProject.createProgramFromTsconfig(
76-
fileSystem.resolve(tsconfigPath),
77-
fileSystem,
78-
);
93+
const analyzedFiles = new Set<WorkspacePath>();
94+
const componentsToMigrate = getComponentsToMigrate(options.components);
95+
const migrators = MIGRATORS.filter(m => componentsToMigrate.has(m.component));
96+
let additionalStylesheetPaths = options.directory
97+
? findStylesheetFiles(tree, options.directory)
98+
: [];
99+
let success = true;
79100

80-
const additionalStylesheetPaths = findStylesheetFiles(tree, migrationDir);
81-
const project = new UpdateProject(context, program, fileSystem, new Set(), context.logger);
82-
const {hasFailures} = project.migrate(
83-
[ThemingStylesMigration, TemplateMigration, RuntimeCodeMigration],
84-
null,
85-
migrators,
86-
additionalStylesheetPaths,
87-
);
101+
logger.info(`Migrating components:\n${[...componentsToMigrate].join('\n')}`);
102+
103+
for (const projectName of projectNames) {
104+
const project = workspace.projects.get(projectName)!;
105+
const tsconfigPaths = [
106+
getTargetTsconfigPath(project, 'build'),
107+
getTargetTsconfigPath(project, 'test'),
108+
].filter((p): p is WorkspacePath => !!p);
109+
110+
if (!tsconfigPaths.length) {
111+
logger.warn(
112+
`Skipping migration for project ${projectName}. Unable to determine 'tsconfig.json' file in workspace config.`,
113+
);
114+
continue;
115+
}
116+
117+
if (!options.directory) {
118+
additionalStylesheetPaths = findStylesheetFiles(tree, project.root);
119+
}
120+
121+
logger.info(`Migrating project: ${projectName}`);
122+
123+
for (const tsconfigPath of tsconfigPaths) {
124+
success &&= runMigrations(
125+
context,
126+
fileSystem,
127+
tsconfigPath,
128+
migrators,
129+
analyzedFiles,
130+
additionalStylesheetPaths,
131+
);
132+
}
133+
}
88134

89135
// Commit all recorded edits in the update recorder. We apply the edits after all
90136
// migrations ran because otherwise offsets in the TypeScript program would be
91137
// shifted and individual migrations could no longer update the same source file.
92138
fileSystem.commitEdits();
93139

94-
if (hasFailures) {
95-
context.logger.error('Unable to migrate project. See errors above.');
140+
if (!success) {
141+
logger.error('Unable to migrate project. See errors above.');
96142
} else {
97-
context.logger.info('Successfully migrated the project.');
143+
logger.info('Successfully migrated the project.');
98144
}
99-
100-
return tree;
101145
};
102146
}

src/material/schematics/ng-generate/mdc-migration/schema.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
*/
88

99
export interface Schema {
10-
/**
11-
* Workspace-relative path to the TypeScript project that should be migrated.
12-
*/
13-
tsconfig: string;
1410
/**
1511
* Workspace-relative path to a directory which will be migrated.
1612
*

src/material/schematics/ng-generate/mdc-migration/schema.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
"title": "Material MDC Migration Schema",
55
"type": "object",
66
"properties": {
7-
"tsconfig": {
8-
"type": "string",
9-
"format": "path",
10-
"description": "Workspace-relative path to the TypeScript project that should be migrated."
11-
},
127
"directory": {
138
"type": "string",
149
"format": "path",

0 commit comments

Comments
 (0)