-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test: improve coverage for use command #10345
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
+128
−0
Merged
Changes from 3 commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { expect } from "chai"; | ||
| import * as sinon from "sinon"; | ||
| import { RC } from "../rc"; | ||
|
|
||
| import { command } from "./use"; | ||
| import * as projects from "../management/projects"; | ||
| import * as studio from "../management/studio"; | ||
| import * as prompt from "../prompt"; | ||
| import * as utils from "../utils"; | ||
| import * as auth from "../requireAuth"; | ||
| import * as detect from "../detectProjectRoot"; | ||
| import * as rcModule from "../rc"; | ||
| import { FirebaseProjectMetadata } from "../types/project"; | ||
|
|
||
| describe("use command", () => { | ||
| let getProjectStub: sinon.SinonStub; | ||
| let makeActiveProjectStub: sinon.SinonStub; | ||
| let detectProjectRootStub: sinon.SinonStub; | ||
|
|
||
| beforeEach(() => { | ||
| getProjectStub = sinon.stub(projects, "getProject").resolves({ | ||
| projectId: "my-project", | ||
| projectNumber: "123", | ||
| lifecycleState: "ACTIVE", | ||
| name: "projects/my-project", | ||
| createTime: "2026-04-23T00:00:00Z", | ||
| parent: { type: "organization", id: "123" }, | ||
| } as projects.ProjectInfo); | ||
| sinon.stub(projects, "listFirebaseProjects").resolves([ | ||
| { | ||
| name: "projects/my-project", | ||
| projectId: "my-project", | ||
| projectNumber: "123", | ||
| }, | ||
| ] as FirebaseProjectMetadata[]); | ||
| sinon.stub(studio, "updateStudioFirebaseProject").resolves(); | ||
| makeActiveProjectStub = sinon.stub(utils, "makeActiveProject").returns(); | ||
| sinon.stub(prompt, "select").resolves("my-project"); | ||
| sinon.stub(prompt, "input").resolves("staging"); | ||
| sinon.stub(auth, "requireAuth").resolves(); | ||
| detectProjectRootStub = sinon.stub(detect, "detectProjectRoot").returns("/path/to/project"); | ||
| sinon | ||
| .stub(rcModule, "loadRC") | ||
| .callsFake((options: { [other: string]: any; cwd?: string }) => options.rc || new RC()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it("should throw if not in a project root", async () => { | ||
| detectProjectRootStub.returns(undefined); | ||
| const options = { rc: new RC() }; | ||
| await expect(command.runner()("my-project", options)).to.be.rejectedWith( | ||
| /must be run from a Firebase project directory/, | ||
| ); | ||
| }); | ||
|
|
||
| it("should set active project for existing alias", async () => { | ||
| const rc = new RC(undefined, { projects: { staging: "my-project" } }); | ||
| const options = { rc, projectRoot: "/path/to/project" }; | ||
|
|
||
| await command.runner()("staging", options); | ||
|
|
||
| expect(makeActiveProjectStub).to.have.been.calledWith("/path/to/project", "staging"); | ||
| }); | ||
|
|
||
| it("should set active project for project ID directly", async () => { | ||
| const rc = new RC(); | ||
| const options = { rc, projectRoot: "/path/to/project" }; | ||
|
|
||
| await command.runner()("my-project", options); | ||
|
|
||
| expect(makeActiveProjectStub).to.have.been.calledWith("/path/to/project", "my-project"); | ||
| }); | ||
|
|
||
| it("should throw if alias not found and not valid project ID", async () => { | ||
| const rc = new RC(); | ||
| const options = { rc, projectRoot: "/path/to/project" }; | ||
| getProjectStub.rejects(new Error("Not found")); | ||
|
|
||
| await expect(command.runner()("nonexistent", options)).to.be.rejectedWith( | ||
| /Invalid project selection/, | ||
| ); | ||
| }); | ||
|
|
||
| it("should unalias a project", async () => { | ||
| const rc = new RC(undefined, { projects: { staging: "my-project" } }); | ||
| const options = { rc, projectRoot: "/path/to/project", unalias: "staging" }; | ||
|
|
||
| await command.runner()(undefined, options); | ||
|
|
||
| expect(rc.hasProjectAlias("staging")).to.be.false; | ||
| }); | ||
|
|
||
| it("should add a new alias interactively", async () => { | ||
| const rc = new RC(); | ||
| const options = { rc, projectRoot: "/path/to/project", add: true, interactive: true }; | ||
|
|
||
| await command.runner()(undefined, options); | ||
|
|
||
| expect(rc.resolveAlias("staging")).to.equal("my-project"); | ||
| expect(makeActiveProjectStub).to.have.been.calledWith("/path/to/project", "staging"); | ||
| }); | ||
|
|
||
| it("should clear the active project", async () => { | ||
| const rc = new RC(); | ||
| const options = { rc, projectRoot: "/path/to/project", clear: true, projectAlias: "staging" }; | ||
|
|
||
| await command.runner()(undefined, options); | ||
|
|
||
| expect(makeActiveProjectStub).to.have.been.calledWith("/path/to/project", undefined); | ||
| }); | ||
|
|
||
| it("should display generic use info if no arguments passed", async () => { | ||
| const rc = new RC(undefined, { projects: { staging: "my-project" } }); | ||
| const options = { | ||
| rc, | ||
| projectRoot: "/path/to/project", | ||
| projectAlias: "staging", | ||
| project: "my-project", | ||
| }; | ||
|
|
||
| const result = await command.runner()(undefined, options); | ||
|
|
||
| expect(result).to.equal("my-project"); | ||
| }); | ||
| }); |
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
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.
nit: This is functionally(I think) the same as
.before(requireAuth), and since we use.before(requireAuth)everywhere else, we might want to use.before(requireAuth)for consistency.