Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions __tests__/installer/linux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
Expand Down
17 changes: 11 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions src/installer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export abstract class ToolchainInstaller<Snapshot extends ToolchainSnapshot> {
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`
Expand All @@ -60,7 +61,7 @@ export abstract class ToolchainInstaller<Snapshot extends ToolchainSnapshot> {
`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)
Expand All @@ -69,20 +70,25 @@ export abstract class ToolchainInstaller<Snapshot extends ToolchainSnapshot> {
}
} 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})
Expand Down
Loading