From 54060104a7c01c69be4715b50692a554c4af7164 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 00:22:09 +0000 Subject: [PATCH] refactor(test): dedupe gemini oauth fixture setup --- .../google-gemini-cli-auth/oauth.test.ts | 211 ++++++------------ 1 file changed, 65 insertions(+), 146 deletions(-) diff --git a/extensions/google-gemini-cli-auth/oauth.test.ts b/extensions/google-gemini-cli-auth/oauth.test.ts index 334e297014b..018eae78dd6 100644 --- a/extensions/google-gemini-cli-auth/oauth.test.ts +++ b/extensions/google-gemini-cli-auth/oauth.test.ts @@ -35,6 +35,67 @@ describe("extractGeminiCliCredentials", () => { let originalPath: string | undefined; + function makeFakeLayout() { + const binDir = join(rootDir, "fake", "bin"); + const geminiPath = join(binDir, "gemini"); + const resolvedPath = join( + rootDir, + "fake", + "lib", + "node_modules", + "@google", + "gemini-cli", + "dist", + "index.js", + ); + const oauth2Path = join( + rootDir, + "fake", + "lib", + "node_modules", + "@google", + "gemini-cli", + "node_modules", + "@google", + "gemini-cli-core", + "dist", + "src", + "code_assist", + "oauth2.js", + ); + + return { binDir, geminiPath, resolvedPath, oauth2Path }; + } + + function installGeminiLayout(params: { + oauth2Exists?: boolean; + oauth2Content?: string; + readdir?: string[]; + }) { + const layout = makeFakeLayout(); + process.env.PATH = layout.binDir; + + mockExistsSync.mockImplementation((p: string) => { + const normalized = normalizePath(p); + if (normalized === normalizePath(layout.geminiPath)) { + return true; + } + if (params.oauth2Exists && normalized === normalizePath(layout.oauth2Path)) { + return true; + } + return false; + }); + mockRealpathSync.mockReturnValue(layout.resolvedPath); + if (params.oauth2Content !== undefined) { + mockReadFileSync.mockReturnValue(params.oauth2Content); + } + if (params.readdir) { + mockReaddirSync.mockReturnValue(params.readdir); + } + + return layout; + } + beforeEach(async () => { vi.clearAllMocks(); originalPath = process.env.PATH; @@ -54,48 +115,7 @@ describe("extractGeminiCliCredentials", () => { }); it("extracts credentials from oauth2.js in known path", async () => { - const fakeBinDir = join(rootDir, "fake", "bin"); - const fakeGeminiPath = join(fakeBinDir, "gemini"); - const fakeResolvedPath = join( - rootDir, - "fake", - "lib", - "node_modules", - "@google", - "gemini-cli", - "dist", - "index.js", - ); - const fakeOauth2Path = join( - rootDir, - "fake", - "lib", - "node_modules", - "@google", - "gemini-cli", - "node_modules", - "@google", - "gemini-cli-core", - "dist", - "src", - "code_assist", - "oauth2.js", - ); - - process.env.PATH = fakeBinDir; - - mockExistsSync.mockImplementation((p: string) => { - const normalized = normalizePath(p); - if (normalized === normalizePath(fakeGeminiPath)) { - return true; - } - if (normalized === normalizePath(fakeOauth2Path)) { - return true; - } - return false; - }); - mockRealpathSync.mockReturnValue(fakeResolvedPath); - mockReadFileSync.mockReturnValue(FAKE_OAUTH2_CONTENT); + installGeminiLayout({ oauth2Exists: true, oauth2Content: FAKE_OAUTH2_CONTENT }); const { extractGeminiCliCredentials, clearCredentialsCache } = await import("./oauth.js"); clearCredentialsCache(); @@ -108,26 +128,7 @@ describe("extractGeminiCliCredentials", () => { }); it("returns null when oauth2.js cannot be found", async () => { - const fakeBinDir = join(rootDir, "fake", "bin"); - const fakeGeminiPath = join(fakeBinDir, "gemini"); - const fakeResolvedPath = join( - rootDir, - "fake", - "lib", - "node_modules", - "@google", - "gemini-cli", - "dist", - "index.js", - ); - - process.env.PATH = fakeBinDir; - - mockExistsSync.mockImplementation( - (p: string) => normalizePath(p) === normalizePath(fakeGeminiPath), - ); - mockRealpathSync.mockReturnValue(fakeResolvedPath); - mockReaddirSync.mockReturnValue([]); // Empty directory for recursive search + installGeminiLayout({ oauth2Exists: false, readdir: [] }); const { extractGeminiCliCredentials, clearCredentialsCache } = await import("./oauth.js"); clearCredentialsCache(); @@ -135,48 +136,7 @@ describe("extractGeminiCliCredentials", () => { }); it("returns null when oauth2.js lacks credentials", async () => { - const fakeBinDir = join(rootDir, "fake", "bin"); - const fakeGeminiPath = join(fakeBinDir, "gemini"); - const fakeResolvedPath = join( - rootDir, - "fake", - "lib", - "node_modules", - "@google", - "gemini-cli", - "dist", - "index.js", - ); - const fakeOauth2Path = join( - rootDir, - "fake", - "lib", - "node_modules", - "@google", - "gemini-cli", - "node_modules", - "@google", - "gemini-cli-core", - "dist", - "src", - "code_assist", - "oauth2.js", - ); - - process.env.PATH = fakeBinDir; - - mockExistsSync.mockImplementation((p: string) => { - const normalized = normalizePath(p); - if (normalized === normalizePath(fakeGeminiPath)) { - return true; - } - if (normalized === normalizePath(fakeOauth2Path)) { - return true; - } - return false; - }); - mockRealpathSync.mockReturnValue(fakeResolvedPath); - mockReadFileSync.mockReturnValue("// no credentials here"); + installGeminiLayout({ oauth2Exists: true, oauth2Content: "// no credentials here" }); const { extractGeminiCliCredentials, clearCredentialsCache } = await import("./oauth.js"); clearCredentialsCache(); @@ -184,48 +144,7 @@ describe("extractGeminiCliCredentials", () => { }); it("caches credentials after first extraction", async () => { - const fakeBinDir = join(rootDir, "fake", "bin"); - const fakeGeminiPath = join(fakeBinDir, "gemini"); - const fakeResolvedPath = join( - rootDir, - "fake", - "lib", - "node_modules", - "@google", - "gemini-cli", - "dist", - "index.js", - ); - const fakeOauth2Path = join( - rootDir, - "fake", - "lib", - "node_modules", - "@google", - "gemini-cli", - "node_modules", - "@google", - "gemini-cli-core", - "dist", - "src", - "code_assist", - "oauth2.js", - ); - - process.env.PATH = fakeBinDir; - - mockExistsSync.mockImplementation((p: string) => { - const normalized = normalizePath(p); - if (normalized === normalizePath(fakeGeminiPath)) { - return true; - } - if (normalized === normalizePath(fakeOauth2Path)) { - return true; - } - return false; - }); - mockRealpathSync.mockReturnValue(fakeResolvedPath); - mockReadFileSync.mockReturnValue(FAKE_OAUTH2_CONTENT); + installGeminiLayout({ oauth2Exists: true, oauth2Content: FAKE_OAUTH2_CONTENT }); const { extractGeminiCliCredentials, clearCredentialsCache } = await import("./oauth.js"); clearCredentialsCache();