From 658b51b920c3c16aa0a14fe22636755c8b9ad38f Mon Sep 17 00:00:00 2001
From: Sergey Dolin <sergey.dolin@gmail.com>
Date: Thu, 24 Nov 2022 18:52:31 +0100
Subject: [PATCH] requested changes

---
 __tests__/installer.test.ts      | 39 ++++++----------------
 __tests__/installer.unit.test.ts | 10 ------
 dist/setup/index.js              | 51 ++++++++++++++--------------
 src/installer.ts                 | 57 ++++++++++++++------------------
 4 files changed, 60 insertions(+), 97 deletions(-)

diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts
index 0041ab84..3ec837c2 100644
--- a/__tests__/installer.test.ts
+++ b/__tests__/installer.test.ts
@@ -72,7 +72,6 @@ describe('setup-node', () => {
     exSpy = jest.spyOn(tc, 'extractTar');
     cacheSpy = jest.spyOn(tc, 'cacheDir');
     getManifestSpy = jest.spyOn(tc, 'getManifestFromRepo');
-    // @ts-ignore
     getDistSpy = jest.spyOn(im, 'getVersionsFromDist');
     parseNodeVersionSpy = jest.spyOn(im, 'parseNodeVersionFile');
 
@@ -1302,37 +1301,25 @@ describe('setup-node', () => {
 
       await main.run();
 
-      expect(dbgSpy.mock.calls[0][0]).toBe('requested v8 canary distribution');
-      expect(dbgSpy.mock.calls[1][0]).toBe('evaluating 17 versions');
-      expect(dbgSpy.mock.calls[2][0]).toBe(
-        'matched: v20.0.0-v8-canary20221103f7e2421e91'
-      );
+      expect(dbgSpy.mock.calls[0][0]).toBe('evaluating 0 versions');
+      expect(dbgSpy.mock.calls[1][0]).toBe('match not found');
       expect(logSpy.mock.calls[0][0]).toBe(
-        'getting v8-canary node version v20.0.0-v8-canary20221103f7e2421e91...'
+        `Attempting to download ${versionSpec}...`
       );
-      expect(logSpy.mock.calls[1][0]).toBe(
-        'Attempt to find existing version in cache...'
-      );
-      expect(dbgSpy.mock.calls[3][0]).toBe('evaluating 0 versions');
-      expect(dbgSpy.mock.calls[4][0]).toBe('match not found');
-      expect(logSpy.mock.calls[2][0]).toBe(
-        'Attempting to download v20.0.0-v8-canary20221103f7e2421e91...'
-      );
-      expect(dbgSpy.mock.calls[5][0]).toBe('No manifest cached');
-      expect(dbgSpy.mock.calls[6][0]).toBe(
+      expect(dbgSpy.mock.calls[2][0]).toBe('No manifest cached');
+      expect(dbgSpy.mock.calls[3][0]).toBe(
         'Getting manifest from actions/node-versions@main'
       );
-      expect(dbgSpy.mock.calls[7][0].slice(0, 6)).toBe('check ');
-      expect(dbgSpy.mock.calls[13][0].slice(0, 6)).toBe('check ');
-      expect(logSpy.mock.calls[3][0]).toBe(
+      expect(dbgSpy.mock.calls[4][0].slice(0, 6)).toBe('check ');
+      expect(dbgSpy.mock.calls[10][0].slice(0, 6)).toBe('check ');
+      expect(logSpy.mock.calls[1][0]).toBe(
         'Not found in manifest.  Falling back to download directly from Node'
       );
-      expect(dbgSpy.mock.calls[14][0]).toBe('evaluating 17 versions');
-      expect(dbgSpy.mock.calls[15][0]).toBe(
+      expect(dbgSpy.mock.calls[12][0]).toBe('evaluating 17 versions');
+      expect(dbgSpy.mock.calls[13][0]).toBe(
         'matched: v20.0.0-v8-canary20221103f7e2421e91'
       );
-      expect(dbgSpy.mock.calls[16][0]).toBe('requested v8 canary distribution');
-      expect(logSpy.mock.calls[4][0]).toBe(
+      expect(logSpy.mock.calls[2][0]).toBe(
         'Acquiring 20.0.0-v8-canary20221103f7e2421e91 - x64 from https://nodejs.org/download/v8-canary/v20.0.0-v8-canary20221103f7e2421e91/node-v20.0.0-v8-canary20221103f7e2421e91-linux-x64.tar.gz'
       );
 
@@ -1373,14 +1360,12 @@ describe('setup-node', () => {
 describe('helper methods', () => {
   it('is not LTS alias', async () => {
     const versionSpec = 'v99.0.0-v8-canary';
-    // @ts-ignore
     const isLtsAlias = im.isLtsAlias(versionSpec);
     expect(isLtsAlias).toBeFalsy();
   });
 
   it('is not isLatestSyntax', async () => {
     const versionSpec = 'v99.0.0-v8-canary';
-    // @ts-ignore
     const isLatestSyntax = im.isLatestSyntax(versionSpec);
     expect(isLatestSyntax).toBeFalsy();
   });
@@ -1388,14 +1373,12 @@ describe('helper methods', () => {
   describe('getNodejsDistUrl', () => {
     it('dist url to be https://nodejs.org/download/v8-canary for input versionSpec', () => {
       const versionSpec = 'v99.0.0-v8-canary';
-      // @ts-ignore
       const url = im.getNodejsDistUrl(versionSpec);
       expect(url).toBe('https://nodejs.org/download/v8-canary');
     });
 
     it('dist url to be https://nodejs.org/download/v8-canary for full versionSpec', () => {
       const versionSpec = 'v20.0.0-v8-canary20221103f7e2421e91';
-      // @ts-ignore
       const url = im.getNodejsDistUrl(versionSpec);
       expect(url).toBe('https://nodejs.org/download/v8-canary');
     });
diff --git a/__tests__/installer.unit.test.ts b/__tests__/installer.unit.test.ts
index 26c674d4..11d4b1bb 100644
--- a/__tests__/installer.unit.test.ts
+++ b/__tests__/installer.unit.test.ts
@@ -41,16 +41,6 @@ describe('setup-node unit tests', () => {
         Distributions.CANARY
       );
     });
-    it('1.1.1-canary should throw exception', () => {
-      expect(() => distributionOf('1.1.1-canary')).toThrow(
-        'Canary version must have "-v8-canary suffix"'
-      );
-    });
-    it('1.1.1-canary20221103f7e2421e91 should throw exception', () => {
-      expect(() => distributionOf('1.1.1-canary20221103f7e2421e91')).toThrow(
-        'Canary version must have "-v8-canary suffix"'
-      );
-    });
     it('1.1.1-nightly should be NIGHTLY', () => {
       expect(distributionOf('1.1.1-nightly')).toBe(Distributions.NIGHTLY);
     });
diff --git a/dist/setup/index.js b/dist/setup/index.js
index 848bec33..2d4f882f 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -73218,23 +73218,20 @@ const semver = __importStar(__nccwpck_require__(5911));
 const fs_1 = __importDefault(__nccwpck_require__(7147));
 var Distributions;
 (function (Distributions) {
-    Distributions[Distributions["DEFAULT"] = 0] = "DEFAULT";
-    Distributions[Distributions["CANARY"] = 1] = "CANARY";
-    Distributions[Distributions["NIGHTLY"] = 2] = "NIGHTLY";
-    Distributions[Distributions["RC"] = 3] = "RC";
+    Distributions["DEFAULT"] = "default";
+    Distributions["CANARY"] = "v8-canary";
+    Distributions["NIGHTLY"] = "nightly";
+    Distributions["RC"] = "rc";
 })(Distributions = exports.Distributions || (exports.Distributions = {}));
-exports.distributionOf = (versionSpec) => versionSpec.includes('-v8-canary')
-    ? Distributions.CANARY
-    : // TODO: i'd like to have this check, do you?
-        versionSpec.includes('-canary')
-            ? (() => {
-                throw Error('Canary version must have "-v8-canary suffix"');
-            })()
-            : versionSpec.includes('nightly')
-                ? Distributions.NIGHTLY
-                : semver.prerelease(versionSpec)
-                    ? Distributions.RC
-                    : Distributions.DEFAULT;
+exports.distributionOf = (versionSpec) => {
+    if (versionSpec.includes('-v8-canary'))
+        return Distributions.CANARY;
+    if (versionSpec.includes('nightly'))
+        return Distributions.NIGHTLY;
+    if (semver.prerelease(versionSpec))
+        return Distributions.RC;
+    return Distributions.DEFAULT;
+};
 exports.semverVersionMatcherFactory = (range) => {
     const matcher = (potential) => semver.satisfies(potential, range);
     matcher.factory = exports.semverVersionMatcherFactory;
@@ -73325,17 +73322,20 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os_1.default.arc
             manifest = yield getManifest(auth);
             versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
         }
-        // TODO: 183-189 and 193-194   seems to be the same. Why do we need them?
-        if (isLatestSyntax(versionSpec) || distribution == Distributions.CANARY) {
+        if (isLatestSyntax(versionSpec)) {
             nodeVersions = yield getVersionsFromDist(versionSpec);
             versionSpec = yield queryDistForMatch(versionSpec, arch, nodeVersions);
-            core.info(`getting ${distribution == Distributions.CANARY ? 'v8-canary' : 'latest'} node version ${versionSpec}...`);
+            core.info(`getting latest node version ${versionSpec}...`);
         }
-        if (distribution === Distributions.NIGHTLY && checkLatest) {
+        if ((distribution === Distributions.NIGHTLY ||
+            distribution === Distributions.CANARY) &&
+            checkLatest) {
             nodeVersions = yield getVersionsFromDist(versionSpec);
             versionSpec = yield queryDistForMatch(versionSpec, arch, nodeVersions);
         }
-        if (checkLatest && distribution !== Distributions.NIGHTLY) {
+        if (checkLatest &&
+            distribution !== Distributions.NIGHTLY &&
+            distribution !== Distributions.CANARY) {
             core.info('Attempt to resolve the latest version from manifest...');
             const resolvedVersion = yield resolveVersionFromManifest(versionSpec, stable, auth, osArch, manifest);
             if (resolvedVersion) {
@@ -73347,7 +73347,6 @@ function getNode(versionSpec, stable, checkLatest, auth, arch = os_1.default.arc
             }
         }
         // check cache
-        core.info('Attempt to find existing version in cache...');
         let toolPath;
         if (distribution === Distributions.DEFAULT) {
             toolPath = tc.find('node', versionSpec, osArch);
@@ -73562,13 +73561,10 @@ exports.evaluateVersions = evaluateVersions;
 function getNodejsDistUrl(version) {
     switch (exports.distributionOf(version)) {
         case Distributions.CANARY:
-            core.debug('requested v8 canary distribution');
             return 'https://nodejs.org/download/v8-canary';
         case Distributions.NIGHTLY:
-            core.debug('requested nightly distribution');
             return 'https://nodejs.org/download/nightly';
         case Distributions.RC:
-            core.debug('requested release candidates distribution');
             return 'https://nodejs.org/download/rc';
         case Distributions.DEFAULT:
             return 'https://nodejs.org/dist';
@@ -73602,7 +73598,7 @@ function queryDistForMatch(versionSpec, arch = os_1.default.arch(), nodeVersions
             core.info(`getting latest node version...`);
             return nodeVersions[0].version;
         }
-        let versions = [];
+        const versions = [];
         nodeVersions.forEach((nodeVersion) => {
             // ensure this version supports your os and platform
             if (nodeVersion.files.indexOf(dataFileName) >= 0) {
@@ -73610,7 +73606,8 @@ function queryDistForMatch(versionSpec, arch = os_1.default.arch(), nodeVersions
             }
         });
         // get the latest version that matches the version spec
-        return evaluateVersions(versions, versionSpec);
+        const version = evaluateVersions(versions, versionSpec);
+        return version;
     });
 }
 exports.queryDistForMatch = queryDistForMatch;
diff --git a/src/installer.ts b/src/installer.ts
index e3c4df47..c3dc349f 100644
--- a/src/installer.ts
+++ b/src/installer.ts
@@ -31,25 +31,18 @@ interface INodeRelease extends tc.IToolRelease {
 }
 
 export enum Distributions {
-  DEFAULT,
-  CANARY,
-  NIGHTLY,
-  RC
+  DEFAULT = 'default',
+  CANARY = 'v8-canary',
+  NIGHTLY = 'nightly',
+  RC = 'rc'
 }
 
-export const distributionOf = (versionSpec: string): Distributions =>
-  versionSpec.includes('-v8-canary')
-    ? Distributions.CANARY
-    : // TODO: i'd like to have this check, do you?
-    versionSpec.includes('-canary')
-    ? (() => {
-        throw Error('Canary version must have "-v8-canary suffix"');
-      })()
-    : versionSpec.includes('nightly')
-    ? Distributions.NIGHTLY
-    : semver.prerelease(versionSpec)
-    ? Distributions.RC
-    : Distributions.DEFAULT;
+export const distributionOf = (versionSpec: string): Distributions => {
+  if (versionSpec.includes('-v8-canary')) return Distributions.CANARY;
+  if (versionSpec.includes('nightly')) return Distributions.NIGHTLY;
+  if (semver.prerelease(versionSpec)) return Distributions.RC;
+  return Distributions.DEFAULT;
+};
 
 interface VersionMatcher {
   (potential: string): boolean;
@@ -190,23 +183,26 @@ export async function getNode(
     versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
   }
 
-  // TODO: 183-189 and 193-194   seems to be the same. Why do we need them?
-  if (isLatestSyntax(versionSpec) || distribution == Distributions.CANARY) {
+  if (isLatestSyntax(versionSpec)) {
     nodeVersions = await getVersionsFromDist(versionSpec);
     versionSpec = await queryDistForMatch(versionSpec, arch, nodeVersions);
-    core.info(
-      `getting ${
-        distribution == Distributions.CANARY ? 'v8-canary' : 'latest'
-      } node version ${versionSpec}...`
-    );
+    core.info(`getting latest node version ${versionSpec}...`);
   }
 
-  if (distribution === Distributions.NIGHTLY && checkLatest) {
+  if (
+    (distribution === Distributions.NIGHTLY ||
+      distribution === Distributions.CANARY) &&
+    checkLatest
+  ) {
     nodeVersions = await getVersionsFromDist(versionSpec);
     versionSpec = await queryDistForMatch(versionSpec, arch, nodeVersions);
   }
 
-  if (checkLatest && distribution !== Distributions.NIGHTLY) {
+  if (
+    checkLatest &&
+    distribution !== Distributions.NIGHTLY &&
+    distribution !== Distributions.CANARY
+  ) {
     core.info('Attempt to resolve the latest version from manifest...');
     const resolvedVersion = await resolveVersionFromManifest(
       versionSpec,
@@ -224,7 +220,6 @@ export async function getNode(
   }
 
   // check cache
-  core.info('Attempt to find existing version in cache...');
   let toolPath: string;
   if (distribution === Distributions.DEFAULT) {
     toolPath = tc.find('node', versionSpec, osArch);
@@ -521,13 +516,10 @@ export function evaluateVersions(
 export function getNodejsDistUrl(version: string) {
   switch (distributionOf(version)) {
     case Distributions.CANARY:
-      core.debug('requested v8 canary distribution');
       return 'https://nodejs.org/download/v8-canary';
     case Distributions.NIGHTLY:
-      core.debug('requested nightly distribution');
       return 'https://nodejs.org/download/nightly';
     case Distributions.RC:
-      core.debug('requested release candidates distribution');
       return 'https://nodejs.org/download/rc';
     case Distributions.DEFAULT:
       return 'https://nodejs.org/dist';
@@ -568,7 +560,7 @@ export async function queryDistForMatch(
     return nodeVersions[0].version;
   }
 
-  let versions: string[] = [];
+  const versions: string[] = [];
   nodeVersions.forEach((nodeVersion: INodeVersion) => {
     // ensure this version supports your os and platform
     if (nodeVersion.files.indexOf(dataFileName) >= 0) {
@@ -577,7 +569,8 @@ export async function queryDistForMatch(
   });
 
   // get the latest version that matches the version spec
-  return evaluateVersions(versions, versionSpec);
+  const version = evaluateVersions(versions, versionSpec);
+  return version;
 }
 
 export async function getVersionsFromDist(