Skip to content

Commit

Permalink
change getinput to getstate for cache (actions#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-shibanov authored Aug 10, 2023
1 parent d82f92a commit bea5baf
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 32 deletions.
83 changes: 53 additions & 30 deletions __tests__/cache-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ describe('run', () => {

it('Package manager is not valid, skip caching', async () => {
inputs['cache'] = 'yarn3';
getStateSpy.mockImplementation(key =>
key === State.CachePackageManager ? inputs['cache'] : ''
);

await run();

Expand All @@ -108,7 +111,9 @@ describe('run', () => {
it('should not save cache for yarn1', async () => {
inputs['cache'] = 'yarn';
getStateSpy.mockImplementation(key =>
key === State.CachePrimaryKey || key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
? yarnFileHash
: key === State.CachePaths
? '["/foo/bar"]'
Expand All @@ -117,8 +122,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(infoSpy).toHaveBeenCalledWith(
Expand All @@ -130,7 +135,9 @@ describe('run', () => {
it('should not save cache for yarn2', async () => {
inputs['cache'] = 'yarn';
getStateSpy.mockImplementation(key =>
key === State.CachePrimaryKey || key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
? yarnFileHash
: key === State.CachePaths
? '["/foo/bar"]'
Expand All @@ -139,8 +146,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(infoSpy).toHaveBeenCalledWith(
Expand All @@ -152,7 +159,9 @@ describe('run', () => {
it('should not save cache for npm', async () => {
inputs['cache'] = 'npm';
getStateSpy.mockImplementation(key =>
key === State.CachePrimaryKey || key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
? yarnFileHash
: key === State.CachePaths
? '["/foo/bar"]'
Expand All @@ -162,8 +171,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(setFailedSpy).not.toHaveBeenCalled();
Expand All @@ -172,7 +181,9 @@ describe('run', () => {
it('should not save cache for pnpm', async () => {
inputs['cache'] = 'pnpm';
getStateSpy.mockImplementation(key =>
key === State.CachePrimaryKey || key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CachePrimaryKey || key === State.CacheMatchedKey
? yarnFileHash
: key === State.CachePaths
? '["/foo/bar"]'
Expand All @@ -181,8 +192,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(setFailedSpy).not.toHaveBeenCalled();
Expand All @@ -193,7 +204,9 @@ describe('run', () => {
it('saves cache from yarn 1', async () => {
inputs['cache'] = 'yarn';
getStateSpy.mockImplementation((key: string) =>
key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CacheMatchedKey
? yarnFileHash
: key === State.CachePrimaryKey
? npmFileHash
Expand All @@ -204,8 +217,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(infoSpy).not.toHaveBeenCalledWith(
Expand All @@ -221,7 +234,9 @@ describe('run', () => {
it('saves cache from yarn 2', async () => {
inputs['cache'] = 'yarn';
getStateSpy.mockImplementation((key: string) =>
key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CacheMatchedKey
? yarnFileHash
: key === State.CachePrimaryKey
? npmFileHash
Expand All @@ -232,8 +247,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(infoSpy).not.toHaveBeenCalledWith(
Expand All @@ -249,7 +264,9 @@ describe('run', () => {
it('saves cache from npm', async () => {
inputs['cache'] = 'npm';
getStateSpy.mockImplementation((key: string) =>
key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CacheMatchedKey
? npmFileHash
: key === State.CachePrimaryKey
? yarnFileHash
Expand All @@ -260,8 +277,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(infoSpy).not.toHaveBeenCalledWith(
Expand All @@ -277,7 +294,9 @@ describe('run', () => {
it('saves cache from pnpm', async () => {
inputs['cache'] = 'pnpm';
getStateSpy.mockImplementation((key: string) =>
key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CacheMatchedKey
? pnpmFileHash
: key === State.CachePrimaryKey
? npmFileHash
Expand All @@ -288,8 +307,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(infoSpy).not.toHaveBeenCalledWith(
Expand All @@ -305,7 +324,9 @@ describe('run', () => {
it('save with -1 cacheId , should not fail workflow', async () => {
inputs['cache'] = 'npm';
getStateSpy.mockImplementation((key: string) =>
key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CacheMatchedKey
? npmFileHash
: key === State.CachePrimaryKey
? yarnFileHash
Expand All @@ -319,8 +340,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(infoSpy).not.toHaveBeenCalledWith(
Expand All @@ -336,7 +357,9 @@ describe('run', () => {
it('saves with error from toolkit, should fail workflow', async () => {
inputs['cache'] = 'npm';
getStateSpy.mockImplementation((key: string) =>
key === State.CacheMatchedKey
key === State.CachePackageManager
? inputs['cache']
: key === State.CacheMatchedKey
? npmFileHash
: key === State.CachePrimaryKey
? yarnFileHash
Expand All @@ -350,8 +373,8 @@ describe('run', () => {

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(3);
expect(getInputSpy).not.toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(4);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(0);
expect(debugSpy).toHaveBeenCalledTimes(0);
expect(infoSpy).not.toHaveBeenCalledWith(
Expand Down
3 changes: 2 additions & 1 deletion dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60381,7 +60381,7 @@ process.on('uncaughtException', e => {
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const cacheLock = core.getInput('cache');
const cacheLock = core.getState(constants_1.State.CachePackageManager);
yield cachePackages(cacheLock);
}
catch (error) {
Expand Down Expand Up @@ -60692,6 +60692,7 @@ var LockType;
})(LockType = exports.LockType || (exports.LockType = {}));
var State;
(function (State) {
State["CachePackageManager"] = "SETUP_NODE_CACHE_PACKAGE_MANAGER";
State["CachePrimaryKey"] = "CACHE_KEY";
State["CacheMatchedKey"] = "CACHE_RESULT";
State["CachePaths"] = "CACHE_PATHS";
Expand Down
3 changes: 3 additions & 0 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71479,6 +71479,7 @@ var LockType;
})(LockType = exports.LockType || (exports.LockType = {}));
var State;
(function (State) {
State["CachePackageManager"] = "SETUP_NODE_CACHE_PACKAGE_MANAGER";
State["CachePrimaryKey"] = "CACHE_KEY";
State["CacheMatchedKey"] = "CACHE_RESULT";
State["CachePaths"] = "CACHE_PATHS";
Expand Down Expand Up @@ -72209,6 +72210,7 @@ const cache_restore_1 = __nccwpck_require__(9517);
const cache_utils_1 = __nccwpck_require__(1678);
const installer_factory_1 = __nccwpck_require__(5617);
const util_1 = __nccwpck_require__(2629);
const constants_1 = __nccwpck_require__(9042);
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
Expand Down Expand Up @@ -72249,6 +72251,7 @@ function run() {
auth.configAuthentication(registryUrl, alwaysAuth);
}
if (cache && cache_utils_1.isCacheFeatureAvailable()) {
core.saveState(constants_1.State.CachePackageManager, cache);
const cacheDependencyPath = core.getInput('cache-dependency-path');
yield cache_restore_1.restoreCache(cache, cacheDependencyPath);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cache-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ process.on('uncaughtException', e => {

export async function run() {
try {
const cacheLock = core.getInput('cache');
const cacheLock = core.getState(State.CachePackageManager);
await cachePackages(cacheLock);
} catch (error) {
core.setFailed(error.message);
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum LockType {
}

export enum State {
CachePackageManager = 'SETUP_NODE_CACHE_PACKAGE_MANAGER',
CachePrimaryKey = 'CACHE_KEY',
CacheMatchedKey = 'CACHE_RESULT',
CachePaths = 'CACHE_PATHS'
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {restoreCache} from './cache-restore';
import {isCacheFeatureAvailable} from './cache-utils';
import {getNodejsDistribution} from './distributions/installer-factory';
import {parseNodeVersionFile, printEnvDetailsAndSetOutput} from './util';
import {State} from './constants';

export async function run() {
try {
Expand Down Expand Up @@ -60,6 +61,7 @@ export async function run() {
}

if (cache && isCacheFeatureAvailable()) {
core.saveState(State.CachePackageManager, cache);
const cacheDependencyPath = core.getInput('cache-dependency-path');
await restoreCache(cache, cacheDependencyPath);
}
Expand Down

0 comments on commit bea5baf

Please sign in to comment.