diff --git a/packages/schematics/angular/library/index_spec.ts b/packages/schematics/angular/library/index_spec.ts index 97e517767f1f..ecf628577f11 100644 --- a/packages/schematics/angular/library/index_spec.ts +++ b/packages/schematics/angular/library/index_spec.ts @@ -8,7 +8,6 @@ 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'; @@ -16,7 +15,7 @@ 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', () => { @@ -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"/); }); @@ -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/); }); @@ -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\]/); }); diff --git a/packages/schematics/angular/pipe/index_spec.ts b/packages/schematics/angular/pipe/index_spec.ts index 51f890e54ee1..677995a4bab9 100644 --- a/packages/schematics/angular/pipe/index_spec.ts +++ b/packages/schematics/angular/pipe/index_spec.ts @@ -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'; @@ -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'); @@ -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'); @@ -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'); @@ -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'/); }); @@ -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\]/); }); @@ -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); }); @@ -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/); }); diff --git a/packages/schematics/angular/utility/ast-utils_spec.ts b/packages/schematics/angular/utility/ast-utils_spec.ts index b5e2cda692c5..43259ccd6c8e 100644 --- a/packages/schematics/angular/utility/ast-utils_spec.ts +++ b/packages/schematics/angular/utility/ast-utils_spec.ts @@ -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, @@ -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', () => { diff --git a/packages/schematics/angular/utility/test/get-file-content.ts b/packages/schematics/angular/utility/test/get-file-content.ts deleted file mode 100644 index 18cbb746e8f8..000000000000 --- a/packages/schematics/angular/utility/test/get-file-content.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @license - * Copyright Google LLC All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.dev/license - */ - -import { Tree } from '@angular-devkit/schematics'; - -export function getFileContent(tree: Tree, path: string): string { - const fileEntry = tree.get(path); - - if (!fileEntry) { - throw new Error(`The file (${path}) does not exist.`); - } - - return fileEntry.content.toString(); -} diff --git a/packages/schematics/angular/utility/test/index.ts b/packages/schematics/angular/utility/test/index.ts index 03261cb3a222..b9b32c0dad2a 100644 --- a/packages/schematics/angular/utility/test/index.ts +++ b/packages/schematics/angular/utility/test/index.ts @@ -7,4 +7,3 @@ */ export * from './create-app-module'; -export * from './get-file-content';