From f44a8c179969568ece28d1278b4ed54ef694cd1b Mon Sep 17 00:00:00 2001 From: HarithaVattikuti <73516759+HarithaVattikuti@users.noreply.github.com> Date: Mon, 11 Dec 2023 12:43:10 +0530 Subject: [PATCH] test --- __tests__/main.test.ts | 1 + dist/cache-save/index.js | 25 ++++++++++++++++++++++--- dist/setup/index.js | 34 ++++++++++++++++++++++++++++++---- src/main.ts | 13 ++++++++++++- src/util.ts | 30 +++++++++++++++++++++++++++--- 5 files changed, 92 insertions(+), 11 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index b5724875..36024e65 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -101,6 +101,7 @@ describe('main tests', () => { ${' 14.1.0 '} | ${'14.1.0'} ${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'}| ${'>=14.0.0 <=17.0.0'} ${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'} + ${'{}'} | ${null} `.it('parses "$contents"', ({contents, expected}) => { expect(util.parseNodeVersionFile(contents)).toBe(expected); }); diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index 0b7060e5..d523d1bd 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -83324,14 +83324,33 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.unique = exports.printEnvDetailsAndSetOutput = exports.parseNodeVersionFile = void 0; const core = __importStar(__nccwpck_require__(2186)); const exec = __importStar(__nccwpck_require__(1514)); +//export function parseNodeVersionFile(contents: string): string { function parseNodeVersionFile(contents) { var _a, _b, _c; let nodeVersion; // Try parsing the file as an NPM `package.json` file. try { - nodeVersion = (_a = JSON.parse(contents).volta) === null || _a === void 0 ? void 0 : _a.node; - if (!nodeVersion) - nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node; + //nodeVersion = JSON.parse(contents).volta?.node; + //if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node; + const manifest = JSON.parse(contents); + // JSON can parse numbers, but that's handled later + if (typeof manifest === 'object') { + nodeVersion = (_a = manifest.volta) === null || _a === void 0 ? void 0 : _a.node; + if (!nodeVersion) + nodeVersion = (_b = manifest.engines) === null || _b === void 0 ? void 0 : _b.node; + // if contents are an object, we parsed JSON + // this can happen if node-version-file is a package.json + // yet contains no volta.node or engines.node + // + // if node-version file is _not_ json, control flow + // will not have reached these lines. + // + // And because we've reached here, we know the contents + // *are* JSON, so no further string parsing makes sense. + if (!nodeVersion) { + return null; + } + } } catch (_d) { core.info('Node version file is not JSON file'); diff --git a/dist/setup/index.js b/dist/setup/index.js index 76a3ee3c..fba4b72a 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -93729,7 +93729,14 @@ function resolveVersionInput() { if (!fs_1.default.existsSync(versionFilePath)) { throw new Error(`The specified node version file at: ${versionFilePath} does not exist`); } - version = (0, util_1.parseNodeVersionFile)(fs_1.default.readFileSync(versionFilePath, 'utf8')); + //version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8')); + const parsedVersion = (0, util_1.parseNodeVersionFile)(fs_1.default.readFileSync(versionFilePath, 'utf8')); + if (parsedVersion) { + version = parsedVersion; + } + else { + core.warning(`Could not determine node version from ${versionFilePath}. Falling back`); + } core.info(`Resolved ${versionFileInput} as ${version}`); } return version; @@ -93779,14 +93786,33 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.unique = exports.printEnvDetailsAndSetOutput = exports.parseNodeVersionFile = void 0; const core = __importStar(__nccwpck_require__(2186)); const exec = __importStar(__nccwpck_require__(1514)); +//export function parseNodeVersionFile(contents: string): string { function parseNodeVersionFile(contents) { var _a, _b, _c; let nodeVersion; // Try parsing the file as an NPM `package.json` file. try { - nodeVersion = (_a = JSON.parse(contents).volta) === null || _a === void 0 ? void 0 : _a.node; - if (!nodeVersion) - nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node; + //nodeVersion = JSON.parse(contents).volta?.node; + //if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node; + const manifest = JSON.parse(contents); + // JSON can parse numbers, but that's handled later + if (typeof manifest === 'object') { + nodeVersion = (_a = manifest.volta) === null || _a === void 0 ? void 0 : _a.node; + if (!nodeVersion) + nodeVersion = (_b = manifest.engines) === null || _b === void 0 ? void 0 : _b.node; + // if contents are an object, we parsed JSON + // this can happen if node-version-file is a package.json + // yet contains no volta.node or engines.node + // + // if node-version file is _not_ json, control flow + // will not have reached these lines. + // + // And because we've reached here, we know the contents + // *are* JSON, so no further string parsing makes sense. + if (!nodeVersion) { + return null; + } + } } catch (_d) { core.info('Node version file is not JSON file'); diff --git a/src/main.ts b/src/main.ts index 41fe5fc9..42240146 100644 --- a/src/main.ts +++ b/src/main.ts @@ -106,7 +106,18 @@ function resolveVersionInput(): string { ); } - version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8')); + //version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8')); + const parsedVersion = parseNodeVersionFile( + fs.readFileSync(versionFilePath, 'utf8') + ); + + if (parsedVersion) { + version = parsedVersion; + } else { + core.warning( + `Could not determine node version from ${versionFilePath}. Falling back` + ); + } core.info(`Resolved ${versionFileInput} as ${version}`); } diff --git a/src/util.ts b/src/util.ts index 3ae94a2d..0df43b45 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,13 +1,37 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; -export function parseNodeVersionFile(contents: string): string { +//export function parseNodeVersionFile(contents: string): string { +export function parseNodeVersionFile(contents: string): string | null { + let nodeVersion: string | undefined; // Try parsing the file as an NPM `package.json` file. try { - nodeVersion = JSON.parse(contents).volta?.node; - if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node; + //nodeVersion = JSON.parse(contents).volta?.node; + //if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node; + + const manifest = JSON.parse(contents); + + // JSON can parse numbers, but that's handled later + if (typeof manifest === 'object') { + nodeVersion = manifest.volta?.node; + if (!nodeVersion) nodeVersion = manifest.engines?.node; + + // if contents are an object, we parsed JSON + // this can happen if node-version-file is a package.json + // yet contains no volta.node or engines.node + // + // if node-version file is _not_ json, control flow + // will not have reached these lines. + // + // And because we've reached here, we know the contents + // *are* JSON, so no further string parsing makes sense. + if (!nodeVersion) { + return null; + } + } + } catch { core.info('Node version file is not JSON file'); }