Skip to content

Commit

Permalink
Adds input for upload chunk size
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadka committed Oct 2, 2020
1 parent d606e03 commit a6f1f4b
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 11 deletions.
15 changes: 15 additions & 0 deletions __tests__/actionUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ beforeAll(() => {
afterEach(() => {
delete process.env[Events.Key];
delete process.env[RefKey];
testUtils.clearInputs();
});

test("isGhes returns true if server url is not github.com", () => {
Expand Down Expand Up @@ -212,3 +213,17 @@ test("getInputAsArray handles empty lines correctly", () => {
testUtils.setInput("foo", "\n\nbar\n\nbaz\n\n");
expect(actionUtils.getInputAsArray("foo")).toEqual(["bar", "baz"]);
});

test("getInputAsInt returns undefined if input not set", () => {
expect(actionUtils.getInputAsInt("foo")).toBeUndefined();
});

test("getInputAsInt returns value if input is valid", () => {
testUtils.setInput("foo", "8");
expect(actionUtils.getInputAsInt("foo")).toBe(8);
});

test("getInputAsInt returns undefined if input is invalid or NaN", () => {
testUtils.setInput("foo", "bar");
expect(actionUtils.getInputAsInt("foo")).toBeUndefined();
});
31 changes: 27 additions & 4 deletions __tests__/save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ beforeAll(() => {
}
);

jest.spyOn(actionUtils, "getInputAsInt").mockImplementation(
(name, options) => {
return jest
.requireActual("../src/utils/actionUtils")
.getInputAsInt(name, options);
}
);

jest.spyOn(actionUtils, "isExactKeyMatch").mockImplementation(
(key, cacheResult) => {
return jest
Expand Down Expand Up @@ -193,7 +201,11 @@ test("save with large cache outputs warning", async () => {
await run();

expect(saveCacheMock).toHaveBeenCalledTimes(1);
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
expect(saveCacheMock).toHaveBeenCalledWith(
[inputPath],
primaryKey,
expect.anything()
);

expect(logWarningMock).toHaveBeenCalledTimes(1);
expect(logWarningMock).toHaveBeenCalledWith(
Expand Down Expand Up @@ -236,7 +248,11 @@ test("save with reserve cache failure outputs warning", async () => {
await run();

expect(saveCacheMock).toHaveBeenCalledTimes(1);
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
expect(saveCacheMock).toHaveBeenCalledWith(
[inputPath],
primaryKey,
expect.anything()
);

expect(infoMock).toHaveBeenCalledWith(
`Unable to reserve cache with key ${primaryKey}, another job may be creating this cache.`
Expand Down Expand Up @@ -274,7 +290,11 @@ test("save with server error outputs warning", async () => {
await run();

expect(saveCacheMock).toHaveBeenCalledTimes(1);
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
expect(saveCacheMock).toHaveBeenCalledWith(
[inputPath],
primaryKey,
expect.anything()
);

expect(logWarningMock).toHaveBeenCalledTimes(1);
expect(logWarningMock).toHaveBeenCalledWith("HTTP Error Occurred");
Expand All @@ -300,6 +320,7 @@ test("save with valid inputs uploads a cache", async () => {

const inputPath = "node_modules";
testUtils.setInput(Inputs.Path, inputPath);
testUtils.setInput(Inputs.UploadChunkSize, "4000000");

const cacheId = 4;
const saveCacheMock = jest
Expand All @@ -311,7 +332,9 @@ test("save with valid inputs uploads a cache", async () => {
await run();

expect(saveCacheMock).toHaveBeenCalledTimes(1);
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey);
expect(saveCacheMock).toHaveBeenCalledWith([inputPath], primaryKey, {
uploadChunkSize: 4000000
});

expect(failedMock).toHaveBeenCalledTimes(0);
});
11 changes: 10 additions & 1 deletion dist/restore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31296,7 +31296,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInputAsArray = exports.isValidEvent = exports.logWarning = exports.getCacheState = exports.setOutputAndState = exports.setCacheHitOutput = exports.setCacheState = exports.isExactKeyMatch = exports.isGhes = void 0;
exports.getInputAsInt = exports.getInputAsArray = exports.isValidEvent = exports.logWarning = exports.getCacheState = exports.setOutputAndState = exports.setCacheHitOutput = exports.setCacheState = exports.isExactKeyMatch = exports.isGhes = void 0;
const core = __importStar(__webpack_require__(470));
const constants_1 = __webpack_require__(694);
function isGhes() {
Expand Down Expand Up @@ -31353,6 +31353,14 @@ function getInputAsArray(name, options) {
.filter(x => x !== "");
}
exports.getInputAsArray = getInputAsArray;
function getInputAsInt(name, options) {
const value = Number(core.getInput(name, options));
if (Number.isNaN(value) || value < 0) {
return undefined;
}
return value;
}
exports.getInputAsInt = getInputAsInt;


/***/ }),
Expand Down Expand Up @@ -38485,6 +38493,7 @@ var Inputs;
Inputs["Key"] = "key";
Inputs["Path"] = "path";
Inputs["RestoreKeys"] = "restore-keys";
Inputs["UploadChunkSize"] = "upload-chunk-size";
})(Inputs = exports.Inputs || (exports.Inputs = {}));
var Outputs;
(function (Outputs) {
Expand Down
15 changes: 13 additions & 2 deletions dist/save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31296,7 +31296,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInputAsArray = exports.isValidEvent = exports.logWarning = exports.getCacheState = exports.setOutputAndState = exports.setCacheHitOutput = exports.setCacheState = exports.isExactKeyMatch = exports.isGhes = void 0;
exports.getInputAsInt = exports.getInputAsArray = exports.isValidEvent = exports.logWarning = exports.getCacheState = exports.setOutputAndState = exports.setCacheHitOutput = exports.setCacheState = exports.isExactKeyMatch = exports.isGhes = void 0;
const core = __importStar(__webpack_require__(470));
const constants_1 = __webpack_require__(694);
function isGhes() {
Expand Down Expand Up @@ -31353,6 +31353,14 @@ function getInputAsArray(name, options) {
.filter(x => x !== "");
}
exports.getInputAsArray = getInputAsArray;
function getInputAsInt(name, options) {
const value = Number(core.getInput(name, options));
if (Number.isNaN(value) || value < 0) {
return undefined;
}
return value;
}
exports.getInputAsInt = getInputAsInt;


/***/ }),
Expand Down Expand Up @@ -38353,7 +38361,9 @@ function run() {
required: true
});
try {
yield cache.saveCache(cachePaths, primaryKey);
yield cache.saveCache(cachePaths, primaryKey, {
uploadChunkSize: utils.getInputAsInt(constants_1.Inputs.UploadChunkSize)
});
}
catch (error) {
if (error.name === cache.ValidationError.name) {
Expand Down Expand Up @@ -38574,6 +38584,7 @@ var Inputs;
Inputs["Key"] = "key";
Inputs["Path"] = "path";
Inputs["RestoreKeys"] = "restore-keys";
Inputs["UploadChunkSize"] = "upload-chunk-size";
})(Inputs = exports.Inputs || (exports.Inputs = {}));
var Outputs;
(function (Outputs) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cache",
"version": "2.1.1",
"version": "2.1.2",
"private": true,
"description": "Cache dependencies and build outputs",
"main": "dist/restore/index.js",
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",
UploadChunkSize = "upload-chunk-size"
}

export enum Outputs {
Expand Down
4 changes: 3 additions & 1 deletion src/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ async function run(): Promise<void> {
});

try {
await cache.saveCache(cachePaths, primaryKey);
await cache.saveCache(cachePaths, primaryKey, {
uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize)
});
} catch (error) {
if (error.name === cache.ValidationError.name) {
throw error;
Expand Down
11 changes: 11 additions & 0 deletions src/utils/actionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ export function getInputAsArray(
.map(s => s.trim())
.filter(x => x !== "");
}

export function getInputAsInt(
name: string,
options?: core.InputOptions
): number | undefined {
const value = Number(core.getInput(name, options));
if (Number.isNaN(value) || value < 0) {
return undefined;
}
return value;
}
1 change: 1 addition & 0 deletions src/utils/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export function clearInputs(): void {
delete process.env[getInputName(Inputs.Path)];
delete process.env[getInputName(Inputs.Key)];
delete process.env[getInputName(Inputs.RestoreKeys)];
delete process.env[getInputName(Inputs.UploadChunkSize)];
}

0 comments on commit a6f1f4b

Please sign in to comment.