-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(git-submodules): separate and make tests easier mockable (#23065)
- Loading branch information
Showing
2 changed files
with
98 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,69 +1,83 @@ | ||
import is from '@sindresorhus/is'; | ||
import { mock } from 'jest-mock-extended'; | ||
import _simpleGit, { | ||
Response, | ||
SimpleGit, | ||
SimpleGitFactory, | ||
TaskOptions, | ||
} from 'simple-git'; | ||
import { Response, SimpleGit, SimpleGitFactory, simpleGit } from 'simple-git'; | ||
import { GlobalConfig } from '../../../config/global'; | ||
import * as hostRules from '../../../util/host-rules'; | ||
import type { PackageFileContent } from '../types'; | ||
import { extractPackageFile } from '.'; | ||
|
||
jest.mock('simple-git'); | ||
const simpleGit: jest.Mock<Partial<SimpleGit>> = _simpleGit as never; | ||
const simpleGitFactoryMock = simpleGit as jest.Mock<Partial<SimpleGit>>; | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion | ||
const Git = jest.requireActual('simple-git') as SimpleGitFactory; | ||
|
||
const gitMock = mock<SimpleGit>(); | ||
|
||
describe('modules/manager/git-submodules/extract', () => { | ||
// flaky ci tests | ||
// jest.setTimeout(10 * 1000); | ||
beforeEach(() => { | ||
GlobalConfig.set({ localDir: `${__dirname}/__fixtures__` }); | ||
// clear host rules | ||
hostRules.clear(); | ||
// clear environment variables | ||
process.env = {}; | ||
|
||
beforeAll(() => { | ||
simpleGit.mockImplementation((basePath: string) => { | ||
simpleGitFactoryMock.mockImplementation((basePath: string) => { | ||
const git = Git(basePath); | ||
const lsRemote: Record<string, string> = { | ||
'https://[email protected]/some/other.git': '', | ||
'https://gitlab-ci-token:[email protected]/some/repo.git': | ||
'ref: refs/heads/dev HEAD\n', | ||
}; | ||
return { | ||
subModule(): Response<string> { | ||
return Promise.resolve( | ||
'4b825dc642cb6eb9a060e54bf8d69288fbee4904' | ||
) as Response<string>; | ||
}, | ||
raw(options: string | string[] | TaskOptions): Response<string> { | ||
if ( | ||
(is.string(options) || is.array(options, is.string)) && | ||
options.includes('remote.origin.url') | ||
) { | ||
return Promise.resolve( | ||
'https://github.com/renovatebot/renovate.git' | ||
) as Response<string>; | ||
} | ||
return git.raw(options); | ||
}, | ||
listRemote(options: TaskOptions): Response<string> { | ||
if ( | ||
is.array(options, is.string) && | ||
lsRemote[options[1]] !== undefined | ||
) { | ||
return Promise.resolve(lsRemote[options[1]]) as Response<string>; | ||
} | ||
|
||
gitMock.env.mockImplementation(() => gitMock); | ||
gitMock.subModule.mockResolvedValue( | ||
'4b825dc642cb6eb9a060e54bf8d69288fbee4904' | ||
); | ||
|
||
gitMock.raw.mockImplementation((options) => { | ||
if ( | ||
(is.string(options) || is.array(options, is.string)) && | ||
options.includes('remote.origin.url') | ||
) { | ||
return Promise.resolve( | ||
'ref: refs/heads/main HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD' | ||
'https://github.com/renovatebot/renovate.git' | ||
) as Response<string>; | ||
}, | ||
...mock<Omit<SimpleGit, 'subModule' | 'raw' | 'listRemote'>>(), | ||
}; | ||
} | ||
return git.raw(options); | ||
}); | ||
return gitMock; | ||
}); | ||
}); | ||
|
||
describe('extractPackageFile()', () => { | ||
it('extracts submodules', async () => { | ||
GlobalConfig.set({ localDir: `${__dirname}/__fixtures__` }); | ||
it('empty submodule returns null', async () => { | ||
expect(await extractPackageFile('', '.gitmodules.1', {})).toBeNull(); | ||
}); | ||
|
||
it('default branch is detected when no branch is specified', async () => { | ||
gitMock.listRemote.mockResolvedValueOnce( | ||
'ref: refs/heads/main HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD' | ||
); | ||
const res = await extractPackageFile('', '.gitmodules.2', {}); | ||
expect(res?.deps).toHaveLength(1); | ||
expect(res?.deps[0].currentValue).toBe('main'); | ||
}); | ||
|
||
it('default to master if no branch can be detected', async () => { | ||
const res = await extractPackageFile('', '.gitmodules.2', {}); | ||
expect(res?.deps).toHaveLength(1); | ||
expect(res?.deps[0].currentValue).toBe('master'); | ||
}); | ||
|
||
it('given branch is used when branch is specified', async () => { | ||
const res = await extractPackageFile('', '.gitmodules.3', {}); | ||
expect(res?.deps).toHaveLength(1); | ||
expect(res?.deps[0].currentValue).toBe('staging'); | ||
}); | ||
|
||
it('submodule packageName is constructed from relative path', async () => { | ||
const res = await extractPackageFile('', '.gitmodules.4', {}); | ||
expect(res?.deps).toHaveLength(1); | ||
expect(res?.deps[0].packageName).toBe( | ||
'https://github.com/PowerShell/PowerShell-Docs' | ||
); | ||
}); | ||
|
||
it('extracts multiple submodules', async () => { | ||
hostRules.add({ matchHost: 'github.com', token: '123test' }); | ||
hostRules.add({ | ||
matchHost: 'domain.test', | ||
|
@@ -75,16 +89,22 @@ describe('modules/manager/git-submodules/extract', () => { | |
token: 'xyz', | ||
hostType: 'gitlab', | ||
}); | ||
let res: PackageFileContent | null; | ||
expect(await extractPackageFile('', '.gitmodules.1', {})).toBeNull(); | ||
res = await extractPackageFile('', '.gitmodules.2', {}); | ||
expect(res?.deps).toHaveLength(1); | ||
expect(res?.deps[0].currentValue).toBe('main'); | ||
res = await extractPackageFile('', '.gitmodules.3', {}); | ||
expect(res?.deps).toHaveLength(1); | ||
res = await extractPackageFile('', '.gitmodules.4', {}); | ||
expect(res?.deps).toHaveLength(1); | ||
res = await extractPackageFile('', '.gitmodules.5', {}); | ||
gitMock.listRemote.mockResolvedValueOnce( | ||
'ref: refs/heads/main HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD' | ||
); | ||
gitMock.listRemote.mockResolvedValueOnce( | ||
'ref: refs/heads/main HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD' | ||
); | ||
gitMock.listRemote.mockResolvedValueOnce( | ||
'ref: refs/heads/main HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD' | ||
); | ||
gitMock.listRemote.mockResolvedValueOnce( | ||
'ref: refs/heads/master HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD' | ||
); | ||
gitMock.listRemote.mockResolvedValueOnce( | ||
'ref: refs/heads/dev HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD' | ||
); | ||
const res = await extractPackageFile('', '.gitmodules.5', {}); | ||
expect(res).toEqual({ | ||
datasource: 'git-refs', | ||
deps: [ | ||
|
This file contains 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