diff options
| author | Yashwanth Anantharaju <yaananth@users.noreply.github.com> | 2026-06-01 11:35:58 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-06-01 11:35:58 -0400 |
| commit | eb9dcad7e007cb96364b4e612799ce0ff736cb09 (patch) | |
| tree | 2730243bfbc90a8bc802720dc13e2e7ce433e7f2 /__test__ | |
| parent | 45c7dc79a6c1ac19a6a0981f08146cb419b30891 (diff) | |
Fix checkout init for SHA-256 repositories (#2439)
* Fix checkout init for SHA-256 repositories
* Remove unused object format result field
Diffstat (limited to '__test__')
| -rw-r--r-- | __test__/git-command-manager.test.ts | 53 | ||||
| -rw-r--r-- | __test__/github-api-helper.test.ts | 98 |
2 files changed, 151 insertions, 0 deletions
diff --git a/__test__/git-command-manager.test.ts b/__test__/git-command-manager.test.ts index 8a97d82..47a0f8f 100644 --- a/__test__/git-command-manager.test.ts +++ b/__test__/git-command-manager.test.ts @@ -378,6 +378,59 @@ describe('Test fetchDepth and fetchTags options', () => { }) }) +describe('repository initialization object format', () => { + beforeEach(async () => { + jest.spyOn(fshelper, 'fileExistsSync').mockImplementation(jest.fn()) + jest.spyOn(fshelper, 'directoryExistsSync').mockImplementation(jest.fn()) + }) + + afterEach(() => { + jest.restoreAllMocks() + }) + + it('initializes SHA-256 repositories with the matching object format', async () => { + mockExec.mockImplementation((path, args, options) => { + if (args.includes('version')) { + options.listeners.stdout(Buffer.from('git version 2.50.1')) + } + + return 0 + }) + jest.spyOn(exec, 'exec').mockImplementation(mockExec) + + git = await commandManager.createCommandManager('test', false, false) + + await git.init('sha256') + + expect(mockExec).toHaveBeenCalledWith( + expect.any(String), + ['init', '--object-format=sha256', 'test'], + expect.any(Object) + ) + }) + + it('initializes SHA-1 repositories with existing default arguments', async () => { + mockExec.mockImplementation((path, args, options) => { + if (args.includes('version')) { + options.listeners.stdout(Buffer.from('git version 2.50.1')) + } + + return 0 + }) + jest.spyOn(exec, 'exec').mockImplementation(mockExec) + + git = await commandManager.createCommandManager('test', false, false) + + await git.init('sha1') + + expect(mockExec).toHaveBeenCalledWith( + expect.any(String), + ['init', 'test'], + expect.any(Object) + ) + }) +}) + describe('git user-agent with orchestration ID', () => { beforeEach(async () => { jest.spyOn(fshelper, 'fileExistsSync').mockImplementation(jest.fn()) diff --git a/__test__/github-api-helper.test.ts b/__test__/github-api-helper.test.ts new file mode 100644 index 0000000..6319e20 --- /dev/null +++ b/__test__/github-api-helper.test.ts @@ -0,0 +1,98 @@ +import * as core from '@actions/core' +import * as github from '@actions/github' +import * as githubApiHelper from '../lib/github-api-helper' + +describe('github-api-helper object format', () => { + let getOctokitSpy: jest.SpyInstance + let debugSpy: jest.SpyInstance + let request: jest.Mock + + function mockHashAlgorithmApi(hashAlgorithm: string): void { + request = jest.fn(async () => ({ + data: { + hash_algorithm: hashAlgorithm + } + })) + getOctokitSpy = jest.spyOn(github, 'getOctokit').mockReturnValue({ + request + } as any) + } + + beforeEach(() => { + debugSpy = jest.spyOn(core, 'debug').mockImplementation(jest.fn()) + }) + + afterEach(() => { + jest.restoreAllMocks() + }) + + it('detects SHA-256 from the repository hash algorithm endpoint', async () => { + mockHashAlgorithmApi('sha256') + + await expect( + githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo') + ).resolves.toEqual({format: 'sha256', succeeded: true}) + + expect(getOctokitSpy).toHaveBeenCalledWith( + 'token', + expect.objectContaining({baseUrl: 'https://api.github.com'}) + ) + expect(request).toHaveBeenCalledWith( + 'GET /repos/{owner}/{repo}/hash-algorithm', + {owner: 'owner', repo: 'repo'} + ) + }) + + it('detects SHA-1 from the repository hash algorithm endpoint', async () => { + mockHashAlgorithmApi('sha1') + + await expect( + githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo') + ).resolves.toEqual({format: 'sha1', succeeded: true}) + }) + + it('detects object format from an existing commit without API calls', async () => { + const commitSha = + '9422233ca7ee1b17f1e905d0e141faf0c401556c41cdc6acd71c6bd685da2e92' + getOctokitSpy = jest.spyOn(github, 'getOctokit') + + await expect( + githubApiHelper.tryGetRepositoryObjectFormat( + 'token', + 'owner', + 'repo', + undefined, + commitSha + ) + ).resolves.toEqual({format: 'sha256', succeeded: true}) + + expect(getOctokitSpy).not.toHaveBeenCalled() + }) + + it('returns unsuccessful when the hash algorithm endpoint value is not recognized', async () => { + mockHashAlgorithmApi('unknown') + + await expect( + githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo') + ).resolves.toEqual({format: '', succeeded: false}) + expect(debugSpy).toHaveBeenCalledWith( + 'Unable to determine repository object format from hash-algorithm endpoint' + ) + }) + + it('returns unsuccessful when the hash algorithm API lookup fails', async () => { + request = jest.fn(async () => { + throw new Error('not found') + }) + jest.spyOn(github, 'getOctokit').mockReturnValue({ + request + } as any) + + await expect( + githubApiHelper.tryGetRepositoryObjectFormat('token', 'owner', 'repo') + ).resolves.toEqual({format: '', succeeded: false}) + expect(debugSpy).toHaveBeenCalledWith( + 'Unable to determine repository object format from hash-algorithm endpoint: not found' + ) + }) +}) |
