Make oauth2 test windows compatible. (#4895)

This commit is contained in:
Tommaso Sciortino 2025-07-25 14:29:54 -07:00 committed by GitHub
parent eb65034117
commit 65aabfede8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 37 deletions

View File

@ -255,13 +255,6 @@ describe('oauth2', () => {
let mockComputeClient: Compute; let mockComputeClient: Compute;
beforeEach(() => { beforeEach(() => {
vi.spyOn(os, 'homedir').mockReturnValue('/user/home');
vi.spyOn(fs.promises, 'mkdir').mockResolvedValue(undefined);
vi.spyOn(fs.promises, 'writeFile').mockResolvedValue(undefined);
vi.spyOn(fs.promises, 'readFile').mockRejectedValue(
new Error('File not found'),
); // Default to no cached creds
mockGetAccessToken.mockResolvedValue({ token: 'test-access-token' }); mockGetAccessToken.mockResolvedValue({ token: 'test-access-token' });
mockComputeClient = { mockComputeClient = {
credentials: { refresh_token: 'test-refresh-token' }, credentials: { refresh_token: 'test-refresh-token' },
@ -273,9 +266,9 @@ describe('oauth2', () => {
it('should attempt to load cached credentials first', async () => { it('should attempt to load cached credentials first', async () => {
const cachedCreds = { refresh_token: 'cached-token' }; const cachedCreds = { refresh_token: 'cached-token' };
vi.spyOn(fs.promises, 'readFile').mockResolvedValue( const credsPath = path.join(tempHomeDir, '.gemini', 'oauth_creds.json');
JSON.stringify(cachedCreds), await fs.promises.mkdir(path.dirname(credsPath), { recursive: true });
); await fs.promises.writeFile(credsPath, JSON.stringify(cachedCreds));
const mockClient = { const mockClient = {
setCredentials: vi.fn(), setCredentials: vi.fn(),
@ -291,10 +284,6 @@ describe('oauth2', () => {
await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig); await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig);
expect(fs.promises.readFile).toHaveBeenCalledWith(
'/user/home/.gemini/oauth_creds.json',
'utf-8',
);
expect(mockClient.setCredentials).toHaveBeenCalledWith(cachedCreds); expect(mockClient.setCredentials).toHaveBeenCalledWith(cachedCreds);
expect(mockClient.getAccessToken).toHaveBeenCalled(); expect(mockClient.getAccessToken).toHaveBeenCalled();
expect(mockClient.getTokenInfo).toHaveBeenCalled(); expect(mockClient.getTokenInfo).toHaveBeenCalled();
@ -315,7 +304,8 @@ describe('oauth2', () => {
await getOauthClient(AuthType.CLOUD_SHELL, mockConfig); await getOauthClient(AuthType.CLOUD_SHELL, mockConfig);
expect(fs.promises.writeFile).not.toHaveBeenCalled(); const credsPath = path.join(tempHomeDir, '.gemini', 'oauth_creds.json');
expect(fs.existsSync(credsPath)).toBe(false);
}); });
it('should return the Compute client on successful ADC authentication', async () => { it('should return the Compute client on successful ADC authentication', async () => {
@ -361,10 +351,6 @@ describe('oauth2', () => {
.mockResolvedValue({ email: 'test-gcp-account@gmail.com' }), .mockResolvedValue({ email: 'test-gcp-account@gmail.com' }),
} as unknown as Response); } as unknown as Response);
const writeFileSpy = vi
.spyOn(fs.promises, 'writeFile')
.mockResolvedValue(undefined);
const client = await getOauthClient( const client = await getOauthClient(
AuthType.LOGIN_WITH_GOOGLE, AuthType.LOGIN_WITH_GOOGLE,
mockConfig, mockConfig,
@ -392,18 +378,11 @@ describe('oauth2', () => {
'.gemini', '.gemini',
'google_accounts.json', 'google_accounts.json',
); );
expect(writeFileSpy).toHaveBeenCalledWith( const cachedContent = fs.readFileSync(googleAccountPath, 'utf-8');
googleAccountPath, expect(JSON.parse(cachedContent)).toEqual({
JSON.stringify(
{
active: 'test-gcp-account@gmail.com', active: 'test-gcp-account@gmail.com',
old: [], old: [],
}, });
null,
2,
),
'utf-8',
);
}); });
it('should not use GCP token if GOOGLE_CLOUD_ACCESS_TOKEN is not set', async () => { it('should not use GCP token if GOOGLE_CLOUD_ACCESS_TOKEN is not set', async () => {
@ -426,9 +405,9 @@ describe('oauth2', () => {
// Make it fall through to cached credentials path // Make it fall through to cached credentials path
const cachedCreds = { refresh_token: 'cached-token' }; const cachedCreds = { refresh_token: 'cached-token' };
vi.spyOn(fs.promises, 'readFile').mockResolvedValue( const credsPath = path.join(tempHomeDir, '.gemini', 'oauth_creds.json');
JSON.stringify(cachedCreds), await fs.promises.mkdir(path.dirname(credsPath), { recursive: true });
); await fs.promises.writeFile(credsPath, JSON.stringify(cachedCreds));
await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig); await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig);
@ -457,9 +436,9 @@ describe('oauth2', () => {
// Make it fall through to cached credentials path // Make it fall through to cached credentials path
const cachedCreds = { refresh_token: 'cached-token' }; const cachedCreds = { refresh_token: 'cached-token' };
vi.spyOn(fs.promises, 'readFile').mockResolvedValue( const credsPath = path.join(tempHomeDir, '.gemini', 'oauth_creds.json');
JSON.stringify(cachedCreds), await fs.promises.mkdir(path.dirname(credsPath), { recursive: true });
); await fs.promises.writeFile(credsPath, JSON.stringify(cachedCreds));
await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig); await getOauthClient(AuthType.LOGIN_WITH_GOOGLE, mockConfig);