mirror of
https://git.mirrors.martin98.com/https://github.com/actions/toolkit
synced 2026-05-01 11:08:04 +08:00
React to feedback
This commit is contained in:
19
packages/cache/__tests__/cacheUtils.test.ts
vendored
19
packages/cache/__tests__/cacheUtils.test.ts
vendored
@@ -1,24 +1,11 @@
|
||||
import * as io from '@actions/io'
|
||||
import {promises as fs} from 'fs'
|
||||
import * as path from 'path'
|
||||
import * as cacheUtils from '../src/internal/cacheUtils'
|
||||
|
||||
jest.mock('@actions/core')
|
||||
jest.mock('os')
|
||||
|
||||
function getTempDir(): string {
|
||||
return path.join(__dirname, '_temp', 'cacheUtils')
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
delete process.env['GITHUB_WORKSPACE']
|
||||
await io.rmRF(getTempDir())
|
||||
})
|
||||
|
||||
test('getArchiveFileSize returns file size', () => {
|
||||
test('getArchiveFileSizeIsBytes returns file size', () => {
|
||||
const filePath = path.join(__dirname, '__fixtures__', 'helloWorld.txt')
|
||||
|
||||
const size = cacheUtils.getArchiveFileSize(filePath)
|
||||
const size = cacheUtils.getArchiveFileSizeIsBytes(filePath)
|
||||
|
||||
expect(size).toBe(11)
|
||||
})
|
||||
@@ -28,6 +15,8 @@ test('unlinkFile unlinks file', async () => {
|
||||
const testFile = path.join(testDirectory, 'test.txt')
|
||||
await fs.writeFile(testFile, 'hello world')
|
||||
|
||||
await expect(fs.stat(testFile)).resolves.not.toThrow()
|
||||
|
||||
await cacheUtils.unlinkFile(testFile)
|
||||
|
||||
// This should throw as testFile should not exist
|
||||
|
||||
17
packages/cache/__tests__/create-cache-files.sh
vendored
Executable file
17
packages/cache/__tests__/create-cache-files.sh
vendored
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Validate args
|
||||
prefix="$1"
|
||||
if [ -z "$prefix" ]; then
|
||||
echo "Must supply prefix argument"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
path="$2"
|
||||
if [ -z "$path" ]; then
|
||||
echo "Must supply path argument"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p $path
|
||||
echo "$prefix $GITHUB_RUN_ID" > $path/test-file.txt
|
||||
44
packages/cache/__tests__/restoreCache.test.ts
vendored
44
packages/cache/__tests__/restoreCache.test.ts
vendored
@@ -12,6 +12,12 @@ jest.mock('../src/internal/cacheUtils')
|
||||
jest.mock('../src/internal/tar')
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {})
|
||||
jest.spyOn(core, 'debug').mockImplementation(() => {})
|
||||
jest.spyOn(core, 'info').mockImplementation(() => {})
|
||||
jest.spyOn(core, 'warning').mockImplementation(() => {})
|
||||
jest.spyOn(core, 'error').mockImplementation(() => {})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
||||
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
|
||||
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
|
||||
@@ -56,7 +62,6 @@ test('restore with no cache found', async () => {
|
||||
const paths = ['node_modules']
|
||||
const key = 'node-test'
|
||||
|
||||
const infoMock = jest.spyOn(core, 'info')
|
||||
jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(async () => {
|
||||
return Promise.resolve(null)
|
||||
})
|
||||
@@ -64,9 +69,6 @@ test('restore with no cache found', async () => {
|
||||
const cacheKey = await restoreCache(paths, key)
|
||||
|
||||
expect(cacheKey).toBe(undefined)
|
||||
expect(infoMock).toHaveBeenCalledWith(
|
||||
`Cache not found for input keys: ${key}`
|
||||
)
|
||||
})
|
||||
|
||||
test('restore with server error should fail', async () => {
|
||||
@@ -87,8 +89,6 @@ test('restore with restore keys and no cache found', async () => {
|
||||
const key = 'node-test'
|
||||
const restoreKey = 'node-'
|
||||
|
||||
const infoMock = jest.spyOn(core, 'info')
|
||||
|
||||
jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(async () => {
|
||||
return Promise.resolve(null)
|
||||
})
|
||||
@@ -96,17 +96,12 @@ test('restore with restore keys and no cache found', async () => {
|
||||
const cacheKey = await restoreCache(paths, key, [restoreKey])
|
||||
|
||||
expect(cacheKey).toBe(undefined)
|
||||
expect(infoMock).toHaveBeenCalledWith(
|
||||
`Cache not found for input keys: ${key}, ${restoreKey}`
|
||||
)
|
||||
})
|
||||
|
||||
test('restore with gzip compressed cache found', async () => {
|
||||
const paths = ['node_modules']
|
||||
const key = 'node-test'
|
||||
|
||||
const infoMock = jest.spyOn(core, 'info')
|
||||
|
||||
const cacheEntry: ArtifactCacheEntry = {
|
||||
cacheKey: key,
|
||||
scope: 'refs/heads/master',
|
||||
@@ -128,8 +123,8 @@ test('restore with gzip compressed cache found', async () => {
|
||||
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
|
||||
|
||||
const fileSize = 142
|
||||
const getArchiveFileSizeMock = jest
|
||||
.spyOn(cacheUtils, 'getArchiveFileSize')
|
||||
const getArchiveFileSizeIsBytesMock = jest
|
||||
.spyOn(cacheUtils, 'getArchiveFileSizeIsBytes')
|
||||
.mockReturnValue(fileSize)
|
||||
|
||||
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
||||
@@ -151,7 +146,7 @@ test('restore with gzip compressed cache found', async () => {
|
||||
cacheEntry.archiveLocation,
|
||||
archivePath
|
||||
)
|
||||
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath)
|
||||
expect(getArchiveFileSizeIsBytesMock).toHaveBeenCalledWith(archivePath)
|
||||
|
||||
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
||||
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
|
||||
@@ -159,11 +154,10 @@ test('restore with gzip compressed cache found', async () => {
|
||||
expect(unlinkFileMock).toHaveBeenCalledTimes(1)
|
||||
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
|
||||
|
||||
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`)
|
||||
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
test('restore with a pull request event and zstd compressed cache found', async () => {
|
||||
test('restore with zstd compressed cache found', async () => {
|
||||
const paths = ['node_modules']
|
||||
const key = 'node-test'
|
||||
|
||||
@@ -189,8 +183,8 @@ test('restore with a pull request event and zstd compressed cache found', async
|
||||
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
|
||||
|
||||
const fileSize = 62915000
|
||||
const getArchiveFileSizeMock = jest
|
||||
.spyOn(cacheUtils, 'getArchiveFileSize')
|
||||
const getArchiveFileSizeIsBytesMock = jest
|
||||
.spyOn(cacheUtils, 'getArchiveFileSizeIsBytes')
|
||||
.mockReturnValue(fileSize)
|
||||
|
||||
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
||||
@@ -210,13 +204,11 @@ test('restore with a pull request event and zstd compressed cache found', async
|
||||
cacheEntry.archiveLocation,
|
||||
archivePath
|
||||
)
|
||||
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath)
|
||||
expect(getArchiveFileSizeIsBytesMock).toHaveBeenCalledWith(archivePath)
|
||||
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`)
|
||||
|
||||
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
||||
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
|
||||
|
||||
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`)
|
||||
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
@@ -247,8 +239,8 @@ test('restore with cache found for restore key', async () => {
|
||||
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
|
||||
|
||||
const fileSize = 142
|
||||
const getArchiveFileSizeMock = jest
|
||||
.spyOn(cacheUtils, 'getArchiveFileSize')
|
||||
const getArchiveFileSizeIsBytesMock = jest
|
||||
.spyOn(cacheUtils, 'getArchiveFileSizeIsBytes')
|
||||
.mockReturnValue(fileSize)
|
||||
|
||||
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
||||
@@ -268,14 +260,10 @@ test('restore with cache found for restore key', async () => {
|
||||
cacheEntry.archiveLocation,
|
||||
archivePath
|
||||
)
|
||||
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath)
|
||||
expect(getArchiveFileSizeIsBytesMock).toHaveBeenCalledWith(archivePath)
|
||||
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`)
|
||||
|
||||
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
||||
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
|
||||
|
||||
expect(infoMock).toHaveBeenCalledWith(
|
||||
`Cache restored from key: ${restoreKey}`
|
||||
)
|
||||
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
12
packages/cache/__tests__/saveCache.test.ts
vendored
12
packages/cache/__tests__/saveCache.test.ts
vendored
@@ -1,3 +1,4 @@
|
||||
import * as core from '@actions/core'
|
||||
import * as path from 'path'
|
||||
import {saveCache} from '../src/cache'
|
||||
import * as cacheHttpClient from '../src/internal/cacheHttpClient'
|
||||
@@ -5,12 +6,17 @@ import * as cacheUtils from '../src/internal/cacheUtils'
|
||||
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
||||
import * as tar from '../src/internal/tar'
|
||||
|
||||
jest.mock('@actions/core')
|
||||
jest.mock('../src/internal/cacheHttpClient')
|
||||
jest.mock('../src/internal/cacheUtils')
|
||||
jest.mock('../src/internal/tar')
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {})
|
||||
jest.spyOn(core, 'debug').mockImplementation(() => {})
|
||||
jest.spyOn(core, 'info').mockImplementation(() => {})
|
||||
jest.spyOn(core, 'warning').mockImplementation(() => {})
|
||||
jest.spyOn(core, 'error').mockImplementation(() => {})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
||||
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
|
||||
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
|
||||
@@ -42,7 +48,9 @@ test('save with large cache outputs should fail', async () => {
|
||||
const createTarMock = jest.spyOn(tar, 'createTar')
|
||||
|
||||
const cacheSize = 6 * 1024 * 1024 * 1024 //~6GB, over the 5GB limit
|
||||
jest.spyOn(cacheUtils, 'getArchiveFileSize').mockReturnValueOnce(cacheSize)
|
||||
jest
|
||||
.spyOn(cacheUtils, 'getArchiveFileSizeIsBytes')
|
||||
.mockReturnValueOnce(cacheSize)
|
||||
const compression = CompressionMethod.Gzip
|
||||
const getCompressionMock = jest
|
||||
.spyOn(cacheUtils, 'getCompressionMethod')
|
||||
|
||||
36
packages/cache/__tests__/verify-cache-files.sh
vendored
Executable file
36
packages/cache/__tests__/verify-cache-files.sh
vendored
Executable file
@@ -0,0 +1,36 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Validate args
|
||||
prefix="$1"
|
||||
if [ -z "$prefix" ]; then
|
||||
echo "Must supply prefix argument"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
path="$2"
|
||||
if [ -z "$path" ]; then
|
||||
echo "Must specify path argument"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Sanity check GITHUB_RUN_ID defined
|
||||
if [ -z "$GITHUB_RUN_ID" ]; then
|
||||
echo "GITHUB_RUN_ID not defined"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify file exists
|
||||
file="$path/test-file.txt"
|
||||
echo "Checking for $file"
|
||||
if [ ! -e $file ]; then
|
||||
echo "File does not exist"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify file content
|
||||
content="$(cat $file)"
|
||||
echo "File content:\n$content"
|
||||
if [ -z "$(echo $content | grep --fixed-strings "$prefix $GITHUB_RUN_ID")" ]; then
|
||||
echo "Unexpected file content"
|
||||
exit 1
|
||||
fi
|
||||
Reference in New Issue
Block a user