Add tests

pull/321/head
Sergey Dolin 2 years ago
parent db0ba7321f
commit b283174af6

@ -2,7 +2,13 @@ import * as exec from '@actions/exec';
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import * as cacheUtils from '../src/cache-utils';
import {PackageManagerInfo} from '../src/package-managers';
import {
getCurrentPackageManager,
PackageManagerInfo
} from '../src/package-managers';
import fs, {Dirent, PathLike} from 'fs';
import {getPackageManagerInfo} from '../src/cache-utils';
import MockInstance = jest.MockInstance;
describe('getCommandOutput', () => {
//Arrange
@ -213,3 +219,65 @@ describe('isCacheFeatureAvailable', () => {
expect(warningSpy).toHaveBeenCalledWith(warningMessage);
});
});
describe('isCacheEnabled', () => {
let inputs = {} as any;
const inSpy = jest.spyOn(core, 'getInput');
const getBooleanInputSpy = jest.spyOn(core, 'getBooleanInput');
const readdirSyncSpy = (jest.spyOn(
fs,
'readdirSync'
) as unknown) as MockInstance<string[], any[]>;
beforeAll(async () => {
inSpy.mockImplementation(name => inputs[name] || '');
getBooleanInputSpy.mockImplementation((name, options) => {
const trueValue = ['true', 'True', 'TRUE'];
const falseValue = ['false', 'False', 'FALSE'];
const val = core.getInput(name, options);
if (trueValue.includes(val)) return true;
if (falseValue.includes(val)) return false;
throw new TypeError(
`Input does not meet YAML 1.2 "Core Schema" specification: ${name}\n` +
`Support boolean input list: \`true | True | TRUE | false | False | FALSE\``
);
});
});
afterEach(() => {
jest.clearAllMocks();
inputs = {};
});
afterAll(async () => {
jest.restoreAllMocks();
}, 100000);
it('should return false if `cache` input set to false', async () => {
inputs = {cache: 'false'};
const cacheEnabled = await cacheUtils.isCacheEnabled();
expect(cacheEnabled).toBeFalsy();
});
it('should return false if `cache` input set to true', async () => {
inputs = {cache: 'true'};
const cacheEnabled = await cacheUtils.isCacheEnabled();
expect(cacheEnabled).toBeTruthy();
});
it('should returns false if `cache` input unset and no dependencies file', async () => {
inputs = {};
process.env['GITHUB_WORKSPACE'] = '/tmp';
const packageManager = getCurrentPackageManager();
const packageManagerInfo = await getPackageManagerInfo(packageManager);
readdirSyncSpy.mockImplementation(() => [
packageManagerInfo.dependencyFilePattern
]);
await expect(cacheUtils.isCacheEnabled()).resolves.toBeTruthy();
});
});

@ -957,4 +957,30 @@ use .
}
);
});
it('should not throw exception if `cache` is not set', async () => {
process.env['GITHUB_WORKSPACE'] = '/tmp';
const arch = 'x64';
os.platform = 'darwin';
os.arch = arch;
inputs['go-version'] = '1.17.6';
inputs['architecture'] = os.arch;
const setFailedSpy = jest.spyOn(core, 'setFailed');
setFailedSpy.mockImplementation(line => {
process.stderr.write('log:' + line + '\n');
});
existsSpy.mockImplementation(() => true);
let toolPath = path.normalize('/cache/go/1.17.6/x64');
findSpy.mockImplementation(() => false);
dlSpy.mockImplementation(() => '/some/temp/path');
extractTarSpy.mockImplementation(() => '/some/other/temp/path');
cacheSpy.mockImplementation(() => toolPath);
execSpy.mockImplementation(() => 'go version go1.17.6');
await main.run();
expect(setFailedSpy).not.toHaveBeenCalled();
});
});

Loading…
Cancel
Save