Skip to content

Commit

Permalink
Check local cache before checking manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoH2O1999 committed Dec 28, 2022
1 parent a600ee0 commit 3fd77af
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
22 changes: 22 additions & 0 deletions src/__tests__/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import * as manifestTC from '@actions/tool-cache/lib/manifest';
import * as tc from '@actions/tool-cache';
import {BuildBehavior, PythonVersion} from '../inputs';
import {PyPyTest, SetupPythonTests, manifestUrl} from './version.fixtures';
import {SetupPythonResult, getSetupPythonResult, isPyPy} from '../version';
import {beforeAll, describe, expect, jest, test} from '@jest/globals';
Expand Down Expand Up @@ -78,9 +79,30 @@ describe('getSetupPythonResult', () => {
mockedTC.getManifestFromRepo.mockResolvedValue(manifest);
});

test('returns found version if present in local tool cache', async () => {
mockedTC.find.mockReturnValue('local version');
const expected: SetupPythonResult = {
success: true,
version: 'local version'
};

const result = await getSetupPythonResult({
architecture: process.arch,
buildBehavior: BuildBehavior.Info,
cache: false,
token: 'token',
version: new PythonVersion('3.9')
});

expect(result).toEqual(expected);
expect(mockedTC.findFromManifest).not.toBeCalled();
expect(mockedTC.getManifestFromRepo).not.toBeCalled();
});

test.each(SetupPythonTests)(
`returns $expectedResult.${process.platform} for input version $inputs.version.type-$inputs.version.version and architecture $inputs.architecture`,
async ({expectedResult, inputs}) => {
mockedTC.find.mockReturnValue('');
let platformResult: SetupPythonResult;
if (process.platform === 'linux') {
platformResult = expectedResult.linux;
Expand Down
50 changes: 31 additions & 19 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,42 @@ export async function getSetupPythonResult(
}
} else {
core.debug('Version is CPython.');
core.debug('Downloading manifest...');
const manifest = await tc.getManifestFromRepo(
ManifestUrl.OWNER,
ManifestUrl.REPO,
`token ${inputs.token}`,
ManifestUrl.BRANCH
);
core.debug(
`Checking manifest for version "${inputs.version.version}" and arch "${inputs.architecture}"...`
);
const matchVersion = await tc.findFromManifest(
core.debug('Checking local tool cache...');
const localVersion = tc.find(
'Python',
inputs.version.version,
false,
manifest,
inputs.architecture
);
if (matchVersion === undefined) {
success = false;
resultVersionString = '';
core.debug('Could not find specified version in manifest.');
} else {
resultVersionString = matchVersion.version;
if (localVersion !== '') {
resultVersionString = localVersion;
success = true;
core.debug(`CPython version resolved to "${resultVersionString}"`);
} else {
core.debug('Downloading manifest...');
const manifest = await tc.getManifestFromRepo(
ManifestUrl.OWNER,
ManifestUrl.REPO,
`token ${inputs.token}`,
ManifestUrl.BRANCH
);
core.debug(
`Checking manifest for version "${inputs.version.version}" and arch "${inputs.architecture}"...`
);
const matchVersion = await tc.findFromManifest(
inputs.version.version,
false,
manifest,
inputs.architecture
);
if (matchVersion === undefined) {
success = false;
resultVersionString = '';
core.debug('Could not find specified version in manifest.');
} else {
resultVersionString = matchVersion.version;
success = true;
core.debug(`CPython version resolved to "${resultVersionString}"`);
}
}
}
return {
Expand Down

0 comments on commit 3fd77af

Please sign in to comment.