diff --git a/__tests__/installer/linux.test.ts b/__tests__/installer/linux.test.ts index 18671e5..34babbc 100644 --- a/__tests__/installer/linux.test.ts +++ b/__tests__/installer/linux.test.ts @@ -114,20 +114,46 @@ describe('linux toolchain installation verification', () => { } ) - it('tests installation with cache', async () => { + it('tests installation with action cache', async () => { + const installer = new LinuxToolchainInstaller(toolchain) + const cached = path.resolve('tool', 'cached', 'path') + const swiftPath = path.join(cached, 'usr', 'bin') + jest.spyOn(toolCache, 'find').mockReturnValue('') + jest.spyOn(cache, 'restoreCache').mockResolvedValue(cached) + jest.spyOn(core, 'getBooleanInput').mockReturnValue(true) + jest.spyOn(exec, 'exec').mockResolvedValue(0) + const downloadSpy = jest.spyOn(toolCache, 'downloadTool') + const extractSpy = jest.spyOn(toolCache, 'extractTar') + const toolCacheSpy = jest.spyOn(toolCache, 'cacheDir') + const actionCacheSpy = jest.spyOn(cache, 'saveCache') + toolCacheSpy.mockResolvedValue(cached) + await installer.install('aarch64') + const toolCacheKey = `${toolchain.dir}-${toolchain.platform}` + const tmpDir = process.env.RUNNER_TEMP || os.tmpdir() + const restore = path.join(tmpDir, 'setup-swift', toolCacheKey) + expect(process.env.PATH?.includes(swiftPath)).toBeTruthy() + expect(toolCacheSpy.mock.calls[0]?.[0]).toBe(restore) + expect(toolCacheSpy.mock.calls[0]?.[1]).toBe(toolCacheKey) + expect(toolCacheSpy.mock.calls[0]?.[2]).toBe('5.8.0') + for (const spy of [downloadSpy, extractSpy, actionCacheSpy]) { + expect(spy).not.toHaveBeenCalled() + } + }) + + it('tests installation with tool cache', async () => { const installer = new LinuxToolchainInstaller(toolchain) const cached = path.resolve('tool', 'cached', 'path') const swiftPath = path.join(cached, 'usr', 'bin') jest.spyOn(toolCache, 'find').mockReturnValue(cached) - jest.spyOn(toolCache, 'cacheDir').mockResolvedValue(cached) - jest.spyOn(cache, 'saveCache').mockResolvedValue(1) jest.spyOn(core, 'getBooleanInput').mockReturnValue(true) jest.spyOn(exec, 'exec').mockResolvedValue(0) const downloadSpy = jest.spyOn(toolCache, 'downloadTool') const extractSpy = jest.spyOn(toolCache, 'extractTar') + const toolCacheSpy = jest.spyOn(toolCache, 'cacheDir') + const actionCacheSpy = jest.spyOn(cache, 'saveCache') await installer.install('aarch64') expect(process.env.PATH?.includes(swiftPath)).toBeTruthy() - for (const spy of [downloadSpy, extractSpy]) { + for (const spy of [downloadSpy, extractSpy, toolCacheSpy, actionCacheSpy]) { expect(spy).not.toHaveBeenCalled() } }) diff --git a/dist/index.js b/dist/index.js index aeccfbe..5e91b97 100644 --- a/dist/index.js +++ b/dist/index.js @@ -117,7 +117,8 @@ class ToolchainInstaller { const actionCacheKey = arch ? `${toolCacheKey}-${arch}` : toolCacheKey; const version = (_a = this.version) === null || _a === void 0 ? void 0 : _a.raw; let tool; - let cacheHit = false; + let toolCacheHit = false; + let actionCacheHit = false; if (version) { core.debug(`Finding tool with key: "${toolCacheKey}", version: "${version}" and arch: "${arch}" in tool cache`); tool = toolCache.find(toolCacheKey, version, arch).trim(); @@ -128,7 +129,7 @@ class ToolchainInstaller { if (yield cache.restoreCache([restore], actionCacheKey)) { core.debug(`Restored snapshot at "${restore}" from key "${actionCacheKey}"`); tool = restore; - cacheHit = true; + actionCacheHit = true; } else { const resource = yield this.download(); @@ -139,18 +140,22 @@ class ToolchainInstaller { } else { core.debug(`Found tool at "${tool}" in tool cache`); - cacheHit = true; + actionCacheHit = true; + toolCacheHit = true; } if (tool && version) { - tool = yield toolCache.cacheDir(tool, toolCacheKey, version, arch); + if (!toolCacheHit) { + tool = yield toolCache.cacheDir(tool, toolCacheKey, version, arch); + core.debug(`Added to tool cache at "${tool}"`); + } if (core.isDebug()) { core.exportVariable('SWIFT_SETUP_TOOL_KEY', toolCacheKey); } - core.debug(`Added to tool cache at "${tool}"`); } if (tool && core.getBooleanInput('cache-snapshot') && - !cacheHit && + !actionCacheHit && + !toolCacheHit && !this.data.preventCaching) { yield fs_1.promises.cp(tool, restore, { recursive: true }); yield cache.saveCache([restore], actionCacheKey); diff --git a/src/installer/base.ts b/src/installer/base.ts index 5dcbb79..9d19900 100644 --- a/src/installer/base.ts +++ b/src/installer/base.ts @@ -44,7 +44,8 @@ export abstract class ToolchainInstaller { const actionCacheKey = arch ? `${toolCacheKey}-${arch}` : toolCacheKey const version = this.version?.raw let tool: string | undefined - let cacheHit = false + let toolCacheHit = false + let actionCacheHit = false if (version) { core.debug( `Finding tool with key: "${toolCacheKey}", version: "${version}" and arch: "${arch}" in tool cache` @@ -60,7 +61,7 @@ export abstract class ToolchainInstaller { `Restored snapshot at "${restore}" from key "${actionCacheKey}"` ) tool = restore - cacheHit = true + actionCacheHit = true } else { const resource = await this.download() const installation = await this.unpack(resource) @@ -69,20 +70,25 @@ export abstract class ToolchainInstaller { } } else { core.debug(`Found tool at "${tool}" in tool cache`) - cacheHit = true + actionCacheHit = true + toolCacheHit = true } if (tool && version) { - tool = await toolCache.cacheDir(tool, toolCacheKey, version, arch) + if (!toolCacheHit) { + tool = await toolCache.cacheDir(tool, toolCacheKey, version, arch) + core.debug(`Added to tool cache at "${tool}"`) + } + if (core.isDebug()) { core.exportVariable('SWIFT_SETUP_TOOL_KEY', toolCacheKey) } - core.debug(`Added to tool cache at "${tool}"`) } if ( tool && core.getBooleanInput('cache-snapshot') && - !cacheHit && + !actionCacheHit && + !toolCacheHit && !this.data.preventCaching ) { await fs.cp(tool, restore, {recursive: true})