8000 add supporting unit tests for artifact downloads with ids · actions/download-artifact@d219c63 · GitHub
[go: up one dir, main page]

Skip to content

Commit d219c63

Browse files
committed
add supporting unit tests for artifact downloads with ids
1 parent 54124fb commit d219c63

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

__tests__/download.test.ts

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,154 @@ describe('download', () => {
221221
expect.stringContaining('digest validation failed')
222222
)
223223
})
224+
225+
test('downloads a single artifact by ID', async () => {
226+
const mockArtifact = {
227+
id: 456,
228+
name: 'artifact-by-id',
229+
size: 1024,
230+
digest: 'def456'
231+
}
232+
233+
mockInputs({
234+
[Inputs.Name]: '',
235+
[Inputs.Pattern]: '',
236+
[Inputs.ArtifactIds]: '456'
237+
})
238+
239+
jest.spyOn(artifact, 'listArtifacts').mockImplementation(() =>
240+
Promise.resolve({
241+
artifacts: [mockArtifact]
242+
})
243+
)
244+
245+
await run()
246+
247+
expect(core.info).toHaveBeenCalledWith('Downloading artifacts by ID')
248+
expect(core.debug).toHaveBeenCalledWith('Parsed artifact IDs: ["456"]')
249+
expect(artifact.downloadArtifact).toHaveBeenCalledTimes(1)
250+
expect(artifact.downloadArtifact).toHaveBeenCalledWith(
251+
456,
252+
expect.objectContaining({
253+
expectedHash: mockArtifact.digest
254+
})
255+
)
256+
expect(core.info).toHaveBeenCalledWith('Total of 1 artifact(s) downloaded')
257+
})
258+
259+
test('downloads multiple artifacts by ID', async () => {
260+
const mockArtifacts = [
261+
{id: 123, name: 'first-artifact', size: 1024, digest: 'abc123'},
262+
{id: 456, name: 'second-artifact', size: 2048, digest: 'def456'},
263+
{id: 789, name: 'third-artifact', size: 3072, digest: 'ghi789'}
264+
]
265+
266+
mockInputs({
267+
[Inputs.Name]: '',
268+
[Inputs.Pattern]: '',
269+
[Inputs.ArtifactIds]: '123, 456, 789'
270+
})
271+
272+
jest.spyOn(artifact, 'listArtifacts').mockImplementation(() =>
273+
Promise.resolve({
274+
artifacts: mockArtifacts
275+
})
276+
)
277+
278+
await run()
279+
280+
expect(core.info).toHaveBeenCalledWith('Downloading artifacts by ID')
281+
expect(core.debug).toHaveBeenCalledWith(
282+
'Parsed artifact IDs: ["123","456","789"]'
283+
)
284+
expect(artifact.downloadArtifact).toHaveBeenCalledTimes(3)
285+
mockArtifacts.forEach(mockArtifact => {
286+
expect(artifact.downloadArtifact).toHaveBeenCalledWith(
287+
mockArtifact.id,
288+
expect.objectContaining({
289+
expectedHash: mockArtifact.digest
290+
})
291+
)
292+
})
293+
expect(core.info).toHaveBeenCalledWith('Total of 3 artifact(s) downloaded')
294+
})
295+
296+
test('warns when some artifact IDs are not found', async () => {
297+
const mockArtifacts = [
298+
{id: 123, name: 'found-artifact', size: 1024, digest: 'abc123'}
299+
]
300+
301+
mockInputs({
302+
[Inputs.Name]: '',
303+
[Inputs.Pattern]: '',
304+
[Inputs.ArtifactIds]: '123, 456, 789'
305+
})
306+
307+
jest.spyOn(artifact, 'listArtifacts').mockImplementation(() =>
308+
Promise.resolve({
309+
artifacts: mockArtifacts
310+
})
311+
)
312+
313+
await run()
314+
315+
expect(core.warning).toHaveBeenCalledWith(
316+
'Could not find the following artifact IDs: 456, 789'
317+
)
318+
expect(core.debug).toHaveBeenCalledWith('Found 1 artifacts by ID')
319+
expect(artifact.downloadArtifact).toHaveBeenCalledTimes(1)
320+
})
321+
322+
test('throws error when no artifacts with requested IDs are found', async () => {
323+
mockInputs({
324+
[Inputs.Name]: '',
325+
[Inputs.Pattern]: '',
326+
[Inputs.ArtifactIds]: '123, 456'
327+
})
328+
329+
jest.spyOn(artifact, 'listArtifacts').mockImplementation(() =>
330+
Promise.resolve({
331+
artifacts: []
332+
})
333+
)
334+
335+
await expect(run()).rejects.toThrow(
336+
'None of the provided artifact IDs were found'
337+
)
338+
})
339+
340+
test('throws error when artifact-ids input is empty', async () => {
341+
mockInputs({
342+
[Inputs.Name]: '',
343+
[Inputs.Pattern]: '',
344+
[Inputs.ArtifactIds]: ' '
345+
})
346+
347+
await expect(run()).rejects.toThrow(
348+
"No valid artifact IDs provided in 'artifact-ids' input"
349+
)
350+
})
351+
352+
test('throws error when some artifact IDs are not valid numbers', async () => {
353+
mockInputs({
354+
[Inputs.Name]: '',
355+
[Inputs.Pattern]: '',
356+
[Inputs.ArtifactIds]: '123, abc, 456'
357+
})
358+
359+
await expect(run()).rejects.toThrow(
360+
"Invalid artifact ID: 'abc'. Must be a number."
361+
)
362+
})
363+
364+
test('throws error when both name and artifact-ids are provided', async () => {
365+
mockInputs({
366+
[Inputs.Name]: 'some-artifact',
367+
[Inputs.ArtifactIds]: '123'
368+
})
369+
370+
await expect(run()).rejects.toThrow(
371+
"Inputs 'name' and 'artifact-ids' cannot be used together. Please specify only one."
372+
)
373+
})
224374
})

0 commit comments

Comments
 (0)
0