|
1 | 1 | import fs from 'node:fs'; |
2 | | -import {createRequire} from 'node:module'; |
3 | 2 | import os from 'node:os'; |
4 | 3 | import path from 'node:path'; |
5 | 4 | import {vi} from 'vitest'; |
6 | 5 |
|
7 | 6 | const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-action-')); |
8 | 7 |
|
| 8 | +const githubPayload = { |
| 9 | + repository: { |
| 10 | + private: true |
| 11 | + } |
| 12 | +}; |
| 13 | + |
| 14 | +const githubEventPath = path.join(tmpDir, 'github-event.json'); |
| 15 | +fs.writeFileSync(githubEventPath, JSON.stringify(githubPayload)); |
| 16 | + |
9 | 17 | process.env = Object.assign({}, process.env, { |
10 | 18 | TEMP: tmpDir, |
11 | 19 | GITHUB_REPOSITORY: 'docker/build-push-action', |
| 20 | + GITHUB_REF: 'refs/heads/master', |
| 21 | + GITHUB_RUN_ID: '123456789', |
| 22 | + GITHUB_RUN_ATTEMPT: '1', |
| 23 | + GITHUB_EVENT_PATH: githubEventPath, |
12 | 24 | RUNNER_TEMP: path.join(tmpDir, 'runner-temp'), |
13 | 25 | RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache') |
14 | 26 | }); |
15 | 27 |
|
16 | | -const require = createRequire(import.meta.url); |
17 | | -type RequireCacheEntry = NonNullable<(typeof require.cache)[string]>; |
18 | | - |
19 | | -const githubMock = { |
| 28 | +vi.mock('@actions/github', () => ({ |
20 | 29 | context: { |
21 | 30 | repo: { |
22 | 31 | owner: 'docker', |
23 | 32 | repo: 'build-push-action' |
24 | 33 | }, |
25 | 34 | ref: 'refs/heads/master', |
26 | 35 | runId: 123456789, |
27 | | - payload: { |
28 | | - after: '860c1904a1ce19322e91ac35af1ab07466440c37', |
29 | | - base_ref: null, |
30 | | - before: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a', |
31 | | - commits: [ |
32 | | - { |
33 | | - author: { |
34 | | - email: 'crazy-max@users.noreply.github.com', |
35 | | - name: 'CrazyMax', |
36 | | - username: 'crazy-max' |
37 | | - }, |
38 | | - committer: { |
39 | | - email: 'crazy-max@users.noreply.github.com', |
40 | | - name: 'CrazyMax', |
41 | | - username: 'crazy-max' |
42 | | - }, |
43 | | - distinct: true, |
44 | | - id: '860c1904a1ce19322e91ac35af1ab07466440c37', |
45 | | - message: 'hello dev', |
46 | | - timestamp: '2022-04-19T11:27:24+02:00', |
47 | | - tree_id: 'd2c60af597e863787d2d27f569e30495b0b92820', |
48 | | - url: 'https://github.com/docker/test-docker-action/commit/860c1904a1ce19322e91ac35af1ab07466440c37' |
49 | | - } |
50 | | - ], |
51 | | - compare: 'https://github.com/docker/test-docker-action/compare/5f3331d7f704...860c1904a1ce', |
52 | | - created: false, |
53 | | - deleted: false, |
54 | | - forced: false, |
55 | | - head_commit: { |
56 | | - author: { |
57 | | - email: 'crazy-max@users.noreply.github.com', |
58 | | - name: 'CrazyMax', |
59 | | - username: 'crazy-max' |
60 | | - }, |
61 | | - committer: { |
62 | | - email: 'crazy-max@users.noreply.github.com', |
63 | | - name: 'CrazyMax', |
64 | | - username: 'crazy-max' |
65 | | - }, |
66 | | - distinct: true, |
67 | | - id: '860c1904a1ce19322e91ac35af1ab07466440c37', |
68 | | - message: 'hello dev', |
69 | | - timestamp: '2022-04-19T11:27:24+02:00', |
70 | | - tree_id: 'd2c60af597e863787d2d27f569e30495b0b92820', |
71 | | - url: 'https://github.com/docker/test-docker-action/commit/860c1904a1ce19322e91ac35af1ab07466440c37' |
72 | | - }, |
73 | | - organization: { |
74 | | - avatar_url: 'https://avatars.githubusercontent.com/u/5429470?v=4', |
75 | | - description: 'Docker helps developers bring their ideas to life by conquering the complexity of app development.', |
76 | | - events_url: 'https://api.github.com/orgs/docker/events', |
77 | | - hooks_url: 'https://api.github.com/orgs/docker/hooks', |
78 | | - id: 5429470, |
79 | | - issues_url: 'https://api.github.com/orgs/docker/issues', |
80 | | - login: 'docker', |
81 | | - members_url: 'https://api.github.com/orgs/docker/members{/member}', |
82 | | - node_id: 'MDEyOk9yZ2FuaXphdGlvbjU0Mjk0NzA=', |
83 | | - public_members_url: 'https://api.github.com/orgs/docker/public_members{/member}', |
84 | | - repos_url: 'https://api.github.com/orgs/docker/repos', |
85 | | - url: 'https://api.github.com/orgs/docker' |
86 | | - }, |
87 | | - pusher: { |
88 | | - email: 'github@crazymax.dev', |
89 | | - name: 'crazy-max' |
90 | | - }, |
91 | | - ref: 'refs/heads/dev', |
92 | | - repository: { |
93 | | - allow_forking: true, |
94 | | - archive_url: 'https://api.github.com/repos/docker/test-docker-action/{archive_format}{/ref}', |
95 | | - archived: false, |
96 | | - assignees_url: 'https://api.github.com/repos/docker/test-docker-action/assignees{/user}', |
97 | | - blobs_url: 'https://api.github.com/repos/docker/test-docker-action/git/blobs{/sha}', |
98 | | - branches_url: 'https://api.github.com/repos/docker/test-docker-action/branches{/branch}', |
99 | | - clone_url: 'https://github.com/docker/test-docker-action.git', |
100 | | - collaborators_url: 'https://api.github.com/repos/docker/test-docker-action/collaborators{/collaborator}', |
101 | | - comments_url: 'https://api.github.com/repos/docker/test-docker-action/comments{/number}', |
102 | | - commits_url: 'https://api.github.com/repos/docker/test-docker-action/commits{/sha}', |
103 | | - compare_url: 'https://api.github.com/repos/docker/test-docker-action/compare/{base}...{head}', |
104 | | - contents_url: 'https://api.github.com/repos/docker/test-docker-action/contents/{+path}', |
105 | | - contributors_url: 'https://api.github.com/repos/docker/test-docker-action/contributors', |
106 | | - created_at: 1596792180, |
107 | | - default_branch: 'master', |
108 | | - deployments_url: 'https://api.github.com/repos/docker/test-docker-action/deployments', |
109 | | - description: 'Test "Docker" Actions', |
110 | | - disabled: false, |
111 | | - downloads_url: 'https://api.github.com/repos/docker/test-docker-action/downloads', |
112 | | - events_url: 'https://api.github.com/repos/docker/test-docker-action/events', |
113 | | - fork: false, |
114 | | - forks: 1, |
115 | | - forks_count: 1, |
116 | | - forks_url: 'https://api.github.com/repos/docker/test-docker-action/forks', |
117 | | - full_name: 'docker/test-docker-action', |
118 | | - git_commits_url: 'https://api.github.com/repos/docker/test-docker-action/git/commits{/sha}', |
119 | | - git_refs_url: 'https://api.github.com/repos/docker/test-docker-action/git/refs{/sha}', |
120 | | - git_tags_url: 'https://api.github.com/repos/docker/test-docker-action/git/tags{/sha}', |
121 | | - git_url: 'git://github.com/docker/test-docker-action.git', |
122 | | - has_downloads: true, |
123 | | - has_issues: true, |
124 | | - has_pages: false, |
125 | | - has_projects: true, |
126 | | - has_wiki: true, |
127 | | - homepage: '', |
128 | | - hooks_url: 'https://api.github.com/repos/docker/test-docker-action/hooks', |
129 | | - html_url: 'https://github.com/docker/test-docker-action', |
130 | | - id: 285789493, |
131 | | - is_template: false, |
132 | | - issue_comment_url: 'https://api.github.com/repos/docker/test-docker-action/issues/comments{/number}', |
133 | | - issue_events_url: 'https://api.github.com/repos/docker/test-docker-action/issues/events{/number}', |
134 | | - issues_url: 'https://api.github.com/repos/docker/test-docker-action/issues{/number}', |
135 | | - keys_url: 'https://api.github.com/repos/docker/test-docker-action/keys{/key_id}', |
136 | | - labels_url: 'https://api.github.com/repos/docker/test-docker-action/labels{/name}', |
137 | | - language: 'JavaScript', |
138 | | - languages_url: 'https://api.github.com/repos/docker/test-docker-action/languages', |
139 | | - license: { |
140 | | - key: 'mit', |
141 | | - name: 'MIT License', |
142 | | - node_id: 'MDc6TGljZW5zZTEz', |
143 | | - spdx_id: 'MIT', |
144 | | - url: 'https://api.github.com/licenses/mit' |
145 | | - }, |
146 | | - master_branch: 'master', |
147 | | - merges_url: 'https://api.github.com/repos/docker/test-docker-action/merges', |
148 | | - milestones_url: 'https://api.github.com/repos/docker/test-docker-action/milestones{/number}', |
149 | | - mirror_url: null, |
150 | | - name: 'test-docker-action', |
151 | | - node_id: 'MDEwOlJlcG9zaXRvcnkyODU3ODk0OTM=', |
152 | | - notifications_url: 'https://api.github.com/repos/docker/test-docker-action/notifications{?since,all,participating}', |
153 | | - open_issues: 6, |
154 | | - open_issues_count: 6, |
155 | | - organization: 'docker', |
156 | | - owner: { |
157 | | - avatar_url: 'https://avatars.githubusercontent.com/u/5429470?v=4', |
158 | | - email: 'info@docker.com', |
159 | | - events_url: 'https://api.github.com/users/docker/events{/privacy}', |
160 | | - followers_url: 'https://api.github.com/users/docker/followers', |
161 | | - following_url: 'https://api.github.com/users/docker/following{/other_user}', |
162 | | - gists_url: 'https://api.github.com/users/docker/gists{/gist_id}', |
163 | | - gravatar_id: '', |
164 | | - html_url: 'https://github.com/docker', |
165 | | - id: 5429470, |
166 | | - login: 'docker', |
167 | | - name: 'docker', |
168 | | - node_id: 'MDEyOk9yZ2FuaXphdGlvbjU0Mjk0NzA=', |
169 | | - organizations_url: 'https://api.github.com/users/docker/orgs', |
170 | | - received_events_url: 'https://api.github.com/users/docker/received_events', |
171 | | - repos_url: 'https://api.github.com/users/docker/repos', |
172 | | - site_admin: false, |
173 | | - starred_url: 'https://api.github.com/users/docker/starred{/owner}{/repo}', |
174 | | - subscriptions_url: 'https://api.github.com/users/docker/subscriptions', |
175 | | - type: 'Organization', |
176 | | - url: 'https://api.github.com/users/docker' |
177 | | - }, |
178 | | - private: true, |
179 | | - pulls_url: 'https://api.github.com/repos/docker/test-docker-action/pulls{/number}', |
180 | | - pushed_at: 1650360446, |
181 | | - releases_url: 'https://api.github.com/repos/docker/test-docker-action/releases{/id}', |
182 | | - size: 796, |
183 | | - ssh_url: 'git@github.com:docker/test-docker-action.git', |
184 | | - stargazers: 0, |
185 | | - stargazers_count: 0, |
186 | | - stargazers_url: 'https://api.github.com/repos/docker/test-docker-action/stargazers', |
187 | | - statuses_url: 'https://api.github.com/repos/docker/test-docker-action/statuses/{sha}', |
188 | | - subscribers_url: 'https://api.github.com/repos/docker/test-docker-action/subscribers', |
189 | | - subscription_url: 'https://api.github.com/repos/docker/test-docker-action/subscription', |
190 | | - svn_url: 'https://github.com/docker/test-docker-action', |
191 | | - tags_url: 'https://api.github.com/repos/docker/test-docker-action/tags', |
192 | | - teams_url: 'https://api.github.com/repos/docker/test-docker-action/teams', |
193 | | - topics: [], |
194 | | - trees_url: 'https://api.github.com/repos/docker/test-docker-action/git/trees{/sha}', |
195 | | - updated_at: '2022-04-19T09:05:09Z', |
196 | | - url: 'https://github.com/docker/test-docker-action', |
197 | | - visibility: 'private', |
198 | | - watchers: 0, |
199 | | - watchers_count: 0 |
200 | | - }, |
201 | | - sender: { |
202 | | - avatar_url: 'https://avatars.githubusercontent.com/u/1951866?v=4', |
203 | | - events_url: 'https://api.github.com/users/crazy-max/events{/privacy}', |
204 | | - followers_url: 'https://api.github.com/users/crazy-max/followers', |
205 | | - following_url: 'https://api.github.com/users/crazy-max/following{/other_user}', |
206 | | - gists_url: 'https://api.github.com/users/crazy-max/gists{/gist_id}', |
207 | | - gravatar_id: '', |
208 | | - html_url: 'https://github.com/crazy-max', |
209 | | - id: 1951866, |
210 | | - login: 'crazy-max', |
211 | | - node_id: 'MDQ6VXNlcjE5NTE4NjY=', |
212 | | - organizations_url: 'https://api.github.com/users/crazy-max/orgs', |
213 | | - received_events_url: 'https://api.github.com/users/crazy-max/received_events', |
214 | | - repos_url: 'https://api.github.com/users/crazy-max/repos', |
215 | | - site_admin: false, |
216 | | - starred_url: 'https://api.github.com/users/crazy-max/starred{/owner}{/repo}', |
217 | | - subscriptions_url: 'https://api.github.com/users/crazy-max/subscriptions', |
218 | | - type: 'User', |
219 | | - url: 'https://api.github.com/users/crazy-max' |
220 | | - } |
221 | | - } |
| 36 | + payload: githubPayload |
222 | 37 | }, |
223 | 38 | getOctokit: vi.fn() |
224 | | -}; |
225 | | - |
226 | | -vi.mock('@actions/github', () => githubMock); |
227 | | -vi.doMock(require.resolve('@docker/actions-toolkit/node_modules/@actions/github'), () => githubMock); |
228 | | - |
229 | | -for (const mod of ['@docker/actions-toolkit/node_modules/@actions/github']) { |
230 | | - try { |
231 | | - const resolved = require.resolve(mod); |
232 | | - require.cache[resolved] = { |
233 | | - id: resolved, |
234 | | - filename: resolved, |
235 | | - loaded: true, |
236 | | - exports: githubMock, |
237 | | - children: [], |
238 | | - paths: [] |
239 | | - } as RequireCacheEntry; |
240 | | - } catch { |
241 | | - // Ignore unresolved optional paths; vi.mock handles module-level mocking. |
242 | | - } |
243 | | -} |
| 39 | +})); |
0 commit comments