|
1 | 1 | import Auto from '@auto-it/core';
|
| 2 | +import { dummyLog } from '@auto-it/core/dist/utils/logger'; |
| 3 | +import { makeHooks } from '@auto-it/core/dist/utils/make-hooks'; |
2 | 4 | import OneReleaseCommit from '../src';
|
3 | 5 |
|
| 6 | +const exec = jest.fn(); |
| 7 | +const getGitLog = jest.fn(); |
| 8 | + |
| 9 | +jest.mock("../../../packages/core/dist/utils/get-current-branch", () => ({ |
| 10 | + getCurrentBranch: () => "main", |
| 11 | +})); |
| 12 | +jest.mock( |
| 13 | + "../../../packages/core/dist/utils/exec-promise", |
| 14 | + () => (...args: any[]) => exec(...args) |
| 15 | +); |
| 16 | + |
| 17 | +const setup = (mockGit?: any) => { |
| 18 | + const plugin = new OneReleaseCommit({}); |
| 19 | + const hooks = makeHooks(); |
| 20 | + |
| 21 | + plugin.apply(({ |
| 22 | + hooks, |
| 23 | + git: mockGit, |
| 24 | + remote: "origin", |
| 25 | + baseBranch: "main", |
| 26 | + logger: dummyLog(), |
| 27 | + } as unknown) as Auto.Auto); |
| 28 | + |
| 29 | + return { plugin, hooks }; |
| 30 | +}; |
| 31 | + |
4 | 32 | describe('One-Release-Commit Plugin', () => {
|
5 |
| - test('should do something', async () => { |
| 33 | + const headCommitHash = "dd53ea5d7b151306ba6275a332ee333800fb39e8"; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + exec.mockReset(); |
| 37 | + exec.mockResolvedValueOnce(`${headCommitHash} refs/heads/main`); |
| 38 | + getGitLog.mockReset(); |
| 39 | + getGitLog.mockResolvedValueOnce([{hash: "c2241048"},{hash: "c2241049"}]); |
| 40 | + }); |
| 41 | + |
| 42 | + function expectListGitHistoryCalled() { |
| 43 | + expect(exec).toHaveBeenCalled(); |
| 44 | + expect(exec.mock.calls[0]).toMatchObject(["git",["ls-remote", "--heads", "origin", "main"]]); |
| 45 | + expect(getGitLog).toHaveBeenCalledTimes(1); |
| 46 | + expect(getGitLog.mock.calls[0]).toMatchObject([headCommitHash]); |
| 47 | + } |
| 48 | + |
| 49 | + function expectLookingForGitTagOnCommit(callIdx: number, commitSha: string) { |
| 50 | + expect(exec.mock.calls.length >= callIdx).toBe(true); |
| 51 | + expect(exec.mock.calls[callIdx]).toMatchObject(["git",["describe", "--tags", "--exact-match", commitSha]]); |
| 52 | + } |
| 53 | + |
| 54 | + function expectResetAndRecreateANewReleaseCommit(callIdx: number) { |
| 55 | + expect(exec.mock.calls.length > callIdx).toBe(true); |
| 56 | + expect(exec.mock.calls[callIdx]).toMatchObject(["git",["reset", "--soft", headCommitHash]]); |
| 57 | + expect(exec.mock.calls[callIdx+1]).toMatchObject(["git",["commit", "-m", '"Release version v1.2.3 [skip ci]"', "--no-verify"]]); |
| 58 | + } |
| 59 | + |
| 60 | + test("should setup hooks", () => { |
| 61 | + const {hooks} = setup(); |
| 62 | + |
| 63 | + expect(hooks.validateConfig.isUsed()).toBe(true); |
| 64 | + expect(hooks.afterVersion.isUsed()).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("validateConfig", () => { |
| 68 | + test('should validate the configuration', async () => { |
| 69 | + const {hooks, plugin} = setup(); |
| 70 | + await expect(hooks.validateConfig.promise("not-me", {})).resolves.toBeUndefined(); |
| 71 | + await expect(hooks.validateConfig.promise(plugin.name, {})).resolves.toStrictEqual([]); |
| 72 | + |
| 73 | + const res = await hooks.validateConfig.promise(plugin.name, {invalidKey: "value"}); |
| 74 | + expect(res).toHaveLength(1); |
| 75 | + expect(res[0]).toContain(plugin.name); |
| 76 | + expect(res[0]).toContain("Found unknown configuration keys:"); |
| 77 | + expect(res[0]).toContain("invalidKey"); |
| 78 | + |
| 79 | + await expect(hooks.validateConfig.promise(plugin.name, {commitMessage: -1})).resolves.toMatchObject([{ |
| 80 | + expectedType: '"string"', |
| 81 | + path: "one-release-commit.commitMessage", |
| 82 | + value: -1, |
| 83 | + }]); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("afterVersion", () => { |
| 88 | + test('should do nothing on dryRun', async () => { |
| 89 | + const {hooks} = setup(); |
| 90 | + await expect(hooks.afterVersion.promise({dryRun: true})).resolves.toBeUndefined(); |
| 91 | + expect(exec).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + test('should do nothing without version', async () => { |
| 95 | + const {hooks} = setup(); |
| 96 | + await expect(hooks.afterVersion.promise({})).resolves.toBeUndefined(); |
| 97 | + expect(exec).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + test('should do nothing without git', async () => { |
| 101 | + const {hooks} = setup(); |
| 102 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).resolves.toBeUndefined(); |
| 103 | + expect(exec).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + test('should be executed in a less priority group', async () => { |
| 107 | + getGitLog.mockReset(); |
| 108 | + getGitLog.mockResolvedValueOnce([]); |
| 109 | + |
| 110 | + const {hooks} = setup({ getGitLog }); |
| 111 | + hooks.afterVersion.tapPromise("dummy", async () => { |
| 112 | + expect(exec).not.toHaveBeenCalled(); |
| 113 | + expect(getGitLog).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).resolves.toBeUndefined(); |
| 116 | + |
| 117 | + expectListGitHistoryCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + test('should do nothing when there no release commits', async () => { |
| 121 | + getGitLog.mockReset(); |
| 122 | + getGitLog.mockResolvedValueOnce([]); |
| 123 | + |
| 124 | + const {hooks} = setup({ getGitLog }); |
| 125 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).resolves.toBeUndefined(); |
| 126 | + |
| 127 | + expectListGitHistoryCalled(); |
| 128 | + }); |
| 129 | + |
| 130 | + test('should create a single release commit when there is one existing commit', async () => { |
| 131 | + getGitLog.mockReset(); |
| 132 | + getGitLog.mockResolvedValueOnce([{hash: "c2241048"}]); |
| 133 | + |
| 134 | + const {hooks} = setup({ getGitLog }); |
| 135 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).resolves.toBeUndefined(); |
| 136 | + |
| 137 | + expectListGitHistoryCalled(); |
| 138 | + |
| 139 | + expect(exec).toHaveBeenCalledTimes(4); |
| 140 | + expectLookingForGitTagOnCommit(1, "c2241048"); |
| 141 | + expectResetAndRecreateANewReleaseCommit(2); |
| 142 | + }); |
| 143 | + |
| 144 | + test('should create a single release commit when there is multiple existing commit', async () => { |
| 145 | + const {hooks} = setup({ getGitLog }); |
| 146 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).resolves.toBeUndefined(); |
| 147 | + |
| 148 | + expectListGitHistoryCalled(); |
| 149 | + |
| 150 | + expect(exec).toHaveBeenCalledTimes(5); |
| 151 | + expectLookingForGitTagOnCommit(1, "c2241048"); |
| 152 | + expectLookingForGitTagOnCommit(2, "c2241049"); |
| 153 | + expectResetAndRecreateANewReleaseCommit(3); |
| 154 | + }); |
| 155 | + |
| 156 | + test('should recreate all existing tags', async () => { |
| 157 | + exec.mockResolvedValueOnce('v1.2.4') |
| 158 | + .mockResolvedValueOnce('submobule-v1.2.4') |
| 159 | + .mockResolvedValueOnce(' Tag message for v1.2.4 ') |
| 160 | + .mockResolvedValueOnce(' Another multiline\ntag message\n'); |
| 161 | + |
| 162 | + const {hooks} = setup({ getGitLog }); |
| 163 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).resolves.toBeUndefined(); |
| 164 | + |
| 165 | + expectListGitHistoryCalled(); |
| 166 | + |
| 167 | + expect(exec).toHaveBeenCalledTimes(9); |
| 168 | + expectLookingForGitTagOnCommit(1, "c2241048"); |
| 169 | + expectLookingForGitTagOnCommit(2, "c2241049"); |
| 170 | + expect(exec.mock.calls[3]).toMatchObject(["git",["tag", "v1.2.4", "-l", '--format="%(contents)"']]); |
| 171 | + expect(exec.mock.calls[4]).toMatchObject(["git",["tag", "submobule-v1.2.4", "-l", '--format="%(contents)"']]); |
| 172 | + expectResetAndRecreateANewReleaseCommit(5); |
| 173 | + }); |
| 174 | + |
| 175 | + test('should not failed when there is no tag on commit', async () => { |
| 176 | + exec.mockResolvedValueOnce('v1.2.4') |
| 177 | + .mockRejectedValueOnce(new Error('no tag exactly matches xyz')) |
| 178 | + .mockResolvedValueOnce(' Tag message for v1.2.4 '); |
| 179 | + |
| 180 | + const {hooks} = setup({ getGitLog }); |
| 181 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).resolves.toBeUndefined(); |
| 182 | + |
| 183 | + expectListGitHistoryCalled(); |
| 184 | + |
| 185 | + expect(exec).toHaveBeenCalledTimes(7); |
| 186 | + expectLookingForGitTagOnCommit(1, "c2241048"); |
| 187 | + expectLookingForGitTagOnCommit(2, "c2241049"); |
| 188 | + expect(exec.mock.calls[3]).toMatchObject(["git",["tag", "v1.2.4", "-l", '--format="%(contents)"']]); |
| 189 | + expectResetAndRecreateANewReleaseCommit(4); |
| 190 | + }); |
| 191 | + |
| 192 | + test.each([ |
| 193 | + [new Error('unknown failure')], |
| 194 | + ['not an error'], |
| 195 | + ])( 'should failed when retrieving tags failed with : %p', async (cause) => { |
| 196 | + exec.mockRejectedValueOnce(cause); |
| 197 | + |
| 198 | + const {hooks} = setup({ getGitLog }); |
| 199 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).rejects.toBe(cause); |
| 200 | + |
| 201 | + expectListGitHistoryCalled(); |
| 202 | + |
| 203 | + expect(exec).toHaveBeenCalledTimes(3); |
| 204 | + expectLookingForGitTagOnCommit(1, "c2241048"); |
| 205 | + expectLookingForGitTagOnCommit(2, "c2241049"); |
| 206 | + }); |
| 207 | + |
| 208 | + test('should failed when not remote head found', async () => { |
| 209 | + exec.mockReset(); |
| 210 | + exec.mockResolvedValueOnce(''); |
| 211 | + |
| 212 | + const {hooks} = setup({ getGitLog }); |
| 213 | + await expect(hooks.afterVersion.promise({version: 'v1.2.3'})).rejects.toStrictEqual(new Error('No remote found for branch : "main"')); |
| 214 | + |
| 215 | + expect(exec).toHaveBeenCalledTimes(1); |
| 216 | + expect(exec.mock.calls[0]).toMatchObject(["git",["ls-remote", "--heads", "origin", "main"]]); |
| 217 | + expect(getGitLog).not.toHaveBeenCalled(); |
| 218 | + }); |
6 | 219 | });
|
7 | 220 | });
|
0 commit comments