-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifacts.test.ts
More file actions
233 lines (189 loc) · 7.02 KB
/
artifacts.test.ts
File metadata and controls
233 lines (189 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import type { SimpleGit } from 'simple-git';
import type { Tags } from '@model/tags';
import { info } from '@actions/core';
import type { getInput } from '@actions/core';
import { it, jest, describe, expect } from '@jest/globals';
import { exec } from '@actions/exec';
import { fromPartial } from '@total-typescript/shoehorn';
import { Configuration } from '@/configuration';
import { Artifacts } from '@model/artifacts';
jest.mock('@actions/exec', () => ({
__esModule: true,
exec: jest.fn(),
}));
jest.mock('@model/tags', () => ({
__esModule: true,
Tags: {
collect: jest.fn(),
move: jest.fn(),
},
}));
jest.mock('@actions/core', () => ({
getInput: jest.fn(),
info: jest.fn(),
startGroup: jest.fn(),
endGroup: jest.fn()
}))
describe('Artifacts', () => {
it('Compile the assets and Deploy when finished', async () => {
const git = fromPartial<SimpleGit>({
commit: jest.fn(() =>
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
),
push: jest.fn(() =>
Promise.resolve({
remoteMessages: {
all: [''],
},
})
),
});
const tags = fromPartial<Tags>({ collect: jest.fn(), move: jest.fn() });
const artifacts = new Artifacts(git, tags, configuration());
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
await artifacts.update();
expect(jest.mocked(exec)).toHaveBeenNthCalledWith(1, 'yarn build');
expect(jest.mocked(exec)).toHaveBeenNthCalledWith(2, 'git add -f ./build/*');
});
it('Throw an error when failing to compile', async () => {
const tags = fromPartial<Tags>({});
const git = fromPartial<SimpleGit>({});
const artifacts = new Artifacts(git, tags, configuration());
jest.mocked(exec).mockImplementation(async () => Promise.resolve(1));
await expect(artifacts.update()).rejects.toThrow(
'Failed creating artifacts: Failing to compile artifacts. Process exited with non-zero code.'
);
});
it('Throw an error when artifacts commit fails', async () => {
const git = fromPartial<SimpleGit>({
commit: jest.fn(() => Promise.reject(new Error('Failed to commit'))),
});
const tags = fromPartial<Tags>({ collect: jest.fn() });
const artifacts = new Artifacts(git, tags, configuration());
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
await expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to commit');
});
it('Throw an error when artifacts push fails', async () => {
const git = fromPartial<SimpleGit>({
commit: jest.fn(() =>
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
),
push: jest.fn(() => Promise.reject(new Error('Failed to push'))),
});
const tags = fromPartial<Tags>({ collect: jest.fn() });
const artifacts = new Artifacts(git, tags, configuration());
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
await expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to push');
});
it('Do not push when the action is not configured to do so', async () => {
const push = jest.fn();
const git = fromPartial<SimpleGit>({
commit: jest.fn(() =>
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
),
push,
});
const tags = fromPartial<Tags>({ collect: jest.fn(), move: jest.fn() });
const artifacts = new Artifacts(
git,
tags,
configuration(undefined, {
'can-push': 'false',
})
);
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
await artifacts.update();
expect(push).not.toHaveBeenCalled();
expect(info).toHaveBeenCalledWith('Skipping pushing artifacts.');
});
it('Throw an error when failing to git-add', async () => {
const git = fromPartial<SimpleGit>({});
const tags = fromPartial<Tags>({ collect: jest.fn() });
const artifacts = new Artifacts(git, tags, configuration());
jest.mocked(exec).mockImplementation(async (command) => (command === 'yarn build' ? 0 : 1));
await expect(artifacts.update()).rejects.toThrow(
'Failed creating artifacts: Failing to git-add the artifacts build. Process exited with non-zero code.'
);
});
it('Throw an error when collecting tags fails', () => {
const git = fromPartial<SimpleGit>({});
const tags = fromPartial<Tags>({
collect: jest.fn(() => Promise.reject(new Error('Failed to collect tags'))),
});
const artifacts = new Artifacts(git, tags, configuration());
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
expect(artifacts.update()).rejects.toThrow('Failed creating artifacts: Failed to collect tags');
});
it('Collect tags before moving them', async () => {
const git = fromPartial<SimpleGit>({
commit: jest.fn(() =>
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
),
push: jest.fn(() =>
Promise.resolve({
remoteMessages: {
all: [''],
},
})
),
});
const collect = jest.fn();
const move = jest.fn();
const tags = fromPartial<Tags>({ collect, move });
const artifacts = new Artifacts(git, tags, configuration());
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
await artifacts.update();
expect(collect.mock.invocationCallOrder[0]).toBeLessThan(move.mock.invocationCallOrder[0] ?? 0);
});
it('Do not perform any tasks associated to tags when the action is not running for tags', async () => {
const git = fromPartial<SimpleGit>({
commit: jest.fn(() =>
Promise.resolve({ summary: { changes: 0, insertions: 0, deletions: 0 } })
),
push: jest.fn(() =>
Promise.resolve({
remoteMessages: {
all: [''],
},
})
),
});
const collect = jest.fn();
const move = jest.fn();
const tags = fromPartial<Tags>({ collect, move });
const _configuration = configuration({
GITHUB_REF: 'refs/heads/main',
});
const artifacts = new Artifacts(git, tags, _configuration);
jest.mocked(exec).mockImplementation(async () => Promise.resolve(0));
await artifacts.update();
expect(collect).not.toHaveBeenCalled();
expect(move).not.toHaveBeenCalled();
});
});
type InputsConfiguration = Readonly<Record<string, unknown>>;
function configuration(
env?: Readonly<NodeJS.ProcessEnv>,
inputsConfiguration: InputsConfiguration = {}
): Configuration {
let _env = env;
if (!_env) {
_env = {
GITHUB_REF: 'refs/tags/v1.0.0',
};
}
return new Configuration(
stubGetInput({
command: 'yarn build',
'target-dir': './build',
'can-push': 'true',
...inputsConfiguration,
}),
_env
);
}
function stubGetInput(inputsConfiguration: InputsConfiguration): typeof getInput {
return jest.fn((name: string): string => {
return String(Object.hasOwn(inputsConfiguration, name) ? inputsConfiguration[name] : undefined);
});
}