Skip to content

Commit 4ce3252

Browse files
committed
Handle globs in cacheDependencyPath
1 parent 52e352b commit 4ce3252

File tree

11 files changed

+1854
-291
lines changed

11 files changed

+1854
-291
lines changed

__tests__/authutil.test.ts

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -210,97 +210,4 @@ describe('authutil tests', () => {
210210
`@otherscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
211211
);
212212
});
213-
214-
describe('getPackageManagerWorkingDir', () => {
215-
let existsSpy: jest.SpyInstance;
216-
let lstatSpy: jest.SpyInstance;
217-
218-
beforeEach(() => {
219-
existsSpy = jest.spyOn(fs, 'existsSync');
220-
existsSpy.mockImplementation(() => true);
221-
222-
lstatSpy = jest.spyOn(fs, 'lstatSync');
223-
lstatSpy.mockImplementation(arg => ({
224-
isDirectory: () => true
225-
}));
226-
});
227-
228-
afterEach(() => {
229-
existsSpy.mockRestore();
230-
lstatSpy.mockRestore();
231-
});
232-
233-
it('getPackageManagerWorkingDir should return null for not yarn', async () => {
234-
process.env['INPUT_CACHE'] = 'some';
235-
delete process.env['INPUT_CACHE-DEPENDENCY-PATH'];
236-
const dir = cacheUtils.getPackageManagerWorkingDir();
237-
expect(dir).toBeNull();
238-
});
239-
240-
it('getPackageManagerWorkingDir should return null for not yarn with cache-dependency-path', async () => {
241-
process.env['INPUT_CACHE'] = 'some';
242-
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = '/foo/bar';
243-
const dir = cacheUtils.getPackageManagerWorkingDir();
244-
expect(dir).toBeNull();
245-
});
246-
247-
it('getPackageManagerWorkingDir should return null for yarn but without cache-dependency-path', async () => {
248-
process.env['INPUT_CACHE'] = 'yarn';
249-
delete process.env['INPUT_CACHE-DEPENDENCY-PATH'];
250-
const dir = cacheUtils.getPackageManagerWorkingDir();
251-
expect(dir).toBeNull();
252-
});
253-
254-
it('getPackageManagerWorkingDir should return null for yarn with cache-dependency-path for not-existing directory', async () => {
255-
process.env['INPUT_CACHE'] = 'yarn';
256-
const cachePath = '/foo/bar';
257-
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
258-
lstatSpy.mockImplementation(arg => ({
259-
isDirectory: () => false
260-
}));
261-
const dir = cacheUtils.getPackageManagerWorkingDir();
262-
expect(dir).toBeNull();
263-
});
264-
265-
it('getPackageManagerWorkingDir should return path for yarn with cache-dependency-path', async () => {
266-
process.env['INPUT_CACHE'] = 'yarn';
267-
const cachePath = '/foo/bar';
268-
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
269-
const dir = cacheUtils.getPackageManagerWorkingDir();
270-
expect(dir).toEqual(path.dirname(cachePath));
271-
});
272-
273-
it('getCommandOutput(getPackageManagerVersion) should be called from with getPackageManagerWorkingDir result', async () => {
274-
process.env['INPUT_CACHE'] = 'yarn';
275-
const cachePath = '/foo/bar';
276-
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
277-
const getCommandOutputSpy = jest
278-
.spyOn(cacheUtils, 'getCommandOutput')
279-
.mockReturnValue(Promise.resolve('baz'));
280-
281-
const version = await cacheUtils.getPackageManagerVersion('foo', 'bar');
282-
expect(getCommandOutputSpy).toHaveBeenCalledWith(
283-
`foo bar`,
284-
path.dirname(cachePath)
285-
);
286-
});
287-
288-
it('getCommandOutput(getCacheDirectoryPath) should be called from with getPackageManagerWorkingDir result', async () => {
289-
process.env['INPUT_CACHE'] = 'yarn';
290-
const cachePath = '/foo/bar';
291-
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
292-
const getCommandOutputSpy = jest
293-
.spyOn(cacheUtils, 'getCommandOutput')
294-
.mockReturnValue(Promise.resolve('baz'));
295-
296-
const version = await cacheUtils.getCacheDirectoryPath(
297-
{lockFilePatterns: [], getCacheFolderCommand: 'quz'},
298-
''
299-
);
300-
expect(getCommandOutputSpy).toHaveBeenCalledWith(
301-
`quz`,
302-
path.dirname(cachePath)
303-
);
304-
});
305-
});
306213
});

__tests__/cache-restore.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ describe('cache-restore', () => {
3232

3333
function findCacheFolder(command: string) {
3434
switch (command) {
35-
case utils.supportedPackageManagers.npm.getCacheFolderCommand:
35+
case utils.npmGetCacheFolderCommand:
3636
return npmCachePath;
37-
case utils.supportedPackageManagers.pnpm.getCacheFolderCommand:
37+
case utils.pnpmGetCacheFolderCommand:
3838
return pnpmCachePath;
39-
case utils.supportedPackageManagers.yarn1.getCacheFolderCommand:
39+
case utils.yarn1GetCacheFolderCommand:
4040
return yarn1CachePath;
41-
case utils.supportedPackageManagers.yarn2.getCacheFolderCommand:
41+
case utils.yarn2GetCacheFolderCommand:
4242
return yarn2CachePath;
4343
default:
4444
return 'packge/not/found';
@@ -108,7 +108,7 @@ describe('cache-restore', () => {
108108
it.each([['npm7'], ['npm6'], ['pnpm6'], ['yarn1'], ['yarn2'], ['random']])(
109109
'Throw an error because %s is not supported',
110110
async packageManager => {
111-
await expect(restoreCache(packageManager)).rejects.toThrow(
111+
await expect(restoreCache(packageManager, '')).rejects.toThrow(
112112
`Caching for '${packageManager}' is not supported`
113113
);
114114
}
@@ -132,7 +132,7 @@ describe('cache-restore', () => {
132132
}
133133
});
134134

135-
await restoreCache(packageManager);
135+
await restoreCache(packageManager, '');
136136
expect(hashFilesSpy).toHaveBeenCalled();
137137
expect(infoSpy).toHaveBeenCalledWith(
138138
`Cache restored from key: node-cache-${platform}-${packageManager}-v2-${fileHash}`
@@ -163,7 +163,7 @@ describe('cache-restore', () => {
163163
});
164164

165165
restoreCacheSpy.mockImplementationOnce(() => undefined);
166-
await restoreCache(packageManager);
166+
await restoreCache(packageManager, '');
167167
expect(hashFilesSpy).toHaveBeenCalled();
168168
expect(infoSpy).toHaveBeenCalledWith(
169169
`${packageManager} cache is not found`

__tests__/cache-save.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ describe('run', () => {
117117
expect(getInputSpy).toHaveBeenCalled();
118118
expect(getStateSpy).toHaveBeenCalledTimes(2);
119119
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
120-
expect(debugSpy).toHaveBeenCalledWith(`yarn path is ${commonPath}/yarn1`);
120+
expect(debugSpy).toHaveBeenCalledWith(
121+
'yarn path is /some/random/path/yarn1 (derived from cache-dependency-path: "")'
122+
);
121123
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 1.2.3');
122124
expect(infoSpy).toHaveBeenCalledWith(
123125
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
@@ -137,7 +139,9 @@ describe('run', () => {
137139
expect(getInputSpy).toHaveBeenCalled();
138140
expect(getStateSpy).toHaveBeenCalledTimes(2);
139141
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
140-
expect(debugSpy).toHaveBeenCalledWith(`yarn path is ${commonPath}/yarn2`);
142+
expect(debugSpy).toHaveBeenCalledWith(
143+
'yarn path is /some/random/path/yarn2 (derived from cache-dependency-path: "")'
144+
);
141145
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 2.2.3');
142146
expect(infoSpy).toHaveBeenCalledWith(
143147
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
@@ -199,7 +203,9 @@ describe('run', () => {
199203
expect(getInputSpy).toHaveBeenCalled();
200204
expect(getStateSpy).toHaveBeenCalledTimes(2);
201205
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
202-
expect(debugSpy).toHaveBeenCalledWith(`yarn path is ${commonPath}/yarn1`);
206+
expect(debugSpy).toHaveBeenCalledWith(
207+
'yarn path is /some/random/path/yarn1 (derived from cache-dependency-path: "")'
208+
);
203209
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 1.2.3');
204210
expect(infoSpy).not.toHaveBeenCalledWith(
205211
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
@@ -229,7 +235,9 @@ describe('run', () => {
229235
expect(getInputSpy).toHaveBeenCalled();
230236
expect(getStateSpy).toHaveBeenCalledTimes(2);
231237
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
232-
expect(debugSpy).toHaveBeenCalledWith(`yarn path is ${commonPath}/yarn2`);
238+
expect(debugSpy).toHaveBeenCalledWith(
239+
'yarn path is /some/random/path/yarn2 (derived from cache-dependency-path: "")'
240+
);
233241
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 2.2.3');
234242
expect(infoSpy).not.toHaveBeenCalledWith(
235243
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`

0 commit comments

Comments
 (0)