-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@angular/cli): detect ng-add schematics after install #33080
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
Open
OlimjonovOtabek
wants to merge
1
commit into
angular:main
Choose a base branch
from
OlimjonovOtabek:fix/ng-add-private-registry-schematics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| /** | ||
| * @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 { logging } from '@angular-devkit/core'; | ||
| import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import type { Argv } from 'yargs'; | ||
| import type { CommandContext } from '../../command-builder/definitions'; | ||
| import type { PackageManager, PackageManifest } from '../../package-managers'; | ||
| import AddCommandModule from './cli'; | ||
|
|
||
| describe('AddCommandModule', () => { | ||
| let root: string; | ||
| let logger: logging.Logger; | ||
|
|
||
| beforeEach(async () => { | ||
| root = await mkdtemp(join(tmpdir(), 'angular-cli-add-')); | ||
| logger = { | ||
| info: jasmine.createSpy('info'), | ||
| error: jasmine.createSpy('error'), | ||
| warn: jasmine.createSpy('warn'), | ||
| debug: jasmine.createSpy('debug'), | ||
| fatal: jasmine.createSpy('fatal'), | ||
| } as unknown as logging.Logger; | ||
|
|
||
| await writeFile(join(root, 'package.json'), '{}'); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await rm(root, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('uses the installed package manifest to detect ng-add schematics', async () => { | ||
| const packageName = '@private/package'; | ||
| const packageManager = createPackageManager({ | ||
| async add() { | ||
| await writeInstalledPackageManifest(packageName, { | ||
| name: packageName, | ||
| version: '1.0.0', | ||
| schematics: './collection.json', | ||
| }); | ||
| }, | ||
| getManifest: jasmine | ||
| .createSpy('getManifest') | ||
| .and.resolveTo({ name: packageName, version: '1.0.0' }), | ||
| }); | ||
| const command = createCommand(packageManager); | ||
| const { createSchematic } = mockSchematicWorkflow(command); | ||
|
|
||
| const result = await command.run({ | ||
| collection: `${packageName}@1.0.0`, | ||
| defaults: false, | ||
| dryRun: false, | ||
| force: false, | ||
| interactive: false, | ||
| skipConfirmation: true, | ||
| }); | ||
|
|
||
| expect(result).toBe(0); | ||
| expect(packageManager.add).toHaveBeenCalled(); | ||
| expect(createSchematic).toHaveBeenCalledWith('ng-add', true); | ||
| expect(command.executeSchematic).toHaveBeenCalledWith( | ||
| jasmine.objectContaining({ collection: packageName }), | ||
| ); | ||
| }); | ||
|
|
||
| it('uses the temporary package manifest to detect ng-add schematics', async () => { | ||
| const packageName = '@private/package'; | ||
| const workingDirectory = join(root, 'temp-install'); | ||
| const packageManager = createPackageManager({ | ||
| async acquireTempPackage() { | ||
| await writeInstalledPackageManifest( | ||
| packageName, | ||
| { | ||
| name: packageName, | ||
| version: '1.0.0', | ||
| schematics: './collection.json', | ||
| }, | ||
| workingDirectory, | ||
| ); | ||
|
|
||
| return { workingDirectory, cleanup: jasmine.createSpy('cleanup') }; | ||
| }, | ||
| getManifest: jasmine.createSpy('getManifest').and.resolveTo({ | ||
| name: packageName, | ||
| version: '1.0.0', | ||
| 'ng-add': { save: false }, | ||
| }), | ||
| }); | ||
| const command = createCommand(packageManager); | ||
| const { createSchematic } = mockSchematicWorkflow(command); | ||
|
|
||
| const result = await command.run({ | ||
| collection: `${packageName}@1.0.0`, | ||
| defaults: false, | ||
| dryRun: false, | ||
| force: false, | ||
| interactive: false, | ||
| skipConfirmation: true, | ||
| }); | ||
|
|
||
| expect(result).toBe(0); | ||
| expect(packageManager.add).not.toHaveBeenCalled(); | ||
| expect(packageManager.acquireTempPackage).toHaveBeenCalled(); | ||
| expect(createSchematic).toHaveBeenCalledWith('ng-add', true); | ||
| expect(command.executeSchematic).toHaveBeenCalledWith( | ||
| jasmine.objectContaining({ | ||
| collection: join(workingDirectory, 'node_modules', ...packageName.split('/')), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| function createCommand(packageManager: PackageManager): AddCommandModuleInternals { | ||
| const context = { | ||
| args: { | ||
| positional: [], | ||
| options: { | ||
| getYargsCompletions: false, | ||
| help: false, | ||
| jsonHelp: false, | ||
| }, | ||
| }, | ||
| currentDirectory: root, | ||
| globalConfiguration: {}, | ||
| logger, | ||
| packageManager, | ||
| root, | ||
| yargsInstance: {} as Argv, | ||
| } as unknown as CommandContext; | ||
|
|
||
| const command = new AddCommandModule(context) as unknown as AddCommandModuleInternals; | ||
| command.executeSchematic = jasmine.createSpy('executeSchematic').and.resolveTo(0); | ||
|
|
||
| return command; | ||
| } | ||
|
|
||
| function createPackageManager(options: { | ||
| acquireTempPackage?: PackageManager['acquireTempPackage']; | ||
| add?: PackageManager['add']; | ||
| getManifest: jasmine.Spy; | ||
| }): PackageManager { | ||
| const packageManager = { | ||
| acquireTempPackage: jasmine | ||
| .createSpy('acquireTempPackage') | ||
| .and.callFake(options.acquireTempPackage ?? fail), | ||
| add: jasmine.createSpy('add').and.callFake(options.add ?? fail), | ||
| getManifest: options.getManifest, | ||
| name: 'npm', | ||
| } as unknown as PackageManager; | ||
|
|
||
| return packageManager; | ||
| } | ||
|
|
||
| function mockSchematicWorkflow(command: AddCommandModuleInternals): { | ||
| createSchematic: jasmine.Spy; | ||
| } { | ||
| const createSchematic = jasmine.createSpy('createSchematic'); | ||
|
|
||
| command.getOrCreateWorkflowForBuilder = jasmine | ||
| .createSpy('getOrCreateWorkflowForBuilder') | ||
| .and.returnValue({ | ||
| engine: { | ||
| createCollection: jasmine.createSpy('createCollection').and.returnValue({ | ||
| createSchematic, | ||
| }), | ||
| }, | ||
| }); | ||
|
|
||
| return { createSchematic }; | ||
| } | ||
|
|
||
| async function writeInstalledPackageManifest( | ||
| packageName: string, | ||
| manifest: PackageManifest, | ||
| basePath = root, | ||
| ): Promise<void> { | ||
| const packagePath = join(basePath, 'node_modules', ...packageName.split('/')); | ||
|
|
||
| await mkdir(packagePath, { recursive: true }); | ||
| await writeFile(join(packagePath, 'package.json'), JSON.stringify(manifest)); | ||
| } | ||
| }); | ||
|
|
||
| type AddCommandModuleInternals = { | ||
| executeSchematic: jasmine.Spy; | ||
| getOrCreateWorkflowForBuilder: jasmine.Spy; | ||
| run: AddCommandModule['run']; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
context.collectionName ?? ''as an argument toresolvePackageJsoncan lead to unintended behavior. IfcollectionNameis undefined or an empty string,resolvePackageJson('')will attempt to resolve thepackage.jsonfile in the project root instead of a package withinnode_modules. It is safer to only attempt resolution when a valid collection name is available.