|
| 1 | +/** |
| 2 | + * Copyright 2025 actions-toolkit authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; |
| 18 | +import fs from 'fs'; |
| 19 | +import os from 'os'; |
| 20 | +import path from 'path'; |
| 21 | +import * as rimraf from 'rimraf'; |
| 22 | +import osm = require('os'); |
| 23 | + |
| 24 | +import {Install} from '../../src/compose/install'; |
| 25 | + |
| 26 | +const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'compose-install-')); |
| 27 | + |
| 28 | +afterEach(function () { |
| 29 | + rimraf.sync(tmpDir); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('download', () => { |
| 33 | + // prettier-ignore |
| 34 | + test.each([ |
| 35 | + ['v2.31.0', false], |
| 36 | + ['v2.32.4', true], |
| 37 | + ['latest', true] |
| 38 | + ])( |
| 39 | + 'acquires %p of compose (standalone: %p)', async (version, standalone) => { |
| 40 | + const install = new Install({standalone: standalone}); |
| 41 | + const toolPath = await install.download(version); |
| 42 | + expect(fs.existsSync(toolPath)).toBe(true); |
| 43 | + let composeBin: string; |
| 44 | + if (standalone) { |
| 45 | + composeBin = await install.installStandalone(toolPath, tmpDir); |
| 46 | + } else { |
| 47 | + composeBin = await install.installPlugin(toolPath, tmpDir); |
| 48 | + } |
| 49 | + expect(fs.existsSync(composeBin)).toBe(true); |
| 50 | + }, |
| 51 | + 100000 |
| 52 | + ); |
| 53 | + |
| 54 | + // prettier-ignore |
| 55 | + test.each([ |
| 56 | + // following versions are already cached to htc from previous test cases |
| 57 | + ['v2.31.0'], |
| 58 | + ['v2.32.4'], |
| 59 | + ])( |
| 60 | + 'acquires %p of compose with cache', async (version) => { |
| 61 | + const install = new Install({standalone: false}); |
| 62 | + const toolPath = await install.download(version); |
| 63 | + expect(fs.existsSync(toolPath)).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + // prettier-ignore |
| 67 | + test.each([ |
| 68 | + ['v2.27.1'], |
| 69 | + ['v2.28.0'], |
| 70 | + ])( |
| 71 | + 'acquires %p of compose without cache', async (version) => { |
| 72 | + const install = new Install({standalone: false}); |
| 73 | + const toolPath = await install.download(version, true); |
| 74 | + expect(fs.existsSync(toolPath)).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + // TODO: add tests for arm |
| 78 | + // prettier-ignore |
| 79 | + test.each([ |
| 80 | + ['win32', 'x64'], |
| 81 | + ['win32', 'arm64'], |
| 82 | + ['darwin', 'x64'], |
| 83 | + ['darwin', 'arm64'], |
| 84 | + ['linux', 'x64'], |
| 85 | + ['linux', 'arm64'], |
| 86 | + ['linux', 'ppc64'], |
| 87 | + ['linux', 's390x'], |
| 88 | + ])( |
| 89 | + 'acquires compose for %s/%s', async (os, arch) => { |
| 90 | + jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); |
| 91 | + jest.spyOn(osm, 'arch').mockImplementation(() => arch); |
| 92 | + const install = new Install(); |
| 93 | + const composeBin = await install.download('latest'); |
| 94 | + expect(fs.existsSync(composeBin)).toBe(true); |
| 95 | + }, |
| 96 | + 100000 |
| 97 | + ); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('getDownloadVersion', () => { |
| 101 | + it('returns latest download version', async () => { |
| 102 | + const version = await Install.getDownloadVersion('latest'); |
| 103 | + expect(version.version).toEqual('latest'); |
| 104 | + expect(version.downloadURL).toEqual('https://github.com/docker/compose/releases/download/v%s/%s'); |
| 105 | + expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-releases.json'); |
| 106 | + }); |
| 107 | + it('returns v2.24.3 download version', async () => { |
| 108 | + const version = await Install.getDownloadVersion('v2.24.3'); |
| 109 | + expect(version.version).toEqual('v2.24.3'); |
| 110 | + expect(version.downloadURL).toEqual('https://github.com/docker/compose/releases/download/v%s/%s'); |
| 111 | + expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-releases.json'); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('getRelease', () => { |
| 116 | + it('returns latest GitHub release', async () => { |
| 117 | + const version = await Install.getDownloadVersion('latest'); |
| 118 | + const release = await Install.getRelease(version); |
| 119 | + expect(release).not.toBeNull(); |
| 120 | + expect(release?.tag_name).not.toEqual(''); |
| 121 | + }); |
| 122 | + it('returns v2.24.3 GitHub release', async () => { |
| 123 | + const version = await Install.getDownloadVersion('v2.24.3'); |
| 124 | + const release = await Install.getRelease(version); |
| 125 | + expect(release).not.toBeNull(); |
| 126 | + expect(release?.id).toEqual(138380726); |
| 127 | + expect(release?.tag_name).toEqual('v2.24.3'); |
| 128 | + expect(release?.html_url).toEqual('https://github.com/docker/compose/releases/tag/v2.24.3'); |
| 129 | + }); |
| 130 | + it('unknown release', async () => { |
| 131 | + const version = await Install.getDownloadVersion('foo'); |
| 132 | + await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find Compose release foo in https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-releases.json')); |
| 133 | + }); |
| 134 | +}); |
0 commit comments