Skip to content

Commit

Permalink
Handle globs in cacheDependencyPath
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed May 17, 2023
1 parent 52e352b commit 4ce3252
Show file tree
Hide file tree
Showing 11 changed files with 1,854 additions and 291 deletions.
93 changes: 0 additions & 93 deletions __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,97 +210,4 @@ describe('authutil tests', () => {
`@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`
);
});

describe('getPackageManagerWorkingDir', () => {
let existsSpy: jest.SpyInstance;
let lstatSpy: jest.SpyInstance;

beforeEach(() => {
existsSpy = jest.spyOn(fs, 'existsSync');
existsSpy.mockImplementation(() => true);

lstatSpy = jest.spyOn(fs, 'lstatSync');
lstatSpy.mockImplementation(arg => ({
isDirectory: () => true
}));
});

afterEach(() => {
existsSpy.mockRestore();
lstatSpy.mockRestore();
});

it('getPackageManagerWorkingDir should return null for not yarn', async () => {
process.env['INPUT_CACHE'] = 'some';
delete process.env['INPUT_CACHE-DEPENDENCY-PATH'];
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toBeNull();
});

it('getPackageManagerWorkingDir should return null for not yarn with cache-dependency-path', async () => {
process.env['INPUT_CACHE'] = 'some';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = '/foo/bar';
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toBeNull();
});

it('getPackageManagerWorkingDir should return null for yarn but without cache-dependency-path', async () => {
process.env['INPUT_CACHE'] = 'yarn';
delete process.env['INPUT_CACHE-DEPENDENCY-PATH'];
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toBeNull();
});

it('getPackageManagerWorkingDir should return null for yarn with cache-dependency-path for not-existing directory', async () => {
process.env['INPUT_CACHE'] = 'yarn';
const cachePath = '/foo/bar';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
lstatSpy.mockImplementation(arg => ({
isDirectory: () => false
}));
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toBeNull();
});

it('getPackageManagerWorkingDir should return path for yarn with cache-dependency-path', async () => {
process.env['INPUT_CACHE'] = 'yarn';
const cachePath = '/foo/bar';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
const dir = cacheUtils.getPackageManagerWorkingDir();
expect(dir).toEqual(path.dirname(cachePath));
});

it('getCommandOutput(getPackageManagerVersion) should be called from with getPackageManagerWorkingDir result', async () => {
process.env['INPUT_CACHE'] = 'yarn';
const cachePath = '/foo/bar';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
const getCommandOutputSpy = jest
.spyOn(cacheUtils, 'getCommandOutput')
.mockReturnValue(Promise.resolve('baz'));

const version = await cacheUtils.getPackageManagerVersion('foo', 'bar');
expect(getCommandOutputSpy).toHaveBeenCalledWith(
`foo bar`,
path.dirname(cachePath)
);
});

it('getCommandOutput(getCacheDirectoryPath) should be called from with getPackageManagerWorkingDir result', async () => {
process.env['INPUT_CACHE'] = 'yarn';
const cachePath = '/foo/bar';
process.env['INPUT_CACHE-DEPENDENCY-PATH'] = cachePath;
const getCommandOutputSpy = jest
.spyOn(cacheUtils, 'getCommandOutput')
.mockReturnValue(Promise.resolve('baz'));

const version = await cacheUtils.getCacheDirectoryPath(
{lockFilePatterns: [], getCacheFolderCommand: 'quz'},
''
);
expect(getCommandOutputSpy).toHaveBeenCalledWith(
`quz`,
path.dirname(cachePath)
);
});
});
});
14 changes: 7 additions & 7 deletions __tests__/cache-restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ describe('cache-restore', () => {

function findCacheFolder(command: string) {
switch (command) {
case utils.supportedPackageManagers.npm.getCacheFolderCommand:
case utils.npmGetCacheFolderCommand:
return npmCachePath;
case utils.supportedPackageManagers.pnpm.getCacheFolderCommand:
case utils.pnpmGetCacheFolderCommand:
return pnpmCachePath;
case utils.supportedPackageManagers.yarn1.getCacheFolderCommand:
case utils.yarn1GetCacheFolderCommand:
return yarn1CachePath;
case utils.supportedPackageManagers.yarn2.getCacheFolderCommand:
case utils.yarn2GetCacheFolderCommand:
return yarn2CachePath;
default:
return 'packge/not/found';
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('cache-restore', () => {
it.each([['npm7'], ['npm6'], ['pnpm6'], ['yarn1'], ['yarn2'], ['random']])(
'Throw an error because %s is not supported',
async packageManager => {
await expect(restoreCache(packageManager)).rejects.toThrow(
await expect(restoreCache(packageManager, '')).rejects.toThrow(
`Caching for '${packageManager}' is not supported`
);
}
Expand All @@ -132,7 +132,7 @@ describe('cache-restore', () => {
}
});

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

restoreCacheSpy.mockImplementationOnce(() => undefined);
await restoreCache(packageManager);
await restoreCache(packageManager, '');
expect(hashFilesSpy).toHaveBeenCalled();
expect(infoSpy).toHaveBeenCalledWith(
`${packageManager} cache is not found`
Expand Down
16 changes: 12 additions & 4 deletions __tests__/cache-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
expect(debugSpy).toHaveBeenCalledWith(`yarn path is ${commonPath}/yarn1`);
expect(debugSpy).toHaveBeenCalledWith(
'yarn path is /some/random/path/yarn1 (derived from cache-dependency-path: "")'
);
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 1.2.3');
expect(infoSpy).toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
Expand All @@ -137,7 +139,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
expect(debugSpy).toHaveBeenCalledWith(`yarn path is ${commonPath}/yarn2`);
expect(debugSpy).toHaveBeenCalledWith(
'yarn path is /some/random/path/yarn2 (derived from cache-dependency-path: "")'
);
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 2.2.3');
expect(infoSpy).toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
Expand Down Expand Up @@ -199,7 +203,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
expect(debugSpy).toHaveBeenCalledWith(`yarn path is ${commonPath}/yarn1`);
expect(debugSpy).toHaveBeenCalledWith(
'yarn path is /some/random/path/yarn1 (derived from cache-dependency-path: "")'
);
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 1.2.3');
expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
Expand Down Expand Up @@ -229,7 +235,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
expect(debugSpy).toHaveBeenCalledWith(`yarn path is ${commonPath}/yarn2`);
expect(debugSpy).toHaveBeenCalledWith(
'yarn path is /some/random/path/yarn2 (derived from cache-dependency-path: "")'
);
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 2.2.3');
expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
Expand Down
Loading

0 comments on commit 4ce3252

Please sign in to comment.