|
| 1 | +import type { SimpleGit } from 'simple-git'; |
| 2 | +import type { Tags } from '@model/tags'; |
| 3 | +import type { getInput } from '@actions/core'; |
| 4 | + |
| 5 | +import { it, jest, describe, expect } from '@jest/globals'; |
| 6 | +import { exec } from '@actions/exec'; |
| 7 | +import { fromPartial } from '@total-typescript/shoehorn'; |
| 8 | +import { Configuration } from '@/configuration'; |
| 9 | +import { Artifacts } from '@model/artifacts'; |
| 10 | + |
| 11 | +jest.mock('@actions/exec', () => ({ |
| 12 | + __esModule: true, |
| 13 | + exec: jest.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('@model/tags', () => ({ |
| 17 | + __esModule: true, |
| 18 | + Tags: { |
| 19 | + collect: jest.fn(), |
| 20 | + move: jest.fn(), |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +describe('Artifacts', () => { |
| 25 | + it('Compile the assets and Deploy when finished', async () => { |
| 26 | + const git = fromPartial<SimpleGit>({ |
| 27 | + commit: jest.fn(() => |
| 28 | + Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } }) |
| 29 | + ), |
| 30 | + push: jest.fn(() => |
| 31 | + Promise.resolve({ |
| 32 | + remoteMessages: { |
| 33 | + all: [''], |
| 34 | + }, |
| 35 | + }) |
| 36 | + ), |
| 37 | + }); |
| 38 | + const tags = fromPartial<Tags>({ collect: jest.fn(), move: jest.fn() }); |
| 39 | + const artifacts = new Artifacts(git, tags, configuration()); |
| 40 | + |
| 41 | + jest.mocked(exec).mockImplementation(async () => Promise.resolve(0)); |
| 42 | + |
| 43 | + await artifacts.update(); |
| 44 | + |
| 45 | + expect(jest.mocked(exec)).toHaveBeenNthCalledWith(1, 'yarn build'); |
| 46 | + expect(jest.mocked(exec)).toHaveBeenNthCalledWith(2, 'git add -f ./build/*'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('Throw an error when failing to compile', async () => { |
| 50 | + const tags = fromPartial<Tags>({}); |
| 51 | + const git = fromPartial<SimpleGit>({}); |
| 52 | + const artifacts = new Artifacts(git, tags, configuration()); |
| 53 | + |
| 54 | + jest.mocked(exec).mockImplementation(async () => Promise.resolve(1)); |
| 55 | + |
| 56 | + await expect(artifacts.update()).rejects.toThrow( |
| 57 | + 'Failed creating artifacts: Failing to compile artifacts. Process exited with non-zero code.' |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Throw an error when artifacts commit fails', async () => { |
| 62 | + const git = fromPartial<SimpleGit>({ |
| 63 | + commit: jest.fn(() => Promise.reject(new Error('Failed to commit'))), |
| 64 | + }); |
| 65 | + const tags = fromPartial<Tags>({ collect: jest.fn() }); |
| 66 | + const artifacts = new Artifacts(git, tags, configuration()); |
| 67 | + |
| 68 | + jest.mocked(exec).mockImplementation(async () => Promise.resolve(0)); |
| 69 | + |
| 70 | + await expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to commit'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('Throw an error when artifacts push fails', async () => { |
| 74 | + const git = fromPartial<SimpleGit>({ |
| 75 | + commit: jest.fn(() => |
| 76 | + Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } }) |
| 77 | + ), |
| 78 | + push: jest.fn(() => Promise.reject(new Error('Failed to push'))), |
| 79 | + }); |
| 80 | + const tags = fromPartial<Tags>({ collect: jest.fn() }); |
| 81 | + const artifacts = new Artifacts(git, tags, configuration()); |
| 82 | + |
| 83 | + jest.mocked(exec).mockImplementation(async () => Promise.resolve(0)); |
| 84 | + |
| 85 | + await expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to push'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('Throw an error when failing to git-add', async () => { |
| 89 | + const git = fromPartial<SimpleGit>({}); |
| 90 | + const tags = fromPartial<Tags>({ collect: jest.fn() }); |
| 91 | + const artifacts = new Artifacts(git, tags, configuration()); |
| 92 | + |
| 93 | + jest.mocked(exec).mockImplementation(async (command) => (command === 'yarn build' ? 0 : 1)); |
| 94 | + |
| 95 | + await expect(artifacts.update()).rejects.toThrow( |
| 96 | + 'Failed creating artifacts: Failing to git-add the artifacts build. Process exited with non-zero code.' |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('Throw an error when collecting tags fails', () => { |
| 101 | + const git = fromPartial<SimpleGit>({}); |
| 102 | + const tags = fromPartial<Tags>({ |
| 103 | + collect: jest.fn(() => Promise.reject(new Error('Failed to collect tags'))), |
| 104 | + }); |
| 105 | + const artifacts = new Artifacts(git, tags, configuration()); |
| 106 | + |
| 107 | + jest.mocked(exec).mockImplementation(async () => Promise.resolve(0)); |
| 108 | + |
| 109 | + expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to collect tags'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('Collect tags before moving them', async () => { |
| 113 | + const git = fromPartial<SimpleGit>({ |
| 114 | + commit: jest.fn(() => |
| 115 | + Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } }) |
| 116 | + ), |
| 117 | + push: jest.fn(() => |
| 118 | + Promise.resolve({ |
| 119 | + remoteMessages: { |
| 120 | + all: [''], |
| 121 | + }, |
| 122 | + }) |
| 123 | + ), |
| 124 | + }); |
| 125 | + |
| 126 | + const collect = jest.fn(); |
| 127 | + const move = jest.fn(); |
| 128 | + const tags = fromPartial<Tags>({ collect, move }); |
| 129 | + |
| 130 | + const artifacts = new Artifacts(git, tags, configuration()); |
| 131 | + |
| 132 | + jest.mocked(exec).mockImplementation(async () => Promise.resolve(0)); |
| 133 | + |
| 134 | + await artifacts.update(); |
| 135 | + |
| 136 | + expect(collect.mock.invocationCallOrder[0]).toBeLessThan(move.mock.invocationCallOrder[0] ?? 0); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +function configuration(): Configuration { |
| 141 | + return new Configuration(stubGetInput()); |
| 142 | +} |
| 143 | + |
| 144 | +function stubGetInput(): typeof getInput { |
| 145 | + return jest.fn((name: string): string => { |
| 146 | + return name === 'command' ? 'yarn build' : './build'; |
| 147 | + }); |
| 148 | +} |
0 commit comments