From b78eb9b73be82ad3d2495fc2c3abbf475e3f7906 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Sun, 23 Feb 2025 11:17:02 -0300 Subject: [PATCH] fix(gitlab): Fix the platform test performance (#34410) --- lib/modules/platform/gitlab/index.spec.ts | 94 ++++++++++------------- lib/modules/platform/gitlab/index.ts | 9 +++ 2 files changed, 50 insertions(+), 53 deletions(-) diff --git a/lib/modules/platform/gitlab/index.spec.ts b/lib/modules/platform/gitlab/index.spec.ts index 6d1176dbcbbc4e..acb602fbf86c19 100644 --- a/lib/modules/platform/gitlab/index.spec.ts +++ b/lib/modules/platform/gitlab/index.spec.ts @@ -1,9 +1,9 @@ // TODO fix mocks -import type * as _timers from 'timers/promises'; +import _timers from 'timers/promises'; import { mockDeep } from 'jest-mock-extended'; -import type { Platform, RepoParams } from '..'; +import type { RepoParams } from '..'; import * as httpMock from '../../../../test/http-mock'; -import { mocked } from '../../../../test/util'; +import { git, hostRules, logger, mocked } from '../../../../test/util'; import { CONFIG_GIT_URL_UNAVAILABLE, REPOSITORY_ARCHIVED, @@ -12,36 +12,23 @@ import { REPOSITORY_EMPTY, REPOSITORY_MIRRORED, } from '../../../constants/error-messages'; -import type { logger as _logger } from '../../../logger'; import type { BranchStatus } from '../../../types'; -import type * as _git from '../../../util/git'; import type { LongCommitSha } from '../../../util/git/types'; -import type * as _hostRules from '../../../util/host-rules'; import { toBase64 } from '../../../util/string'; import { getPrBodyStruct } from '../pr-body'; +import * as prBodyModule from '../utils/pr-body'; +import * as gitlab from '.'; jest.mock('../../../util/host-rules', () => mockDeep()); jest.mock('../../../util/git'); jest.mock('timers/promises'); +const timers = mocked(_timers); + const gitlabApiHost = 'https://gitlab.com'; describe('modules/platform/gitlab/index', () => { - let gitlab: Platform; - let hostRules: jest.Mocked; - let git: jest.Mocked; - let logger: jest.Mocked; - let timers: jest.Mocked; - - beforeEach(async () => { - // reset module - jest.resetModules(); - - gitlab = await import('.'); - logger = mocked(await import('../../../logger')).logger; - timers = jest.requireMock('timers/promises'); - hostRules = jest.requireMock('../../../util/host-rules'); - git = jest.requireMock('../../../util/git'); + beforeEach(() => { git.branchExists.mockReturnValue(true); git.isBranchBehindBase.mockResolvedValue(true); git.getBranchCommit.mockReturnValue( @@ -55,6 +42,9 @@ describe('modules/platform/gitlab/index', () => { delete process.env.RENOVATE_X_GITLAB_BRANCH_STATUS_DELAY; delete process.env.RENOVATE_X_GITLAB_AUTO_MERGEABLE_CHECK_ATTEMPS; delete process.env.RENOVATE_X_GITLAB_MERGE_REQUEST_DELAY; + delete process.env.RENOVATE_X_PLATFORM_VERSION; + + gitlab.resetPlatform(); }); async function initFakePlatform(version: string) { @@ -77,11 +67,11 @@ describe('modules/platform/gitlab/index', () => { } describe('initPlatform()', () => { - it(`should throw if no token`, async () => { + it('should throw if no token', async () => { await expect(gitlab.initPlatform({} as any)).rejects.toThrow(); }); - it(`should throw if auth fails`, async () => { + it('should throw if auth fails', async () => { // user httpMock.scope(gitlabApiHost).get('/api/v4/user').reply(403); const res = gitlab.initPlatform({ @@ -91,7 +81,7 @@ describe('modules/platform/gitlab/index', () => { await expect(res).rejects.toThrow('Init: Authentication failure'); }); - it(`should default to gitlab.com`, async () => { + it('should default to gitlab.com', async () => { httpMock.scope(gitlabApiHost).get('/api/v4/user').reply(200, { email: 'a@b.com', name: 'Renovate Bot', @@ -107,7 +97,7 @@ describe('modules/platform/gitlab/index', () => { ).toMatchSnapshot(); }); - it(`should accept custom endpoint`, async () => { + it('should accept custom endpoint', async () => { const endpoint = 'https://gitlab.renovatebot.com'; httpMock .scope(endpoint) @@ -128,7 +118,7 @@ describe('modules/platform/gitlab/index', () => { ).toMatchSnapshot(); }); - it(`should reuse existing gitAuthor`, async () => { + it('should reuse existing gitAuthor', async () => { httpMock.scope(gitlabApiHost).get('/api/v4/version').reply(200, { version: '13.3.6-ee', }); @@ -502,7 +492,7 @@ describe('modules/platform/gitlab/index', () => { merge_method: 'merge', }, ); - expect(await gitlab.getBranchForceRebase!('master')).toBeFalse(); + expect(await gitlab.getBranchForceRebase()).toBeFalse(); }); it('should return true', async () => { @@ -516,7 +506,7 @@ describe('modules/platform/gitlab/index', () => { merge_method: 'ff', }, ); - expect(await gitlab.getBranchForceRebase!('master')).toBeTrue(); + expect(await gitlab.getBranchForceRebase()).toBeTrue(); }); }); @@ -1068,7 +1058,7 @@ describe('modules/platform/gitlab/index', () => { state: 'green', url: 'some-url', }); - expect(logger.warn).toHaveBeenCalledWith( + expect(logger.logger.warn).toHaveBeenCalledWith( 'Failed to get the branch commit SHA', ); }); @@ -1099,7 +1089,7 @@ describe('modules/platform/gitlab/index', () => { state: 'green', url: 'some-url', }); - expect(logger.warn).toHaveBeenCalledWith( + expect(logger.logger.warn).toHaveBeenCalledWith( 'Failed to retrieve commit pipeline', ); }); @@ -1227,13 +1217,13 @@ describe('modules/platform/gitlab/index', () => { expect(timers.setTimeout.mock.calls).toHaveLength(retry + 1); expect(timers.setTimeout.mock.calls[0][0]).toBe(delay); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( `Pipeline not yet created. Retrying 1`, ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( `Pipeline not yet created. Retrying 2`, ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( `Pipeline not yet created after 3 attempts`, ); }); @@ -1571,13 +1561,13 @@ describe('modules/platform/gitlab/index', () => { await expect( gitlab.addAssignees(42, ['someuser', 'someotheruser']), ).toResolve(); - expect(logger.warn).toHaveBeenCalledWith( + expect(logger.logger.warn).toHaveBeenCalledWith( { assignee: 'someuser', }, 'Failed to add assignee - could not get ID', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( { assignee: 'someuser', err: new Error( @@ -1594,7 +1584,7 @@ describe('modules/platform/gitlab/index', () => { it('should not be supported in too low version', async () => { await initFakePlatform('13.8.0'); await gitlab.addReviewers(42, ['someuser', 'foo', 'someotheruser']); - expect(logger.warn).toHaveBeenCalledWith( + expect(logger.logger.warn).toHaveBeenCalledWith( { version: '13.8.0' }, 'Adding reviewers is only available in GitLab 13.9 and onwards', ); @@ -2206,13 +2196,13 @@ describe('modules/platform/gitlab/index', () => { sourceBranch: 'some-branch', title: 'some title', }); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 1', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 2', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 3', ); expect(timers.setTimeout.mock.calls).toMatchObject([ @@ -2262,13 +2252,13 @@ describe('modules/platform/gitlab/index', () => { sourceBranch: 'some-branch', title: 'some title', }); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 1', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 2', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 3', ); expect(timers.setTimeout.mock.calls).toMatchObject([[100], [400], [900]]); @@ -2317,20 +2307,20 @@ describe('modules/platform/gitlab/index', () => { sourceBranch: 'some-branch', title: 'some title', }); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 1', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 2', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( 'PR not yet in mergeable state. Retrying 3', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( expect.any(Object), 'Automerge on PR creation failed. Retrying 1', ); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( expect.any(Object), 'Automerge on PR creation failed. Retrying 2', ); @@ -3381,7 +3371,7 @@ describe('modules/platform/gitlab/index', () => { await expect(gitlab.reattemptPlatformAutomerge?.(pr)).toResolve(); - expect(logger.debug).toHaveBeenLastCalledWith( + expect(logger.logger.debug).toHaveBeenLastCalledWith( 'PR platform automerge re-attempted...prNo: 12345', ); }); @@ -3444,8 +3434,7 @@ These updates have all been created already. Click a checkbox below to force a r }); it('truncates description if too low API version', async () => { - jest.doMock('../utils/pr-body'); - const { smartTruncate } = await import('../utils/pr-body'); + const smartTruncate = jest.spyOn(prBodyModule, 'smartTruncate'); await initFakePlatform('13.3.0'); gitlab.massageMarkdown(prBody); @@ -3454,8 +3443,7 @@ These updates have all been created already. Click a checkbox below to force a r }); it('truncates description for API version gt 13.4', async () => { - jest.doMock('../utils/pr-body'); - const { smartTruncate } = await import('../utils/pr-body'); + const smartTruncate = jest.spyOn(prBodyModule, 'smartTruncate'); await initFakePlatform('13.4.1'); gitlab.massageMarkdown(prBody); @@ -3667,7 +3655,7 @@ These updates have all been created already. Click a checkbox below to force a r '@group', ]); expect(expandedGroupMembers).toEqual(['group']); - expect(logger.debug).toHaveBeenCalledWith( + expect(logger.logger.debug).toHaveBeenCalledWith( expect.any(Object), 'Unable to fetch group', ); diff --git a/lib/modules/platform/gitlab/index.ts b/lib/modules/platform/gitlab/index.ts index 98f56e7371b397..5dfed1c92bc4c8 100644 --- a/lib/modules/platform/gitlab/index.ts +++ b/lib/modules/platform/gitlab/index.ts @@ -86,6 +86,15 @@ let config: { squash: boolean; } = {} as any; +export function resetPlatform(): void { + config = {} as any; + draftPrefix = DRAFT_PREFIX; + defaults.hostType = 'gitlab'; + defaults.endpoint = 'https://gitlab.com/api/v4/'; + defaults.version = '0.0.0'; + setBaseUrl(defaults.endpoint); +} + const defaults = { hostType: 'gitlab', endpoint: 'https://gitlab.com/api/v4/',