Skip to content

Commit b4c67ab

Browse files
committed
Convert to ESM.
Context: actions/typescript-action#969
1 parent 59f089d commit b4c67ab

35 files changed

+166836
-177257
lines changed

__fixtures__/cache.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as cache from '@actions/cache'
2+
import { jest } from '@jest/globals'
3+
4+
export const ReserveCacheError = cache.ReserveCacheError
5+
export const restoreCache = jest.fn<typeof cache.restoreCache>()
6+
export const saveCache = jest.fn<typeof cache.saveCache>()
7+
export const ValidationError = cache.ValidationError

__fixtures__/core.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type * as core from '@actions/core'
2+
import { jest } from '@jest/globals'
3+
4+
export const debug = jest.fn<typeof core.debug>()
5+
export const error = jest.fn<typeof core.error>()
6+
export const exportVariable = jest.fn<typeof core.exportVariable>()
7+
export const getInput = jest.fn<typeof core.getInput>()
8+
export const getState = jest.fn<typeof core.getState>()
9+
export const info = jest.fn<typeof core.info>()
10+
export const saveState = jest.fn<typeof core.saveState>()
11+
export const setFailed = jest.fn<typeof core.setFailed>()
12+
export const setOutput = jest.fn<typeof core.setOutput>()
13+
export const warning = jest.fn<typeof core.warning>()

__fixtures__/github.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as github from '@actions/github'
2+
import { jest } from '@jest/globals'
3+
4+
export const context = {
5+
repo: {
6+
owner: 'test-owner',
7+
repo: 'test-repo'
8+
},
9+
sha: 'test-sha',
10+
ref: 'test-ref',
11+
workflow: 'test-workflow',
12+
job: 'test-job',
13+
runId: '12345'
14+
}
15+
export const getOctokit = jest.fn<typeof github.getOctokit>()

__fixtures__/glob.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as glob from '@actions/glob'
2+
import { jest } from '@jest/globals'
3+
4+
export const create = jest.fn<typeof glob.create>()

__tests__/cache.test.ts

Lines changed: 68 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,28 @@
2424
* Forked from https://github.com/actions/setup-java/blob/5b36705a13905facb447b6812d613a06a07e371d/__tests__/cache.test.ts
2525
*/
2626

27+
import { jest } from '@jest/globals'
2728
import { mkdtempSync } from 'fs'
2829
import { tmpdir } from 'os'
2930
import { join } from 'path'
30-
import { restore, save } from '../src/features/cache'
3131
import * as fs from 'fs'
3232
import * as os from 'os'
33-
import * as core from '@actions/core'
34-
import * as cache from '@actions/cache'
33+
import * as cache from '../__fixtures__/cache.js'
34+
import * as core from '../__fixtures__/core.js'
35+
36+
// Mocks should be declared before the module being tested is imported.
37+
jest.unstable_mockModule('@actions/core', () => core)
38+
jest.unstable_mockModule('@actions/cache', () => cache)
39+
40+
// The module being tested should be imported dynamically. This ensures that the
41+
// mocks are used in place of any actual dependencies.
42+
const { restore, save } = await import('../src/features/cache.js')
3543

3644
describe('dependency cache', () => {
3745
const ORIGINAL_RUNNER_OS = process.env['RUNNER_OS']
3846
const ORIGINAL_GITHUB_WORKSPACE = process.env['GITHUB_WORKSPACE']
3947
const ORIGINAL_CWD = process.cwd()
4048
let workspace: string
41-
let spyInfo: jest.SpyInstance<void, Parameters<typeof core.info>>
42-
let spyWarning: jest.SpyInstance<void, Parameters<typeof core.warning>>
43-
let spyDebug: jest.SpyInstance<void, Parameters<typeof core.debug>>
44-
let spySaveState: jest.SpyInstance<void, Parameters<typeof core.saveState>>
4549

4650
beforeEach(() => {
4751
workspace = mkdtempSync(join(tmpdir(), 'setup-graalvm-cache-'))
@@ -65,17 +69,9 @@ describe('dependency cache', () => {
6569
})
6670

6771
beforeEach(() => {
68-
spyInfo = jest.spyOn(core, 'info')
69-
spyInfo.mockImplementation(() => null)
70-
71-
spyWarning = jest.spyOn(core, 'warning')
72-
spyWarning.mockImplementation(() => null)
73-
74-
spyDebug = jest.spyOn(core, 'debug')
75-
spyDebug.mockImplementation(() => null)
76-
77-
spySaveState = jest.spyOn(core, 'saveState')
78-
spySaveState.mockImplementation(() => null)
72+
core.info.mockImplementation(() => null)
73+
core.warning.mockImplementation(() => null)
74+
core.debug.mockImplementation(() => null)
7975
})
8076

8177
afterEach(() => {
@@ -86,13 +82,8 @@ describe('dependency cache', () => {
8682
})
8783

8884
describe('restore', () => {
89-
let spyCacheRestore: jest.SpyInstance<ReturnType<typeof cache.restoreCache>, Parameters<typeof cache.restoreCache>>
90-
9185
beforeEach(() => {
92-
spyCacheRestore = jest
93-
.spyOn(cache, 'restoreCache')
94-
.mockImplementation((_paths: string[], _primaryKey: string) => Promise.resolve(undefined))
95-
spyWarning.mockImplementation(() => null)
86+
cache.restoreCache.mockImplementation((_paths: string[], _primaryKey: string) => Promise.resolve(undefined))
9687
})
9788

9889
it('throws error if unsupported package manager specified', () => {
@@ -111,9 +102,9 @@ describe('dependency cache', () => {
111102
createFile(join(workspace, 'pom.xml'))
112103

113104
await restore('maven')
114-
expect(spyCacheRestore).toHaveBeenCalled()
115-
expect(spyWarning).not.toHaveBeenCalled()
116-
expect(spyInfo).toHaveBeenCalledWith('maven cache is not found')
105+
expect(cache.restoreCache).toHaveBeenCalled()
106+
expect(core.warning).not.toHaveBeenCalled()
107+
expect(core.info).toHaveBeenCalledWith('maven cache is not found')
117108
})
118109
})
119110
describe('for gradle', () => {
@@ -128,27 +119,27 @@ describe('dependency cache', () => {
128119
createFile(join(workspace, 'build.gradle'))
129120

130121
await restore('gradle')
131-
expect(spyCacheRestore).toHaveBeenCalled()
132-
expect(spyWarning).not.toHaveBeenCalled()
133-
expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found')
122+
expect(cache.restoreCache).toHaveBeenCalled()
123+
expect(core.warning).not.toHaveBeenCalled()
124+
expect(core.info).toHaveBeenCalledWith('gradle cache is not found')
134125
})
135126
it('downloads cache based on build.gradle.kts', async () => {
136127
createFile(join(workspace, 'build.gradle.kts'))
137128

138129
await restore('gradle')
139-
expect(spyCacheRestore).toHaveBeenCalled()
140-
expect(spyWarning).not.toHaveBeenCalled()
141-
expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found')
130+
expect(cache.restoreCache).toHaveBeenCalled()
131+
expect(core.warning).not.toHaveBeenCalled()
132+
expect(core.info).toHaveBeenCalledWith('gradle cache is not found')
142133
})
143134
})
144135
it('downloads cache based on buildSrc/Versions.kt', async () => {
145136
createDirectory(join(workspace, 'buildSrc'))
146137
createFile(join(workspace, 'buildSrc', 'Versions.kt'))
147138

148139
await restore('gradle')
149-
expect(spyCacheRestore).toHaveBeenCalled()
150-
expect(spyWarning).not.toHaveBeenCalled()
151-
expect(spyInfo).toHaveBeenCalledWith('gradle cache is not found')
140+
expect(cache.restoreCache).toHaveBeenCalled()
141+
expect(core.warning).not.toHaveBeenCalled()
142+
expect(core.info).toHaveBeenCalledWith('gradle cache is not found')
152143
})
153144
describe('for sbt', () => {
154145
it('throws error if no build.sbt found', async () => {
@@ -162,39 +153,35 @@ describe('dependency cache', () => {
162153
createFile(join(workspace, 'build.sbt'))
163154

164155
await restore('sbt')
165-
expect(spyCacheRestore).toHaveBeenCalled()
166-
expect(spyWarning).not.toHaveBeenCalled()
167-
expect(spyInfo).toHaveBeenCalledWith('sbt cache is not found')
156+
expect(cache.restoreCache).toHaveBeenCalled()
157+
expect(core.warning).not.toHaveBeenCalled()
158+
expect(core.info).toHaveBeenCalledWith('sbt cache is not found')
168159
})
169160
})
170161
})
171162
describe('save', () => {
172-
let spyCacheSave: jest.SpyInstance<ReturnType<typeof cache.saveCache>, Parameters<typeof cache.saveCache>>
173-
174163
beforeEach(() => {
175-
spyCacheSave = jest
176-
.spyOn(cache, 'saveCache')
177-
.mockImplementation((_paths: string[], _key: string) => Promise.resolve(0))
178-
spyWarning.mockImplementation(() => null)
164+
cache.saveCache.mockImplementation((_paths: string[], _key: string) => Promise.resolve(0))
165+
core.warning.mockImplementation(() => null)
179166
})
180167

181168
it('throws error if unsupported package manager specified', () => {
182169
return expect(save('ant')).rejects.toThrow('unknown package manager specified: ant')
183170
})
184171

185172
it('save with -1 cacheId , should not fail workflow', async () => {
186-
spyCacheSave.mockImplementation(() => Promise.resolve(-1))
173+
cache.saveCache.mockImplementation(() => Promise.resolve(-1))
187174
createStateForMissingBuildFile()
188175

189176
await save('maven')
190-
expect(spyCacheSave).toHaveBeenCalled()
191-
expect(spyWarning).not.toHaveBeenCalled()
192-
expect(spyInfo).toHaveBeenCalled()
193-
expect(spyInfo).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
177+
expect(cache.saveCache).toHaveBeenCalled()
178+
expect(core.warning).not.toHaveBeenCalled()
179+
expect(core.info).toHaveBeenCalled()
180+
expect(core.info).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
194181
})
195182

196183
it('saves with error from toolkit, should fail workflow', async () => {
197-
spyCacheSave.mockImplementation(() => Promise.reject(new cache.ValidationError('Validation failed')))
184+
cache.saveCache.mockImplementation(() => Promise.reject(new cache.ValidationError('Validation failed')))
198185
createStateForMissingBuildFile()
199186

200187
expect.assertions(1)
@@ -205,106 +192,106 @@ describe('dependency cache', () => {
205192
it('uploads cache even if no pom.xml found', async () => {
206193
createStateForMissingBuildFile()
207194
await save('maven')
208-
expect(spyCacheSave).toHaveBeenCalled()
209-
expect(spyWarning).not.toHaveBeenCalled()
195+
expect(cache.saveCache).toHaveBeenCalled()
196+
expect(core.warning).not.toHaveBeenCalled()
210197
})
211198
it('does not upload cache if no restore run before', async () => {
212199
createFile(join(workspace, 'pom.xml'))
213200

214201
await save('maven')
215-
expect(spyCacheSave).not.toHaveBeenCalled()
216-
expect(spyWarning).toHaveBeenCalledWith('Error retrieving key from state.')
202+
expect(cache.saveCache).not.toHaveBeenCalled()
203+
expect(core.warning).toHaveBeenCalledWith('Error retrieving key from state.')
217204
})
218205
it('uploads cache', async () => {
219206
createFile(join(workspace, 'pom.xml'))
220207
createStateForSuccessfulRestore()
221208

222209
await save('maven')
223-
expect(spyCacheSave).toHaveBeenCalled()
224-
expect(spyWarning).not.toHaveBeenCalled()
225-
expect(spyInfo).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
210+
expect(cache.saveCache).toHaveBeenCalled()
211+
expect(core.warning).not.toHaveBeenCalled()
212+
expect(core.info).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
226213
})
227214
})
228215
describe('for gradle', () => {
229216
it('uploads cache even if no build.gradle found', async () => {
230217
createStateForMissingBuildFile()
231218

232219
await save('gradle')
233-
expect(spyCacheSave).toHaveBeenCalled()
234-
expect(spyWarning).not.toHaveBeenCalled()
220+
expect(cache.saveCache).toHaveBeenCalled()
221+
expect(core.warning).not.toHaveBeenCalled()
235222
})
236223
it('does not upload cache if no restore run before', async () => {
237224
createFile(join(workspace, 'build.gradle'))
238225

239226
await save('gradle')
240-
expect(spyCacheSave).not.toHaveBeenCalled()
241-
expect(spyWarning).toHaveBeenCalledWith('Error retrieving key from state.')
227+
expect(cache.saveCache).not.toHaveBeenCalled()
228+
expect(core.warning).toHaveBeenCalledWith('Error retrieving key from state.')
242229
})
243230
it('uploads cache based on build.gradle', async () => {
244231
createFile(join(workspace, 'build.gradle'))
245232
createStateForSuccessfulRestore()
246233

247234
await save('gradle')
248-
expect(spyCacheSave).toHaveBeenCalled()
249-
expect(spyWarning).not.toHaveBeenCalled()
250-
expect(spyInfo).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
235+
expect(cache.saveCache).toHaveBeenCalled()
236+
expect(core.warning).not.toHaveBeenCalled()
237+
expect(core.info).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
251238
})
252239
it('uploads cache based on build.gradle.kts', async () => {
253240
createFile(join(workspace, 'build.gradle.kts'))
254241
createStateForSuccessfulRestore()
255242

256243
await save('gradle')
257-
expect(spyCacheSave).toHaveBeenCalled()
258-
expect(spyWarning).not.toHaveBeenCalled()
259-
expect(spyInfo).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
244+
expect(cache.saveCache).toHaveBeenCalled()
245+
expect(core.warning).not.toHaveBeenCalled()
246+
expect(core.info).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
260247
})
261248
it('uploads cache based on buildSrc/Versions.kt', async () => {
262249
createDirectory(join(workspace, 'buildSrc'))
263250
createFile(join(workspace, 'buildSrc', 'Versions.kt'))
264251
createStateForSuccessfulRestore()
265252

266253
await save('gradle')
267-
expect(spyCacheSave).toHaveBeenCalled()
268-
expect(spyWarning).not.toHaveBeenCalled()
269-
expect(spyInfo).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
254+
expect(cache.saveCache).toHaveBeenCalled()
255+
expect(core.warning).not.toHaveBeenCalled()
256+
expect(core.info).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
270257
})
271258
})
272259
describe('for sbt', () => {
273260
it('uploads cache even if no build.sbt found', async () => {
274261
createStateForMissingBuildFile()
275262
await save('sbt')
276-
expect(spyCacheSave).toHaveBeenCalled()
277-
expect(spyWarning).not.toHaveBeenCalled()
263+
expect(cache.saveCache).toHaveBeenCalled()
264+
expect(core.warning).not.toHaveBeenCalled()
278265
})
279266
it('does not upload cache if no restore run before', async () => {
280267
createFile(join(workspace, 'build.sbt'))
281268

282269
await save('sbt')
283-
expect(spyCacheSave).not.toHaveBeenCalled()
284-
expect(spyWarning).toHaveBeenCalledWith('Error retrieving key from state.')
270+
expect(cache.saveCache).not.toHaveBeenCalled()
271+
expect(core.warning).toHaveBeenCalledWith('Error retrieving key from state.')
285272
})
286273
it('uploads cache', async () => {
287274
createFile(join(workspace, 'build.sbt'))
288275
createStateForSuccessfulRestore()
289276

290277
await save('sbt')
291-
expect(spyCacheSave).toHaveBeenCalled()
292-
expect(spyWarning).not.toHaveBeenCalled()
293-
expect(spyInfo).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
278+
expect(cache.saveCache).toHaveBeenCalled()
279+
expect(core.warning).not.toHaveBeenCalled()
280+
expect(core.info).toHaveBeenCalledWith(expect.stringMatching(/^Cache saved with the key:.*/))
294281
})
295282
})
296283
})
297284
})
298285

299286
function resetState() {
300-
jest.spyOn(core, 'getState').mockReset()
287+
core.getState.mockReset()
301288
}
302289

303290
/**
304291
* Create states to emulate a restore process without build file.
305292
*/
306293
function createStateForMissingBuildFile() {
307-
jest.spyOn(core, 'getState').mockImplementation((name) => {
294+
core.getState.mockImplementation((name) => {
308295
switch (name) {
309296
case 'cache-primary-key':
310297
return 'setup-graalvm-cache-'
@@ -318,7 +305,7 @@ function createStateForMissingBuildFile() {
318305
* Create states to emulate a successful restore process.
319306
*/
320307
function createStateForSuccessfulRestore() {
321-
jest.spyOn(core, 'getState').mockImplementation((name) => {
308+
core.getState.mockImplementation((name) => {
322309
switch (name) {
323310
case 'cache-primary-key':
324311
return 'setup-graalvm-cache-primary-key'

0 commit comments

Comments
 (0)