Skip to content

Commit

Permalink
Merge pull request #526 from IvanZosimov/CacheVersionUpdate
Browse files Browse the repository at this point in the history
Add support for the @actions/cache library 3.0.0
marko-zivic-93 authored Jun 28, 2022

Verified

This commit was signed with the committer’s verified signature.
evenyag Yingwen
2 parents cdcc53e + bcb9f31 commit 7d610f0
Showing 9 changed files with 2,968 additions and 1,285 deletions.
2 changes: 1 addition & 1 deletion .licenses/npm/@actions/cache.dep.yml

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

File renamed without changes.
32 changes: 32 additions & 0 deletions .licenses/npm/@actions/http-client-2.0.1.dep.yml

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

57 changes: 57 additions & 0 deletions __tests__/cache-save.test.ts
Original file line number Diff line number Diff line change
@@ -294,6 +294,63 @@ describe('run', () => {
);
expect(setFailedSpy).not.toHaveBeenCalled();
});

it('save with -1 cacheId , should not fail workflow', async () => {
inputs['cache'] = 'npm';
getStateSpy.mockImplementation((name: string) => {
if (name === State.CacheMatchedKey) {
return npmFileHash;
} else {
return yarnFileHash;
}
});
getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/npm`);
saveCacheSpy.mockImplementation(() => {
return -1;
});

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`);
expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
);
expect(saveCacheSpy).toHaveBeenCalled();
expect(infoSpy).not.toHaveBeenLastCalledWith(
`Cache saved with the key: ${yarnFileHash}`
);
expect(setFailedSpy).not.toHaveBeenCalled();
});

it('saves with error from toolkit, should fail workflow', async () => {
inputs['cache'] = 'npm';
getStateSpy.mockImplementation((name: string) => {
if (name === State.CacheMatchedKey) {
return npmFileHash;
} else {
return yarnFileHash;
}
});
getCommandOutputSpy.mockImplementationOnce(() => `${commonPath}/npm`);
saveCacheSpy.mockImplementation(() => {
throw new cache.ValidationError('Validation failed');
});

await run();

expect(getInputSpy).toHaveBeenCalled();
expect(getStateSpy).toHaveBeenCalledTimes(2);
expect(getCommandOutputSpy).toHaveBeenCalledTimes(1);
expect(debugSpy).toHaveBeenCalledWith(`npm path is ${commonPath}/npm`);
expect(infoSpy).not.toHaveBeenCalledWith(
`Cache hit occurred on the primary key ${npmFileHash}, not saving cache.`
);
expect(saveCacheSpy).toHaveBeenCalled();
expect(setFailedSpy).toHaveBeenCalled();
});
});

afterEach(() => {
1,217 changes: 1,002 additions & 215 deletions dist/cache-save/index.js

Large diffs are not rendered by default.

2,891 changes: 1,844 additions & 1,047 deletions dist/setup/index.js

Large diffs are not rendered by default.

34 changes: 25 additions & 9 deletions package-lock.json
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
"build": "ncc build -o dist/setup src/setup-node.ts && ncc build -o dist/cache-save src/cache-save.ts",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
"test": "jest",
"test": "jest --coverage",
"pre-checkin": "npm run format && npm run build && npm test"
},
"repository": {
@@ -23,7 +23,7 @@
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/cache": "^2.0.2",
"@actions/cache": "^3.0.0",
"@actions/core": "^1.6.0",
"@actions/exec": "^1.1.0",
"@actions/github": "^1.1.0",
16 changes: 5 additions & 11 deletions src/cache-save.ts
Original file line number Diff line number Diff line change
@@ -49,18 +49,12 @@ const cachePackages = async (packageManager: string) => {
return;
}

try {
await cache.saveCache([cachePath], primaryKey);
core.info(`Cache saved with the key: ${primaryKey}`);
} catch (error) {
if (error.name === cache.ValidationError.name) {
throw error;
} else if (error.name === cache.ReserveCacheError.name) {
core.info(error.message);
} else {
core.warning(`${error.message}`);
}
const cacheId = await cache.saveCache([cachePath], primaryKey);
if (cacheId == -1) {
return;
}

core.info(`Cache saved with the key: ${primaryKey}`);
};

run();

0 comments on commit 7d610f0

Please sign in to comment.