Skip to content

refactor(@schematics/angular): remove unneeded getFileContent unit-test helper #30939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { parse as parseJson } from 'jsonc-parser';
import { getFileContent } from '../../angular/utility/test';
import { Schema as ComponentOptions } from '../component/schema';
import { latestVersions } from '../utility/latest-versions';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as GenerateLibrarySchema } from './schema';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getJsonFileContent(tree: UnitTestTree, path: string): any {
return parseJson(tree.readContent(path).toString());
return tree.readJson(path);
}

describe('Library Schematic', () => {
Expand Down Expand Up @@ -119,7 +118,7 @@ describe('Library Schematic', () => {
it('should create a package.json named "foo"', async () => {
const tree = await schematicRunner.runSchematic('library', defaultOptions, workspaceTree);

const fileContent = getFileContent(tree, '/projects/foo/package.json');
const fileContent = tree.readText('/projects/foo/package.json');
expect(fileContent).toMatch(/"name": "foo"/);
});

Expand All @@ -134,14 +133,14 @@ describe('Library Schematic', () => {
it('should add sideEffects: false flag to package.json named "foo"', async () => {
const tree = await schematicRunner.runSchematic('library', defaultOptions, workspaceTree);

const fileContent = getFileContent(tree, '/projects/foo/package.json');
const fileContent = tree.readText('/projects/foo/package.json');
expect(fileContent).toMatch(/"sideEffects": false/);
});

it('should create a README.md named "foo"', async () => {
const tree = await schematicRunner.runSchematic('library', defaultOptions, workspaceTree);

const fileContent = getFileContent(tree, '/projects/foo/README.md');
const fileContent = tree.readText('/projects/foo/README.md');
expect(fileContent).toMatch(/# Foo/);
});

Expand Down Expand Up @@ -425,7 +424,7 @@ describe('Library Schematic', () => {
workspaceTree,
);

const fileContent = getFileContent(tree, '/projects/foo/src/lib/foo-module.ts');
const fileContent = tree.readText('/projects/foo/src/lib/foo-module.ts');
expect(fileContent).toMatch(/exports: \[\n(\s*) {2}Foo\n\1\]/);
});

Expand Down
16 changes: 8 additions & 8 deletions packages/schematics/angular/pipe/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as ApplicationOptions } from '../application/schema';
import { createAppModule, getFileContent } from '../utility/test';
import { createAppModule } from '../utility/test';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as PipeOptions } from './schema';

Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Pipe Schematic', () => {
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo-pipe.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo-pipe.ts');
const moduleContent = getFileContent(tree, '/projects/bar/src/app/app-module.ts');
const moduleContent = tree.readText('/projects/bar/src/app/app-module.ts');
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo-pipe'/);
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooPipe\r?\n/m);
const fileContent = tree.readContent('/projects/bar/src/app/foo-pipe.ts');
Expand All @@ -77,7 +77,7 @@ describe('Pipe Schematic', () => {
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo.pipe.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo.pipe.ts');
const moduleContent = getFileContent(tree, '/projects/bar/src/app/app-module.ts');
const moduleContent = tree.readText('/projects/bar/src/app/app-module.ts');
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo.pipe'/);
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooPipe\r?\n/m);
const fileContent = tree.readContent('/projects/bar/src/app/foo.pipe.ts');
Expand All @@ -96,7 +96,7 @@ describe('Pipe Schematic', () => {
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo-pipe.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo-pipe.ts');
const moduleContent = getFileContent(tree, '/projects/bar/src/app/app-module.ts');
const moduleContent = tree.readText('/projects/bar/src/app/app-module.ts');
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo-pipe'/);
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooPipe\r?\n/m);
const fileContent = tree.readContent('/projects/bar/src/app/foo-pipe.ts');
Expand All @@ -107,7 +107,7 @@ describe('Pipe Schematic', () => {
const options = { ...defaultNonStandaloneOptions, module: 'app-module.ts' };

const tree = await schematicRunner.runSchematic('pipe', options, appTree);
const appModule = getFileContent(tree, '/projects/bar/src/app/app-module.ts');
const appModule = tree.readText('/projects/bar/src/app/app-module.ts');

expect(appModule).toMatch(/import { FooPipe } from '.\/foo-pipe'/);
});
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('Pipe Schematic', () => {
const options = { ...defaultNonStandaloneOptions, export: true };

const tree = await schematicRunner.runSchematic('pipe', options, appTree);
const appModuleContent = getFileContent(tree, '/projects/bar/src/app/app-module.ts');
const appModuleContent = tree.readText('/projects/bar/src/app/app-module.ts');
expect(appModuleContent).toMatch(/exports: \[\n(\s*) {2}FooPipe\n\1\]/);
});

Expand All @@ -150,7 +150,7 @@ describe('Pipe Schematic', () => {
const files = tree.files;
expect(files).toContain('/projects/bar/src/app/foo/foo-pipe.spec.ts');
expect(files).toContain('/projects/bar/src/app/foo/foo-pipe.ts');
const moduleContent = getFileContent(tree, '/projects/bar/src/app/app-module.ts');
const moduleContent = tree.readText('/projects/bar/src/app/app-module.ts');
expect(moduleContent).toMatch(/import.*Foo.*from '.\/foo\/foo-pipe'/);
expect(moduleContent).toMatch(/declarations:\s*\[[^\]]+?,\r?\n\s+FooPipe\r?\n/m);
});
Expand All @@ -161,7 +161,7 @@ describe('Pipe Schematic', () => {
const newTree = createAppModule(appTree, routingModulePath);
const options = { ...defaultNonStandaloneOptions, module: routingFileName };
const tree = await schematicRunner.runSchematic('pipe', options, newTree);
const content = getFileContent(tree, routingModulePath);
const content = tree.readText(routingModulePath);
expect(content).toMatch(/import { FooPipe } from '.\/foo-pipe/);
});

Expand Down
3 changes: 1 addition & 2 deletions packages/schematics/angular/utility/ast-utils_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { tags } from '@angular-devkit/core';
import { HostTree } from '@angular-devkit/schematics';
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
import { Change, InsertChange } from '../utility/change';
import { getFileContent } from '../utility/test';
import {
addDeclarationToModule,
addExportToModule,
Expand Down Expand Up @@ -38,7 +37,7 @@ function applyChanges(path: string, content: string, changes: Change[]): string
}
tree.commitUpdate(exportRecorder);

return getFileContent(tree, path);
return tree.readText(path);
}

describe('ast utils', () => {
Expand Down
19 changes: 0 additions & 19 deletions packages/schematics/angular/utility/test/get-file-content.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/schematics/angular/utility/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
*/

export * from './create-app-module';
export * from './get-file-content';
Loading