-
Notifications
You must be signed in to change notification settings - Fork 9
fix(schematics): resolve valid project template before upgrading packages #1799
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
ivanvpetrov
wants to merge
3
commits into
master
Choose a base branch
from
ipetrov/fix-angular-schematics-upgrade
base: master
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,42 @@ | ||
| import { ProjectLibrary, ProjectTemplate } from "../types"; | ||
|
|
||
| /** Returns true if the given project template is safe to use for upgrade operations, | ||
| * i.e. it's not hidden/partial-only and actually implements `upgradeIgniteUIPackages`. */ | ||
| function isUpgradeableProject(project: ProjectTemplate): boolean { | ||
| return !!project && !project.isHidden && typeof project.upgradeIgniteUIPackages === "function"; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the project template to use for Ignite UI package upgrade operations. | ||
| * | ||
| * Resolution order: | ||
| * 1. The configured `projectTemplate` id, if it exists in the library and is a valid, | ||
| * non-hidden project that implements `upgradeIgniteUIPackages`. | ||
| * 2. The first project in `library.projectIds` (declared order) that is valid, | ||
| * non-hidden, and implements `upgradeIgniteUIPackages`. | ||
| * | ||
| * This avoids blindly falling back to `projectIds[0]`, which can select hidden/partial | ||
| * project templates (e.g. `ai-config`) that don't support upgrading. | ||
| * | ||
| * @returns the resolved project template, or `null` if none could be found. | ||
| */ | ||
| export function resolveUpgradeableProject( | ||
| library: ProjectLibrary, | ||
| configuredTemplateId?: string | ||
| ): ProjectTemplate | null { | ||
| if (configuredTemplateId && library.hasProject(configuredTemplateId)) { | ||
| const configuredProject = library.getProject(configuredTemplateId); | ||
| if (isUpgradeableProject(configuredProject)) { | ||
| return configuredProject; | ||
| } | ||
| } | ||
|
|
||
| for (const id of library.projectIds) { | ||
| const project = library.getProject(id); | ||
| if (isUpgradeableProject(project)) { | ||
| return project; | ||
| } | ||
|
ivanvpetrov marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return null; | ||
| } | ||
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
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,87 @@ | ||
| import { ProjectLibrary, ProjectTemplate, resolveUpgradeableProject } from "@igniteui/cli-core"; | ||
|
|
||
| describe("Unit - resolveUpgradeableProject", () => { | ||
|
|
||
| function mockProject(id: string, options: { isHidden?: boolean; upgradeable?: boolean } = {}): ProjectTemplate { | ||
| return { | ||
| id, | ||
| isHidden: !!options.isHidden, | ||
| upgradeIgniteUIPackages: options.upgradeable === false ? undefined : jasmine.createSpy().and.returnValue(Promise.resolve(true)) | ||
| } as unknown as ProjectTemplate; | ||
| } | ||
|
|
||
| function mockLibrary(projects: { [id: string]: ProjectTemplate }, projectIds: string[]): ProjectLibrary { | ||
| return { | ||
| projectIds, | ||
| hasProject: jasmine.createSpy().and.callFake((id: string) => projectIds.indexOf(id) > -1), | ||
| getProject: jasmine.createSpy().and.callFake((id: string) => projects[id] || null) | ||
| } as unknown as ProjectLibrary; | ||
| } | ||
|
|
||
| it("returns the configured project template when it exists and is upgradeable", () => { | ||
| const emptyProject = mockProject("empty"); | ||
| const library = mockLibrary({ empty: emptyProject }, ["ai-config", "empty"]); | ||
|
|
||
| const result = resolveUpgradeableProject(library, "empty"); | ||
|
|
||
| expect(result).toBe(emptyProject); | ||
| expect(library.getProject).toHaveBeenCalledWith("empty"); | ||
| }); | ||
|
|
||
| it("skips the configured project template when it is hidden and falls back to the first upgradeable project", () => { | ||
| const hiddenConfigured = mockProject("hidden-configured", { isHidden: true }); | ||
| const emptyProject = mockProject("empty"); | ||
| const library = mockLibrary( | ||
| { "hidden-configured": hiddenConfigured, empty: emptyProject }, | ||
| ["ai-config", "empty"] | ||
| ); | ||
|
|
||
| const result = resolveUpgradeableProject(library, "hidden-configured"); | ||
|
|
||
| expect(result).toBe(emptyProject); | ||
| }); | ||
|
|
||
| it("skips hidden projects like ai-config and falls back to the first non-hidden upgradeable project", () => { | ||
| const aiConfig = mockProject("ai-config", { isHidden: true }); | ||
| const base = mockProject("base", { isHidden: true }); | ||
| const empty = mockProject("empty"); | ||
| const library = mockLibrary( | ||
| { "ai-config": aiConfig, base, empty }, | ||
| ["ai-config", "base", "empty"] | ||
| ); | ||
|
|
||
| const result = resolveUpgradeableProject(library, "invalid-configured-template"); | ||
|
|
||
| expect(result).toBe(empty); | ||
| }); | ||
|
|
||
| it("skips projects that don't implement upgradeIgniteUIPackages", () => { | ||
| const notUpgradeable = mockProject("not-upgradeable", { upgradeable: false }); | ||
| const empty = mockProject("empty"); | ||
| const library = mockLibrary( | ||
| { "not-upgradeable": notUpgradeable, empty }, | ||
| ["not-upgradeable", "empty"] | ||
| ); | ||
|
|
||
| const result = resolveUpgradeableProject(library); | ||
|
|
||
| expect(result).toBe(empty); | ||
| }); | ||
|
|
||
| it("returns null when the configured template is invalid and no project in the library is upgradeable", () => { | ||
| const aiConfig = mockProject("ai-config", { isHidden: true }); | ||
| const library = mockLibrary({ "ai-config": aiConfig }, ["ai-config"]); | ||
|
|
||
| const result = resolveUpgradeableProject(library, "invalid-configured-template"); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when the library has no projects at all", () => { | ||
| const library = mockLibrary({}, []); | ||
|
|
||
| const result = resolveUpgradeableProject(library); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.