From fdd541042be1bb391a2c056ee54d601d796baf87 Mon Sep 17 00:00:00 2001
From: panticmilos <panticmilos@github.com>
Date: Wed, 27 Apr 2022 17:28:52 +0200
Subject: [PATCH] add unit tests

---
 __tests__/installer.test.ts | 40 +++++++++++++++++++++++++++++++++++++
 src/installer.ts            | 14 +++++++------
 2 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts
index 19692cf2..59abd129 100644
--- a/__tests__/installer.test.ts
+++ b/__tests__/installer.test.ts
@@ -909,4 +909,44 @@ describe('setup-node', () => {
       );
     });
   });
+
+  describe('latest alias syntax', () => {
+    it.each(['latest', 'current', 'node'])('download the %s version if alias is provided', async (inputVersion) => {
+      // Arrange
+      inputs['node-version'] = inputVersion;
+
+      os.platform = 'darwin';
+      os.arch = 'x64';
+
+      const expectedVersion = nodeTestDist[0];
+
+      let expectedUrl = `https://nodejs.org/dist/${expectedVersion.version}/node-${expectedVersion.version}-${os.platform}-${os.arch}.tar.gz`;
+
+      findSpy.mockImplementation(() => '');
+      getManifestSpy.mockImplementation(() => {
+        throw new Error('Unable to download manifest');
+      });
+
+      // Act
+      await main.run();
+
+      // Assert
+      expect(logSpy).toHaveBeenCalledWith(
+        `Attempting to download ${inputVersion}...`
+      );
+
+      expect(logSpy).toHaveBeenCalledWith(
+        'Unable to download manifest'
+      );
+
+      expect(logSpy).toHaveBeenCalledWith(
+        'getting latest node version...'
+      );
+
+      expect(logSpy).toHaveBeenCalledWith(
+        `Acquiring ${expectedVersion.version.substring(1, expectedVersion.version.length)} - ${os.arch} from ${expectedUrl}`
+      );
+
+    });
+  });
 });
diff --git a/src/installer.ts b/src/installer.ts
index 9848e472..73aa3faa 100644
--- a/src/installer.ts
+++ b/src/installer.ts
@@ -7,6 +7,7 @@ import * as tc from '@actions/tool-cache';
 import * as path from 'path';
 import * as semver from 'semver';
 import fs = require('fs');
+import * as installer from './installer';
 
 //
 // Node versions interface
@@ -237,7 +238,7 @@ function resolveLtsAliasFromManifest(
   return release.version.split('.')[0];
 }
 
-async function getInfoFromManifest(
+export async function getInfoFromManifest(
   versionSpec: string,
   stable: boolean,
   auth: string | undefined,
@@ -263,7 +264,7 @@ async function getInfoFromManifest(
   return info;
 }
 
-async function getInfoFromDist(
+export async function getInfoFromDist(
   versionSpec: string,
   arch: string = os.arch()
 ): Promise<INodeVersionInfo | null> {
@@ -320,7 +321,7 @@ async function resolveVersionFromManifest(
 }
 
 // TODO - should we just export this from @actions/tool-cache? Lifted directly from there
-function evaluateVersions(versions: string[], versionSpec: string): string {
+export function evaluateVersions(versions: string[], versionSpec: string): string {
   let version = '';
   core.debug(`evaluating ${versions.length} versions`);
   versions = versions.sort((a, b) => {
@@ -347,7 +348,7 @@ function evaluateVersions(versions: string[], versionSpec: string): string {
   return version;
 }
 
-async function queryDistForMatch(
+export async function queryDistForMatch(
   versionSpec: string,
   arch: string = os.arch()
 ): Promise<string> {
@@ -371,10 +372,11 @@ async function queryDistForMatch(
   }
 
   let versions: string[] = [];
-  let nodeVersions = await getVersionsFromDist();
+  let nodeVersions = await installer.getVersionsFromDist();
 
   if (versionSpec === 'current' || versionSpec === 'latest' || versionSpec === 'node') {
-    return nodeVersions[0].version
+    core.info(`getting latest node version...`);
+    return nodeVersions[0].version;
   }
 
   nodeVersions.forEach((nodeVersion: INodeVersion) => {