Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable caching by default #321

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 69 additions & 1 deletion __tests__/cache-utils.test.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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();
});
});
26 changes: 26 additions & 0 deletions __tests__/setup-go.test.ts
Expand Up @@ -957,4 +957,30 @@ use .
}
);
});

it('should not throw exception if `cache` is not set', async () => {
process.env['GITHUB_WORKSPACE'] = '.';

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();
});
});
2 changes: 1 addition & 1 deletion action.yml
Expand Up @@ -14,7 +14,7 @@ inputs:
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
cache:
description: Used to specify whether caching is needed. Set to true, if you'd like to enable caching.
default: false
required: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
required: false
default: true
required: false

cache-dependency-path:
description: 'Used to specify the path to a dependency file - go.sum'
architecture:
Expand Down