Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fail-on-cache-miss not working #1327

Merged
merged 3 commits into from
Mar 19, 2024
Merged

Fix fail-on-cache-miss not working #1327

merged 3 commits into from
Mar 19, 2024

Conversation

cdce8p
Copy link
Contributor

@cdce8p cdce8p commented Feb 15, 2024

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

/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

  • 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 cdce8p requested a review from a team as a code owner February 15, 2024 21:20
@cdce8p cdce8p changed the title Fixfail-on-cache-miss not working Fix fail-on-cache-miss not working Feb 15, 2024
@cdce8p
Copy link
Contributor Author

cdce8p commented Mar 1, 2024

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

@bethanyj28
Copy link
Contributor

@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!

@cdce8p
Copy link
Contributor Author

cdce8p commented Mar 1, 2024

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.

@bethanyj28
Copy link
Contributor

👋🏻 @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!

Co-authored-by: Bethany <bethanyj28@users.noreply.github.com>
Copy link
Contributor

@bethanyj28 bethanyj28 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@bethanyj28 bethanyj28 merged commit 0c45773 into actions:main Mar 19, 2024
15 checks passed
@cdce8p cdce8p deleted the fix-fail-on-cache-miss branch March 19, 2024 13:32
@cdce8p
Copy link
Contributor Author

cdce8p commented Mar 19, 2024

@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.

@bethanyj28
Copy link
Contributor

Definitely! I appreciate you updating the version 🙏🏻

Bzomak added a commit to Bzomak/oolite-msys2 that referenced this pull request Mar 22, 2024
github-actions bot pushed a commit to cirruslabs/cache that referenced this pull request Apr 8, 2024
@Emmanuelruina
Copy link

CACHING ACTION

jonathanperret added a commit to jonathanperret/ayab-desktop that referenced this pull request Jul 12, 2024
Mainly to fix the fact that fail-on-cache-miss doesn't work, see actions/cache#1327
jonathanperret added a commit to jonathanperret/ayab-desktop that referenced this pull request Jul 12, 2024
Mainly to fix the fact that fail-on-cache-miss doesn't work, see actions/cache#1327
jonathanperret added a commit to jonathanperret/ayab-desktop that referenced this pull request Sep 8, 2024
Mainly to fix the fact that fail-on-cache-miss doesn't work, see actions/cache#1327
jonathanperret added a commit to jonathanperret/ayab-desktop that referenced this pull request Sep 8, 2024
Fixes build slowness (AllYarnsAreBeautiful#702).

Also fixes fail-on-cache-miss having no effect, see
actions/cache#1327
dl1com pushed a commit to AllYarnsAreBeautiful/ayab-desktop that referenced this pull request Sep 9, 2024
Fixes build slowness (#702).

Also fixes fail-on-cache-miss having no effect, see
actions/cache#1327
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fail-on-cache-miss for restore action not failing the workflow
4 participants