Unverified Commit 6fd2d453 authored by Sampark Sharma's avatar Sampark Sharma Committed by GitHub
Browse files

Add support to opt-in enable cross-os caching on windows (#1056)

* Add support to opt-in enable cross-os caching on windows

* Fix tests

* Address review comments and update tests

* Fix tests

* Address review comments

* Address review comments
parent 1f414295
Loading
Loading
Loading
Loading

File changed.

Preview suppressed by a .gitattributes entry or the file's encoding is unsupported.

+20
Changes for __tests__/actionUtils.test.ts: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -174,6 +174,26 @@ test("getInputAsInt throws if required and value missing", () => {
    ).toThrowError();
});

test("getInputAsBool returns false if input not set", () => {
    expect(actionUtils.getInputAsBool("undefined")).toBe(false);
});

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

test("getInputAsBool returns false if input is invalid or NaN", () => {
    testUtils.setInput("foo", "bar");
    expect(actionUtils.getInputAsBool("foo")).toBe(false);
});

test("getInputAsBool throws if required and value missing", () => {
    expect(() =>
        actionUtils.getInputAsBool("undefined2", { required: true })
    ).toThrowError();
});

test("isCacheFeatureAvailable for ac enabled", () => {
    jest.spyOn(cache, "isFeatureAvailable").mockImplementation(() => true);

+32 −8
Changes for __tests__/restore.test.ts: 32 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -27,9 +27,17 @@ beforeAll(() => {
            return actualUtils.getInputAsArray(name, options);
        }
    );

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

beforeEach(() => {
    jest.restoreAllMocks();
    process.env[Events.Key] = Events.Push;
    process.env[RefKey] = "refs/heads/feature-branch";

@@ -50,7 +58,8 @@ test("restore with no cache found", async () => {
    const key = "node-test";
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -65,7 +74,7 @@ test("restore with no cache found", async () => {
    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(stateMock).toHaveBeenCalledTimes(1);
@@ -84,7 +93,8 @@ test("restore with restore keys and no cache found", async () => {
    testUtils.setInputs({
        path: path,
        key,
        restoreKeys: [restoreKey]
        restoreKeys: [restoreKey],
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -99,7 +109,13 @@ test("restore with restore keys and no cache found", async () => {
    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [restoreKey]);
    expect(restoreCacheMock).toHaveBeenCalledWith(
        [path],
        key,
        [restoreKey],
        {},
        false
    );

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(stateMock).toHaveBeenCalledTimes(1);
@@ -116,7 +132,8 @@ test("restore with cache found for key", async () => {
    const key = "node-test";
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -132,7 +149,7 @@ test("restore with cache found for key", async () => {
    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", key);
@@ -152,7 +169,8 @@ test("restore with cache found for restore key", async () => {
    testUtils.setInputs({
        path: path,
        key,
        restoreKeys: [restoreKey]
        restoreKeys: [restoreKey],
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -168,7 +186,13 @@ test("restore with cache found for restore key", async () => {
    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [restoreKey]);
    expect(restoreCacheMock).toHaveBeenCalledWith(
        [path],
        key,
        [restoreKey],
        {},
        false
    );

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(stateMock).toHaveBeenCalledWith("CACHE_RESULT", restoreKey);
+50 −16
Changes for __tests__/restoreImpl.test.ts: 50 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -28,9 +28,17 @@ beforeAll(() => {
            return actualUtils.getInputAsArray(name, options);
        }
    );

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

beforeEach(() => {
    jest.restoreAllMocks();
    process.env[Events.Key] = Events.Push;
    process.env[RefKey] = "refs/heads/feature-branch";

@@ -97,7 +105,8 @@ test("restore on GHES with AC available ", async () => {
    const key = "node-test";
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -113,7 +122,7 @@ test("restore on GHES with AC available ", async () => {
    await run(new StateProvider());

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
@@ -152,13 +161,20 @@ test("restore with too many keys should fail", async () => {
    testUtils.setInputs({
        path: path,
        key,
        restoreKeys
        restoreKeys,
        enableCrossOsArchive: false
    });
    const failedMock = jest.spyOn(core, "setFailed");
    const restoreCacheMock = jest.spyOn(cache, "restoreCache");
    await run(new StateProvider());
    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, restoreKeys);
    expect(restoreCacheMock).toHaveBeenCalledWith(
        [path],
        key,
        restoreKeys,
        {},
        false
    );
    expect(failedMock).toHaveBeenCalledWith(
        `Key Validation Error: Keys are limited to a maximum of 10.`
    );
@@ -169,13 +185,14 @@ test("restore with large key should fail", async () => {
    const key = "foo".repeat(512); // Over the 512 character limit
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });
    const failedMock = jest.spyOn(core, "setFailed");
    const restoreCacheMock = jest.spyOn(cache, "restoreCache");
    await run(new StateProvider());
    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);
    expect(failedMock).toHaveBeenCalledWith(
        `Key Validation Error: ${key} cannot be larger than 512 characters.`
    );
@@ -186,13 +203,14 @@ test("restore with invalid key should fail", async () => {
    const key = "comma,comma";
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });
    const failedMock = jest.spyOn(core, "setFailed");
    const restoreCacheMock = jest.spyOn(cache, "restoreCache");
    await run(new StateProvider());
    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);
    expect(failedMock).toHaveBeenCalledWith(
        `Key Validation Error: ${key} cannot contain commas.`
    );
@@ -203,7 +221,8 @@ test("restore with no cache found", async () => {
    const key = "node-test";
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -218,7 +237,7 @@ test("restore with no cache found", async () => {
    await run(new StateProvider());

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(failedMock).toHaveBeenCalledTimes(0);
@@ -235,7 +254,8 @@ test("restore with restore keys and no cache found", async () => {
    testUtils.setInputs({
        path: path,
        key,
        restoreKeys: [restoreKey]
        restoreKeys: [restoreKey],
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -250,7 +270,13 @@ test("restore with restore keys and no cache found", async () => {
    await run(new StateProvider());

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [restoreKey]);
    expect(restoreCacheMock).toHaveBeenCalledWith(
        [path],
        key,
        [restoreKey],
        {},
        false
    );

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(failedMock).toHaveBeenCalledTimes(0);
@@ -265,7 +291,8 @@ test("restore with cache found for key", async () => {
    const key = "node-test";
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -281,7 +308,7 @@ test("restore with cache found for key", async () => {
    await run(new StateProvider());

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
@@ -298,7 +325,8 @@ test("restore with cache found for restore key", async () => {
    testUtils.setInputs({
        path: path,
        key,
        restoreKeys: [restoreKey]
        restoreKeys: [restoreKey],
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -314,7 +342,13 @@ test("restore with cache found for restore key", async () => {
    await run(new StateProvider());

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [restoreKey]);
    expect(restoreCacheMock).toHaveBeenCalledWith(
        [path],
        key,
        [restoreKey],
        {},
        false
    );

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
    expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
+33 −8
Changes for __tests__/restoreOnly.test.ts: 33 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -27,9 +27,18 @@ beforeAll(() => {
            return actualUtils.getInputAsArray(name, options);
        }
    );

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

beforeEach(() => {
    jest.restoreAllMocks();
    process.env[Events.Key] = Events.Push;
    process.env[RefKey] = "refs/heads/feature-branch";

@@ -50,7 +59,8 @@ test("restore with no cache found", async () => {
    const key = "node-test";
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -65,7 +75,7 @@ test("restore with no cache found", async () => {
    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);

    expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
    expect(outputMock).toHaveBeenCalledTimes(1);
@@ -83,7 +93,8 @@ test("restore with restore keys and no cache found", async () => {
    testUtils.setInputs({
        path: path,
        key,
        restoreKeys: [restoreKey]
        restoreKeys: [restoreKey],
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -98,7 +109,13 @@ test("restore with restore keys and no cache found", async () => {
    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [restoreKey]);
    expect(restoreCacheMock).toHaveBeenCalledWith(
        [path],
        key,
        [restoreKey],
        {},
        false
    );

    expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
    expect(failedMock).toHaveBeenCalledTimes(0);
@@ -113,7 +130,8 @@ test("restore with cache found for key", async () => {
    const key = "node-test";
    testUtils.setInputs({
        path: path,
        key
        key,
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -128,7 +146,7 @@ test("restore with cache found for key", async () => {
    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [], {}, false);

    expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
    expect(outputMock).toHaveBeenCalledWith("cache-hit", "true");
@@ -147,7 +165,8 @@ test("restore with cache found for restore key", async () => {
    testUtils.setInputs({
        path: path,
        key,
        restoreKeys: [restoreKey]
        restoreKeys: [restoreKey],
        enableCrossOsArchive: false
    });

    const infoMock = jest.spyOn(core, "info");
@@ -162,7 +181,13 @@ test("restore with cache found for restore key", async () => {
    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, [restoreKey]);
    expect(restoreCacheMock).toHaveBeenCalledWith(
        [path],
        key,
        [restoreKey],
        {},
        false
    );

    expect(outputMock).toHaveBeenCalledWith("cache-primary-key", key);
    expect(outputMock).toHaveBeenCalledWith("cache-hit", "false");
Loading