Skip to content

Commit 51f8149

Browse files
authored
fix: removed tool caching if tool cache is already present (#332)
1 parent 04b2609 commit 51f8149

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

__tests__/installer/linux.test.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,46 @@ describe('linux toolchain installation verification', () => {
114114
}
115115
)
116116

117-
it('tests installation with cache', async () => {
117+
it('tests installation with action cache', async () => {
118+
const installer = new LinuxToolchainInstaller(toolchain)
119+
const cached = path.resolve('tool', 'cached', 'path')
120+
const swiftPath = path.join(cached, 'usr', 'bin')
121+
jest.spyOn(toolCache, 'find').mockReturnValue('')
122+
jest.spyOn(cache, 'restoreCache').mockResolvedValue(cached)
123+
jest.spyOn(core, 'getBooleanInput').mockReturnValue(true)
124+
jest.spyOn(exec, 'exec').mockResolvedValue(0)
125+
const downloadSpy = jest.spyOn(toolCache, 'downloadTool')
126+
const extractSpy = jest.spyOn(toolCache, 'extractTar')
127+
const toolCacheSpy = jest.spyOn(toolCache, 'cacheDir')
128+
const actionCacheSpy = jest.spyOn(cache, 'saveCache')
129+
toolCacheSpy.mockResolvedValue(cached)
130+
await installer.install('aarch64')
131+
const toolCacheKey = `${toolchain.dir}-${toolchain.platform}`
132+
const tmpDir = process.env.RUNNER_TEMP || os.tmpdir()
133+
const restore = path.join(tmpDir, 'setup-swift', toolCacheKey)
134+
expect(process.env.PATH?.includes(swiftPath)).toBeTruthy()
135+
expect(toolCacheSpy.mock.calls[0]?.[0]).toBe(restore)
136+
expect(toolCacheSpy.mock.calls[0]?.[1]).toBe(toolCacheKey)
137+
expect(toolCacheSpy.mock.calls[0]?.[2]).toBe('5.8.0')
138+
for (const spy of [downloadSpy, extractSpy, actionCacheSpy]) {
139+
expect(spy).not.toHaveBeenCalled()
140+
}
141+
})
142+
143+
it('tests installation with tool cache', async () => {
118144
const installer = new LinuxToolchainInstaller(toolchain)
119145
const cached = path.resolve('tool', 'cached', 'path')
120146
const swiftPath = path.join(cached, 'usr', 'bin')
121147
jest.spyOn(toolCache, 'find').mockReturnValue(cached)
122-
jest.spyOn(toolCache, 'cacheDir').mockResolvedValue(cached)
123-
jest.spyOn(cache, 'saveCache').mockResolvedValue(1)
124148
jest.spyOn(core, 'getBooleanInput').mockReturnValue(true)
125149
jest.spyOn(exec, 'exec').mockResolvedValue(0)
126150
const downloadSpy = jest.spyOn(toolCache, 'downloadTool')
127151
const extractSpy = jest.spyOn(toolCache, 'extractTar')
152+
const toolCacheSpy = jest.spyOn(toolCache, 'cacheDir')
153+
const actionCacheSpy = jest.spyOn(cache, 'saveCache')
128154
await installer.install('aarch64')
129155
expect(process.env.PATH?.includes(swiftPath)).toBeTruthy()
130-
for (const spy of [downloadSpy, extractSpy]) {
156+
for (const spy of [downloadSpy, extractSpy, toolCacheSpy, actionCacheSpy]) {
131157
expect(spy).not.toHaveBeenCalled()
132158
}
133159
})

dist/index.js

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/installer/base.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export abstract class ToolchainInstaller<Snapshot extends ToolchainSnapshot> {
4444
const actionCacheKey = arch ? `${toolCacheKey}-${arch}` : toolCacheKey
4545
const version = this.version?.raw
4646
let tool: string | undefined
47-
let cacheHit = false
47+
let toolCacheHit = false
48+
let actionCacheHit = false
4849
if (version) {
4950
core.debug(
5051
`Finding tool with key: "${toolCacheKey}", version: "${version}" and arch: "${arch}" in tool cache`
@@ -60,7 +61,7 @@ export abstract class ToolchainInstaller<Snapshot extends ToolchainSnapshot> {
6061
`Restored snapshot at "${restore}" from key "${actionCacheKey}"`
6162
)
6263
tool = restore
63-
cacheHit = true
64+
actionCacheHit = true
6465
} else {
6566
const resource = await this.download()
6667
const installation = await this.unpack(resource)
@@ -69,20 +70,25 @@ export abstract class ToolchainInstaller<Snapshot extends ToolchainSnapshot> {
6970
}
7071
} else {
7172
core.debug(`Found tool at "${tool}" in tool cache`)
72-
cacheHit = true
73+
actionCacheHit = true
74+
toolCacheHit = true
7375
}
7476

7577
if (tool && version) {
76-
tool = await toolCache.cacheDir(tool, toolCacheKey, version, arch)
78+
if (!toolCacheHit) {
79+
tool = await toolCache.cacheDir(tool, toolCacheKey, version, arch)
80+
core.debug(`Added to tool cache at "${tool}"`)
81+
}
82+
7783
if (core.isDebug()) {
7884
core.exportVariable('SWIFT_SETUP_TOOL_KEY', toolCacheKey)
7985
}
80-
core.debug(`Added to tool cache at "${tool}"`)
8186
}
8287
if (
8388
tool &&
8489
core.getBooleanInput('cache-snapshot') &&
85-
!cacheHit &&
90+
!actionCacheHit &&
91+
!toolCacheHit &&
8692
!this.data.preventCaching
8793
) {
8894
await fs.cp(tool, restore, {recursive: true})

0 commit comments

Comments
 (0)