From ca842d5a5e52d71c1c7f9e9051efb94fd357905a Mon Sep 17 00:00:00 2001
From: Dmitry Shibanov <shibanov-1997@inbox.ru>
Date: Sun, 23 Oct 2022 14:25:50 +0200
Subject: [PATCH] fix tests and update documentation

---
 README.md                   | 12 +++---
 __tests__/installer.test.ts | 80 -------------------------------------
 dist/setup/index.js         |  6 +--
 docs/advanced-usage.md      | 36 +++++++++++++++++
 src/installer.ts            |  7 ++--
 5 files changed, 50 insertions(+), 91 deletions(-)

diff --git a/README.md b/README.md
index c3d07869..a73c6646 100644
--- a/README.md
+++ b/README.md
@@ -116,11 +116,13 @@ jobs:
 1. [Check latest version](docs/advanced-usage.md#check-latest-version)
 2. [Using a node version file](docs/advanced-usage.md#node-version-file)
 3. [Using different architectures](docs/advanced-usage.md#architecture)
-4. [Caching packages data](docs/advanced-usage.md#caching-packages-data)
-5. [Using multiple operating systems and architectures](docs/advanced-usage.md#multiple-operating-systems-and-architectures)
-6. [Publishing to npmjs and GPR with npm](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm)
-7. [Publishing to npmjs and GPR with yarn](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-yarn)
-8. [Using private packages](docs/advanced-usage.md#use-private-packages)
+4. [Using nigthly versions](docs/advanced-usage.md#nightly-versions)
+5. [Using rc versions](docs/advanced-usage.md#rc-versions)
+6. [Caching packages data](docs/advanced-usage.md#caching-packages-data)
+7. [Using multiple operating systems and architectures](docs/advanced-usage.md#multiple-operating-systems-and-architectures)
+8. [Publishing to npmjs and GPR with npm](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-npm)
+9. [Publishing to npmjs and GPR with yarn](docs/advanced-usage.md#publish-to-npmjs-and-gpr-with-yarn)
+10. [Using private packages](docs/advanced-usage.md#use-private-packages)
 
 ## License
 
diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts
index 7111265d..3c3105e2 100644
--- a/__tests__/installer.test.ts
+++ b/__tests__/installer.test.ts
@@ -391,18 +391,6 @@ describe('setup-node', () => {
   });
 
   it('acquires specified architecture of node', async () => {
-    getJsonSpy.mockImplementation(url => {
-      let res: any;
-      if (url.includes('/rc')) {
-        res = <im.INodeVersion>nodeTestDistRc;
-      } else if (url.includes('/nightly')) {
-        res = <im.INodeVersion>nodeTestDistNightly;
-      } else {
-        res = <im.INodeVersion>nodeTestDist;
-      }
-
-      return {result: res};
-    });
     for (const {arch, version, osSpec} of [
       {arch: 'x86', version: '12.16.2', osSpec: 'win32'},
       {arch: 'x86', version: '14.0.0', osSpec: 'win32'}
@@ -443,20 +431,6 @@ describe('setup-node', () => {
   }, 100000);
 
   describe('check-latest flag', () => {
-    beforeEach(() => {
-      getJsonSpy.mockImplementation(url => {
-        let res: any;
-        if (url.includes('/rc')) {
-          res = <im.INodeVersion>nodeTestDistRc;
-        } else if (url.includes('/nightly')) {
-          res = <im.INodeVersion>nodeTestDistNightly;
-        } else {
-          res = <im.INodeVersion>nodeTestDist;
-        }
-
-        return {result: res};
-      });
-    });
     it('use local version and dont check manifest if check-latest is not specified', async () => {
       os.platform = 'linux';
       os.arch = 'x64';
@@ -956,21 +930,6 @@ describe('setup-node', () => {
   });
 
   describe('rc versions', () => {
-    beforeEach(() => {
-      getJsonSpy.mockImplementation(url => {
-        let res: any;
-        if (url.includes('/rc')) {
-          res = <im.INodeVersion>nodeTestDistRc;
-        } else if (url.includes('/nightly')) {
-          res = <im.INodeVersion>nodeTestDistNightly;
-        } else {
-          res = <im.INodeVersion>nodeTestDist;
-        }
-
-        return {result: res};
-      });
-    });
-
     it.each([
       [
         '13.10.1-rc.0',
@@ -1081,21 +1040,6 @@ describe('setup-node', () => {
   });
 
   describe('nightly versions', () => {
-    beforeEach(() => {
-      getJsonSpy.mockImplementation(url => {
-        let res: any;
-        if (url.includes('/rc')) {
-          res = <im.INodeVersion>nodeTestDistRc;
-        } else if (url.includes('/nightly')) {
-          res = <im.INodeVersion>nodeTestDistNightly;
-        } else {
-          res = <im.INodeVersion>nodeTestDist;
-        }
-
-        return {result: res};
-      });
-    });
-
     it.each([
       [
         '17.5.0-nightly',
@@ -1260,18 +1204,6 @@ describe('setup-node', () => {
     it.each(['latest', 'current', 'node'])(
       'download the %s version if alias is provided',
       async inputVersion => {
-        getJsonSpy.mockImplementation(url => {
-          let res: any;
-          if (url.includes('/rc')) {
-            res = <im.INodeVersion>nodeTestDistRc;
-          } else if (url.includes('/nightly')) {
-            res = <im.INodeVersion>nodeTestDistNightly;
-          } else {
-            res = <im.INodeVersion>nodeTestDist;
-          }
-
-          return {result: res};
-        });
         // Arrange
         inputs['node-version'] = inputVersion;
 
@@ -1298,18 +1230,6 @@ describe('setup-node', () => {
     it.each(['latest', 'current', 'node'])(
       'download the %s version if alias is provided',
       async inputVersion => {
-        getJsonSpy.mockImplementation(url => {
-          let res: any;
-          if (url.includes('/rc')) {
-            res = <im.INodeVersion>nodeTestDistRc;
-          } else if (url.includes('/nightly')) {
-            res = <im.INodeVersion>nodeTestDistNightly;
-          } else {
-            res = <im.INodeVersion>nodeTestDist;
-          }
-
-          return {result: res};
-        });
         // Arrange
         inputs['node-version'] = inputVersion;
         const expectedVersion = nodeTestDist[0];
diff --git a/dist/setup/index.js b/dist/setup/index.js
index 97df553f..3a820b6b 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -73489,10 +73489,10 @@ function getNodejsDistUrl(version) {
     if (version.includes('nightly')) {
         return 'https://nodejs.org/download/nightly';
     }
-    else if (!prerelease) {
-        return 'https://nodejs.org/dist';
+    else if (prerelease) {
+        return 'https://nodejs.org/download/rc';
     }
-    return 'https://nodejs.org/download/rc';
+    return 'https://nodejs.org/dist';
 }
 exports.getNodejsDistUrl = getNodejsDistUrl;
 function queryDistForMatch(versionSpec, arch = os_1.default.arch(), nodeVersions) {
diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md
index 4b620cbd..287b130f 100644
--- a/docs/advanced-usage.md
+++ b/docs/advanced-usage.md
@@ -104,6 +104,42 @@ jobs:
       - run: npm test
 ```
 
+## Nightly versions
+
+You can specify a nightly version to download it from https://nodejs.org/download/nightly.
+
+```yaml
+jobs:
+  build:
+    runs-on: ubuntu-latest
+    name: Node sample
+    steps:
+      - uses: actions/checkout@v3
+      - uses: actions/setup-node@v3
+        with:
+          node-version: '16.0.0-nightly' # or 16-nightly
+      - run: npm ci
+      - run: npm test
+```
+
+## RC versions
+
+You can use specify a rc version to download it from https://nodejs.org/download/rc.
+
+```yaml
+jobs:
+  build:
+    runs-on: ubuntu-latest
+    name: Node sample
+    steps:
+      - uses: actions/checkout@v3
+      - uses: actions/setup-node@v3
+        with:
+          node-version: '16.0.0-rc.1'
+      - run: npm ci
+      - run: npm test
+```
+
 ## Caching packages data
 The action follows [actions/cache](https://github.com/actions/cache/blob/main/examples.md#node---npm) guidelines, and caches global cache on the machine instead of `node_modules`, so cache can be reused between different Node.js versions.
 
diff --git a/src/installer.ts b/src/installer.ts
index 0b00bb1c..dac4f5d9 100644
--- a/src/installer.ts
+++ b/src/installer.ts
@@ -443,10 +443,11 @@ export function getNodejsDistUrl(version: string) {
   const prerelease = semver.prerelease(version);
   if (version.includes('nightly')) {
     return 'https://nodejs.org/download/nightly';
-  } else if (!prerelease) {
-    return 'https://nodejs.org/dist';
+  } else if (prerelease) {
+    return 'https://nodejs.org/download/rc';
   }
-  return 'https://nodejs.org/download/rc';
+
+  return 'https://nodejs.org/dist';
 }
 
 async function queryDistForMatch(