Add unit tests

This commit is contained in:
Sergey Dolin 2023-04-19 16:15:09 +02:00
parent 928eb4967d
commit 339630e034
4 changed files with 71 additions and 5 deletions

View file

@ -4,6 +4,8 @@ import * as path from 'path';
import * as core from '@actions/core';
import * as io from '@actions/io';
import * as auth from '../src/authutil';
import * as cacheUtils from '../src/cache-utils';
import {getCacheDirectoryPath} from '../src/cache-utils';
let rcFile: string;
@ -209,4 +211,66 @@ 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`
);
});
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 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)
);
});
});