From 6aeae83a713db206e155395aa1283263da15c210 Mon Sep 17 00:00:00 2001 From: mahabaleshwars <147705296+mahabaleshwars@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:05:25 +0530 Subject: [PATCH] testing with conditions --- __tests__/install-python.test.ts | 2 +- dist/setup/index.js | 37 ++++++++++++++++++++++--- src/install-python.ts | 46 +++++++++++++++++++++++++++++--- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/__tests__/install-python.test.ts b/__tests__/install-python.test.ts index c3a6e7b4..8763aed5 100644 --- a/__tests__/install-python.test.ts +++ b/__tests__/install-python.test.ts @@ -9,7 +9,7 @@ import * as tc from '@actions/tool-cache'; jest.mock('@actions/http-client'); jest.mock('@actions/tool-cache'); -const mockManifest = [{version: '1.0.0'}]; +const mockManifest = [{version: '3.12.0'}]; describe('getManifest', () => { it('should return manifest from repo', async () => { diff --git a/dist/setup/index.js b/dist/setup/index.js index 52db022a..8afafd18 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -91651,6 +91651,10 @@ function getManifest() { const manifestFromRepo = yield getManifestFromRepo(); core.info('Successfully fetched the manifest from the repo.'); core.info(`Manifest from repo: ${JSON.stringify(manifestFromRepo)}`); + if (!Array.isArray(manifestFromRepo) || + !manifestFromRepo.every(isValidManifestEntry)) { + throw new Error('Invalid response'); + } return manifestFromRepo; } catch (err) { @@ -91659,13 +91663,38 @@ function getManifest() { core.info(err.message); } } - const manifestFromURL = yield getManifestFromURL(); - core.info('Successfully fetched the manifest from the URL.'); - core.info(`Manifest from URL: ${JSON.stringify(manifestFromURL)}`); - return manifestFromURL; + try { + const manifestFromURL = yield getManifestFromURL(); + core.info('Successfully fetched the manifest from the URL.'); + core.info(`Manifest from URL: ${JSON.stringify(manifestFromURL)}`); + return manifestFromURL; + } + catch (err) { + core.info('Fetching the manifest from the URL failed.'); + if (err instanceof Error) { + core.info(err.message); + } + // Rethrow the error or return a default value + throw new Error('Failed to fetch the manifest from both the repo and the URL.'); + } }); } exports.getManifest = getManifest; +function isValidManifestEntry(entry) { + return (typeof entry.version === 'string' && + typeof entry.stable === 'boolean' && + typeof entry.release_url === 'string' && + Array.isArray(entry.files) && + entry.files.every(isValidFileEntry)); +} +function isValidFileEntry(file) { + return (typeof file.filename === 'string' && + typeof file.arch === 'string' && + typeof file.platform === 'string' && + (typeof file.platform_version === 'string' || + file.platform_version === undefined) && + typeof file.download_url === 'string'); +} function getManifestFromRepo() { core.info(`Getting manifest from ${MANIFEST_REPO_OWNER}/${MANIFEST_REPO_NAME}@${MANIFEST_REPO_BRANCH}`); return tc.getManifestFromRepo(MANIFEST_REPO_OWNER, MANIFEST_REPO_NAME, AUTH, MANIFEST_REPO_BRANCH); diff --git a/src/install-python.ts b/src/install-python.ts index 9b1ef24a..20767567 100644 --- a/src/install-python.ts +++ b/src/install-python.ts @@ -38,6 +38,12 @@ export async function getManifest(): Promise { const manifestFromRepo = await getManifestFromRepo(); core.info('Successfully fetched the manifest from the repo.'); core.info(`Manifest from repo: ${JSON.stringify(manifestFromRepo)}`); + if ( + !Array.isArray(manifestFromRepo) || + !manifestFromRepo.every(isValidManifestEntry) + ) { + throw new Error('Invalid response'); + } return manifestFromRepo; } catch (err) { core.info('Fetching the manifest via the API failed.'); @@ -45,10 +51,42 @@ export async function getManifest(): Promise { core.info(err.message); } } - const manifestFromURL = await getManifestFromURL(); - core.info('Successfully fetched the manifest from the URL.'); - core.info(`Manifest from URL: ${JSON.stringify(manifestFromURL)}`); - return manifestFromURL; + try { + const manifestFromURL = await getManifestFromURL(); + core.info('Successfully fetched the manifest from the URL.'); + core.info(`Manifest from URL: ${JSON.stringify(manifestFromURL)}`); + return manifestFromURL; + } catch (err) { + core.info('Fetching the manifest from the URL failed.'); + if (err instanceof Error) { + core.info(err.message); + } + // Rethrow the error or return a default value + throw new Error( + 'Failed to fetch the manifest from both the repo and the URL.' + ); + } +} + +function isValidManifestEntry(entry: any): boolean { + return ( + typeof entry.version === 'string' && + typeof entry.stable === 'boolean' && + typeof entry.release_url === 'string' && + Array.isArray(entry.files) && + entry.files.every(isValidFileEntry) + ); +} + +function isValidFileEntry(file: any): boolean { + return ( + typeof file.filename === 'string' && + typeof file.arch === 'string' && + typeof file.platform === 'string' && + (typeof file.platform_version === 'string' || + file.platform_version === undefined) && + typeof file.download_url === 'string' + ); } export function getManifestFromRepo(): Promise {