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
7 changes: 7 additions & 0 deletions .github/workflows/pr-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
222 changes: 111 additions & 111 deletions dist/index.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions src/cache-save/run.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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

Expand Down
47 changes: 47 additions & 0 deletions test/cache-save.test.mjs
Original file line number Diff line number Diff line change
@@ -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)
})
Loading