1
0
Fork 0
mirror of https://github.com/actions/setup-python.git synced 2024-11-22 06:26:25 +00:00

Cache hit output (#373)

* Remove useless await in getCacheDistributor calls

* Added cache-hit output

* Build action with cache-hit output

* Remove PromiseReturnType, add matchedKey == primaryKey check

* Update cache-distributor.ts

* Fix tests and rebuild
This commit is contained in:
Alexey 2022-04-05 06:57:13 -07:00 committed by GitHub
parent 7933d5a3dd
commit 21c0493ecf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 17 deletions

View file

@ -27,6 +27,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
let debugSpy: jest.SpyInstance; let debugSpy: jest.SpyInstance;
let saveSatetSpy: jest.SpyInstance; let saveSatetSpy: jest.SpyInstance;
let getStateSpy: jest.SpyInstance; let getStateSpy: jest.SpyInstance;
let setOutputSpy: jest.SpyInstance;
// cache spy // cache spy
let restoreCacheSpy: jest.SpyInstance; let restoreCacheSpy: jest.SpyInstance;
@ -64,6 +65,9 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
return {stdout: '', stderr: 'Error occured', exitCode: 2}; return {stdout: '', stderr: 'Error occured', exitCode: 2};
}); });
setOutputSpy = jest.spyOn(core, 'setOutput');
setOutputSpy.mockImplementation(input => undefined);
restoreCacheSpy = jest.spyOn(cache, 'restoreCache'); restoreCacheSpy = jest.spyOn(cache, 'restoreCache');
restoreCacheSpy.mockImplementation( restoreCacheSpy.mockImplementation(
(cachePaths: string[], primaryKey: string, restoreKey?: string) => { (cachePaths: string[], primaryKey: string, restoreKey?: string) => {
@ -100,7 +104,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
])( ])(
'restored dependencies for %s by primaryKey', 'restored dependencies for %s by primaryKey',
async (packageManager, pythonVersion, dependencyFile, fileHash) => { async (packageManager, pythonVersion, dependencyFile, fileHash) => {
const cacheDistributor = await getCacheDistributor( const cacheDistributor = getCacheDistributor(
packageManager, packageManager,
pythonVersion, pythonVersion,
dependencyFile dependencyFile
@ -126,7 +130,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
dependencyFile, dependencyFile,
cacheDependencyPath cacheDependencyPath
) => { ) => {
const cacheDistributor = await getCacheDistributor( const cacheDistributor = getCacheDistributor(
packageManager, packageManager,
pythonVersion, pythonVersion,
dependencyFile dependencyFile
@ -162,7 +166,7 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
return primaryKey !== fileHash && restoreKey ? pipFileLockHash : ''; return primaryKey !== fileHash && restoreKey ? pipFileLockHash : '';
} }
); );
const cacheDistributor = await getCacheDistributor( const cacheDistributor = getCacheDistributor(
packageManager, packageManager,
pythonVersion, pythonVersion,
dependencyFile dependencyFile
@ -187,6 +191,38 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
); );
}); });
describe('Check if handleMatchResult', () => {
it.each([
['pip', '3.8.12', 'requirements.txt', 'someKey', 'someKey', true],
['pipenv', '3.9.1', 'requirements.txt', 'someKey', 'someKey', true],
['poetry', '3.8.12', 'requirements.txt', 'someKey', 'someKey', true],
['pip', '3.9.2', 'requirements.txt', undefined, 'someKey', false],
['pipenv', '3.8.12', 'requirements.txt', undefined, 'someKey', false],
['poetry', '3.9.12', 'requirements.txt', undefined, 'someKey', false]
])(
'sets correct outputs',
async (
packageManager,
pythonVersion,
dependencyFile,
matchedKey,
restoredKey,
expectedOutputValue
) => {
const cacheDistributor = getCacheDistributor(
packageManager,
pythonVersion,
dependencyFile
);
cacheDistributor.handleMatchResult(matchedKey, restoredKey);
expect(setOutputSpy).toHaveBeenCalledWith(
'cache-hit',
expectedOutputValue
);
}
);
});
afterEach(() => { afterEach(() => {
jest.resetAllMocks(); jest.resetAllMocks();
jest.clearAllMocks(); jest.clearAllMocks();

View file

@ -19,6 +19,8 @@ inputs:
outputs: outputs:
python-version: python-version:
description: "The installed python version. Useful when given a version range as input." description: "The installed python version. Useful when given a version range as input."
cache-hit:
description: 'A boolean value to indicate a cache entry was found'
runs: runs:
using: 'node16' using: 'node16'
main: 'dist/setup/index.js' main: 'dist/setup/index.js'

View file

@ -37234,6 +37234,10 @@ class CacheDistributor {
core.saveState(State.CACHE_PATHS, cachePath); core.saveState(State.CACHE_PATHS, cachePath);
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey); core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey); const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey);
this.handleMatchResult(matchedKey, primaryKey);
});
}
handleMatchResult(matchedKey, primaryKey) {
if (matchedKey) { if (matchedKey) {
core.saveState(State.CACHE_MATCHED_KEY, matchedKey); core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
core.info(`Cache restored from key: ${matchedKey}`); core.info(`Cache restored from key: ${matchedKey}`);
@ -37241,7 +37245,7 @@ class CacheDistributor {
else { else {
core.info(`${this.packageManager} cache is not found`); core.info(`${this.packageManager} cache is not found`);
} }
}); core.setOutput('cache-hit', matchedKey === primaryKey);
} }
} }
exports.default = CacheDistributor; exports.default = CacheDistributor;

6
dist/setup/index.js vendored
View file

@ -42579,6 +42579,10 @@ class CacheDistributor {
core.saveState(State.CACHE_PATHS, cachePath); core.saveState(State.CACHE_PATHS, cachePath);
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey); core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey); const matchedKey = yield cache.restoreCache(cachePath, primaryKey, restoreKey);
this.handleMatchResult(matchedKey, primaryKey);
});
}
handleMatchResult(matchedKey, primaryKey) {
if (matchedKey) { if (matchedKey) {
core.saveState(State.CACHE_MATCHED_KEY, matchedKey); core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
core.info(`Cache restored from key: ${matchedKey}`); core.info(`Cache restored from key: ${matchedKey}`);
@ -42586,7 +42590,7 @@ class CacheDistributor {
else { else {
core.info(`${this.packageManager} cache is not found`); core.info(`${this.packageManager} cache is not found`);
} }
}); core.setOutput('cache-hit', matchedKey === primaryKey);
} }
} }
exports.default = CacheDistributor; exports.default = CacheDistributor;

View file

@ -41,12 +41,17 @@ abstract class CacheDistributor {
restoreKey restoreKey
); );
this.handleMatchResult(matchedKey, primaryKey);
}
public handleMatchResult(matchedKey: string | undefined, primaryKey: string) {
if (matchedKey) { if (matchedKey) {
core.saveState(State.CACHE_MATCHED_KEY, matchedKey); core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
core.info(`Cache restored from key: ${matchedKey}`); core.info(`Cache restored from key: ${matchedKey}`);
} else { } else {
core.info(`${this.packageManager} cache is not found`); core.info(`${this.packageManager} cache is not found`);
} }
core.setOutput('cache-hit', matchedKey === primaryKey);
} }
} }