fix: skip saving missing cache directories (#294)

* fix: skip saving missing cache directories

* ci: run cache regression tests on Node.js 24
This commit is contained in:
Zoltan Kochan
2026-11-03 14:40:50 +01:00
committed by GitHub
parent 343d2b1d95
commit c05d46d6ff
5 changed files with 179 additions and 112 deletions
+7
View File
@@ -21,6 +21,13 @@ jobs:
run_install: true
version: 11
- uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6
with:
node-version: 24
- name: Test
run: pnpm test
- name: Update dist/index.js
run: pnpm run build
+111 -111
View File
File diff suppressed because one or more lines are too long
+2 -1
View File
@@ -4,7 +4,8 @@
"build:bundle": "esbuild src/index.ts --bundle --platform=node --target=node24 --format=cjs --minify --outfile=dist/index.js --loader:.json=json",
"build": "pnpm run build:bundle",
"start": "pnpm run build && sh ./run.sh",
"update-bootstrap": "node scripts/update-bootstrap.mjs"
"update-bootstrap": "node scripts/update-bootstrap.mjs",
"test": "node --experimental-test-module-mocks --test test/*.test.mjs"
},
"dependencies": {
"@actions/cache": "^4.1.0",
+12
View File
@@ -1,5 +1,6 @@
import { saveCache } from '@actions/cache'
import { getState, info } from '@actions/core'
import { access } from 'fs/promises'
export async function runSaveCache() {
const state = getState('cache_restored_key')
@@ -11,6 +12,17 @@ export async function runSaveCache() {
return
}
try {
await access(cachePath)
} catch (error) {
if (!(error instanceof Error)) throw error
if (!('code' in error)) throw error
if (error.code !== 'ENOENT') throw error
info(`Cache path ${cachePath} does not exist, not saving cache.`)
return
}
const cacheId = await saveCache([cachePath], primaryKey)
if (cacheId == -1) return
+47
View File
@@ -0,0 +1,47 @@
import assert from 'node:assert/strict'
import { mkdtemp, rm, access } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { after, beforeEach, mock, test } from 'node:test'
const directory = await mkdtemp(join(tmpdir(), 'pnpm-cache-save-'))
after(() => rm(directory, { recursive: true, force: true }))
let state
let checkAccess
const saveCache = mock.fn(async () => 1)
mock.module('@actions/cache', { namedExports: { saveCache } })
mock.module('@actions/core', {
namedExports: { getState: name => state[name], info: () => {} },
})
mock.module('fs/promises', { namedExports: { access: path => checkAccess(path) } })
const { runSaveCache } = await import('../src/cache-save/run.ts')
beforeEach(() => {
state = { cache_restored_key: 'old', cache_primary_key: 'new', cache_path: directory }
checkAccess = access
saveCache.mock.resetCalls()
})
test('skips saving a missing directory', async () => {
state.cache_path = join(directory, 'missing')
await runSaveCache()
assert.equal(saveCache.mock.callCount(), 0)
})
test('saves an existing directory with the primary key', async () => {
await runSaveCache()
assert.deepEqual(saveCache.mock.calls[0].arguments, [[directory], 'new'])
})
test('skips saving a primary-key cache hit', async () => {
state.cache_restored_key = 'new'
await runSaveCache()
assert.equal(saveCache.mock.callCount(), 0)
})
test('propagates unexpected filesystem errors', async () => {
const error = Object.assign(new Error('access denied'), { code: 'EACCES' })
checkAccess = async () => { throw error }
await assert.rejects(runSaveCache(), error)
assert.equal(saveCache.mock.callCount(), 0)
})