|
| 1 | +import * as tc from '../src/tool-cache' |
| 2 | +import * as mm from '../src/manifest' // --> OFF |
| 3 | + |
| 4 | +// needs to be require for core node modules to be mocked |
| 5 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 6 | +import osm = require('os') |
| 7 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 8 | +import cp = require('child_process') |
| 9 | +//import {coerce} from 'semver' |
| 10 | + |
| 11 | +// we fetch the manifest file from master of a repo |
| 12 | +const owner = 'actions' |
| 13 | +const repo = 'some-tool' |
| 14 | +const fakeToken = 'notrealtoken' |
| 15 | + |
| 16 | +// just loading data and require handles BOMs etc. |
| 17 | +// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires |
| 18 | +const manifestData = require('./data/versions-manifest.json') |
| 19 | + |
| 20 | +describe('@actions/tool-cache-manifest', () => { |
| 21 | + let os: {platform: string; arch: string} |
| 22 | + |
| 23 | + let getSpy: jest.SpyInstance |
| 24 | + let platSpy: jest.SpyInstance |
| 25 | + let archSpy: jest.SpyInstance |
| 26 | + let execSpy: jest.SpyInstance |
| 27 | + let readLsbSpy: jest.SpyInstance |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + // node |
| 31 | + os = {platform: '', arch: ''} |
| 32 | + platSpy = jest.spyOn(osm, 'platform') |
| 33 | + |
| 34 | + platSpy.mockImplementation(() => os.platform) |
| 35 | + archSpy = jest.spyOn(osm, 'arch') |
| 36 | + archSpy.mockImplementation(() => os.arch) |
| 37 | + |
| 38 | + execSpy = jest.spyOn(cp, 'execSync') |
| 39 | + readLsbSpy = jest.spyOn(mm, '_readLinuxVersionFile') |
| 40 | + |
| 41 | + getSpy = jest.spyOn(tc, 'getManifestFromRepo') |
| 42 | + getSpy.mockImplementation(() => <mm.IToolRelease[]>manifestData) |
| 43 | + }) |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + jest.resetAllMocks() |
| 47 | + jest.clearAllMocks() |
| 48 | + //jest.restoreAllMocks(); |
| 49 | + }) |
| 50 | + |
| 51 | + afterAll(async () => {}, 100000) |
| 52 | + |
| 53 | + it('can query versions', async () => { |
| 54 | + const manifest: mm.IToolRelease[] | null = await tc.getManifestFromRepo( |
| 55 | + owner, |
| 56 | + repo, |
| 57 | + fakeToken |
| 58 | + ) |
| 59 | + |
| 60 | + expect(manifest).toBeDefined() |
| 61 | + const l: number = manifest ? manifest.length : 0 |
| 62 | + expect(l).toBe(4) |
| 63 | + }) |
| 64 | + |
| 65 | + it('can match stable major version for linux x64', async () => { |
| 66 | + os.platform = 'linux' |
| 67 | + os.arch = 'x64' |
| 68 | + |
| 69 | + const manifest: mm.IToolRelease[] | null = await tc.getManifestFromRepo( |
| 70 | + owner, |
| 71 | + repo, |
| 72 | + fakeToken |
| 73 | + ) |
| 74 | + const release: tc.IToolRelease | undefined = await tc.findFromManifest( |
| 75 | + '2.x', |
| 76 | + true, |
| 77 | + manifest |
| 78 | + ) |
| 79 | + expect(release).toBeDefined() |
| 80 | + expect(release?.version).toBe('2.0.2') |
| 81 | + expect(release?.files.length).toBe(1) |
| 82 | + const file = release?.files[0] |
| 83 | + expect(file).toBeDefined() |
| 84 | + expect(file?.arch).toBe('x64') |
| 85 | + expect(file?.platform).toBe('linux') |
| 86 | + expect(file?.download_url).toBe( |
| 87 | + 'https://github.com/actions/sometool/releases/tag/2.0.2-20200402.6/sometool-2.0.2-linux-x64.tar.gz' |
| 88 | + ) |
| 89 | + expect(file?.filename).toBe('sometool-2.0.2-linux-x64.tar.gz') |
| 90 | + }) |
| 91 | + |
| 92 | + it('can match stable exact version for linux x64', async () => { |
| 93 | + os.platform = 'linux' |
| 94 | + os.arch = 'x64' |
| 95 | + |
| 96 | + const manifest: mm.IToolRelease[] | null = await tc.getManifestFromRepo( |
| 97 | + owner, |
| 98 | + repo, |
| 99 | + fakeToken |
| 100 | + ) |
| 101 | + const release: tc.IToolRelease | undefined = await tc.findFromManifest( |
| 102 | + '1.2.3', |
| 103 | + true, |
| 104 | + manifest |
| 105 | + ) |
| 106 | + expect(release).toBeDefined() |
| 107 | + expect(release?.version).toBe('1.2.3') |
| 108 | + expect(release?.files.length).toBe(1) |
| 109 | + const file = release?.files[0] |
| 110 | + expect(file).toBeDefined() |
| 111 | + expect(file?.arch).toBe('x64') |
| 112 | + expect(file?.platform).toBe('linux') |
| 113 | + expect(file?.download_url).toBe( |
| 114 | + 'https://github.com/actions/sometool/releases/tag/1.2.3-20200402.6/sometool-1.2.3-linux-x64.tar.gz' |
| 115 | + ) |
| 116 | + expect(file?.filename).toBe('sometool-1.2.3-linux-x64.tar.gz') |
| 117 | + }) |
| 118 | + |
| 119 | + it('can match with linux platform version spec', async () => { |
| 120 | + os.platform = 'linux' |
| 121 | + os.arch = 'x64' |
| 122 | + |
| 123 | + readLsbSpy.mockImplementation(() => { |
| 124 | + return `DISTRIB_ID=Ubuntu |
| 125 | + DISTRIB_RELEASE=18.04 |
| 126 | + DISTRIB_CODENAME=bionic |
| 127 | + DISTRIB_DESCRIPTION=Ubuntu 18.04.4 LTS` |
| 128 | + }) |
| 129 | + |
| 130 | + const manifest: mm.IToolRelease[] | null = await tc.getManifestFromRepo( |
| 131 | + owner, |
| 132 | + repo, |
| 133 | + fakeToken |
| 134 | + ) |
| 135 | + const release: tc.IToolRelease | undefined = await tc.findFromManifest( |
| 136 | + '1.2.4', |
| 137 | + true, |
| 138 | + manifest |
| 139 | + ) |
| 140 | + expect(release).toBeDefined() |
| 141 | + expect(release?.version).toBe('1.2.4') |
| 142 | + expect(release?.files.length).toBe(1) |
| 143 | + const file = release?.files[0] |
| 144 | + expect(file).toBeDefined() |
| 145 | + expect(file?.arch).toBe('x64') |
| 146 | + expect(file?.platform).toBe('linux') |
| 147 | + expect(file?.download_url).toBe( |
| 148 | + 'https://github.com/actions/sometool/releases/tag/1.2.4-20200402.6/sometool-1.2.4-ubuntu1804-x64.tar.gz' |
| 149 | + ) |
| 150 | + expect(file?.filename).toBe('sometool-1.2.4-ubuntu1804-x64.tar.gz') |
| 151 | + }) |
| 152 | + |
| 153 | + it('can match with darwin platform version spec', async () => { |
| 154 | + os.platform = 'darwin' |
| 155 | + os.arch = 'x64' |
| 156 | + |
| 157 | + execSpy.mockImplementation(() => '10.15.1') |
| 158 | + |
| 159 | + const manifest: mm.IToolRelease[] | null = await tc.getManifestFromRepo( |
| 160 | + owner, |
| 161 | + repo, |
| 162 | + fakeToken |
| 163 | + ) |
| 164 | + const release: tc.IToolRelease | undefined = await tc.findFromManifest( |
| 165 | + '1.2.4', |
| 166 | + true, |
| 167 | + manifest |
| 168 | + ) |
| 169 | + expect(release).toBeDefined() |
| 170 | + expect(release?.version).toBe('1.2.4') |
| 171 | + expect(release?.files.length).toBe(1) |
| 172 | + const file = release?.files[0] |
| 173 | + expect(file).toBeDefined() |
| 174 | + expect(file?.arch).toBe('x64') |
| 175 | + expect(file?.platform).toBe('darwin') |
| 176 | + expect(file?.download_url).toBe( |
| 177 | + 'https://github.com/actions/sometool/releases/tag/1.2.4-20200402.6/sometool-1.2.4-darwin1015-x64.tar.gz' |
| 178 | + ) |
| 179 | + expect(file?.filename).toBe('sometool-1.2.4-darwin1015-x64.tar.gz') |
| 180 | + }) |
| 181 | + |
| 182 | + it('does not match with unmatched linux platform version spec', async () => { |
| 183 | + os.platform = 'linux' |
| 184 | + os.arch = 'x64' |
| 185 | + |
| 186 | + readLsbSpy.mockImplementation(() => { |
| 187 | + return `DISTRIB_ID=Ubuntu |
| 188 | + DISTRIB_RELEASE=16.04 |
| 189 | + DISTRIB_CODENAME=xenial |
| 190 | + DISTRIB_DESCRIPTION=Ubuntu 16.04.4 LTS` |
| 191 | + }) |
| 192 | + |
| 193 | + const manifest: mm.IToolRelease[] | null = await tc.getManifestFromRepo( |
| 194 | + owner, |
| 195 | + repo, |
| 196 | + fakeToken |
| 197 | + ) |
| 198 | + const release: tc.IToolRelease | undefined = await tc.findFromManifest( |
| 199 | + '1.2.4', |
| 200 | + true, |
| 201 | + manifest |
| 202 | + ) |
| 203 | + expect(release).toBeUndefined() |
| 204 | + }) |
| 205 | + |
| 206 | + it('does not match with unmatched darwin platform version spec', async () => { |
| 207 | + os.platform = 'darwin' |
| 208 | + os.arch = 'x64' |
| 209 | + |
| 210 | + execSpy.mockImplementation(() => '10.14.6') |
| 211 | + |
| 212 | + const manifest: mm.IToolRelease[] | null = await tc.getManifestFromRepo( |
| 213 | + owner, |
| 214 | + repo, |
| 215 | + fakeToken |
| 216 | + ) |
| 217 | + const release: tc.IToolRelease | undefined = await tc.findFromManifest( |
| 218 | + '1.2.4', |
| 219 | + true, |
| 220 | + manifest |
| 221 | + ) |
| 222 | + expect(release).toBeUndefined() |
| 223 | + }) |
| 224 | + |
| 225 | + it('can get version from lsb on ubuntu-18.04', async () => { |
| 226 | + os.platform = 'linux' |
| 227 | + os.arch = 'x64' |
| 228 | + |
| 229 | + //existsSpy.mockImplementation(() => true) |
| 230 | + readLsbSpy.mockImplementation(() => { |
| 231 | + return `DISTRIB_ID=Ubuntu |
| 232 | + DISTRIB_RELEASE=18.04 |
| 233 | + DISTRIB_CODENAME=bionic |
| 234 | + DISTRIB_DESCRIPTION=Ubuntu 18.04.4 LTS` |
| 235 | + }) |
| 236 | + |
| 237 | + const version = mm._getOsVersion() |
| 238 | + |
| 239 | + expect(osm.platform()).toBe('linux') |
| 240 | + expect(version).toBe('18.04') |
| 241 | + }) |
| 242 | + |
| 243 | + it('can get version from lsb on ubuntu-16.04', async () => { |
| 244 | + os.platform = 'linux' |
| 245 | + os.arch = 'x64' |
| 246 | + |
| 247 | + readLsbSpy.mockImplementation(() => { |
| 248 | + return `DISTRIB_ID=Ubuntu |
| 249 | + DISTRIB_RELEASE=16.04 |
| 250 | + DISTRIB_CODENAME=xenial |
| 251 | + DISTRIB_DESCRIPTION="Ubuntu 16.04.6 LTS"` |
| 252 | + }) |
| 253 | + |
| 254 | + const version = mm._getOsVersion() |
| 255 | + |
| 256 | + expect(osm.platform()).toBe('linux') |
| 257 | + expect(version).toBe('16.04') |
| 258 | + }) |
| 259 | + |
| 260 | + // sw_vers -productVersion |
| 261 | + it('can get version on macOS', async () => { |
| 262 | + os.platform = 'darwin' |
| 263 | + os.arch = 'x64' |
| 264 | + |
| 265 | + execSpy.mockImplementation(() => '10.14.6') |
| 266 | + |
| 267 | + const version = mm._getOsVersion() |
| 268 | + |
| 269 | + expect(osm.platform()).toBe('darwin') |
| 270 | + expect(version).toBe('10.14.6') |
| 271 | + }) |
| 272 | +}) |
0 commit comments