check- filaure fix

This commit is contained in:
Aparna Jyothi 2024-09-13 16:16:25 +05:30
parent ce2cdf5444
commit a2a7aa78ff

View File

@ -12,8 +12,8 @@ import {
import fs from 'fs'; import fs from 'fs';
import * as cacheUtils from '../src/cache-utils'; import * as cacheUtils from '../src/cache-utils';
import * as glob from '@actions/glob'; import * as glob from '@actions/glob';
import { Globber } from '@actions/glob'; import {Globber} from '@actions/glob';
import { MockGlobber } from './mock/glob-mock'; import {MockGlobber} from './mock/glob-mock';
describe('cache-utils', () => { describe('cache-utils', () => {
const versionYarn1 = '1.2.3'; const versionYarn1 = '1.2.3';
@ -26,26 +26,32 @@ describe('cache-utils', () => {
let fsRealPathSyncSpy: jest.SpyInstance; let fsRealPathSyncSpy: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
console.log('::stop-commands::stoptoken');
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
debugSpy = jest.spyOn(core, 'debug'); debugSpy = jest.spyOn(core, 'debug');
debugSpy.mockImplementation(() => {}); debugSpy.mockImplementation(msg => {});
info = jest.spyOn(core, 'info'); info = jest.spyOn(core, 'info');
warningSpy = jest.spyOn(core, 'warning'); warningSpy = jest.spyOn(core, 'warning');
isFeatureAvailable = jest.spyOn(cache, 'isFeatureAvailable'); isFeatureAvailable = jest.spyOn(cache, 'isFeatureAvailable');
getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput'); getCommandOutputSpy = jest.spyOn(utils, 'getCommandOutput');
fsRealPathSyncSpy = jest.spyOn(fs, 'realpathSync'); fsRealPathSyncSpy = jest.spyOn(fs, 'realpathSync');
fsRealPathSyncSpy.mockImplementation(dirName => dirName); fsRealPathSyncSpy.mockImplementation(dirName => {
return dirName;
});
}); });
afterEach(() => { afterEach(() => {
jest.resetAllMocks(); jest.resetAllMocks();
process.env['GITHUB_SERVER_URL'] = ''; // Ensure environment variables are cleaned up jest.clearAllMocks();
//jest.restoreAllMocks();
}); });
afterAll(async () => { afterAll(async () => {
console.log('::stoptoken::');
jest.restoreAllMocks(); jest.restoreAllMocks();
}, 100000); }, 100000);
@ -59,7 +65,9 @@ describe('cache-utils', () => {
['npm7', null] ['npm7', null]
])('getPackageManagerInfo for %s is %o', async (packageManager, result) => { ])('getPackageManagerInfo for %s is %o', async (packageManager, result) => {
getCommandOutputSpy.mockImplementationOnce(() => versionYarn1); getCommandOutputSpy.mockImplementationOnce(() => versionYarn1);
await expect(utils.getPackageManagerInfo(packageManager)).resolves.toBe(result); await expect(utils.getPackageManagerInfo(packageManager)).resolves.toBe(
result
);
}); });
}); });
@ -73,7 +81,7 @@ describe('cache-utils', () => {
); );
}); });
it('isCacheFeatureAvailable for GHES has an internal error', () => { it('isCacheFeatureAvailable for GHES has an interhal error', () => {
isFeatureAvailable.mockImplementation(() => false); isFeatureAvailable.mockImplementation(() => false);
process.env['GITHUB_SERVER_URL'] = ''; process.env['GITHUB_SERVER_URL'] = '';
isCacheFeatureAvailable(); isCacheFeatureAvailable();
@ -88,6 +96,12 @@ describe('cache-utils', () => {
expect(isCacheFeatureAvailable()).toStrictEqual(true); expect(isCacheFeatureAvailable()).toStrictEqual(true);
}); });
afterEach(() => {
process.env['GITHUB_SERVER_URL'] = '';
jest.resetAllMocks();
jest.clearAllMocks();
});
describe('getCacheDirectoriesPaths', () => { describe('getCacheDirectoriesPaths', () => {
let existsSpy: jest.SpyInstance; let existsSpy: jest.SpyInstance;
let lstatSpy: jest.SpyInstance; let lstatSpy: jest.SpyInstance;
@ -103,8 +117,10 @@ describe('cache-utils', () => {
})); }));
globCreateSpy = jest.spyOn(glob, 'create'); globCreateSpy = jest.spyOn(glob, 'create');
globCreateSpy.mockImplementation( globCreateSpy.mockImplementation(
(pattern: string): Promise<Globber> => MockGlobber.create(['/foo', '/bar']) (pattern: string): Promise<Globber> =>
MockGlobber.create(['/foo', '/bar'])
); );
resetProjectDirectoriesMemoized(); resetProjectDirectoriesMemoized();
@ -123,18 +139,29 @@ describe('cache-utils', () => {
[supportedPackageManagers.pnpm, ''], [supportedPackageManagers.pnpm, ''],
[supportedPackageManagers.pnpm, '/dir/file.lock'], [supportedPackageManagers.pnpm, '/dir/file.lock'],
[supportedPackageManagers.pnpm, '/**/file.lock'] [supportedPackageManagers.pnpm, '/**/file.lock']
])('getCacheDirectoriesPaths should return one dir for non yarn', async (packageManagerInfo, cacheDependency) => { ])(
getCommandOutputSpy.mockImplementation(() => 'foo'); 'getCacheDirectoriesPaths should return one dir for non yarn',
async (packageManagerInfo, cacheDependency) => {
getCommandOutputSpy.mockImplementation(() => 'foo');
const dirs = await cacheUtils.getCacheDirectories(packageManagerInfo, cacheDependency); const dirs = await cacheUtils.getCacheDirectories(
expect(dirs).toEqual(['foo']); packageManagerInfo,
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1); cacheDependency
}); );
expect(dirs).toEqual(['foo']);
// to do not call for a version
// call once for get cache folder
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
}
);
it('getCacheDirectoriesPaths should return one dir for yarn without cacheDependency', async () => { it('getCacheDirectoriesPaths should return one dir for yarn without cacheDependency', async () => {
getCommandOutputSpy.mockImplementation(() => 'foo'); getCommandOutputSpy.mockImplementation(() => 'foo');
const dirs = await cacheUtils.getCacheDirectories(supportedPackageManagers.yarn, ''); const dirs = await cacheUtils.getCacheDirectories(
supportedPackageManagers.yarn,
''
);
expect(dirs).toEqual(['foo']); expect(dirs).toEqual(['foo']);
}); });
@ -148,124 +175,189 @@ describe('cache-utils', () => {
[supportedPackageManagers.yarn, ''], [supportedPackageManagers.yarn, ''],
[supportedPackageManagers.yarn, '/dir/file.lock'], [supportedPackageManagers.yarn, '/dir/file.lock'],
[supportedPackageManagers.yarn, '/**/file.lock'] [supportedPackageManagers.yarn, '/**/file.lock']
])('getCacheDirectoriesPaths should throw for getCommandOutput returning empty', async (packageManagerInfo, cacheDependency) => { ])(
getCommandOutputSpy.mockImplementation((command: string) => 'getCacheDirectoriesPaths should throw for getCommandOutput returning empty',
command.includes('version') ? '1.' : '' async (packageManagerInfo, cacheDependency) => {
); getCommandOutputSpy.mockImplementation((command: string) =>
// return empty string to indicate getCacheFolderPath failed
// --version still works
command.includes('version') ? '1.' : ''
);
await expect(cacheUtils.getCacheDirectories(packageManagerInfo, cacheDependency)).rejects.toThrow(); await expect(
}); cacheUtils.getCacheDirectories(packageManagerInfo, cacheDependency)
).rejects.toThrow(); //'Could not get cache folder path for /dir');
}
);
it.each([ it.each([
[supportedPackageManagers.yarn, '/dir/file.lock'], [supportedPackageManagers.yarn, '/dir/file.lock'],
[supportedPackageManagers.yarn, '/**/file.lock'] [supportedPackageManagers.yarn, '/**/file.lock']
])('getCacheDirectoriesPaths should not throw in case of having no directories', async (packageManagerInfo, cacheDependency) => { ])(
lstatSpy.mockImplementation(arg => ({ 'getCacheDirectoriesPaths should nothrow in case of having not directories',
isDirectory: () => false async (packageManagerInfo, cacheDependency) => {
})); lstatSpy.mockImplementation(arg => ({
isDirectory: () => false
}));
await cacheUtils.getCacheDirectories(packageManagerInfo, cacheDependency); await cacheUtils.getCacheDirectories(
expect(warningSpy).toHaveBeenCalledTimes(1); packageManagerInfo,
expect(warningSpy).toHaveBeenCalledWith( cacheDependency
`No existing directories found containing cache-dependency-path="${cacheDependency}"` );
); expect(warningSpy).toHaveBeenCalledTimes(1);
}); expect(warningSpy).toHaveBeenCalledWith(
`No existing directories found containing cache-dependency-path="${cacheDependency}"`
);
}
);
it.each(['1.1.1', '2.2.2'])('getCacheDirectoriesPaths yarn v%s should return one dir without cacheDependency', async version => { it.each(['1.1.1', '2.2.2'])(
getCommandOutputSpy.mockImplementationOnce(() => version); 'getCacheDirectoriesPaths yarn v%s should return one dir without cacheDependency',
getCommandOutputSpy.mockImplementationOnce(() => `foo${version}`); async version => {
getCommandOutputSpy.mockImplementationOnce(() => version);
getCommandOutputSpy.mockImplementationOnce(() => `foo${version}`);
const dirs = await cacheUtils.getCacheDirectories(supportedPackageManagers.yarn, ''); const dirs = await cacheUtils.getCacheDirectories(
expect(dirs).toEqual([`foo${version}`]); supportedPackageManagers.yarn,
}); ''
);
expect(dirs).toEqual([`foo${version}`]);
}
);
it.each(['1.1.1', '2.2.2'])('getCacheDirectoriesPaths yarn v%s should return 2 dirs with globbed cacheDependency', async version => { it.each(['1.1.1', '2.2.2'])(
let dirNo = 1; 'getCacheDirectoriesPaths yarn v%s should return 2 dirs with globbed cacheDependency',
getCommandOutputSpy.mockImplementation((command: string) => async version => {
command.includes('version') ? version : `file_${version}_${dirNo++}` let dirNo = 1;
); getCommandOutputSpy.mockImplementation((command: string) =>
globCreateSpy.mockImplementation( command.includes('version') ? version : `file_${version}_${dirNo++}`
(pattern: string): Promise<Globber> => MockGlobber.create(['/tmp/dir1/file', '/tmp/dir2/file']) );
); globCreateSpy.mockImplementation(
(pattern: string): Promise<Globber> =>
MockGlobber.create(['/tmp/dir1/file', '/tmp/dir2/file'])
);
const dirs = await cacheUtils.getCacheDirectories(supportedPackageManagers.yarn, '/tmp/**/file'); const dirs = await cacheUtils.getCacheDirectories(
expect(dirs).toEqual([`file_${version}_1`, `file_${version}_2`]); supportedPackageManagers.yarn,
}); '/tmp/**/file'
);
expect(dirs).toEqual([`file_${version}_1`, `file_${version}_2`]);
}
);
it.each(['1.1.1', '2.2.2'])('getCacheDirectoriesPaths yarn v%s should return 2 dirs with globbed cacheDependency expanding to duplicates', async version => { it.each(['1.1.1', '2.2.2'])(
let dirNo = 1; 'getCacheDirectoriesPaths yarn v%s should return 2 dirs with globbed cacheDependency expanding to duplicates',
getCommandOutputSpy.mockImplementation((command: string) => async version => {
command.includes('version') ? version : `file_${version}_${dirNo++}` let dirNo = 1;
); getCommandOutputSpy.mockImplementation((command: string) =>
globCreateSpy.mockImplementation( command.includes('version') ? version : `file_${version}_${dirNo++}`
(pattern: string): Promise<Globber> => MockGlobber.create([ );
'/tmp/dir1/file', globCreateSpy.mockImplementation(
'/tmp/dir2/file', (pattern: string): Promise<Globber> =>
'/tmp/dir1/file' MockGlobber.create([
]) '/tmp/dir1/file',
); '/tmp/dir2/file',
'/tmp/dir1/file'
])
);
const dirs = await cacheUtils.getCacheDirectories(supportedPackageManagers.yarn, '/tmp/**/file'); const dirs = await cacheUtils.getCacheDirectories(
expect(dirs).toEqual([`file_${version}_1`, `file_${version}_2`]); supportedPackageManagers.yarn,
}); '/tmp/**/file'
);
expect(dirs).toEqual([`file_${version}_1`, `file_${version}_2`]);
}
);
it.each(['1.1.1', '2.2.2'])('getCacheDirectoriesPaths yarn v%s should return 2 unique dirs despite duplicate cache directories', async version => { it.each(['1.1.1', '2.2.2'])(
let dirNo = 1; 'getCacheDirectoriesPaths yarn v%s should return 2 uniq dirs despite duplicate cache directories',
getCommandOutputSpy.mockImplementation((command: string) => async version => {
command.includes('version') ? version : `file_${version}_${dirNo++ % 2}` let dirNo = 1;
); getCommandOutputSpy.mockImplementation((command: string) =>
globCreateSpy.mockImplementation( command.includes('version')
(pattern: string): Promise<Globber> => MockGlobber.create([ ? version
'/tmp/dir1/file', : `file_${version}_${dirNo++ % 2}`
'/tmp/dir2/file', );
'/tmp/dir3/file' globCreateSpy.mockImplementation(
]) (pattern: string): Promise<Globber> =>
); MockGlobber.create([
'/tmp/dir1/file',
'/tmp/dir2/file',
'/tmp/dir3/file'
])
);
const dirs = await cacheUtils.getCacheDirectories(supportedPackageManagers.yarn, '/tmp/**/file'); const dirs = await cacheUtils.getCacheDirectories(
expect(dirs).toEqual([`file_${version}_1`, `file_${version}_0`]); supportedPackageManagers.yarn,
expect(getCommandOutputSpy).toHaveBeenCalledTimes(6); '/tmp/**/file'
expect(getCommandOutputSpy).toHaveBeenCalledWith('yarn --version', '/tmp/dir1'); );
expect(getCommandOutputSpy).toHaveBeenCalledWith('yarn --version', '/tmp/dir2'); expect(dirs).toEqual([`file_${version}_1`, `file_${version}_0`]);
expect(getCommandOutputSpy).toHaveBeenCalledWith('yarn --version', '/tmp/dir3'); expect(getCommandOutputSpy).toHaveBeenCalledTimes(6);
expect(getCommandOutputSpy).toHaveBeenCalledWith( expect(getCommandOutputSpy).toHaveBeenCalledWith(
version.startsWith('1.') ? 'yarn cache dir' : 'yarn config get cacheFolder', 'yarn --version',
'/tmp/dir1' '/tmp/dir1'
); );
expect(getCommandOutputSpy).toHaveBeenCalledWith( expect(getCommandOutputSpy).toHaveBeenCalledWith(
version.startsWith('1.') ? 'yarn cache dir' : 'yarn config get cacheFolder', 'yarn --version',
'/tmp/dir2' '/tmp/dir2'
); );
expect(getCommandOutputSpy).toHaveBeenCalledWith( expect(getCommandOutputSpy).toHaveBeenCalledWith(
version.startsWith('1.') ? 'yarn cache dir' : 'yarn config get cacheFolder', 'yarn --version',
'/tmp/dir3' '/tmp/dir3'
); );
}); expect(getCommandOutputSpy).toHaveBeenCalledWith(
version.startsWith('1.')
? 'yarn cache dir'
: 'yarn config get cacheFolder',
'/tmp/dir1'
);
expect(getCommandOutputSpy).toHaveBeenCalledWith(
version.startsWith('1.')
? 'yarn cache dir'
: 'yarn config get cacheFolder',
'/tmp/dir2'
);
expect(getCommandOutputSpy).toHaveBeenCalledWith(
version.startsWith('1.')
? 'yarn cache dir'
: 'yarn config get cacheFolder',
'/tmp/dir3'
);
}
);
it.each(['1.1.1', '2.2.2'])('getCacheDirectoriesPaths yarn v%s should return 4 dirs with multiple globs', async version => { it.each(['1.1.1', '2.2.2'])(
const cacheDependencyPath = `/tmp/dir1/file 'getCacheDirectoriesPaths yarn v%s should return 4 dirs with multiple globs',
/tmp/dir2/file async version => {
// simulate wrong indents
const cacheDependencyPath = `/tmp/dir1/file
/tmp/dir2/file
/tmp/**/file /tmp/**/file
`; `;
globCreateSpy.mockImplementation( globCreateSpy.mockImplementation(
(pattern: string): Promise<Globber> => MockGlobber.create([ (pattern: string): Promise<Globber> =>
'/tmp/dir1/file', MockGlobber.create([
'/tmp/dir2/file', '/tmp/dir1/file',
'/tmp/dir3/file', '/tmp/dir2/file',
'/tmp/dir4/file' '/tmp/dir3/file',
]) '/tmp/dir4/file'
); ])
let dirNo = 1; );
getCommandOutputSpy.mockImplementation((command: string) => let dirNo = 1;
command.includes('version') ? version : `file_${version}_${dirNo++}` getCommandOutputSpy.mockImplementation((command: string) =>
); command.includes('version') ? version : `file_${version}_${dirNo++}`
const dirs = await cacheUtils.getCacheDirectories(supportedPackageManagers.yarn, cacheDependencyPath); );
expect(dirs).toEqual([ const dirs = await cacheUtils.getCacheDirectories(
`file_${version}_1`, supportedPackageManagers.yarn,
`file_${version}_2`, cacheDependencyPath
`file_${version}_3`, );
`file_${version}_4` expect(dirs).toEqual([
]); `file_${version}_1`,
}); `file_${version}_2`,
`file_${version}_3`,
`file_${version}_4`
]);
}
);
}); });
}); });