diff --git a/__tests__/get-arch.test.ts b/__tests__/get-arch.test.ts index f9c44bd..08b6d3f 100644 --- a/__tests__/get-arch.test.ts +++ b/__tests__/get-arch.test.ts @@ -1,15 +1,135 @@ import getArch from '../src/get-arch'; describe('getArch', () => { - test('processor architecture', () => { - expect(getArch('x64')).toBe('64bit'); - expect(getArch('arm')).toBe('ARM'); - expect(getArch('arm64')).toBe('ARM64'); - }); + const groups = [ + { + condition: 'when hugo version < 0.102.0', + conventions: { + arch: { + darwinUniversal: false, + droppedWindowsArmSupport: false, + standardizedNaming: false + }, + os: { + renamedMacOS: false, + downcasedAll: false + } + }, + tests: [ + {arch: 'x64', os: 'linux', expected: '64bit'}, + {arch: 'x64', os: 'darwin', expected: '64bit'}, + {arch: 'x64', os: 'macOS', expected: '64bit'}, + {arch: 'x64', os: 'windows', expected: '64bit'}, + {arch: 'arm', os: 'linux', expected: 'ARM'}, + {arch: 'arm', os: 'darwin', expected: 'ARM'}, + {arch: 'arm', os: 'macOS', expected: 'ARM'}, + {arch: 'arm', os: 'windows', expected: 'ARM'}, + {arch: 'arm64', os: 'linux', expected: 'ARM64'}, + {arch: 'arm64', os: 'darwin', expected: 'ARM64'}, + {arch: 'arm64', os: 'macOS', expected: 'ARM64'}, + {arch: 'arm64', os: 'windows', expected: 'ARM64'} + ] + }, + { + condition: 'when hugo version === 0.102.z', + conventions: { + arch: { + darwinUniversal: true, + droppedWindowsArmSupport: true, + standardizedNaming: false + }, + os: { + renamedMacOS: false, + downcasedAll: false + } + }, + tests: [ + {arch: 'x64', os: 'linux', expected: '64bit'}, + {arch: 'x64', os: 'macOS', expected: 'universal'}, + {arch: 'x64', os: 'windows', expected: '64bit'}, + {arch: 'arm', os: 'linux', expected: 'ARM'}, + {arch: 'arm', os: 'macOS', expected: 'universal'}, + {arch: 'arm', os: 'windows', throws: true}, + {arch: 'arm64', os: 'linux', expected: 'ARM64'}, + {arch: 'arm64', os: 'macOS', expected: 'universal'}, + {arch: 'arm64', os: 'windows', expected: 'ARM64'} + ] + }, + { + condition: 'when hugo version >= 0.103.0', + conventions: { + arch: { + darwinUniversal: true, + droppedWindowsArmSupport: true, + standardizedNaming: true + }, + os: { + renamedMacOS: true, + downcasedAll: true + } + }, + tests: [ + {arch: 'x64', os: 'linux', expected: 'amd64'}, + {arch: 'x64', os: 'macOS', expected: 'universal'}, + {arch: 'x64', os: 'windows', expected: 'amd64'}, + {arch: 'arm', os: 'linux', expected: 'arm'}, + {arch: 'arm', os: 'macOS', expected: 'universal'}, + {arch: 'arm', os: 'windows', throws: true}, + {arch: 'arm64', os: 'linux', expected: 'arm64'}, + {arch: 'arm64', os: 'macOS', expected: 'universal'}, + {arch: 'arm64', os: 'windows', expected: 'arm64'} + ] + }, + { + condition: 'when the architecture is unsupported for the action', + conventions: { + arch: { + darwinUniversal: false, + droppedWindowsArmSupport: false, + standardizedNaming: false + }, + os: { + renamedMacOS: false, + downcasedAll: false + } + }, + tests: [{arch: 'mips', os: 'linux', throws: true}] + } + ]; - test('exception', () => { - expect(() => { - getArch('mips'); - }).toThrow('mips is not supported'); - }); + const passingTests = groups.flatMap(group => + group.tests + .filter(example => !example.throws) + .map(example => ({ + ...example, + condition: group.condition, + conventions: group.conventions + })) + ); + + const throwingTests = groups.flatMap(group => + group.tests + .filter(example => example.throws) + .map(example => ({ + ...example, + condition: group.condition, + conventions: group.conventions + })) + ); + + test.each(passingTests)( + '$condition: $os on $arch returns $expected', + ({arch, os, conventions, expected}) => { + expect(getArch(arch, os, conventions)).toBe(expected); + } + ); + + test.each(throwingTests)( + '$condition: $os on $arch throws as not supported', + ({arch, os, conventions}) => { + expect(() => { + getArch(arch, os, conventions); + }).toThrow(`${arch} is not supported`); + } + ); }); diff --git a/__tests__/get-conventions.test.ts b/__tests__/get-conventions.test.ts new file mode 100644 index 0000000..7596060 --- /dev/null +++ b/__tests__/get-conventions.test.ts @@ -0,0 +1,61 @@ +import {getConventions} from '../src/get-conventions'; + +describe('getConventions()', () => { + const groups = [ + { + condition: 'when hugo version < 0.102.0', + version: '0.101.0', + expected: { + arch: { + darwinUniversal: false, + droppedWindowsArmSupport: false, + standardizedNaming: false + }, + os: { + renamedMacOS: false, + downcasedAll: false + } + } + }, + { + condition: 'when hugo version === 0.102.z', + version: '0.102.0', + expected: { + arch: { + darwinUniversal: true, + droppedWindowsArmSupport: true, + standardizedNaming: false + }, + os: { + renamedMacOS: false, + downcasedAll: false + } + } + }, + { + condition: 'when hugo version >= 0.103.0', + version: '0.103.0', + expected: { + arch: { + darwinUniversal: true, + droppedWindowsArmSupport: true, + standardizedNaming: true + }, + os: { + renamedMacOS: true, + downcasedAll: true + } + } + } + ].map(function (group) { + return Object.assign(group, { + toString: function () { + return group.condition; + } + }); + }); + + test.each(groups)('%s', ({expected, version}) => { + expect(getConventions(version)).toEqual(expected); + }); +}); diff --git a/__tests__/get-latest-version.test.ts b/__tests__/get-latest-version.test.ts index 81bed2c..52bde27 100644 --- a/__tests__/get-latest-version.test.ts +++ b/__tests__/get-latest-version.test.ts @@ -1,6 +1,6 @@ import {getURL, getLatestVersion} from '../src/get-latest-version'; -import nock from 'nock'; import {FetchError} from 'node-fetch'; +import {clearMockFetchResponses, mockFetchResponse} from './mocks/node-fetch'; import {Tool} from '../src/constants'; import jsonTestBrew from './data/brew.json'; import jsonTestGithub from './data/github.json'; @@ -10,7 +10,7 @@ beforeEach(() => { }); afterEach(() => { - nock.cleanAll(); + clearMockFetchResponses(); }); describe('getURL()', () => { @@ -27,23 +27,25 @@ describe('getURL()', () => { describe('getLatestVersion()', () => { test('return latest version via brew', async () => { - nock('https://formulae.brew.sh').get(`/api/formula/${Tool.Repo}.json`).reply(200, jsonTestBrew); + mockFetchResponse(`https://formulae.brew.sh/api/formula/${Tool.Repo}.json`, 200, jsonTestBrew); const versionLatest: string = await getLatestVersion(Tool.Org, Tool.Repo, 'brew'); expect(versionLatest).toMatch(Tool.TestVersionLatest); }); test('return latest version via GitHub', async () => { - nock('https://api.github.com') - .get(`/repos/${Tool.Org}/${Tool.Repo}/releases/latest`) - .reply(200, jsonTestGithub); + mockFetchResponse( + `https://api.github.com/repos/${Tool.Org}/${Tool.Repo}/releases/latest`, + 200, + jsonTestGithub + ); const versionLatest: string = await getLatestVersion(Tool.Org, Tool.Repo, 'github'); expect(versionLatest).toMatch(Tool.TestVersionLatest); }); test('return exception 404', async () => { - nock('https://formulae.brew.sh').get(`/api/formula/${Tool.Repo}.json`).reply(404); + mockFetchResponse(`https://formulae.brew.sh/api/formula/${Tool.Repo}.json`, 404); await expect(getLatestVersion(Tool.Org, Tool.Repo, 'brew')).rejects.toThrow(FetchError); }); diff --git a/__tests__/get-os.test.ts b/__tests__/get-os.test.ts index 1f60b24..d2c02e6 100644 --- a/__tests__/get-os.test.ts +++ b/__tests__/get-os.test.ts @@ -1,15 +1,100 @@ import getOS from '../src/get-os'; describe('getOS', () => { - test('os type', () => { - expect(getOS('linux')).toBe('Linux'); - expect(getOS('darwin')).toBe('macOS'); - expect(getOS('win32')).toBe('Windows'); + const groups = [ + { + condition: 'when hugo version < 0.102.0', + conventions: { + arch: { + darwinUniversal: false, + droppedWindowsArmSupport: false, + standardizedNaming: false + }, + os: { + renamedMacOS: false, + downcasedAll: false + } + }, + tests: [ + {os: 'linux', expected: 'Linux'}, + {os: 'darwin', expected: 'macOS'}, + {os: 'win32', expected: 'Windows'} + ] + }, + { + condition: 'when hugo version === 0.102.z', + conventions: { + arch: { + darwinUniversal: true, + droppedWindowsArmSupport: true, + standardizedNaming: false + }, + os: { + renamedMacOS: false, + downcasedAll: false + } + }, + tests: [ + {os: 'linux', expected: 'Linux'}, + {os: 'darwin', expected: 'macOS'}, + {os: 'win32', expected: 'Windows'} + ] + }, + { + condition: 'when hugo version >= 0.103.0', + conventions: { + arch: { + darwinUniversal: true, + droppedWindowsArmSupport: true, + standardizedNaming: true + }, + os: { + renamedMacOS: true, + downcasedAll: true + } + }, + tests: [ + {os: 'linux', expected: 'linux'}, + {os: 'darwin', expected: 'darwin'}, + {os: 'win32', expected: 'windows'} + ] + } + ].map(function (group) { + group.tests = group.tests.map(function (example) { + return Object.assign(example, { + toString: function () { + return `${example.os} returns ${example.expected}`; + } + }); + }); + return Object.assign(group, { + toString: function () { + return group.condition; + } + }); + }); + + describe.each(groups)('%s', ({conventions, tests}) => { + test.each(tests)('%s', ({os, expected}) => { + expect(getOS(os, conventions)).toBe(expected); + }); }); test('exception', () => { + const conventions = { + arch: { + darwinUniversal: false, + droppedWindowsArmSupport: false, + standardizedNaming: false + }, + os: { + renamedMacOS: false, + downcasedAll: false + } + }; + expect(() => { - getOS('centos'); + getOS('centos', conventions); }).toThrow('centos is not supported'); }); }); diff --git a/__tests__/get-url.test.ts b/__tests__/get-url.test.ts index 7ec9fb2..a3cb6f3 100644 --- a/__tests__/get-url.test.ts +++ b/__tests__/get-url.test.ts @@ -49,6 +49,32 @@ describe('getURL()', () => { ]); }); + test('get URLs to macOS 0.102 universal assets', () => { + const baseURL = 'https://github.com/gohugoio/hugo/releases/download/v0.102.0'; + expect(getURL('macOS', 'universal', 'false', '0.102.0')).toEqual([ + `${baseURL}/hugo_0.102.0_macOS-universal.tar.gz`, + `${baseURL}/hugo_0.102.0_macOS-universal.zip`, + `${baseURL}/hugo_0.102.0_macOS-all.tar.gz`, + `${baseURL}/hugo_0.102.0_darwin-universal.tar.gz`, + `${baseURL}/hugo_0.102.0_darwin-universal.pkg`, + `${baseURL}/hugo_v0.102.0_macOS-universal.tar.gz`, + `${baseURL}/hugo_v0.102.0_macOS-universal.zip`, + `${baseURL}/hugo_v0.102.0_macOS-all.tar.gz`, + `${baseURL}/hugo_v0.102.0_darwin-universal.tar.gz`, + `${baseURL}/hugo_v0.102.0_darwin-universal.pkg` + ]); + }); + + test('get URLs to darwin assets', () => { + const baseURL = 'https://github.com/gohugoio/hugo/releases/download/v0.103.0'; + expect(getURL('darwin', 'universal', 'false', '0.103.0')).toEqual([ + `${baseURL}/hugo_0.103.0_darwin-universal.tar.gz`, + `${baseURL}/hugo_0.103.0_darwin-universal.pkg`, + `${baseURL}/hugo_v0.103.0_darwin-universal.tar.gz`, + `${baseURL}/hugo_v0.103.0_darwin-universal.pkg` + ]); + }); + test('get URLs to legacy macOS assets', () => { const baseURL = 'https://github.com/gohugoio/hugo/releases/download/v0.20.2'; expect(getURL('macOS', '64bit', 'false', '0.20.2')).toEqual([ @@ -81,6 +107,16 @@ describe('getURL()', () => { ]); }); + test('get URLs to downcased Windows assets', () => { + const baseURL = 'https://github.com/gohugoio/hugo/releases/download/v0.103.0'; + expect(getURL('windows', 'amd64', 'false', '0.103.0')).toEqual([ + `${baseURL}/hugo_0.103.0_windows-amd64.zip`, + `${baseURL}/hugo_0.103.0_Windows-amd64.zip`, + `${baseURL}/hugo_v0.103.0_windows-amd64.zip`, + `${baseURL}/hugo_v0.103.0_Windows-amd64.zip` + ]); + }); + test('get URLs to legacy Windows assets', () => { const baseURL = 'https://github.com/gohugoio/hugo/releases/download/v0.20.3'; expect(getURL('Windows', '64bit', 'false', '0.20.3')).toEqual([ @@ -91,6 +127,18 @@ describe('getURL()', () => { ]); }); + test('get URLs to downcased Linux assets', () => { + const baseURL = 'https://github.com/gohugoio/hugo/releases/download/v0.103.0'; + expect(getURL('linux', 'arm', 'false', '0.103.0')).toEqual([ + `${baseURL}/hugo_0.103.0_linux-arm.tar.gz`, + `${baseURL}/hugo_0.103.0_Linux-arm.tar.gz`, + `${baseURL}/hugo_0.103.0_Linux_arm.tar.gz`, + `${baseURL}/hugo_v0.103.0_linux-arm.tar.gz`, + `${baseURL}/hugo_v0.103.0_Linux-arm.tar.gz`, + `${baseURL}/hugo_v0.103.0_Linux_arm.tar.gz` + ]); + }); + test('get a fallback URL to an unknown OS asset', () => { const baseURL = 'https://github.com/gohugoio/hugo/releases/download/v0.58.2'; expect(getURL('MyOS', '64bit', 'false', '0.58.2')).toEqual([ diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 1269989..b025538 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -1,9 +1,9 @@ import * as main from '../src/main'; import * as io from '@actions/io'; import path from 'path'; -import nock from 'nock'; import {Tool, Action} from '../src/constants'; import {FetchError} from 'node-fetch'; +import {clearMockFetchResponses, mockFetchResponse} from './mocks/node-fetch'; import jsonTestBrew from './data/brew.json'; // import jsonTestGithub from './data/github.json'; @@ -20,7 +20,7 @@ describe('Integration testing run()', () => { delete process.env['INPUT_HUGO-VERSION']; delete process.env['INPUT_EXTENDED']; - nock.cleanAll(); + clearMockFetchResponses(); }); test('succeed in installing a custom version', async () => { @@ -44,7 +44,7 @@ describe('Integration testing run()', () => { test('succeed in installing the latest version', async () => { const testVersion = 'latest'; process.env['INPUT_HUGO-VERSION'] = testVersion; - nock('https://formulae.brew.sh').get(`/api/formula/${Tool.Repo}.json`).reply(200, jsonTestBrew); + mockFetchResponse(`https://formulae.brew.sh/api/formula/${Tool.Repo}.json`, 200, jsonTestBrew); const result: main.ActionResult = await main.run(); expect(result.exitcode).toBe(0); expect(result.output).toMatch(`hugo v${Tool.TestVersionLatest}`); @@ -54,7 +54,7 @@ describe('Integration testing run()', () => { const testVersion = 'latest'; process.env['INPUT_HUGO-VERSION'] = testVersion; process.env['INPUT_EXTENDED'] = 'true'; - nock('https://formulae.brew.sh').get(`/api/formula/${Tool.Repo}.json`).reply(200, jsonTestBrew); + mockFetchResponse(`https://formulae.brew.sh/api/formula/${Tool.Repo}.json`, 200, jsonTestBrew); const result: main.ActionResult = await main.run(); expect(result.exitcode).toBe(0); expect(result.output).toMatch(`hugo v${Tool.TestVersionLatest}`); @@ -63,7 +63,7 @@ describe('Integration testing run()', () => { test('fail to install the latest version due to 404 of brew', async () => { process.env['INPUT_HUGO-VERSION'] = 'latest'; - nock('https://formulae.brew.sh').get(`/api/formula/${Tool.Repo}.json`).reply(404); + mockFetchResponse(`https://formulae.brew.sh/api/formula/${Tool.Repo}.json`, 404); await expect(main.run()).rejects.toThrow(FetchError); }); diff --git a/__tests__/mocks/node-fetch.ts b/__tests__/mocks/node-fetch.ts index 9763dae..6e07773 100644 --- a/__tests__/mocks/node-fetch.ts +++ b/__tests__/mocks/node-fetch.ts @@ -22,8 +22,35 @@ interface ResponseLike { json: () => Promise; } +interface MockResponse { + body?: unknown; + status: number; +} + +const mockResponses = new Map(); + +export function mockFetchResponse(url: string, status: number, body?: unknown): void { + mockResponses.set(url, { + body, + status + }); +} + +export function clearMockFetchResponses(): void { + mockResponses.clear(); +} + export default async function fetch(url: string | URL): Promise { const target = url.toString(); + const mockResponse = mockResponses.get(target); + if (mockResponse) { + return { + ok: mockResponse.status >= 200 && mockResponse.status < 300, + status: mockResponse.status, + json: async () => mockResponse.body as unknown + }; + } + const client = target.startsWith('https:') ? https : http; return new Promise((resolve, reject) => { diff --git a/src/get-arch.ts b/src/get-arch.ts index 04e56af..fb0304e 100644 --- a/src/get-arch.ts +++ b/src/get-arch.ts @@ -1,11 +1,21 @@ -export default function getArch(arch: string): string { +import {conventions} from './get-conventions'; + +export default function getArch(arch: string, os: string, conventions: conventions): string { + if (conventions.arch.darwinUniversal && (os === 'darwin' || os === 'macOS')) { + return 'universal'; + } + switch (arch) { case 'x64': - return '64bit'; + return conventions.arch.standardizedNaming ? 'amd64' : '64bit'; case 'arm': - return 'ARM'; + if (conventions.arch.droppedWindowsArmSupport && (os === 'Windows' || os === 'windows')) { + throw new Error(`${arch} is not supported`); + } + + return conventions.arch.standardizedNaming ? 'arm' : 'ARM'; case 'arm64': - return 'ARM64'; + return conventions.arch.standardizedNaming ? 'arm64' : 'ARM64'; default: throw new Error(`${arch} is not supported`); } diff --git a/src/get-conventions.ts b/src/get-conventions.ts new file mode 100644 index 0000000..246b8e6 --- /dev/null +++ b/src/get-conventions.ts @@ -0,0 +1,29 @@ +export interface conventions { + arch: { + darwinUniversal: boolean; + droppedWindowsArmSupport: boolean; + standardizedNaming: boolean; + }; + os: { + renamedMacOS: boolean; + downcasedAll: boolean; + }; +} + +export function getConventions(version: string): conventions { + const segments = version.split('.').map(s => parseInt(s)); + const stableOrNewer = segments[0] > 0; + const newerThan103 = stableOrNewer || segments[1] >= 103; + const newerThan102 = stableOrNewer || segments[1] >= 102; + return { + arch: { + darwinUniversal: newerThan102, + droppedWindowsArmSupport: newerThan102, + standardizedNaming: newerThan103 + }, + os: { + renamedMacOS: newerThan103, + downcasedAll: newerThan103 + } + }; +} diff --git a/src/get-os.ts b/src/get-os.ts index 8360089..b3af9a6 100644 --- a/src/get-os.ts +++ b/src/get-os.ts @@ -1,11 +1,13 @@ -export default function getOS(platform: string): string { +import {conventions} from './get-conventions'; + +export default function getOS(platform: string, conventions: conventions): string { switch (platform) { case 'linux': - return 'Linux'; + return conventions.os.downcasedAll ? 'linux' : 'Linux'; case 'darwin': - return 'macOS'; + return conventions.os.renamedMacOS ? 'darwin' : 'macOS'; case 'win32': - return 'Windows'; + return conventions.os.downcasedAll ? 'windows' : 'Windows'; default: throw new Error(`${platform} is not supported`); } diff --git a/src/get-url.ts b/src/get-url.ts index 93f7493..c9863e9 100644 --- a/src/get-url.ts +++ b/src/get-url.ts @@ -49,22 +49,34 @@ export default function getURL( ); } - if (os === 'Windows') { + if (os === 'darwin') { return assetURLs( assetBases.flatMap(assetBase => [ - `${assetBase}Windows-${arch}.zip`, - `${assetBase}windows-${lowerArch(arch)}.zip` + `${assetBase}darwin-${arch}.tar.gz`, + `${assetBase}darwin-${arch}.pkg` ]) ); } - if (os === 'Linux') { + if (os === 'Windows' || os === 'windows') { + const assetPatterns = + os === 'windows' + ? [`windows-${lowerArch(arch)}.zip`, `Windows-${arch}.zip`] + : [`Windows-${arch}.zip`, `windows-${lowerArch(arch)}.zip`]; + return assetURLs( - assetBases.flatMap(assetBase => [ - `${assetBase}Linux-${arch}.tar.gz`, - `${assetBase}Linux_${arch}.tar.gz`, - `${assetBase}linux-${lowerArch(arch)}.tar.gz` - ]) + assetBases.flatMap(assetBase => assetPatterns.map(asset => `${assetBase}${asset}`)) + ); + } + + if (os === 'Linux' || os === 'linux') { + const assetPatterns = + os === 'linux' + ? [`linux-${lowerArch(arch)}.tar.gz`, `Linux-${arch}.tar.gz`, `Linux_${arch}.tar.gz`] + : [`Linux-${arch}.tar.gz`, `Linux_${arch}.tar.gz`, `linux-${lowerArch(arch)}.tar.gz`]; + + return assetURLs( + assetBases.flatMap(assetBase => assetPatterns.map(asset => `${assetBase}${asset}`)) ); } diff --git a/src/installer.ts b/src/installer.ts index f8410e4..1fd2c59 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -2,6 +2,7 @@ import * as core from '@actions/core'; import * as tc from '@actions/tool-cache'; import * as io from '@actions/io'; import * as exec from '@actions/exec'; +import {getConventions} from './get-conventions'; import getOS from './get-os'; import getArch from './get-arch'; import getURL from './get-url'; @@ -113,10 +114,12 @@ export async function installer(version: string): Promise { const extended: string = core.getInput('extended'); core.debug(`Hugo extended: ${extended}`); - const osName: string = getOS(process.platform); + const conventions = getConventions(version); + + const osName: string = getOS(process.platform, conventions); core.debug(`Operating System: ${osName}`); - const archName: string = getArch(process.arch); + const archName: string = getArch(process.arch, osName, conventions); core.debug(`Processor Architecture: ${archName}`); const toolURLs: string[] = getURL(osName, archName, extended, version);