mirror of
https://github.com/gradle/actions.git
synced 2025-10-17 06:58:36 +08:00
Handle gracefully parse errors of checksum cache file
This commit is contained in:
parent
c04286a41d
commit
e3a87b5ce4
@ -1,6 +1,7 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import {ACTION_METADATA_DIR} from '../configuration'
|
||||
import * as core from '@actions/core'
|
||||
|
||||
export class ChecksumCache {
|
||||
private readonly cacheFile: string
|
||||
@ -12,7 +13,11 @@ export class ChecksumCache {
|
||||
load(): string[] {
|
||||
// Load previously validated checksums saved in Gradle User Home
|
||||
if (fs.existsSync(this.cacheFile)) {
|
||||
return JSON.parse(fs.readFileSync(this.cacheFile, 'utf-8'))
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(this.cacheFile, 'utf-8'))
|
||||
} catch (e) {
|
||||
core.warning(`Failed to parse checksum cache file: ${e}`)
|
||||
}
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import * as validate from '../../../src/wrapper-validation/validate'
|
||||
import {expect, test, jest} from '@jest/globals'
|
||||
import { WrapperChecksums, KNOWN_CHECKSUMS } from '../../../src/wrapper-validation/checksums'
|
||||
import { ChecksumCache } from '../../../src/wrapper-validation/cache'
|
||||
import { ACTION_METADATA_DIR } from '../../../src/configuration'
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
@ -128,3 +129,14 @@ test('can save and load checksums', async () => {
|
||||
expect(checksumCache.load()).toEqual(['123', '456'])
|
||||
expect(fs.existsSync(cacheDir)).toBe(true)
|
||||
})
|
||||
|
||||
test('can load empty checksum file', async () => {
|
||||
const cacheDir = path.join(tmpDir, 'empty-wrapper-validation-cache')
|
||||
const metadataDir = path.join(cacheDir, ACTION_METADATA_DIR)
|
||||
const emptyChecksumFile = path.join(metadataDir, 'valid-wrappers.json')
|
||||
fs.mkdirSync(metadataDir, { recursive: true });
|
||||
fs.writeFileSync(emptyChecksumFile, '')
|
||||
const checksumCache = new ChecksumCache(cacheDir)
|
||||
|
||||
expect(checksumCache.load()).toEqual([])
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user