Skip to content

Commit 1413cd0

Browse files
Aiqiao YanAiqiao Yan
authored andcommitted
Add cache upload options and pull from latest actions/cache master
1 parent c534ad2 commit 1413cd0

File tree

7 files changed

+309
-67
lines changed

7 files changed

+309
-67
lines changed

.github/workflows/cache-tests.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
name: cache-unit-tests
2-
on: push
2+
on:
3+
push:
4+
branches:
5+
- master
6+
paths-ignore:
7+
- '**.md'
8+
pull_request:
9+
paths-ignore:
10+
- '**.md'
311

412
jobs:
513
build:
@@ -21,7 +29,7 @@ jobs:
2129
with:
2230
node-version: 12.x
2331

24-
# In order to save & restore cache artifacts from a shell script, certain env variables need to be set that are only available in the
32+
# In order to save & restore cache from a shell script, certain env variables need to be set that are only available in the
2533
# node context. This runs a local action that gets and sets the necessary env variables that are needed
2634
- name: Set env variables
2735
uses: ./packages/cache/__tests__/__fixtures__/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ $ npm install @actions/artifact --save
8989
Provides functions to cache dependencies and build outputs to improve workflow execution time. Read more [here](packages/cache)
9090

9191
```bash
92-
$ npm install @actions/cache --save
92+
$ npm install @actions/cache
9393
```
9494
<br/>
9595

packages/cache/__tests__/cacheHttpClient.test.ts

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getCacheVersion} from '../src/internal/cacheHttpClient'
1+
import {getCacheVersion, retry} from '../src/internal/cacheHttpClient'
22
import {CompressionMethod} from '../src/internal/constants'
33

44
test('getCacheVersion with one path returns version', async () => {
@@ -34,3 +34,142 @@ test('getCacheVersion with gzip compression does not change vesion', async () =>
3434
'b3e0c6cb5ecf32614eeb2997d905b9c297046d7cbf69062698f25b14b4cb0985'
3535
)
3636
})
37+
38+
interface TestResponse {
39+
statusCode: number
40+
result: string | null
41+
}
42+
43+
async function handleResponse(
44+
response: TestResponse | undefined
45+
): Promise<TestResponse> {
46+
if (!response) {
47+
// eslint-disable-next-line no-undef
48+
fail('Retry method called too many times')
49+
}
50+
51+
if (response.statusCode === 999) {
52+
throw Error('Test Error')
53+
} else {
54+
return Promise.resolve(response)
55+
}
56+
}
57+
58+
async function testRetryExpectingResult(
59+
responses: TestResponse[],
60+
expectedResult: string | null
61+
): Promise<void> {
62+
responses = responses.reverse() // Reverse responses since we pop from end
63+
64+
const actualResult = await retry(
65+
'test',
66+
async () => handleResponse(responses.pop()),
67+
(response: TestResponse) => response.statusCode
68+
)
69+
70+
expect(actualResult.result).toEqual(expectedResult)
71+
}
72+
73+
async function testRetryExpectingError(
74+
responses: TestResponse[]
75+
): Promise<void> {
76+
responses = responses.reverse() // Reverse responses since we pop from end
77+
78+
expect(
79+
retry(
80+
'test',
81+
async () => handleResponse(responses.pop()),
82+
(response: TestResponse) => response.statusCode
83+
)
84+
).rejects.toBeInstanceOf(Error)
85+
}
86+
87+
test('retry works on successful response', async () => {
88+
await testRetryExpectingResult(
89+
[
90+
{
91+
statusCode: 200,
92+
result: 'Ok'
93+
}
94+
],
95+
'Ok'
96+
)
97+
})
98+
99+
test('retry works after retryable status code', async () => {
100+
await testRetryExpectingResult(
101+
[
102+
{
103+
statusCode: 503,
104+
result: null
105+
},
106+
{
107+
statusCode: 200,
108+
result: 'Ok'
109+
}
110+
],
111+
'Ok'
112+
)
113+
})
114+
115+
test('retry fails after exhausting retries', async () => {
116+
await testRetryExpectingError([
117+
{
118+
statusCode: 503,
119+
result: null
120+
},
121+
{
122+
statusCode: 503,
123+
result: null
124+
},
125+
{
126+
statusCode: 200,
127+
result: 'Ok'
128+
}
129+
])
130+
})
131+
132+
test('retry fails after non-retryable status code', async () => {
133+
await testRetryExpectingError([
134+
{
135+
statusCode: 500,
136+
result: null
137+
},
138+
{
139+
statusCode: 200,
140+
result: 'Ok'
141+
}
142+
])
143+
})
144+
145+
test('retry works after error', async () => {
146+
await testRetryExpectingResult(
147+
[
148+
{
149+
statusCode: 999,
150+
result: null
151+
},
152+
{
153+
statusCode: 200,
154+
result: 'Ok'
155+
}
156+
],
157+
'Ok'
158+
)
159+
})
160+
161+
test('retry returns after client error', async () => {
162+
await testRetryExpectingResult(
163+
[
164+
{
165+
statusCode: 400,
166+
result: null
167+
},
168+
{
169+
statusCode: 200,
170+
result: 'Ok'
171+
}
172+
],
173+
null
174+
)
175+
})

packages/cache/__tests__/saveCache.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ test('save with server error should fail', async () => {
135135
)
136136

137137
expect(saveCacheMock).toHaveBeenCalledTimes(1)
138-
expect(saveCacheMock).toHaveBeenCalledWith(cacheId, archiveFile)
138+
expect(saveCacheMock).toHaveBeenCalledWith(cacheId, archiveFile, undefined)
139139
expect(getCompressionMock).toHaveBeenCalledTimes(1)
140140
})
141141

@@ -176,6 +176,6 @@ test('save with valid inputs uploads a cache', async () => {
176176
)
177177

178178
expect(saveCacheMock).toHaveBeenCalledTimes(1)
179-
expect(saveCacheMock).toHaveBeenCalledWith(cacheId, archiveFile)
179+
expect(saveCacheMock).toHaveBeenCalledWith(cacheId, archiveFile, undefined)
180180
expect(getCompressionMock).toHaveBeenCalledTimes(1)
181181
})

packages/cache/src/cache.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from 'path'
33
import * as utils from './internal/cacheUtils'
44
import * as cacheHttpClient from './internal/cacheHttpClient'
55
import {createTar, extractTar} from './internal/tar'
6+
import {UploadOptions} from './options'
67

78
function checkPaths(paths: string[]): void {
89
if (!paths || paths.length === 0) {
@@ -102,9 +103,14 @@ export async function restoreCache(
102103
*
103104
* @param paths a list of file paths to be cached
104105
* @param key an explicit key for restoring the cache
106+
* @param options cache upload options
105107
* @returns number returns cacheId if the cache was saved successfully
106108
*/
107-
export async function saveCache(paths: string[], key: string): Promise<number> {
109+
export async function saveCache(
110+
paths: string[],
111+
key: string,
112+
options?: UploadOptions
113+
): Promise<number> {
108114
checkPaths(paths)
109115
checkKey(key)
110116

@@ -147,7 +153,7 @@ export async function saveCache(paths: string[], key: string): Promise<number> {
147153
}
148154

149155
core.debug(`Saving Cache (ID: ${cacheId})`)
150-
await cacheHttpClient.saveCache(cacheId, archivePath)
156+
await cacheHttpClient.saveCache(cacheId, archivePath, options)
151157

152158
return cacheId
153159
}

0 commit comments

Comments
 (0)