Skip to content

Commit d2b2399

Browse files
Aiqiao YanAiqiao Yan
authored andcommitted
React to feedback
1 parent b3c8e19 commit d2b2399

16 files changed

+59
-73
lines changed

.github/workflows/artifact-tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ jobs:
6464
- name: Verify downloadArtifact()
6565
shell: bash
6666
run: |
67-
scripts/test-artifact-file.sh "artifact-1-directory/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}"
68-
scripts/test-artifact-file.sh "artifact-2-directory/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}"
67+
packages/artifact/__tests__/test-artifact-file.sh "artifact-1-directory/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}"
68+
packages/artifact/__tests__/test-artifact-file.sh "artifact-2-directory/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}"
6969
7070
- name: Download artifacts using downloadAllArtifacts()
7171
run: |
@@ -75,5 +75,5 @@ jobs:
7575
- name: Verify downloadAllArtifacts()
7676
shell: bash
7777
run: |
78-
scripts/test-artifact-file.sh "multi-artifact-directory/my-artifact-1/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}"
79-
scripts/test-artifact-file.sh "multi-artifact-directory/my-artifact-2/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}"
78+
packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-1/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}"
79+
packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-2/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}"

.github/workflows/cache-tests.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ jobs:
4747

4848
- name: Generate files in working directory
4949
shell: bash
50-
run: scripts/create-cache-files.sh ${{ runner.os }} test-cache
50+
run: packages/cache/__tests__/create-cache-files.sh ${{ runner.os }} test-cache
5151

5252
- name: Generate files outside working directory
5353
shell: bash
54-
run: scripts/create-cache-files.sh ${{ runner.os }} ~/test-cache
54+
run: packages/cache/__tests__/create-cache-files.sh ${{ runner.os }} ~/test-cache
5555

5656
# We're using node -e to call the functions directly available in the @actions/cache package
5757
- name: Save cache using saveCache()
@@ -65,5 +65,5 @@ jobs:
6565
- name: Verify cache
6666
shell: bash
6767
run: |
68-
scripts/verify-cache-files.sh ${{ runner.os }} test-cache
69-
scripts/verify-cache-files.sh ${{ runner.os }} ~/test-cache
68+
packages/cache/__tests__/verify-cache-files.sh ${{ runner.os }} test-cache
69+
packages/cache/__tests__/verify-cache-files.sh ${{ runner.os }} ~/test-cache

packages/cache/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
> Functions necessary for caching dependencies and build outputs to improve workflow execution time.
44
5+
See ["Caching dependencies to speed up workflows"](https://help.github.com/github/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows) for how caching works.
6+
7+
Note that GitHub will remove any cache entries that have not been accessed in over 7 days. There is no limit on the number of caches you can store, but the total size of all caches in a repository is limited to 5 GB. If you exceed this limit, GitHub will save your cache but will begin evicting caches until the total size is less than 5 GB.
8+
59
## Usage
610

711
#### Restore Cache
@@ -24,7 +28,7 @@ const cacheKey = await cache.restoreCache(paths, key, restoreKeys)
2428

2529
#### Save Cache
2630

27-
Saves a cache containing the files in `paths` using the `key` provided. Function returns the cache id if the cache was save succesfully.
31+
Saves a cache containing the files in `paths` using the `key` provided. The files would be compressed using zstandard compression algorithm if zstd is installed, otherwise gzip is used. Function returns the cache id if the cache was saved succesfully and throws an error if cache upload fails.
2832

2933
```js
3034
const cache = require('@actions/cache');
@@ -34,8 +38,4 @@ const paths = [
3438
]
3539
const key = 'npm-foobar-d5ea0750'
3640
const cacheId = await cache.saveCache(paths, key)
37-
```
38-
39-
## Additional Documentation
40-
41-
See ["Caching dependencies to speed up workflows"](https://help.github.com/github/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows).
41+
```

packages/cache/RELEASES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# @actions/cache Releases
22

3-
### 1.0.0
3+
### 0.1.0
44

55
- Initial release

packages/cache/__tests__/cacheUtils.test.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
import * as io from '@actions/io'
21
import {promises as fs} from 'fs'
32
import * as path from 'path'
43
import * as cacheUtils from '../src/internal/cacheUtils'
54

6-
jest.mock('@actions/core')
7-
jest.mock('os')
8-
9-
function getTempDir(): string {
10-
return path.join(__dirname, '_temp', 'cacheUtils')
11-
}
12-
13-
afterAll(async () => {
14-
delete process.env['GITHUB_WORKSPACE']
15-
await io.rmRF(getTempDir())
16-
})
17-
18-
test('getArchiveFileSize returns file size', () => {
5+
test('getArchiveFileSizeIsBytes returns file size', () => {
196
const filePath = path.join(__dirname, '__fixtures__', 'helloWorld.txt')
207

21-
const size = cacheUtils.getArchiveFileSize(filePath)
8+
const size = cacheUtils.getArchiveFileSizeIsBytes(filePath)
229

2310
expect(size).toBe(11)
2411
})
@@ -28,6 +15,8 @@ test('unlinkFile unlinks file', async () => {
2815
const testFile = path.join(testDirectory, 'test.txt')
2916
await fs.writeFile(testFile, 'hello world')
3017

18+
await expect(fs.stat(testFile)).resolves.not.toThrow()
19+
3120
await cacheUtils.unlinkFile(testFile)
3221

3322
// This should throw as testFile should not exist

packages/cache/__tests__/restoreCache.test.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ jest.mock('../src/internal/cacheUtils')
1212
jest.mock('../src/internal/tar')
1313

1414
beforeAll(() => {
15+
jest.spyOn(console, 'log').mockImplementation(() => {})
16+
jest.spyOn(core, 'debug').mockImplementation(() => {})
17+
jest.spyOn(core, 'info').mockImplementation(() => {})
18+
jest.spyOn(core, 'warning').mockImplementation(() => {})
19+
jest.spyOn(core, 'error').mockImplementation(() => {})
20+
1521
// eslint-disable-next-line @typescript-eslint/promise-function-async
1622
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
1723
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
@@ -56,17 +62,13 @@ test('restore with no cache found', async () => {
5662
const paths = ['node_modules']
5763
const key = 'node-test'
5864

59-
const infoMock = jest.spyOn(core, 'info')
6065
jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(async () => {
6166
return Promise.resolve(null)
6267
})
6368

6469
const cacheKey = await restoreCache(paths, key)
6570

6671
expect(cacheKey).toBe(undefined)
67-
expect(infoMock).toHaveBeenCalledWith(
68-
`Cache not found for input keys: ${key}`
69-
)
7072
})
7173

7274
test('restore with server error should fail', async () => {
@@ -87,26 +89,19 @@ test('restore with restore keys and no cache found', async () => {
8789
const key = 'node-test'
8890
const restoreKey = 'node-'
8991

90-
const infoMock = jest.spyOn(core, 'info')
91-
9292
jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(async () => {
9393
return Promise.resolve(null)
9494
})
9595

9696
const cacheKey = await restoreCache(paths, key, [restoreKey])
9797

9898
expect(cacheKey).toBe(undefined)
99-
expect(infoMock).toHaveBeenCalledWith(
100-
`Cache not found for input keys: ${key}, ${restoreKey}`
101-
)
10299
})
103100

104101
test('restore with gzip compressed cache found', async () => {
105102
const paths = ['node_modules']
106103
const key = 'node-test'
107104

108-
const infoMock = jest.spyOn(core, 'info')
109-
110105
const cacheEntry: ArtifactCacheEntry = {
111106
cacheKey: key,
112107
scope: 'refs/heads/master',
@@ -128,8 +123,8 @@ test('restore with gzip compressed cache found', async () => {
128123
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
129124

130125
const fileSize = 142
131-
const getArchiveFileSizeMock = jest
132-
.spyOn(cacheUtils, 'getArchiveFileSize')
126+
const getArchiveFileSizeIsBytesMock = jest
127+
.spyOn(cacheUtils, 'getArchiveFileSizeIsBytes')
133128
.mockReturnValue(fileSize)
134129

135130
const extractTarMock = jest.spyOn(tar, 'extractTar')
@@ -151,19 +146,18 @@ test('restore with gzip compressed cache found', async () => {
151146
cacheEntry.archiveLocation,
152147
archivePath
153148
)
154-
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath)
149+
expect(getArchiveFileSizeIsBytesMock).toHaveBeenCalledWith(archivePath)
155150

156151
expect(extractTarMock).toHaveBeenCalledTimes(1)
157152
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
158153

159154
expect(unlinkFileMock).toHaveBeenCalledTimes(1)
160155
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
161156

162-
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`)
163157
expect(getCompressionMock).toHaveBeenCalledTimes(1)
164158
})
165159

166-
test('restore with a pull request event and zstd compressed cache found', async () => {
160+
test('restore with zstd compressed cache found', async () => {
167161
const paths = ['node_modules']
168162
const key = 'node-test'
169163

@@ -189,8 +183,8 @@ test('restore with a pull request event and zstd compressed cache found', async
189183
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
190184

191185
const fileSize = 62915000
192-
const getArchiveFileSizeMock = jest
193-
.spyOn(cacheUtils, 'getArchiveFileSize')
186+
const getArchiveFileSizeIsBytesMock = jest
187+
.spyOn(cacheUtils, 'getArchiveFileSizeIsBytes')
194188
.mockReturnValue(fileSize)
195189

196190
const extractTarMock = jest.spyOn(tar, 'extractTar')
@@ -210,13 +204,11 @@ test('restore with a pull request event and zstd compressed cache found', async
210204
cacheEntry.archiveLocation,
211205
archivePath
212206
)
213-
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath)
207+
expect(getArchiveFileSizeIsBytesMock).toHaveBeenCalledWith(archivePath)
214208
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`)
215209

216210
expect(extractTarMock).toHaveBeenCalledTimes(1)
217211
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
218-
219-
expect(infoMock).toHaveBeenCalledWith(`Cache restored from key: ${key}`)
220212
expect(getCompressionMock).toHaveBeenCalledTimes(1)
221213
})
222214

@@ -247,8 +239,8 @@ test('restore with cache found for restore key', async () => {
247239
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
248240

249241
const fileSize = 142
250-
const getArchiveFileSizeMock = jest
251-
.spyOn(cacheUtils, 'getArchiveFileSize')
242+
const getArchiveFileSizeIsBytesMock = jest
243+
.spyOn(cacheUtils, 'getArchiveFileSizeIsBytes')
252244
.mockReturnValue(fileSize)
253245

254246
const extractTarMock = jest.spyOn(tar, 'extractTar')
@@ -268,14 +260,10 @@ test('restore with cache found for restore key', async () => {
268260
cacheEntry.archiveLocation,
269261
archivePath
270262
)
271-
expect(getArchiveFileSizeMock).toHaveBeenCalledWith(archivePath)
263+
expect(getArchiveFileSizeIsBytesMock).toHaveBeenCalledWith(archivePath)
272264
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`)
273265

274266
expect(extractTarMock).toHaveBeenCalledTimes(1)
275267
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
276-
277-
expect(infoMock).toHaveBeenCalledWith(
278-
`Cache restored from key: ${restoreKey}`
279-
)
280268
expect(getCompressionMock).toHaveBeenCalledTimes(1)
281269
})

packages/cache/__tests__/saveCache.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
import * as core from '@actions/core'
12
import * as path from 'path'
23
import {saveCache} from '../src/cache'
34
import * as cacheHttpClient from '../src/internal/cacheHttpClient'
45
import * as cacheUtils from '../src/internal/cacheUtils'
56
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
67
import * as tar from '../src/internal/tar'
78

8-
jest.mock('@actions/core')
99
jest.mock('../src/internal/cacheHttpClient')
1010
jest.mock('../src/internal/cacheUtils')
1111
jest.mock('../src/internal/tar')
1212

1313
beforeAll(() => {
14+
jest.spyOn(console, 'log').mockImplementation(() => {})
15+
jest.spyOn(core, 'debug').mockImplementation(() => {})
16+
jest.spyOn(core, 'info').mockImplementation(() => {})
17+
jest.spyOn(core, 'warning').mockImplementation(() => {})
18+
jest.spyOn(core, 'error').mockImplementation(() => {})
19+
1420
// eslint-disable-next-line @typescript-eslint/promise-function-async
1521
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
1622
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
@@ -42,7 +48,9 @@ test('save with large cache outputs should fail', async () => {
4248
const createTarMock = jest.spyOn(tar, 'createTar')
4349

4450
const cacheSize = 6 * 1024 * 1024 * 1024 //~6GB, over the 5GB limit
45-
jest.spyOn(cacheUtils, 'getArchiveFileSize').mockReturnValueOnce(cacheSize)
51+
jest
52+
.spyOn(cacheUtils, 'getArchiveFileSizeIsBytes')
53+
.mockReturnValueOnce(cacheSize)
4654
const compression = CompressionMethod.Gzip
4755
const getCompressionMock = jest
4856
.spyOn(cacheUtils, 'getCompressionMethod')

packages/cache/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cache/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@actions/cache",
3-
"version": "1.0.0",
3+
"version": "0.1.0",
44
"preview": true,
55
"description": "Actions cache lib",
66
"keywords": [

packages/cache/src/cache.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ export class ValidationError extends Error {
99
constructor(message: string) {
1010
super(message)
1111
this.name = 'ValidationError'
12+
Object.setPrototypeOf(this, ValidationError.prototype)
1213
}
1314
}
1415

1516
export class ReserveCacheError extends Error {
1617
constructor(message: string) {
1718
super(message)
1819
this.name = 'ReserveCacheError'
20+
Object.setPrototypeOf(this, ReserveCacheError.prototype)
1921
}
2022
}
2123

@@ -47,7 +49,7 @@ function checkKey(key: string): void {
4749
* @param paths a list of file paths to restore from the cache
4850
* @param primaryKey an explicit key for restoring the cache
4951
* @param restoreKeys an optional ordered list of keys to use for restoring the cache if no cache hit occurred for key
50-
* @returns string returns the key for the cache hit, otherwise return undefined
52+
* @returns string returns the key for the cache hit, otherwise returns undefined
5153
*/
5254
export async function restoreCache(
5355
paths: string[],
@@ -78,7 +80,7 @@ export async function restoreCache(
7880
compressionMethod
7981
})
8082
if (!cacheEntry?.archiveLocation) {
81-
core.info(`Cache not found for input keys: ${keys.join(', ')}`)
83+
// Cache not found
8284
return undefined
8385
}
8486

@@ -92,7 +94,7 @@ export async function restoreCache(
9294
// Download the cache from the cache entry
9395
await cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath)
9496

95-
const archiveFileSize = utils.getArchiveFileSize(archivePath)
97+
const archiveFileSize = utils.getArchiveFileSizeIsBytes(archivePath)
9698
core.info(
9799
`Cache Size: ~${Math.round(
98100
archiveFileSize / (1024 * 1024)
@@ -109,8 +111,6 @@ export async function restoreCache(
109111
}
110112
}
111113

112-
core.info(`Cache restored from key: ${cacheEntry && cacheEntry.cacheKey}`)
113-
114114
return cacheEntry.cacheKey
115115
}
116116

@@ -120,7 +120,7 @@ export async function restoreCache(
120120
* @param paths a list of file paths to be cached
121121
* @param key an explicit key for restoring the cache
122122
* @param options cache upload options
123-
* @returns number returns cacheId if the cache was saved successfully
123+
* @returns number returns cacheId if the cache was saved successfully and throws an error if save fails
124124
*/
125125
export async function saveCache(
126126
paths: string[],
@@ -158,7 +158,7 @@ export async function saveCache(
158158
await createTar(archiveFolder, cachePaths, compressionMethod)
159159

160160
const fileSizeLimit = 5 * 1024 * 1024 * 1024 // 5GB per repo limit
161-
const archiveFileSize = utils.getArchiveFileSize(archivePath)
161+
const archiveFileSize = utils.getArchiveFileSizeIsBytes(archivePath)
162162
core.debug(`File Size: ${archiveFileSize}`)
163163
if (archiveFileSize > fileSizeLimit) {
164164
throw new Error(

0 commit comments

Comments
 (0)