Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions __tests__/save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,44 @@ test("save with exact match returns early", async () => {
expect(failedMock).toHaveBeenCalledTimes(0);
});

test("save with exact match and updates enabled updates the cache", async () => {
const infoMock = jest.spyOn(core, "info");
const failedMock = jest.spyOn(core, "setFailed");

const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
const savedCacheKey = primaryKey;

jest.spyOn(core, "getState")
// Cache Entry State
.mockImplementationOnce(() => {
return savedCacheKey;
})
// Cache Key State
.mockImplementationOnce(() => {
return primaryKey;
});

const inputPath = "node_modules";
testUtils.setInput(Inputs.Path, inputPath);
testUtils.setInput(Inputs.Update, "true");

const cacheId = 4;
const saveCacheMock = jest
.spyOn(cache, "saveCache")
.mockImplementationOnce(() => {
return Promise.resolve(cacheId);
});

await run();

expect(infoMock).toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${primaryKey}, but updates were enabled, so updating cache.`
);
expect(saveCacheMock).toHaveBeenCalledTimes(1);
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
expect(failedMock).toHaveBeenCalledTimes(0);
});

test("save with missing input outputs warning", async () => {
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
const failedMock = jest.spyOn(core, "setFailed");
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
restore-keys:
description: 'An ordered list of keys to use for restoring the cache if no cache hit occurred for key'
required: false
update:
description: 'If true, the cache will be updated if the key already exists'
required: false
outputs:
cache-hit:
description: 'A boolean value to indicate an exact match was found for the primary key'
Expand Down
1 change: 1 addition & 0 deletions dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6717,6 +6717,7 @@ var Inputs;
Inputs["Key"] = "key";
Inputs["Path"] = "path";
Inputs["RestoreKeys"] = "restore-keys";
Inputs["Update"] = "update";
})(Inputs = exports.Inputs || (exports.Inputs = {}));
var Outputs;
(function (Outputs) {
Expand Down
10 changes: 8 additions & 2 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6605,8 +6605,13 @@ function run() {
return;
}
if (utils.isExactKeyMatch(primaryKey, state)) {
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
return;
if (core.getInput(constants_1.Inputs.Update) === "true") {
core.info(`Cache hit occurred on the primary key ${primaryKey}, but updates were enabled, so updating cache.`);
}
else {
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
return;
}
}
const cachePaths = utils.getInputAsArray(constants_1.Inputs.Path, {
required: true
Expand Down Expand Up @@ -6802,6 +6807,7 @@ var Inputs;
Inputs["Key"] = "key";
Inputs["Path"] = "path";
Inputs["RestoreKeys"] = "restore-keys";
Inputs["Update"] = "update";
})(Inputs = exports.Inputs || (exports.Inputs = {}));
var Outputs;
(function (Outputs) {
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export enum Inputs {
Key = "key",
Path = "path",
RestoreKeys = "restore-keys"
RestoreKeys = "restore-keys",
Update = "update"
}

export enum Outputs {
Expand Down
14 changes: 10 additions & 4 deletions src/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ async function run(): Promise<void> {
}

if (utils.isExactKeyMatch(primaryKey, state)) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
);
return;
if (core.getInput(Inputs.Update) === "true") {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, but updates were enabled, so updating cache.`
);
} else {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
);
return;
}
}

const cachePaths = utils.getInputAsArray(Inputs.Path, {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ interface CacheInput {
path: string;
key: string;
restoreKeys?: string[];
update?: string;
}

export function setInputs(input: CacheInput): void {
setInput(Inputs.Path, input.path);
setInput(Inputs.Key, input.key);
input.update
? setInput(Inputs.Update, input.update)
: setInput(Inputs.Update, "false");
input.restoreKeys &&
setInput(Inputs.RestoreKeys, input.restoreKeys.join("\n"));
}

export function clearInputs(): void {
delete process.env[getInputName(Inputs.Path)];
delete process.env[getInputName(Inputs.Key)];
delete process.env[getInputName(Inputs.Update)];
delete process.env[getInputName(Inputs.RestoreKeys)];
}