From 171d848ed08d354633a3fabf2769ce11a8a38ff3 Mon Sep 17 00:00:00 2001 From: Jakub Martin Date: Sun, 17 Mar 2024 15:57:43 +0100 Subject: [PATCH] Add tests. Signed-off-by: Jakub Martin --- dist/index.js | 2 +- lib/test/releases.test.js | 189 +++++++++++++++++++++++++++++++++++++- 2 files changed, 186 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index d81e32f..3a4f113 100644 --- a/dist/index.js +++ b/dist/index.js @@ -61,7 +61,7 @@ async function fetchReleases (githubToken) { const semver = __nccwpck_require__(1383); async function findLatestVersion (versions) { - return versions.sort((a, b) => semver.rcompare(a, b))[0]; + return versions.filter((v) => semver.prerelease(v) === null).sort((a, b) => semver.rcompare(a, b))[0]; } async function findLatestVersionInRange (versions, range) { diff --git a/lib/test/releases.test.js b/lib/test/releases.test.js index eb348c8..420d7ae 100644 --- a/lib/test/releases.test.js +++ b/lib/test/releases.test.js @@ -3,6 +3,184 @@ const pkg = require('../releases'); describe('getRelease', () => { function mockFetchReleases () { const mockReleasesMeta = [{ + tag_name: 'v1.7.0-alpha2', + assets: [{ + name: 'tofu_1.7.0-alpha2_386.apk', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_386.apk' + }, { + name: 'tofu_1.7.0-alpha2_386.deb', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_386.deb' + }, { + name: 'tofu_1.7.0-alpha2_386.rpm', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_386.rpm' + }, { + name: 'tofu_1.7.0-alpha2_amd64.apk', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_amd64.apk' + }, { + name: 'tofu_1.7.0-alpha2_amd64.deb', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_amd64.deb' + }, { + name: 'tofu_1.7.0-alpha2_amd64.rpm', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_amd64.rpm' + }, { + name: 'tofu_1.7.0-alpha2_arm.apk', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_arm.apk' + }, { + name: 'tofu_1.7.0-alpha2_arm.deb', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_arm.deb' + }, { + name: 'tofu_1.7.0-alpha2_arm.rpm', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_arm.rpm' + }, { + name: 'tofu_1.7.0-alpha2_arm64.apk', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_arm64.apk' + }, { + name: 'tofu_1.7.0-alpha2_arm64.deb', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_arm64.deb' + }, { + name: 'tofu_1.7.0-alpha2_arm64.rpm', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_arm64.rpm' + }, { + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_darwin_amd64.zip' + }, { + name: 'tofu_1.7.0-alpha2_darwin_arm64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_darwin_arm64.zip' + }, { + name: 'tofu_1.7.0-alpha2_freebsd_386.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_freebsd_386.zip' + }, { + name: 'tofu_1.7.0-alpha2_freebsd_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_freebsd_amd64.zip' + }, { + name: 'tofu_1.7.0-alpha2_freebsd_arm.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_freebsd_arm.zip' + }, { + name: 'tofu_1.7.0-alpha2_linux_386.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_linux_386.zip' + }, { + name: 'tofu_1.7.0-alpha2_linux_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_linux_amd64.zip' + }, { + name: 'tofu_1.7.0-alpha2_linux_arm.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_linux_arm.zip' + }, { + name: 'tofu_1.7.0-alpha2_linux_arm64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_linux_arm64.zip' + }, { + name: 'tofu_1.7.0-alpha2_openbsd_386.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_openbsd_386.zip' + }, { + name: 'tofu_1.7.0-alpha2_openbsd_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_openbsd_amd64.zip' + }, { + name: 'tofu_1.7.0-alpha2_SHA256SUMS', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_SHA256SUMS' + }, { + name: 'tofu_1.7.0-alpha2_SHA256SUMS.pem', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_SHA256SUMS.pem' + }, { + name: 'tofu_1.7.0-alpha2_SHA256SUMS.sig', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_SHA256SUMS.sig' + }, { + name: 'tofu_1.7.0-alpha2_solaris_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_solaris_amd64.zip' + }, { + name: 'tofu_1.7.0-alpha2_windows_386.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_windows_386.zip' + }, { + name: 'tofu_1.7.0-alpha2_windows_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.7.0-alpha2/tofu_1.7.0-alpha2_windows_amd64.zip' + }] + }, { + tag_name: 'v1.6.0', + assets: [{ + name: 'tofu_1.6.0_386.apk', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_386.apk' + }, { + name: 'tofu_1.6.0_386.deb', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_386.deb' + }, { + name: 'tofu_1.6.0_386.rpm', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_386.rpm' + }, { + name: 'tofu_1.6.0_amd64.apk', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_amd64.apk' + }, { + name: 'tofu_1.6.0_amd64.deb', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_amd64.deb' + }, { + name: 'tofu_1.6.0_amd64.rpm', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_amd64.rpm' + }, { + name: 'tofu_1.6.0_arm.apk', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_arm.apk' + }, { + name: 'tofu_1.6.0_arm.deb', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_arm.deb' + }, { + name: 'tofu_1.6.0_arm.rpm', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_arm.rpm' + }, { + name: 'tofu_1.6.0_arm64.apk', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_arm64.apk' + }, { + name: 'tofu_1.6.0_arm64.deb', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_arm64.deb' + }, { + name: 'tofu_1.6.0_arm64.rpm', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_arm64.rpm' + }, { + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_darwin_amd64.zip' + }, { + name: 'tofu_1.6.0_darwin_arm64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_darwin_arm64.zip' + }, { + name: 'tofu_1.6.0_freebsd_386.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_freebsd_386.zip' + }, { + name: 'tofu_1.6.0_freebsd_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_freebsd_amd64.zip' + }, { + name: 'tofu_1.6.0_freebsd_arm.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_freebsd_arm.zip' + }, { + name: 'tofu_1.6.0_linux_386.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_linux_386.zip' + }, { + name: 'tofu_1.6.0_linux_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_linux_amd64.zip' + }, { + name: 'tofu_1.6.0_linux_arm.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_linux_arm.zip' + }, { + name: 'tofu_1.6.0_linux_arm64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_linux_arm64.zip' + }, { + name: 'tofu_1.6.0_openbsd_386.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_openbsd_386.zip' + }, { + name: 'tofu_1.6.0_openbsd_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_openbsd_amd64.zip' + }, { + name: 'tofu_1.6.0_SHA256SUMS', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_SHA256SUMS' + }, { + name: 'tofu_1.6.0_SHA256SUMS.pem', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_SHA256SUMS.pem' + }, { + name: 'tofu_1.6.0_SHA256SUMS.sig', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_SHA256SUMS.sig' + }, { + name: 'tofu_1.6.0_solaris_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_solaris_amd64.zip' + }, { + name: 'tofu_1.6.0_windows_386.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_windows_386.zip' + }, { + name: 'tofu_1.6.0_windows_amd64.zip', + browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_windows_amd64.zip' + }] + }, { tag_name: 'v1.6.0-alpha2', assets: [{ name: 'tofu_1.6.0-alpha2_386.apk', @@ -188,12 +366,15 @@ describe('getRelease', () => { it.each( [ - ['latest', '1.6.0-alpha2'], + ['latest', '1.6.0'], ['<1.6.0-beta', '1.6.0-alpha2'], - ['>1.6.0-alpha1', '1.6.0-alpha2'], + ['>1.6.0-alpha1', '1.6.0'], ['>1.6.0-alpha1 <1.6.0', '1.6.0-alpha2'], - ['~1.6.0-alpha', '1.6.0-alpha2'], - ['<=1.6.0-alpha1', '1.6.0-alpha1'] + ['~1.6.0-alpha', '1.6.0'], + ['<=1.6.0-alpha1', '1.6.0-alpha1'], + ['>1.5.0', '1.6.0'], + ['>1.5.0', '1.6.0'], + ['>1.7.0-alpha', '1.7.0-alpha2'] ] )('happy path: getRelease(\'%s\') -> \'%s\'', async (input, wantVersion) => { const want = mockFetchReleases().find(el => el.version === wantVersion);