-
Notifications
You must be signed in to change notification settings - Fork 491
/
cache-utils.test.ts
388 lines (318 loc) · 11.3 KB
/
cache-utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import * as exec from '@actions/exec';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as cacheUtils from '../src/cache-utils';
import {PackageManagerInfo} from '../src/package-managers';
import fs, {ObjectEncodingOptions, PathLike} from 'fs';
import {getToolchainDirectoriesFromCachedDirectories} from '../src/cache-utils';
describe('getCommandOutput', () => {
//Arrange
const getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
it('should return trimmed stdout in case of successful exit code', async () => {
//Arrange
const stdoutResult = ' stdout ';
const trimmedStdout = stdoutResult.trim();
getExecOutputSpy.mockImplementation((commandLine: string) => {
return new Promise<exec.ExecOutput>(resolve => {
resolve({exitCode: 0, stdout: stdoutResult, stderr: ''});
});
});
//Act + Assert
return cacheUtils
.getCommandOutput('command')
.then(data => expect(data).toBe(trimmedStdout));
});
it('should return error in case of unsuccessful exit code', async () => {
//Arrange
const stderrResult = 'error message';
getExecOutputSpy.mockImplementation((commandLine: string) => {
return new Promise<exec.ExecOutput>(resolve => {
resolve({exitCode: 10, stdout: '', stderr: stderrResult});
});
});
//Act + Assert
await expect(async () => {
await cacheUtils.getCommandOutput('command');
}).rejects.toThrow();
});
});
describe('getPackageManagerInfo', () => {
it('should return package manager info in case of valid package manager name', async () => {
//Arrange
const packageManagerName = 'default';
const expectedResult = {
dependencyFilePattern: 'go.sum',
cacheFolderCommandList: ['go env GOMODCACHE', 'go env GOCACHE']
};
//Act + Assert
return cacheUtils
.getPackageManagerInfo(packageManagerName)
.then(data => expect(data).toEqual(expectedResult));
});
it('should throw the error in case of invalid package manager name', async () => {
//Arrange
const packageManagerName = 'invalidName';
//Act + Assert
await expect(async () => {
await cacheUtils.getPackageManagerInfo(packageManagerName);
}).rejects.toThrow();
});
});
describe('getCacheDirectoryPath', () => {
//Arrange
const getExecOutputSpy = jest.spyOn(exec, 'getExecOutput');
const validPackageManager: PackageManagerInfo = {
dependencyFilePattern: 'go.sum',
cacheFolderCommandList: ['go env GOMODCACHE', 'go env GOCACHE']
};
it('should return path to the cache folders which specified package manager uses', async () => {
//Arrange
getExecOutputSpy.mockImplementation((commandLine: string) => {
return new Promise<exec.ExecOutput>(resolve => {
resolve({exitCode: 0, stdout: 'path/to/cache/folder', stderr: ''});
});
});
const expectedResult = ['path/to/cache/folder', 'path/to/cache/folder'];
//Act + Assert
return cacheUtils
.getCacheDirectoryPath(validPackageManager)
.then(data => expect(data).toEqual(expectedResult));
});
it('should return path to the cache folder if one command return empty str', async () => {
//Arrange
getExecOutputSpy.mockImplementationOnce((commandLine: string) => {
return new Promise<exec.ExecOutput>(resolve => {
resolve({exitCode: 0, stdout: 'path/to/cache/folder', stderr: ''});
});
});
getExecOutputSpy.mockImplementationOnce((commandLine: string) => {
return new Promise<exec.ExecOutput>(resolve => {
resolve({exitCode: 0, stdout: '', stderr: ''});
});
});
const expectedResult = ['path/to/cache/folder'];
//Act + Assert
return cacheUtils
.getCacheDirectoryPath(validPackageManager)
.then(data => expect(data).toEqual(expectedResult));
});
it('should throw if the both commands return empty str', async () => {
getExecOutputSpy.mockImplementation((commandLine: string) => {
return new Promise<exec.ExecOutput>(resolve => {
resolve({exitCode: 10, stdout: '', stderr: ''});
});
});
//Act + Assert
await expect(async () => {
await cacheUtils.getCacheDirectoryPath(validPackageManager);
}).rejects.toThrow();
});
it('should throw if the specified package name is invalid', async () => {
getExecOutputSpy.mockImplementation((commandLine: string) => {
return new Promise<exec.ExecOutput>(resolve => {
resolve({exitCode: 10, stdout: '', stderr: 'Error message'});
});
});
//Act + Assert
await expect(async () => {
await cacheUtils.getCacheDirectoryPath(validPackageManager);
}).rejects.toThrow();
});
});
describe('isCacheFeatureAvailable', () => {
//Arrange
const isFeatureAvailableSpy = jest.spyOn(cache, 'isFeatureAvailable');
const warningSpy = jest.spyOn(core, 'warning');
it('should return true when cache feature is available', () => {
//Arrange
isFeatureAvailableSpy.mockImplementation(() => {
return true;
});
//Act
const functionResult = cacheUtils.isCacheFeatureAvailable();
//Assert
expect(functionResult).toBeTruthy();
});
it('should warn when cache feature is unavailable and GHES is not used', () => {
//Arrange
isFeatureAvailableSpy.mockImplementation(() => {
return false;
});
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
const warningMessage =
'The runner was not able to contact the cache service. Caching will be skipped';
//Act
cacheUtils.isCacheFeatureAvailable();
//Assert
expect(warningSpy).toHaveBeenCalledWith(warningMessage);
});
it('should return false when cache feature is unavailable', () => {
//Arrange
isFeatureAvailableSpy.mockImplementation(() => {
return false;
});
process.env['GITHUB_SERVER_URL'] = 'https://github.com';
//Act
const functionResult = cacheUtils.isCacheFeatureAvailable();
//Assert
expect(functionResult).toBeFalsy();
});
it('should warn when cache feature is unavailable and GHES is used', () => {
//Arrange
isFeatureAvailableSpy.mockImplementation(() => {
return false;
});
process.env['GITHUB_SERVER_URL'] = 'https://nongithub.com';
const warningMessage =
'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.';
//Act + Assert
expect(cacheUtils.isCacheFeatureAvailable()).toBeFalsy();
expect(warningSpy).toHaveBeenCalledWith(warningMessage);
});
});
describe('parseGoModForToolchainVersion', () => {
const readFileSyncSpy = jest.spyOn(fs, 'readFileSync');
afterEach(() => {
jest.clearAllMocks();
});
it('should return null when go.mod file not exist', async () => {
//Arrange
//Act
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion(
'/tmp/non/exist/foo.bar'
);
//Assert
expect(toolchainVersion).toBeNull();
});
it('should return null when go.mod file is empty', async () => {
//Arrange
readFileSyncSpy.mockImplementation(() => '');
//Act
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion('go.mod');
//Assert
expect(toolchainVersion).toBeNull();
});
it('should return null when go.mod file does not contain toolchain version', async () => {
//Arrange
readFileSyncSpy.mockImplementation(() =>
`
module example-mod
go 1.21.0
require golang.org/x/tools v0.13.0
require (
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
)
`.replace(/^\s+/gm, '')
);
//Act
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion('go.mod');
//Assert
expect(toolchainVersion).toBeNull();
});
it('should return go version when go.mod file contains go version', () => {
//Arrange
readFileSyncSpy.mockImplementation(() =>
`
module example-mod
go 1.21.0
toolchain go1.21.1
require golang.org/x/tools v0.13.0
require (
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
)
`.replace(/^\s+/gm, '')
);
//Act
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion('go.mod');
//Assert
expect(toolchainVersion).toBe('1.21.1');
});
it('should return go version when go.mod file contains more than one go version', () => {
//Arrange
readFileSyncSpy.mockImplementation(() =>
`
module example-mod
go 1.21.0
toolchain go1.21.0
toolchain go1.21.1
require golang.org/x/tools v0.13.0
require (
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
)
`.replace(/^\s+/gm, '')
);
//Act
const toolchainVersion = cacheUtils.parseGoModForToolchainVersion('go.mod');
//Assert
expect(toolchainVersion).toBe('1.21.1');
});
});
describe('getToolchainDirectoriesFromCachedDirectories', () => {
const readdirSyncSpy = jest.spyOn(fs, 'readdirSync');
const existsSyncSpy = jest.spyOn(fs, 'existsSync');
const lstatSync = jest.spyOn(fs, 'lstatSync');
afterEach(() => {
jest.clearAllMocks();
});
it('should return empty array when cacheDirectories is empty', async () => {
const toolcacheDirectories = getToolchainDirectoriesFromCachedDirectories(
'foo',
[]
);
expect(toolcacheDirectories).toEqual([]);
});
it('should return empty array when cacheDirectories does not contain /go/pkg', async () => {
readdirSyncSpy.mockImplementation(dir =>
[`${dir}1`, `${dir}2`, `${dir}3`].map(s => {
const de = new fs.Dirent();
de.name = s;
de.isDirectory = () => true;
return de;
})
);
const toolcacheDirectories = getToolchainDirectoriesFromCachedDirectories(
'1.1.1',
['foo', 'bar']
);
expect(toolcacheDirectories).toEqual([]);
});
it('should return empty array when cacheDirectories does not contain toolchain@v[0-9.]+-go{goVersion}', async () => {
readdirSyncSpy.mockImplementation(dir =>
[`${dir}1`, `${dir}2`, `${dir}3`].map(s => {
const de = new fs.Dirent();
de.name = s;
de.isDirectory = () => true;
return de;
})
);
const toolcacheDirectories = getToolchainDirectoriesFromCachedDirectories(
'foo',
['foo/go/pkg/mod', 'bar']
);
expect(toolcacheDirectories).toEqual([]);
});
it('should return one entry when cacheDirectories contains toolchain@v[0-9.]+-go{goVersion} in /pkg/mod', async () => {
let seqNo = 1;
readdirSyncSpy.mockImplementation(dir =>
[`toolchain@v0.0.1-go1.1.1.arch-${seqNo++}`].map(s => {
const de = new fs.Dirent();
de.name = s;
de.isDirectory = () => true;
return de;
})
);
existsSyncSpy.mockReturnValue(true);
// @ts-ignore - jest does not have relaxed mocks, so we ignore not-implemented methods
lstatSync.mockImplementation(() => ({isDirectory: () => true}));
const toolcacheDirectories = getToolchainDirectoriesFromCachedDirectories(
'1.1.1',
['/foo/go/pkg/mod', 'bar']
);
expect(toolcacheDirectories).toEqual([
'/foo/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.1.1.arch-1'
]);
});
});