From 17f8bd926464a1afa4c6a11669539e9c1ba77048 Mon Sep 17 00:00:00 2001 From: Milos Pantic <101411245+panticmilos@users.noreply.github.com> Date: Thu, 12 May 2022 17:26:02 +0200 Subject: [PATCH] Expand current syntax to support aliases for latest version (current/latest/node) (#483) --- .github/workflows/versions.yml | 31 +++++++++++++++++++++++++++++++ README.md | 3 +++ __tests__/installer.test.ts | 26 ++++++++++++++++++++++++++ dist/setup/index.js | 6 ++++++ src/installer.ts | 9 +++++++++ 5 files changed, 75 insertions(+) diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index d13fe4024..3d8067bc9 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -139,3 +139,34 @@ jobs: - name: Verify node run: __tests__/verify-arch.sh "ia32" shell: bash + + node-latest-aliases: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + node-version: [current, latest, node] + steps: + - name: Get node version + run: | + latestNodeVersion=$(curl https://nodejs.org/dist/index.json | jq -r '. [0].version') + echo "::set-output name=LATEST_NODE_VERSION::$latestNodeVersion" + id: version + shell: bash + - uses: actions/checkout@v3 + - name: Setup Node + uses: ./ + with: + node-version: ${{ matrix.node-version }} + - name: Retrieve version after install + run: | + updatedVersion=$(echo $(node --version)) + echo "::set-output name=NODE_VERSION_UPDATED::$updatedVersion" + id: updatedVersion + shell: bash + - name: Compare versions + if: ${{ steps.version.outputs.LATEST_NODE_VERSION != steps.updatedVersion.outputs.NODE_VERSION_UPDATED}} + run: | + echo "Latest node version failed to download." + exit 1 diff --git a/README.md b/README.md index 8e8be6ef6..20e2418d3 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ The `node-version` input supports the following syntax: major versions: `12`, `14`, `16` more specific versions: `10.15`, `14.2.0`, `16.3.0` nvm lts syntax: `lts/erbium`, `lts/fermium`, `lts/*` +latest release: `latest`/`current`/`node` + +**Note:** Since the latest release will not be cached always, there is possibility of hitting rate limit when downloading from dist ### Checking in lockfiles diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 19692cf27..bf2b7bf27 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -909,4 +909,30 @@ 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'; + + findSpy.mockImplementation(() => ''); + getManifestSpy.mockImplementation(() => { + throw new Error('Unable to download manifest'); + }); + + // Act + await main.run(); + + // assert + expect(logSpy).toHaveBeenCalledWith('Unable to download manifest'); + + expect(logSpy).toHaveBeenCalledWith('getting latest node version...'); + } + ); + }); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index d05c5006f..ecf999142 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -62587,6 +62587,12 @@ function queryDistForMatch(versionSpec, arch = os.arch()) { } let versions = []; let nodeVersions = yield getVersionsFromDist(); + if (versionSpec === 'current' || + versionSpec === 'latest' || + versionSpec === 'node') { + core.info(`getting latest node version...`); + return nodeVersions[0].version; + } nodeVersions.forEach((nodeVersion) => { // ensure this version supports your os and platform if (nodeVersion.files.indexOf(dataFileName) >= 0) { diff --git a/src/installer.ts b/src/installer.ts index a9baae0a1..5b87d29a0 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -373,6 +373,15 @@ async function queryDistForMatch( let versions: string[] = []; let nodeVersions = await getVersionsFromDist(); + if ( + versionSpec === 'current' || + versionSpec === 'latest' || + versionSpec === 'node' + ) { + core.info(`getting latest node version...`); + return nodeVersions[0].version; + } + nodeVersions.forEach((nodeVersion: INodeVersion) => { // ensure this version supports your os and platform if (nodeVersion.files.indexOf(dataFileName) >= 0) {