1
1
import * as core from "@actions/core" ;
2
- import * as io from "@actions/io" ;
3
- import { promises as fs } from "fs" ;
4
- import * as os from "os" ;
5
- import * as path from "path" ;
6
2
7
3
import { Events , Outputs , RefKey , State } from "../src/constants" ;
8
- import { ArtifactCacheEntry } from "../src/contracts" ;
9
4
import * as actionUtils from "../src/utils/actionUtils" ;
10
5
11
- import uuid = require( "uuid" ) ;
12
-
13
6
jest . mock ( "@actions/core" ) ;
14
- jest . mock ( "os" ) ;
15
-
16
- function getTempDir ( ) : string {
17
- return path . join ( __dirname , "_temp" , "actionUtils" ) ;
18
- }
19
7
20
8
afterEach ( ( ) => {
21
9
delete process . env [ Events . Key ] ;
22
10
delete process . env [ RefKey ] ;
23
11
} ) ;
24
12
25
- afterAll ( async ( ) => {
26
- delete process . env [ "GITHUB_WORKSPACE" ] ;
27
- await io . rmRF ( getTempDir ( ) ) ;
28
- } ) ;
29
-
30
- test ( "getArchiveFileSize returns file size" , ( ) => {
31
- const filePath = path . join ( __dirname , "__fixtures__" , "helloWorld.txt" ) ;
32
-
33
- const size = actionUtils . getArchiveFileSize ( filePath ) ;
34
-
35
- expect ( size ) . toBe ( 11 ) ;
36
- } ) ;
37
-
38
- test ( "isExactKeyMatch with undefined cache entry returns false" , ( ) => {
13
+ test ( "isExactKeyMatch with undefined cache key returns false" , ( ) => {
39
14
const key = "linux-rust" ;
40
- const cacheEntry = undefined ;
15
+ const cacheKey = undefined ;
41
16
42
- expect ( actionUtils . isExactKeyMatch ( key , cacheEntry ) ) . toBe ( false ) ;
17
+ expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
43
18
} ) ;
44
19
45
- test ( "isExactKeyMatch with empty cache entry returns false" , ( ) => {
20
+ test ( "isExactKeyMatch with empty cache key returns false" , ( ) => {
46
21
const key = "linux-rust" ;
47
- const cacheEntry : ArtifactCacheEntry = { } ;
22
+ const cacheKey = "" ;
48
23
49
- expect ( actionUtils . isExactKeyMatch ( key , cacheEntry ) ) . toBe ( false ) ;
24
+ expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
50
25
} ) ;
51
26
52
27
test ( "isExactKeyMatch with different keys returns false" , ( ) => {
53
28
const key = "linux-rust" ;
54
- const cacheEntry : ArtifactCacheEntry = {
55
- cacheKey : "linux-"
56
- } ;
29
+ const cacheKey = "linux-" ;
57
30
58
- expect ( actionUtils . isExactKeyMatch ( key , cacheEntry ) ) . toBe ( false ) ;
31
+ expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
59
32
} ) ;
60
33
61
34
test ( "isExactKeyMatch with different key accents returns false" , ( ) => {
62
35
const key = "linux-áccent" ;
63
- const cacheEntry : ArtifactCacheEntry = {
64
- cacheKey : "linux-accent"
65
- } ;
36
+ const cacheKey = "linux-accent" ;
66
37
67
- expect ( actionUtils . isExactKeyMatch ( key , cacheEntry ) ) . toBe ( false ) ;
38
+ expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( false ) ;
68
39
} ) ;
69
40
70
41
test ( "isExactKeyMatch with same key returns true" , ( ) => {
71
42
const key = "linux-rust" ;
72
- const cacheEntry : ArtifactCacheEntry = {
73
- cacheKey : "linux-rust"
74
- } ;
43
+ const cacheKey = "linux-rust" ;
75
44
76
- expect ( actionUtils . isExactKeyMatch ( key , cacheEntry ) ) . toBe ( true ) ;
45
+ expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( true ) ;
77
46
} ) ;
78
47
79
48
test ( "isExactKeyMatch with same key and different casing returns true" , ( ) => {
80
49
const key = "linux-rust" ;
81
- const cacheEntry : ArtifactCacheEntry = {
82
- cacheKey : "LINUX-RUST"
83
- } ;
50
+ const cacheKey = "LINUX-RUST" ;
84
51
85
- expect ( actionUtils . isExactKeyMatch ( key , cacheEntry ) ) . toBe ( true ) ;
52
+ expect ( actionUtils . isExactKeyMatch ( key , cacheKey ) ) . toBe ( true ) ;
86
53
} ) ;
87
54
88
55
test ( "setOutputAndState with undefined entry to set cache-hit output" , ( ) => {
89
56
const key = "linux-rust" ;
90
- const cacheEntry = undefined ;
57
+ const cacheKey = undefined ;
91
58
92
59
const setOutputMock = jest . spyOn ( core , "setOutput" ) ;
93
60
const saveStateMock = jest . spyOn ( core , "saveState" ) ;
94
61
95
- actionUtils . setOutputAndState ( key , cacheEntry ) ;
62
+ actionUtils . setOutputAndState ( key , cacheKey ) ;
96
63
97
64
expect ( setOutputMock ) . toHaveBeenCalledWith ( Outputs . CacheHit , "false" ) ;
98
65
expect ( setOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -102,43 +69,33 @@ test("setOutputAndState with undefined entry to set cache-hit output", () => {
102
69
103
70
test ( "setOutputAndState with exact match to set cache-hit output and state" , ( ) => {
104
71
const key = "linux-rust" ;
105
- const cacheEntry : ArtifactCacheEntry = {
106
- cacheKey : "linux-rust"
107
- } ;
72
+ const cacheKey = "linux-rust" ;
108
73
109
74
const setOutputMock = jest . spyOn ( core , "setOutput" ) ;
110
75
const saveStateMock = jest . spyOn ( core , "saveState" ) ;
111
76
112
- actionUtils . setOutputAndState ( key , cacheEntry ) ;
77
+ actionUtils . setOutputAndState ( key , cacheKey ) ;
113
78
114
79
expect ( setOutputMock ) . toHaveBeenCalledWith ( Outputs . CacheHit , "true" ) ;
115
80
expect ( setOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
116
81
117
- expect ( saveStateMock ) . toHaveBeenCalledWith (
118
- State . CacheResult ,
119
- JSON . stringify ( cacheEntry )
120
- ) ;
82
+ expect ( saveStateMock ) . toHaveBeenCalledWith ( State . CacheMatchedKey , cacheKey ) ;
121
83
expect ( saveStateMock ) . toHaveBeenCalledTimes ( 1 ) ;
122
84
} ) ;
123
85
124
86
test ( "setOutputAndState with no exact match to set cache-hit output and state" , ( ) => {
125
87
const key = "linux-rust" ;
126
- const cacheEntry : ArtifactCacheEntry = {
127
- cacheKey : "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43"
128
- } ;
88
+ const cacheKey = "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43" ;
129
89
130
90
const setOutputMock = jest . spyOn ( core , "setOutput" ) ;
131
91
const saveStateMock = jest . spyOn ( core , "saveState" ) ;
132
92
133
- actionUtils . setOutputAndState ( key , cacheEntry ) ;
93
+ actionUtils . setOutputAndState ( key , cacheKey ) ;
134
94
135
95
expect ( setOutputMock ) . toHaveBeenCalledWith ( Outputs . CacheHit , "false" ) ;
136
96
expect ( setOutputMock ) . toHaveBeenCalledTimes ( 1 ) ;
137
97
138
- expect ( saveStateMock ) . toHaveBeenCalledWith (
139
- State . CacheResult ,
140
- JSON . stringify ( cacheEntry )
141
- ) ;
98
+ expect ( saveStateMock ) . toHaveBeenCalledWith ( State . CacheMatchedKey , cacheKey ) ;
142
99
expect ( saveStateMock ) . toHaveBeenCalledTimes ( 1 ) ;
143
100
} ) ;
144
101
@@ -152,27 +109,23 @@ test("getCacheState with no state returns undefined", () => {
152
109
153
110
expect ( state ) . toBe ( undefined ) ;
154
111
155
- expect ( getStateMock ) . toHaveBeenCalledWith ( State . CacheResult ) ;
112
+ expect ( getStateMock ) . toHaveBeenCalledWith ( State . CacheMatchedKey ) ;
156
113
expect ( getStateMock ) . toHaveBeenCalledTimes ( 1 ) ;
157
114
} ) ;
158
115
159
116
test ( "getCacheState with valid state" , ( ) => {
160
- const cacheEntry : ArtifactCacheEntry = {
161
- cacheKey : "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43" ,
162
- scope : "refs/heads/master" ,
163
- creationTime : "2019-11-13T19:18:02+00:00" ,
164
- archiveLocation : "www.actionscache.test/download"
165
- } ;
117
+ const cacheKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43" ;
118
+
166
119
const getStateMock = jest . spyOn ( core , "getState" ) ;
167
120
getStateMock . mockImplementation ( ( ) => {
168
- return JSON . stringify ( cacheEntry ) ;
121
+ return cacheKey ;
169
122
} ) ;
170
123
171
124
const state = actionUtils . getCacheState ( ) ;
172
125
173
- expect ( state ) . toEqual ( cacheEntry ) ;
126
+ expect ( state ) . toEqual ( cacheKey ) ;
174
127
175
- expect ( getStateMock ) . toHaveBeenCalledWith ( State . CacheResult ) ;
128
+ expect ( getStateMock ) . toHaveBeenCalledWith ( State . CacheMatchedKey ) ;
176
129
expect ( getStateMock ) . toHaveBeenCalledTimes ( 1 ) ;
177
130
} ) ;
178
131
@@ -195,137 +148,6 @@ test("isValidEvent returns false for event that does not have a branch or tag",
195
148
expect ( isValidEvent ) . toBe ( false ) ;
196
149
} ) ;
197
150
198
- test ( "resolvePaths with no ~ in path" , async ( ) => {
199
- const filePath = ".cache" ;
200
-
201
- // Create the following layout:
202
- // cwd
203
- // cwd/.cache
204
- // cwd/.cache/file.txt
205
-
206
- const root = path . join ( getTempDir ( ) , "no-tilde" ) ;
207
- // tarball entries will be relative to workspace
208
- process . env [ "GITHUB_WORKSPACE" ] = root ;
209
-
210
- await fs . mkdir ( root , { recursive : true } ) ;
211
- const cache = path . join ( root , ".cache" ) ;
212
- await fs . mkdir ( cache , { recursive : true } ) ;
213
- await fs . writeFile ( path . join ( cache , "file.txt" ) , "cached" ) ;
214
-
215
- const originalCwd = process . cwd ( ) ;
216
-
217
- try {
218
- process . chdir ( root ) ;
219
-
220
- const resolvedPath = await actionUtils . resolvePaths ( [ filePath ] ) ;
221
-
222
- const expectedPath = [ filePath ] ;
223
- expect ( resolvedPath ) . toStrictEqual ( expectedPath ) ;
224
- } finally {
225
- process . chdir ( originalCwd ) ;
226
- }
227
- } ) ;
228
-
229
- test ( "resolvePaths with ~ in path" , async ( ) => {
230
- const cacheDir = uuid ( ) ;
231
- const filePath = `~/${ cacheDir } ` ;
232
- // Create the following layout:
233
- // ~/uuid
234
- // ~/uuid/file.txt
235
-
236
- const homedir = jest . requireActual ( "os" ) . homedir ( ) ;
237
- const homedirMock = jest . spyOn ( os , "homedir" ) ;
238
- homedirMock . mockImplementation ( ( ) => {
239
- return homedir ;
240
- } ) ;
241
-
242
- const target = path . join ( homedir , cacheDir ) ;
243
- await fs . mkdir ( target , { recursive : true } ) ;
244
- await fs . writeFile ( path . join ( target , "file.txt" ) , "cached" ) ;
245
-
246
- const root = getTempDir ( ) ;
247
- process . env [ "GITHUB_WORKSPACE" ] = root ;
248
-
249
- try {
250
- const resolvedPath = await actionUtils . resolvePaths ( [ filePath ] ) ;
251
-
252
- const expectedPath = [ path . relative ( root , target ) ] ;
253
- expect ( resolvedPath ) . toStrictEqual ( expectedPath ) ;
254
- } finally {
255
- await io . rmRF ( target ) ;
256
- }
257
- } ) ;
258
-
259
- test ( "resolvePaths with home not found" , async ( ) => {
260
- const filePath = "~/.cache/yarn" ;
261
- const homedirMock = jest . spyOn ( os , "homedir" ) ;
262
- homedirMock . mockImplementation ( ( ) => {
263
- return "" ;
264
- } ) ;
265
-
266
- await expect ( actionUtils . resolvePaths ( [ filePath ] ) ) . rejects . toThrow (
267
- "Unable to determine HOME directory"
268
- ) ;
269
- } ) ;
270
-
271
- test ( "resolvePaths inclusion pattern returns found" , async ( ) => {
272
- const pattern = "*.ts" ;
273
- // Create the following layout:
274
- // inclusion-patterns
275
- // inclusion-patterns/miss.txt
276
- // inclusion-patterns/test.ts
277
-
278
- const root = path . join ( getTempDir ( ) , "inclusion-patterns" ) ;
279
- // tarball entries will be relative to workspace
280
- process . env [ "GITHUB_WORKSPACE" ] = root ;
281
-
282
- await fs . mkdir ( root , { recursive : true } ) ;
283
- await fs . writeFile ( path . join ( root , "miss.txt" ) , "no match" ) ;
284
- await fs . writeFile ( path . join ( root , "test.ts" ) , "match" ) ;
285
-
286
- const originalCwd = process . cwd ( ) ;
287
-
288
- try {
289
- process . chdir ( root ) ;
290
-
291
- const resolvedPath = await actionUtils . resolvePaths ( [ pattern ] ) ;
292
-
293
- const expectedPath = [ "test.ts" ] ;
294
- expect ( resolvedPath ) . toStrictEqual ( expectedPath ) ;
295
- } finally {
296
- process . chdir ( originalCwd ) ;
297
- }
298
- } ) ;
299
-
300
- test ( "resolvePaths exclusion pattern returns not found" , async ( ) => {
301
- const patterns = [ "*.ts" , "!test.ts" ] ;
302
- // Create the following layout:
303
- // exclusion-patterns
304
- // exclusion-patterns/miss.txt
305
- // exclusion-patterns/test.ts
306
-
307
- const root = path . join ( getTempDir ( ) , "exclusion-patterns" ) ;
308
- // tarball entries will be relative to workspace
309
- process . env [ "GITHUB_WORKSPACE" ] = root ;
310
-
311
- await fs . mkdir ( root , { recursive : true } ) ;
312
- await fs . writeFile ( path . join ( root , "miss.txt" ) , "no match" ) ;
313
- await fs . writeFile ( path . join ( root , "test.ts" ) , "no match" ) ;
314
-
315
- const originalCwd = process . cwd ( ) ;
316
-
317
- try {
318
- process . chdir ( root ) ;
319
-
320
- const resolvedPath = await actionUtils . resolvePaths ( patterns ) ;
321
-
322
- const expectedPath = [ ] ;
323
- expect ( resolvedPath ) . toStrictEqual ( expectedPath ) ;
324
- } finally {
325
- process . chdir ( originalCwd ) ;
326
- }
327
- } ) ;
328
-
329
151
test ( "isValidEvent returns true for event that has a ref" , ( ) => {
330
152
const event = Events . Push ;
331
153
process . env [ Events . Key ] = event ;
@@ -335,16 +157,3 @@ test("isValidEvent returns true for event that has a ref", () => {
335
157
336
158
expect ( isValidEvent ) . toBe ( true ) ;
337
159
} ) ;
338
-
339
- test ( "unlinkFile unlinks file" , async ( ) => {
340
- const testDirectory = await fs . mkdtemp ( "unlinkFileTest" ) ;
341
- const testFile = path . join ( testDirectory , "test.txt" ) ;
342
- await fs . writeFile ( testFile , "hello world" ) ;
343
-
344
- await actionUtils . unlinkFile ( testFile ) ;
345
-
346
- // This should throw as testFile should not exist
347
- await expect ( fs . stat ( testFile ) ) . rejects . toThrow ( ) ;
348
-
349
- await fs . rmdir ( testDirectory ) ;
350
- } ) ;
0 commit comments