-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathartifact.test.ts
More file actions
118 lines (99 loc) · 3.41 KB
/
artifact.test.ts
File metadata and controls
118 lines (99 loc) · 3.41 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
import { ArtifactNotFoundError } from '@actions/artifact';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as manifest from '@sourceacademy/modules-repotools/manifest';
import { expect, test, vi } from 'vitest';
import { main } from '../index.js';
vi.mock(import('@actions/core'), async importOriginal => {
const original = await importOriginal();
return {
...original,
// Mock these functions to remove stdout output
error: vi.fn(),
info: () => { },
startGroup: () => { },
setFailed: vi.fn(),
endGroup: () => { }
};
});
const mockedResolveAllTabs = vi.spyOn(manifest, 'resolveAllTabs');
const mockedGetArtifact = vi.fn();
vi.mock(import('@actions/artifact'), async importOriginal => {
const original = await importOriginal();
return {
...original,
DefaultArtifactClient: class extends original.DefaultArtifactClient {
override getArtifact = mockedGetArtifact;
override downloadArtifact = () => Promise.resolve({});
}
};
});
const mockedExec = vi.spyOn(exec, 'exec').mockResolvedValue(0);
test('tab resolution errors cause setFailed to be called', async () => {
mockedResolveAllTabs.mockResolvedValueOnce({
severity: 'error',
errors: ['error1']
});
await main();
expect(mockedResolveAllTabs).toHaveBeenCalledOnce();
expect(core.error).toHaveBeenCalledExactlyOnceWith('error1');
expect(core.setFailed).toHaveBeenCalledExactlyOnceWith('Tab resolution failed with errors');
});
test('tabs that can\'t be found are built', async () => {
mockedResolveAllTabs.mockResolvedValueOnce({
severity: 'success',
tabs: {
Tab0: {
type: 'tab',
directory: 'tab0',
name: 'Tab0',
entryPoint: 'tab0/index.tsx',
},
Tab1: {
type: 'tab',
directory: 'tab1',
name: 'Tab1',
entryPoint: 'tab1/index.tsx',
},
}
});
mockedGetArtifact.mockImplementation(name => {
if (name === 'Tab0-tab') {
return { artifact: { id: 0 } };
}
throw new ArtifactNotFoundError();
});
await main();
expect(mockedGetArtifact).toHaveBeenCalledTimes(2);
expect(mockedResolveAllTabs).toHaveBeenCalledOnce();
const [[artifactCall0], [artifactCall1]] = mockedGetArtifact.mock.calls;
expect(artifactCall0).toEqual('Tab0-tab');
expect(artifactCall1).toEqual('Tab1-tab');
expect(exec.exec).toHaveBeenCalledTimes(2);
const [[execCmd0, execCall0], [execCmd1, execCall1]] = vi.mocked(exec.exec).mock.calls;
expect(execCmd0).toEqual('yarn workspaces focus');
expect(execCall0).toContain('@sourceacademy/tab-Tab1');
expect(execCall0).not.toContain('@sourceacademy/tab-Tab0');
expect(execCmd1).toEqual('yarn workspaces foreach -pA');
expect(execCall1).toContain('@sourceacademy/tab-Tab1');
expect(execCall1).not.toContain('@sourceacademy/tab-Tab0');
});
test('install failure means build doesn\'t happen', async () => {
mockedResolveAllTabs.mockResolvedValueOnce({
severity: 'success',
tabs: {
Tab0: {
type: 'tab',
directory: 'tab0',
name: 'Tab0',
entryPoint: 'tab0/index.tsx',
},
}
});
mockedGetArtifact.mockRejectedValueOnce(new ArtifactNotFoundError());
mockedExec.mockResolvedValueOnce(1);
await main();
expect(mockedGetArtifact).toHaveBeenCalledOnce();
expect(exec.exec).toHaveBeenCalledOnce();
expect(core.setFailed).toHaveBeenCalledExactlyOnceWith('yarn workspace focus failed');
});