-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathmain.test.ts
More file actions
332 lines (290 loc) · 9.07 KB
/
main.test.ts
File metadata and controls
332 lines (290 loc) · 9.07 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict'
import { describe, expect, it, jest } from '@jest/globals'
import sgd from '../../src/main'
import type { Config } from '../../src/types/config'
import type { HandlerResult } from '../../src/types/handlerResult'
import {
CopyOperationKind,
emptyResult,
ManifestTarget,
} from '../../src/types/handlerResult'
jest.mock('../../src/utils/LoggingService')
const mockPreBuildTreeIndex = jest.fn()
jest.mock('../../src/adapter/GitAdapter', () => ({
default: {
getInstance: jest.fn(() => ({
preBuildTreeIndex: mockPreBuildTreeIndex,
})),
closeAll: jest.fn(),
},
}))
const mockComputeTreeIndexScope = jest.fn()
jest.mock('../../src/utils/treeIndexScope', () => ({
computeTreeIndexScope: (...args: unknown[]) =>
mockComputeTreeIndexScope(...args),
}))
const mockValidateConfig = jest.fn()
jest.mock('../../src/utils/configValidator', () => {
// biome-ignore lint/suspicious/noExplicitAny: let TS know it is an object
const actualModule: any = jest.requireActual(
'../../src/utils/configValidator'
)
return {
default: jest.fn().mockImplementation(() => {
return {
...actualModule,
validateConfig: mockValidateConfig,
}
}),
}
})
const mockGetLines = jest.fn()
jest.mock('../../src/utils/repoGitDiff', () => {
// biome-ignore lint/suspicious/noExplicitAny: let TS know it is an object
const actualModule: any = jest.requireActual('../../src/utils/repoGitDiff')
return {
default: jest.fn().mockImplementation(() => {
return {
...actualModule,
getLines: mockGetLines,
}
}),
}
})
const mockProcess = jest.fn<(lines: string[]) => Promise<HandlerResult>>()
jest.mock('../../src/service/diffLineInterpreter', () => {
// biome-ignore lint/suspicious/noExplicitAny: let TS know it is an object
const actualModule: any = jest.requireActual(
'../../src/service/diffLineInterpreter'
)
return {
default: jest.fn().mockImplementation(() => {
return {
...actualModule,
process: mockProcess,
}
}),
}
})
const mockCollectAll = jest.fn<() => Promise<HandlerResult>>()
const mockExecuteRemaining = jest.fn()
jest.mock('../../src/post-processor/postProcessorManager', () => {
return {
getPostProcessors: jest.fn().mockImplementation(() => {
return {
collectAll: mockCollectAll,
executeRemaining: mockExecuteRemaining,
}
}),
}
})
const mockExecute = jest.fn()
jest.mock('../../src/adapter/ioExecutor', () => {
return {
default: jest.fn().mockImplementation(() => {
return {
execute: mockExecute,
}
}),
}
})
beforeEach(() => {
jest.clearAllMocks()
mockProcess.mockResolvedValue(emptyResult())
mockCollectAll.mockResolvedValue(emptyResult())
mockGetLines.mockResolvedValue([] as never)
mockComputeTreeIndexScope.mockReturnValue(new Set())
})
describe('external library inclusion', () => {
describe('when configuration is not valid', () => {
beforeEach(() => {
// Arrange
mockValidateConfig.mockImplementationOnce(() =>
Promise.reject(new Error('test'))
)
})
it('it should throw', async () => {
// Arrange
expect.assertions(1)
// Act
try {
await sgd({} as Config)
} catch (error) {
// Assert
expect((error as Error).message).toEqual('test')
}
})
})
describe('when there are no changes', () => {
beforeEach(() => {
// Arrange
mockGetLines.mockImplementationOnce(() => Promise.resolve([]))
})
it('it should not process lines', async () => {
// Act
await sgd({} as Config)
// Assert
expect(mockProcess).toHaveBeenCalledWith([])
})
})
describe('when there are changes', () => {
beforeEach(() => {
// Arrange
mockGetLines.mockImplementationOnce(() => Promise.resolve(['line']))
})
it('it should process those lines', async () => {
// Act
await sgd({} as Config)
// Assert
expect(mockProcess).toHaveBeenCalledWith(['line'])
})
})
describe('orchestration flow', () => {
it('Given valid config, When sgd runs, Then returns work with diffs and empty warnings', async () => {
// Act
const result = await sgd({} as Config)
// Assert
expect(result.diffs).toBeDefined()
expect(result.diffs.package).toBeInstanceOf(Map)
expect(result.diffs.destructiveChanges).toBeInstanceOf(Map)
expect(result.warnings).toEqual([])
})
it('Given handler produces copies, When sgd runs, Then IOExecutor receives combined copies', async () => {
// Arrange
const handlerCopy = {
kind: CopyOperationKind.GitCopy as const,
path: 'test/path',
revision: 'HEAD',
}
mockProcess.mockResolvedValueOnce({
manifests: [],
copies: [handlerCopy],
warnings: [],
})
// Act
await sgd({} as Config)
// Assert
expect(mockExecute).toHaveBeenCalledWith(
expect.arrayContaining([expect.objectContaining({ path: 'test/path' })])
)
})
it('Given post-processor produces results, When sgd runs, Then results are merged into work', async () => {
// Arrange
mockCollectAll.mockResolvedValueOnce({
manifests: [
{
target: ManifestTarget.Package,
type: 'ApexClass',
member: 'TestClass',
},
],
copies: [],
warnings: [],
})
// Act
const result = await sgd({} as Config)
// Assert
expect(result.diffs.package.has('ApexClass')).toBe(true)
expect(mockExecuteRemaining).toHaveBeenCalledTimes(1)
})
it('Given handler and post-processor produce warnings, When sgd runs, Then warnings are collected in work', async () => {
// Arrange
const handlerWarning = new Error('handler warning')
const postWarning = new Error('post-processor warning')
mockProcess.mockResolvedValueOnce({
manifests: [],
copies: [],
warnings: [handlerWarning],
})
mockCollectAll.mockResolvedValueOnce({
manifests: [],
copies: [],
warnings: [postWarning],
})
// Act
const result = await sgd({} as Config)
// Assert
expect(result.warnings).toHaveLength(2)
expect(result.warnings).toContain(handlerWarning)
expect(result.warnings).toContain(postWarning)
})
})
describe('tree index scoping', () => {
it('Given generateDelta is false, When sgd runs, Then preBuildTreeIndex is not called', async () => {
// Act
await sgd({ generateDelta: false } as Config)
// Assert
expect(mockPreBuildTreeIndex).not.toHaveBeenCalled()
})
it('Given generateDelta is true with include set, When sgd runs, Then preBuildTreeIndex is called with config.source', async () => {
// Arrange
const sut = {
generateDelta: true,
include: 'include.txt',
to: 'HEAD',
from: 'HEAD~1',
source: ['force-app'],
} as Config
// Act
await sgd(sut)
// Assert
expect(mockPreBuildTreeIndex).toHaveBeenCalledWith('HEAD', ['force-app'])
expect(mockPreBuildTreeIndex).toHaveBeenCalledWith('HEAD~1', [
'force-app',
])
expect(mockComputeTreeIndexScope).not.toHaveBeenCalled()
})
it('Given generateDelta is true with includeDestructive set, When sgd runs, Then preBuildTreeIndex is called with config.source', async () => {
// Arrange
const sut = {
generateDelta: true,
includeDestructive: 'destructive.txt',
to: 'HEAD',
from: 'HEAD~1',
source: ['src'],
} as Config
// Act
await sgd(sut)
// Assert
expect(mockPreBuildTreeIndex).toHaveBeenCalledWith('HEAD', ['src'])
expect(mockPreBuildTreeIndex).toHaveBeenCalledWith('HEAD~1', ['src'])
})
it('Given generateDelta is true with computed scope paths, When sgd runs, Then preBuildTreeIndex is called with scope paths', async () => {
// Arrange
mockComputeTreeIndexScope.mockReturnValueOnce(
new Set(['force-app/main/default/classes'])
)
const sut = {
generateDelta: true,
to: 'HEAD',
from: 'HEAD~1',
source: ['force-app'],
} as Config
// Act
await sgd(sut)
// Assert
expect(mockComputeTreeIndexScope).toHaveBeenCalled()
expect(mockPreBuildTreeIndex).toHaveBeenCalledWith('HEAD', [
'force-app/main/default/classes',
])
expect(mockPreBuildTreeIndex).toHaveBeenCalledWith('HEAD~1', [
'force-app/main/default/classes',
])
})
it('Given generateDelta is true with empty scope paths, When sgd runs, Then preBuildTreeIndex is not called', async () => {
// Arrange
mockComputeTreeIndexScope.mockReturnValueOnce(new Set())
const sut = {
generateDelta: true,
to: 'HEAD',
from: 'HEAD~1',
source: ['force-app'],
} as Config
// Act
await sgd(sut)
// Assert
expect(mockComputeTreeIndexScope).toHaveBeenCalled()
expect(mockPreBuildTreeIndex).not.toHaveBeenCalled()
})
})
})