|
17 | 17 | import {beforeEach, describe, expect, it, jest} from '@jest/globals';
|
18 | 18 |
|
19 | 19 | import {Git} from '../src/git';
|
| 20 | +import {Exec} from '../src/exec'; |
| 21 | +import {ExecOutput} from '@actions/exec'; |
20 | 22 |
|
21 | 23 | beforeEach(() => {
|
22 | 24 | jest.clearAllMocks();
|
| 25 | + jest.restoreAllMocks(); |
23 | 26 | });
|
24 | 27 |
|
25 |
| -describe('git', () => { |
26 |
| - it('returns git remote ref', async () => { |
| 28 | +describe('context', () => { |
| 29 | + it('returns mocked ref and sha', async () => { |
| 30 | + jest.spyOn(Exec, 'getExecOutput').mockImplementation((cmd, args): Promise<ExecOutput> => { |
| 31 | + const fullCmd = `${cmd} ${args?.join(' ')}`; |
| 32 | + let result = ''; |
| 33 | + switch (fullCmd) { |
| 34 | + case 'git show --format=%H HEAD --quiet --': |
| 35 | + result = 'test-sha'; |
| 36 | + break; |
| 37 | + case 'git symbolic-ref HEAD': |
| 38 | + result = 'refs/heads/test'; |
| 39 | + break; |
| 40 | + } |
| 41 | + return Promise.resolve({ |
| 42 | + stdout: result, |
| 43 | + stderr: '', |
| 44 | + exitCode: 0 |
| 45 | + }); |
| 46 | + }); |
| 47 | + const ctx = await Git.context(); |
| 48 | + expect(ctx.ref).toEqual('refs/heads/test'); |
| 49 | + expect(ctx.sha).toEqual('test-sha'); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('isInsideWorkTree', () => { |
| 54 | + it('have been called', async () => { |
| 55 | + const execSpy = jest.spyOn(Exec, 'getExecOutput'); |
| 56 | + try { |
| 57 | + await Git.isInsideWorkTree(); |
| 58 | + } catch (err) { |
| 59 | + // noop |
| 60 | + } |
| 61 | + expect(execSpy).toHaveBeenCalledWith(`git`, ['rev-parse', '--is-inside-work-tree'], { |
| 62 | + silent: true, |
| 63 | + ignoreReturnCode: true |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe('remoteSha', () => { |
| 69 | + it('returns git remote sha', async () => { |
| 70 | + expect(await Git.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head')).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa'); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('remoteURL', () => { |
| 75 | + it('have been called', async () => { |
| 76 | + const execSpy = jest.spyOn(Exec, 'getExecOutput'); |
| 77 | + try { |
| 78 | + await Git.remoteURL(); |
| 79 | + } catch (err) { |
| 80 | + // noop |
| 81 | + } |
| 82 | + expect(execSpy).toHaveBeenCalledWith(`git`, ['remote', 'get-url', 'origin'], { |
| 83 | + silent: true, |
| 84 | + ignoreReturnCode: true |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('ref', () => { |
| 90 | + it('have been called', async () => { |
| 91 | + const execSpy = jest.spyOn(Exec, 'getExecOutput'); |
| 92 | + try { |
| 93 | + await Git.ref(); |
| 94 | + } catch (err) { |
| 95 | + // noop |
| 96 | + } |
| 97 | + expect(execSpy).toHaveBeenCalledWith(`git`, ['symbolic-ref', 'HEAD'], { |
| 98 | + silent: true, |
| 99 | + ignoreReturnCode: true |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('fullCommit', () => { |
| 105 | + it('have been called', async () => { |
| 106 | + const execSpy = jest.spyOn(Exec, 'getExecOutput'); |
| 107 | + try { |
| 108 | + await Git.fullCommit(); |
| 109 | + } catch (err) { |
| 110 | + // noop |
| 111 | + } |
| 112 | + expect(execSpy).toHaveBeenCalledWith(`git`, ['show', '--format=%H', 'HEAD', '--quiet', '--'], { |
| 113 | + silent: true, |
| 114 | + ignoreReturnCode: true |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe('shortCommit', () => { |
| 120 | + it('have been called', async () => { |
| 121 | + const execSpy = jest.spyOn(Exec, 'getExecOutput'); |
| 122 | + try { |
| 123 | + await Git.shortCommit(); |
| 124 | + } catch (err) { |
| 125 | + // noop |
| 126 | + } |
| 127 | + expect(execSpy).toHaveBeenCalledWith(`git`, ['show', '--format=%h', 'HEAD', '--quiet', '--'], { |
| 128 | + silent: true, |
| 129 | + ignoreReturnCode: true |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe('tag', () => { |
| 135 | + it('have been called', async () => { |
| 136 | + const execSpy = jest.spyOn(Exec, 'getExecOutput'); |
27 | 137 | try {
|
28 |
| - expect(await Git.getRemoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head')).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa'); |
29 |
| - } catch (e) { |
30 |
| - // eslint-disable-next-line jest/no-conditional-expect |
31 |
| - expect(e).toEqual(null); |
| 138 | + await Git.tag(); |
| 139 | + } catch (err) { |
| 140 | + // noop |
32 | 141 | }
|
| 142 | + expect(execSpy).toHaveBeenCalledWith(`git`, ['tag', '--points-at', 'HEAD', '--sort', '-version:creatordate'], { |
| 143 | + silent: true, |
| 144 | + ignoreReturnCode: true |
| 145 | + }); |
33 | 146 | });
|
34 | 147 | });
|
0 commit comments