refactor, introduce cacheDependencyPathToProjectsDirectories

it is necessary for the next PR related yarn optimization
This commit is contained in:
Sergey Dolin 2023-05-19 19:57:43 +02:00
parent 914a8e9bcc
commit 5de08eab2b
5 changed files with 293 additions and 81 deletions

View file

@ -118,9 +118,14 @@ describe('run', () => {
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
expect(debugSpy).toHaveBeenCalledWith(
'yarn path is /some/random/path/yarn1 (derived from cache-dependency-path: "")'
'Project directory "." derived from cache-dependency-path: ""'
);
expect(debugSpy).toHaveBeenCalledWith(
'Consumed yarn version is 1.2.3 (working dir: ".")'
);
expect(debugSpy).toHaveBeenCalledWith(
'yarn\'s cache folder "/some/random/path/yarn1" configured for the directory "."'
);
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 1.2.3');
expect(infoSpy).toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
);
@ -140,9 +145,14 @@ describe('run', () => {
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
expect(debugSpy).toHaveBeenCalledWith(
'yarn path is /some/random/path/yarn2 (derived from cache-dependency-path: "")'
'Project directory "." derived from cache-dependency-path: ""'
);
expect(debugSpy).toHaveBeenCalledWith(
'Consumed yarn version is 2.2.3 (working dir: ".")'
);
expect(debugSpy).toHaveBeenCalledWith(
'yarn\'s cache folder "/some/random/path/yarn2" configured for the directory "."'
);
expect(debugSpy).toHaveBeenCalledWith('Consumed yarn version is 2.2.3');
expect(infoSpy).toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${yarnFileHash}, not saving cache.`
);
@ -159,7 +169,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`);
expect(debugSpy).toHaveBeenCalledWith(
`npm's cache folder "${commonPath}/npm" configured for the root directory`
);
expect(infoSpy).toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
);
@ -176,7 +188,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(`pnpm path is ${commonPath}/pnpm`);
expect(debugSpy).toHaveBeenCalledWith(
`pnpm's cache folder "${commonPath}/pnpm" configured for the root directory`
);
expect(infoSpy).toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${pnpmFileHash}, not saving cache.`
);
@ -204,9 +218,14 @@ describe('run', () => {
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
expect(debugSpy).toHaveBeenCalledWith(
'yarn path is /some/random/path/yarn1 (derived from cache-dependency-path: "")'
'Project directory "." derived from cache-dependency-path: ""'
);
expect(debugSpy).toHaveBeenCalledWith(
'Consumed yarn version is 1.2.3 (working dir: ".")'
);
expect(debugSpy).toHaveBeenCalledWith(
'yarn\'s cache folder "/some/random/path/yarn1" configured for the directory "."'
);
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.`
);
@ -236,9 +255,14 @@ describe('run', () => {
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(2);
expect(debugSpy).toHaveBeenCalledWith(
'yarn path is /some/random/path/yarn2 (derived from cache-dependency-path: "")'
'Project directory "." derived from cache-dependency-path: ""'
);
expect(debugSpy).toHaveBeenCalledWith(
'Consumed yarn version is 2.2.3 (working dir: ".")'
);
expect(debugSpy).toHaveBeenCalledWith(
'yarn\'s cache folder "/some/random/path/yarn2" configured for the directory "."'
);
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.`
);
@ -265,7 +289,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`);
expect(debugSpy).toHaveBeenCalledWith(
`npm's cache folder "${commonPath}/npm" configured for the root directory`
);
expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
);
@ -292,7 +318,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(`pnpm path is ${commonPath}/pnpm`);
expect(debugSpy).toHaveBeenCalledWith(
`pnpm's cache folder "${commonPath}/pnpm" configured for the root directory`
);
expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${pnpmFileHash}, not saving cache.`
);
@ -322,7 +350,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`);
expect(debugSpy).toHaveBeenCalledWith(
`npm's cache folder "${commonPath}/npm" configured for the root directory`
);
expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
);
@ -352,7 +382,9 @@ describe('run', () => {
expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`);
expect(debugSpy).toHaveBeenCalledWith(
`npm's cache folder "${commonPath}/npm" configured for the root directory`
);
expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
);

View file

@ -7,7 +7,8 @@ import {
isCacheFeatureAvailable,
supportedPackageManagers,
getCommandOutput,
expandCacheDependencyPath
expandCacheDependencyPath,
expandedPatternsMemoized
} from '../src/cache-utils';
import fs from 'fs';
import * as cacheUtils from '../src/cache-utils';
@ -104,6 +105,10 @@ describe('cache-utils', () => {
(pattern: string): Promise<Globber> =>
MockGlobber.create(['/foo', '/bar'])
);
Object.keys(expandedPatternsMemoized).forEach(
key => delete expandedPatternsMemoized[key]
);
});
afterEach(() => {
@ -194,13 +199,36 @@ two
[supportedPackageManagers.yarn, '/dir/file.lock'],
[supportedPackageManagers.yarn, '/**/file.lock']
])(
'getCacheDirectoriesPaths should return empty array of folder in case of error',
'getCacheDirectoriesPaths should throw for getCommandOutput returning empty',
async (packageManagerInfo, cacheDependency) => {
getCommandOutputSpy.mockImplementation((command: string) =>
// return empty string to indicate getCacheFolderPath failed
// --version still works
command.includes('version') ? '1.' : ''
);
await expect(
cacheUtils.getCacheDirectoriesPaths(
packageManagerInfo,
cacheDependency
)
).rejects.toThrow(); //'Could not get cache folder path for /dir');
}
);
it.each([
[supportedPackageManagers.npm, ''],
[supportedPackageManagers.npm, '/dir/file.lock'],
[supportedPackageManagers.npm, '/**/file.lock'],
[supportedPackageManagers.pnpm, ''],
[supportedPackageManagers.pnpm, '/dir/file.lock'],
[supportedPackageManagers.pnpm, '/**/file.lock'],
[supportedPackageManagers.yarn, ''],
[supportedPackageManagers.yarn, '/dir/file.lock'],
[supportedPackageManagers.yarn, '/**/file.lock']
])(
'getCacheDirectoriesPaths should throw in case of having not directories',
async (packageManagerInfo, cacheDependency) => {
lstatSpy.mockImplementation(arg => ({
isDirectory: () => false
}));
@ -248,9 +276,8 @@ two
}
);
// TODO: by design - glob is not expected to return duplicates so 3 patterns do not collapse to 2
it.each(['1.1.1', '2.2.2'])(
'getCacheDirectoriesPaths yarn v%s should return 3 dirs with globbed cacheDependency expanding to duplicates',
'getCacheDirectoriesPaths yarn v%s should return 2 dirs with globbed cacheDependency expanding to duplicates',
async version => {
let dirNo = 1;
getCommandOutputSpy.mockImplementation((command: string) =>
@ -269,11 +296,7 @@ two
supportedPackageManagers.yarn,
'/tmp/**/file'
);
expect(dirs).toEqual([
`file_${version}_1`,
`file_${version}_2`,
`file_${version}_3`
]);
expect(dirs).toEqual([`file_${version}_1`, `file_${version}_2`]);
}
);