cache icon indicating copy to clipboard operation
cache copied to clipboard

Fix `fail-on-cache-miss` not working

Open cdce8p opened this issue 2 years ago • 4 comments

Description

process.exit overwrites the exit code set by core.setFailed, e.g. the workflow would continue even though fail-on-cache-miss is set and no cache is found.

Originally introduced in

  • #1217 by @chkimes

/CC @kgrzebien

Motivation and Context

Fixes #1265

How Has This Been Tested?

Created a workflow which should restore a non-existing cache. With v4.0.0 the workflow continues, with this PR it fails as expected (with fail-on-cache-miss set).

Screenshots (if appropriate):

Types of changes

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)
  • [ ] Documentation (add or update README or docs)

Checklist:

  • [ ] My code follows the code style of this project.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [ ] I have read the CONTRIBUTING document.
  • [ ] I have added tests to cover my changes.
  • [ ] All new and existing tests passed.

cdce8p avatar Feb 15 '24 21:02 cdce8p

/CC @bethanyj28 Would you mind taking a look at this one? It fixes a bug which was just recently introduced.

cdce8p avatar Mar 01 '24 06:03 cdce8p

@cdce8p - thanks for the contribution! This seems reasonable to me. Could you add a test case in https://github.com/actions/cache/blob/main/tests/restoreImpl.test.ts to validate? Thank you!

bethanyj28 avatar Mar 01 '24 14:03 bethanyj28

This seems reasonable to me. Could you add a test case in https://github.com/actions/cache/blob/main/tests/restoreImpl.test.ts to validate? Thank you!

I thought about that but I'm not really sure how to do it. The only thing not tested is process.exit(1); however from what I've read online it's difficult to test that with jest. Tbh I wouldn't consider myself an expert in that area. If you've any pointers for me that would be appreciated.

I already looked at #1217 which first introduced the process.exit but AFAICT it didn't add any tests either.

cdce8p avatar Mar 01 '24 14:03 cdce8p

👋🏻 @cdce8p - sorry for the delay! While the initial change might've not had tests, it would be really nice to validate in the future that we don't regress here. I believe mocking process.exit isn't too bad in jest. Could you do something like this?

test("restore failure with earlyExit should call process exit", async () => {
    // mock `process.exit`
    const processExitMock = jest.spyOn(process, "exit").mockImplementation();

    // simulate a failure
    testUtils.setInput(Inputs.Path, "node_modules");
    const failedMock = jest.spyOn(core, "setFailed");
    const restoreCacheMock = jest.spyOn(cache, "restoreCache");

    // set true for `earlyExit`
    await restoreImpl(new StateProvider(), true);
    expect(restoreCacheMock).toHaveBeenCalledTimes(0);
    expect(failedMock).toHaveBeenCalledWith(
        "Input required and not supplied: key"
    );

    // validate `process.exit` called
    expect(processExitMock).toHaveBeenCalledWith(1);
});

Thank you!

bethanyj28 avatar Mar 18 '24 23:03 bethanyj28

@bethanyj28 Would you mind tagging the 4.0.2 release? It would be awesome if this bug would finally be fixed. I already updated the version in package.json and the changelog in this PR.

cdce8p avatar Mar 19 '24 13:03 cdce8p

Definitely! I appreciate you updating the version 🙏🏻

bethanyj28 avatar Mar 19 '24 14:03 bethanyj28