final test commit

This commit is contained in:
mahabaleshwars 2024-09-25 14:32:22 +05:30
parent da830ea0e9
commit d148396753
3 changed files with 72 additions and 78 deletions

View File

@ -9,7 +9,25 @@ import * as tc from '@actions/tool-cache';
jest.mock('@actions/http-client'); jest.mock('@actions/http-client');
jest.mock('@actions/tool-cache'); jest.mock('@actions/tool-cache');
const mockManifest = [{version: '3.12.0'}]; const mockManifest = [
{
version: '3.9.0',
stable: true,
release_url: 'https://example.com/release-url',
files: [
{
filename: 'python-3.9.0-macosx10.9.pkg',
arch: 'x64',
platform: 'darwin',
download_url: 'https://example.com/download-url'
}
]
}
];
beforeEach(() => {
jest.resetAllMocks();
});
describe('getManifest', () => { describe('getManifest', () => {
it('should return manifest from repo', async () => { it('should return manifest from repo', async () => {

63
dist/setup/index.js vendored
View File

@ -91637,7 +91637,6 @@ function findReleaseFromManifest(semanticVersionSpec, architecture, manifest) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
if (!manifest) { if (!manifest) {
manifest = yield getManifest(); manifest = yield getManifest();
core.info('manifest :>> ' + JSON.stringify(manifest));
} }
const foundRelease = yield tc.findFromManifest(semanticVersionSpec, false, manifest, architecture); const foundRelease = yield tc.findFromManifest(semanticVersionSpec, false, manifest, architecture);
core.info(`Found release: ${JSON.stringify(foundRelease)}`); core.info(`Found release: ${JSON.stringify(foundRelease)}`);
@ -91648,28 +91647,14 @@ exports.findReleaseFromManifest = findReleaseFromManifest;
function getManifest() { function getManifest() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
//const manifestFromRepo = await getManifestFromRepo(); const manifestFromRepo = yield getManifestFromRepo();
const manifestFromRepo = {
sha: '5418fd77743bd877e972056787b3ee67a5725566',
node_id: 'MDQ6QmxvYjI1MDA3NzkzMzo1NDE4ZmQ3Nzc0M2JkODc3ZTk3MjA1Njc4N2IzZWU2N2E1NzI1NTY2',
size: 296984,
url: 'https://api.github.com/repos/actions/python-versions/git/blobs/5418fd77743bd877e972056787b3ee67a5725566',
content: 'fasfWfasdkjnflaknc@fakjsdhfjlakjlkfj',
encoding: 'base64'
};
core.info('Successfully fetched the manifest from the repo.'); core.info('Successfully fetched the manifest from the repo.');
core.info(`Manifest from repo: ${JSON.stringify(manifestFromRepo)}`); core.info(`Manifest from repo: ${JSON.stringify(manifestFromRepo)}`);
if (!Array.isArray(manifestFromRepo) || validateManifest(manifestFromRepo);
!manifestFromRepo.every(isValidManifestEntry)) {
throw new Error('Invalid response');
}
return manifestFromRepo; return manifestFromRepo;
} }
catch (err) { catch (err) {
core.info('Fetching the manifest via the API failed.'); logError('Fetching the manifest via the API failed.', err);
if (err instanceof Error) {
core.info(err.message);
}
} }
try { try {
const manifestFromURL = yield getManifestFromURL(); const manifestFromURL = yield getManifestFromURL();
@ -91678,16 +91663,18 @@ function getManifest() {
return manifestFromURL; return manifestFromURL;
} }
catch (err) { catch (err) {
core.info('Fetching the manifest from the URL failed.'); logError('Fetching the manifest via the URL failed.', err);
if (err instanceof Error) {
core.info(err.message);
}
// Rethrow the error or return a default value // Rethrow the error or return a default value
throw new Error('Failed to fetch the manifest from both the repo and the URL.'); throw new Error('Failed to fetch the manifest from both the repo and the URL.');
} }
}); });
} }
exports.getManifest = getManifest; exports.getManifest = getManifest;
function validateManifest(manifest) {
if (!Array.isArray(manifest) || !manifest.every(isValidManifestEntry)) {
throw new Error('Invalid manifest response');
}
}
function isValidManifestEntry(entry) { function isValidManifestEntry(entry) {
return (typeof entry.version === 'string' && return (typeof entry.version === 'string' &&
typeof entry.stable === 'boolean' && typeof entry.stable === 'boolean' &&
@ -91735,34 +91722,29 @@ function installPython(workingDirectory) {
} }
} }
}; };
if (utils_1.IS_WINDOWS) { const script = utils_1.IS_WINDOWS ? 'powershell ./setup.ps1' : 'bash ./setup.sh';
yield exec.exec('powershell', ['./setup.ps1'], options); yield exec.exec(script, [], options);
}
else {
yield exec.exec('bash', ['./setup.sh'], options);
}
}); });
} }
function installCpythonFromRelease(release) { function installCpythonFromRelease(release) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const downloadUrl = release.files[0].download_url; const downloadUrl = release.files[0].download_url;
core.info(`Download from "${downloadUrl}"`); core.info(`Download from "${downloadUrl}"`);
let pythonPath = '';
try { try {
const fileName = (0, utils_1.getDownloadFileName)(downloadUrl); const fileName = (0, utils_1.getDownloadFileName)(downloadUrl);
pythonPath = yield tc.downloadTool(downloadUrl, fileName, AUTH); const pythonPath = yield tc.downloadTool(downloadUrl, fileName, AUTH);
core.info('Extract downloaded archive'); core.info('Extract downloaded archive');
let pythonExtractedFolder; const pythonExtractedFolder = utils_1.IS_WINDOWS
if (utils_1.IS_WINDOWS) { ? yield tc.extractZip(pythonPath)
pythonExtractedFolder = yield tc.extractZip(pythonPath); : yield tc.extractTar(pythonPath);
}
else {
pythonExtractedFolder = yield tc.extractTar(pythonPath);
}
core.info('Execute installation script'); core.info('Execute installation script');
yield installPython(pythonExtractedFolder); yield installPython(pythonExtractedFolder);
} }
catch (err) { catch (err) {
handleDownloadError(err);
throw err;
}
function handleDownloadError(err) {
if (err instanceof tc.HTTPError) { if (err instanceof tc.HTTPError) {
// Rate limit? // Rate limit?
if (err.httpStatusCode === 403 || err.httpStatusCode === 429) { if (err.httpStatusCode === 403 || err.httpStatusCode === 429) {
@ -91775,11 +91757,16 @@ function installCpythonFromRelease(release) {
core.debug(err.stack); core.debug(err.stack);
} }
} }
throw err;
} }
}); });
} }
exports.installCpythonFromRelease = installCpythonFromRelease; exports.installCpythonFromRelease = installCpythonFromRelease;
function logError(message, err) {
core.info(message);
if (err instanceof Error) {
core.info(err.message);
}
}
/***/ }), /***/ }),

View File

@ -20,7 +20,6 @@ export async function findReleaseFromManifest(
): Promise<tc.IToolRelease | undefined> { ): Promise<tc.IToolRelease | undefined> {
if (!manifest) { if (!manifest) {
manifest = await getManifest(); manifest = await getManifest();
core.info('manifest :>> ' + JSON.stringify(manifest));
} }
const foundRelease = await tc.findFromManifest( const foundRelease = await tc.findFromManifest(
@ -35,30 +34,13 @@ export async function findReleaseFromManifest(
export async function getManifest(): Promise<tc.IToolRelease[]> { export async function getManifest(): Promise<tc.IToolRelease[]> {
try { try {
//const manifestFromRepo = await getManifestFromRepo(); const manifestFromRepo = await getManifestFromRepo();
const manifestFromRepo = {
sha: '5418fd77743bd877e972056787b3ee67a5725566',
node_id:
'MDQ6QmxvYjI1MDA3NzkzMzo1NDE4ZmQ3Nzc0M2JkODc3ZTk3MjA1Njc4N2IzZWU2N2E1NzI1NTY2',
size: 296984,
url: 'https://api.github.com/repos/actions/python-versions/git/blobs/5418fd77743bd877e972056787b3ee67a5725566',
content: 'fasfWfasdkjnflaknc@fakjsdhfjlakjlkfj',
encoding: 'base64'
};
core.info('Successfully fetched the manifest from the repo.'); core.info('Successfully fetched the manifest from the repo.');
core.info(`Manifest from repo: ${JSON.stringify(manifestFromRepo)}`); core.info(`Manifest from repo: ${JSON.stringify(manifestFromRepo)}`);
if ( validateManifest(manifestFromRepo);
!Array.isArray(manifestFromRepo) ||
!manifestFromRepo.every(isValidManifestEntry)
) {
throw new Error('Invalid response');
}
return manifestFromRepo; return manifestFromRepo;
} catch (err) { } catch (err) {
core.info('Fetching the manifest via the API failed.'); logError('Fetching the manifest via the API failed.', err);
if (err instanceof Error) {
core.info(err.message);
}
} }
try { try {
const manifestFromURL = await getManifestFromURL(); const manifestFromURL = await getManifestFromURL();
@ -66,10 +48,7 @@ export async function getManifest(): Promise<tc.IToolRelease[]> {
core.info(`Manifest from URL: ${JSON.stringify(manifestFromURL)}`); core.info(`Manifest from URL: ${JSON.stringify(manifestFromURL)}`);
return manifestFromURL; return manifestFromURL;
} catch (err) { } catch (err) {
core.info('Fetching the manifest from the URL failed.'); logError('Fetching the manifest via the URL failed.', err);
if (err instanceof Error) {
core.info(err.message);
}
// Rethrow the error or return a default value // Rethrow the error or return a default value
throw new Error( throw new Error(
'Failed to fetch the manifest from both the repo and the URL.' 'Failed to fetch the manifest from both the repo and the URL.'
@ -77,6 +56,12 @@ export async function getManifest(): Promise<tc.IToolRelease[]> {
} }
} }
function validateManifest(manifest: any): void {
if (!Array.isArray(manifest) || !manifest.every(isValidManifestEntry)) {
throw new Error('Invalid manifest response');
}
}
function isValidManifestEntry(entry: any): boolean { function isValidManifestEntry(entry: any): boolean {
return ( return (
typeof entry.version === 'string' && typeof entry.version === 'string' &&
@ -139,32 +124,30 @@ async function installPython(workingDirectory: string) {
} }
}; };
if (IS_WINDOWS) { const script = IS_WINDOWS ? 'powershell ./setup.ps1' : 'bash ./setup.sh';
await exec.exec('powershell', ['./setup.ps1'], options); await exec.exec(script, [], options);
} else {
await exec.exec('bash', ['./setup.sh'], options);
}
} }
export async function installCpythonFromRelease(release: tc.IToolRelease) { export async function installCpythonFromRelease(release: tc.IToolRelease) {
const downloadUrl = release.files[0].download_url; const downloadUrl = release.files[0].download_url;
core.info(`Download from "${downloadUrl}"`); core.info(`Download from "${downloadUrl}"`);
let pythonPath = '';
try { try {
const fileName = getDownloadFileName(downloadUrl); const fileName = getDownloadFileName(downloadUrl);
pythonPath = await tc.downloadTool(downloadUrl, fileName, AUTH); const pythonPath = await tc.downloadTool(downloadUrl, fileName, AUTH);
core.info('Extract downloaded archive'); core.info('Extract downloaded archive');
let pythonExtractedFolder; const pythonExtractedFolder = IS_WINDOWS
if (IS_WINDOWS) { ? await tc.extractZip(pythonPath)
pythonExtractedFolder = await tc.extractZip(pythonPath); : await tc.extractTar(pythonPath);
} else {
pythonExtractedFolder = await tc.extractTar(pythonPath);
}
core.info('Execute installation script'); core.info('Execute installation script');
await installPython(pythonExtractedFolder); await installPython(pythonExtractedFolder);
} catch (err) { } catch (err) {
handleDownloadError(err);
throw err;
}
function handleDownloadError(err: any): void {
if (err instanceof tc.HTTPError) { if (err instanceof tc.HTTPError) {
// Rate limit? // Rate limit?
if (err.httpStatusCode === 403 || err.httpStatusCode === 429) { if (err.httpStatusCode === 403 || err.httpStatusCode === 429) {
@ -178,6 +161,12 @@ export async function installCpythonFromRelease(release: tc.IToolRelease) {
core.debug(err.stack); core.debug(err.stack);
} }
} }
throw err; }
}
function logError(message: string, err: any): void {
core.info(message);
if (err instanceof Error) {
core.info(err.message);
} }
} }