Skip to content

Commit

Permalink
Refactor fetching & matching packages from index into own file
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Oct 13, 2023
1 parent abd93e5 commit f8cffdc
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 243 deletions.
120 changes: 120 additions & 0 deletions __tests__/packages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {expect, describe, it, beforeAll} from '@jest/globals'
import type * as packagesType from '../src/packages'
import type * as fetchType from 'node-fetch'
import {SpiedModule, spyOnModule} from './spy-on-module'

const fakeIndex: packagesType.PackageIndex = [
{name: 'edgedb-cli', version: '1.0.0+5724c50', revision: '202202100030'},
{name: 'edgedb-cli', version: '1.0.0-rc.2+eea8ba1', revision: '202111111827'},
{name: 'edgedb-cli', version: '1.0.0-rc.3+b13dfe9', revision: '202111301914'},
{name: 'edgedb-cli', version: '1.0.0-rc.6+5626317', revision: '202201191751'},
{name: 'edgedb-cli', version: '1.1.0+96c3d69', revision: '202202222341'},
{name: 'edgedb-cli', version: '1.1.1+5bb8bad', revision: '202203171920'},
{name: 'edgedb-cli', version: '1.1.2+58eb29e', revision: '202204240642'},
{name: 'edgedb-cli', version: '1.2.0+cc78a3d', revision: '202207132102'},
{name: 'edgedb-cli', version: '1.2.1+7ae7e10', revision: '202207142033'},
{name: 'edgedb-cli', version: '1.2.2+2874715', revision: '202207191809'},
{name: 'edgedb-cli', version: '1.2.3+d637394', revision: '202207251735'},
{name: 'edgedb-cli', version: '2.0.0+62ada3f', revision: '202207272022'},
{name: 'edgedb-cli', version: '2.3.1+ef99779', revision: '202302211915'},
{name: 'edgedb-cli', version: '3.0.0+8b024db', revision: '202305171711'},
{name: 'edgedb-cli', version: '3.4.0+160d07d', revision: '202307070213'},
{name: 'edgedb-cli', version: '3.5.0+907ff37', revision: '202309122051'},
{name: 'edgedb-server-3', version: '3.3+8d42667', revision: '202309062039'},
{name: 'edgedb-server-3', version: '3.4+4fc6d86', revision: '202309271921'}
].map((init): packagesType.Package => {
const installRef = `/archive/x86_64-unknown-linux-musl/${init.name}-${init.version}`
return {
...init,
architecture: 'x86_64' as const,
installref: installRef,
downloadUrl: installRef
}
})

describe('packages', () => {
let src: SpiedModule<typeof packagesType>
let fetch: SpiedModule<typeof fetchType>
beforeAll(async () => {
fetch = await spyOnModule<typeof fetchType>('node-fetch')
src = await spyOnModule<typeof packagesType>('../src/packages')
})

it('Finds best package correctly', async () => {
let pkg = src.findBestPackage(fakeIndex, 'edgedb-cli', '1.0.0-rc.2')
expect(pkg?.version).toBe('1.0.0-rc.2+eea8ba1')

pkg = src.findBestPackage(fakeIndex, 'edgedb-cli', '*')
expect(pkg?.version).toBe('3.5.0+907ff37')

pkg = src.findBestPackage(fakeIndex, 'edgedb-server-3', '*')
expect(pkg?.version).toBe('3.4+4fc6d86')

pkg = src.findBestPackage(fakeIndex, 'edgedb-cli', '>=3.2.0 <=3.4.0')
expect(pkg?.version).toBe('3.4.0+160d07d')

pkg = src.findBestPackage(fakeIndex, 'edgedb-cli', '^1')
expect(pkg?.version).toBe('1.2.3+d637394')
})

it('Fetches package index', async () => {
const apiResult = [
{
name: 'edgedb-cli',
version: '3.5.0+907ff37',
revision: '202309122051',
installref: '/path'
},
{
name: 'edgedb-cli',
version: '3.5.0+907ff37',
revision: '202309122053',
installref: '/path'
},
{
name: 'edgedb-server-3',
version: '3.3+8d42667',
revision: '202309062050',
installref: '/path'
},
{
name: 'edgedb-server-3',
version: '3.3+8d42667',
revision: '202309062039',
installref: '/path'
}
]
fetch.default.mockResolvedValue({
json: async () => ({packages: apiResult})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)

const packages = await src.getPackageIndex()

expect(fetch.default).toHaveBeenCalled()
expect(new URL(fetch.default.mock.calls[0][0] as string)).toBeInstanceOf(
URL
)

// De-dupes revisions correctly
expect(packages).toHaveLength(2)
expect(packages).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'edgedb-cli',
version: '3.5.0+907ff37',
revision: '202309122053'
}),
expect.objectContaining({
name: 'edgedb-server-3',
version: '3.3+8d42667',
revision: '202309062050'
})
])
)

// Has valid download URL
expect(new URL(packages[0].downloadUrl)).toBeInstanceOf(URL)
expect(packages[0].downloadUrl.endsWith('/path')).toBe(true)
})
})
58 changes: 20 additions & 38 deletions __tests__/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
jest
} from '@jest/globals'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as process from 'process'
import url from 'url'
import type * as mainType from '../src/main'
import type * as packagesType from '../src/packages'
import {SpiedModule, spyOnModule} from './spy-on-module'

const tempDir = path.join(
Expand All @@ -31,12 +31,14 @@ describe('setup-edgedb', () => {
let core: SpiedModule<typeof coreType>
let exec: SpiedModule<typeof execType>
let tc: SpiedModule<typeof tcType>
let packages: SpiedModule<typeof packagesType>
let main: typeof mainType

beforeAll(async () => {
core = await spyOnModule<typeof coreType>('@actions/core')
tc = await spyOnModule<typeof tcType>('@actions/tool-cache')
exec = await spyOnModule<typeof execType>('@actions/exec')
packages = await spyOnModule<typeof packagesType>('../src/packages')
// After mocks have been set up
main = await import('../src/main')
})
Expand Down Expand Up @@ -68,16 +70,18 @@ describe('setup-edgedb', () => {
})

it('Installs CLI', async () => {
inputs['cli-version'] = '>=3.2.0 <=3.4.0'

let libc = ''
if (os.platform() === 'linux') {
libc = 'musl'
const cliVersionRange = '>=3.2.0 <=3.4.0'
inputs['cli-version'] = cliVersionRange

const fakePackage: packagesType.Package = {
name: 'edgedb-cli',
version: '3.4.0+160d07d',
revision: '202309122051',
architecture: 'x86_64',
installref: '/cli-3.4',
downloadUrl: 'https://edgedb.com/cli-3.4'
}
const baseDist = main.getBaseDist(os.arch(), os.platform(), libc)
const pkgBase = `https://packages.edgedb.com/archive/${baseDist}`
const expectedVer = '3.4.0\\+([0-9a-f]{7})'
const expectedUrl = `${pkgBase}/edgedb-cli-${expectedVer}`
packages.getCliPackage.mockResolvedValue(fakePackage)

const tmpdir = fs.mkdtempSync('edgedb-setup')
let tmp = path.join(tmpdir, 'foo')
Expand All @@ -96,39 +100,27 @@ describe('setup-edgedb', () => {
fs.unlinkSync(tmp)
fs.rmdirSync(tmpdir)

expect(packages.getCliPackage).toHaveBeenCalledWith(cliVersionRange)
expect(tc.downloadTool).toHaveBeenCalled()
expect(core.info).toHaveBeenCalledWith(
expect.stringMatching(
new RegExp(
`Downloading edgedb-cli ${expectedVer} - ${os.arch()} from ${expectedUrl}`
)
)
`Downloading edgedb-cli ${fakePackage.version} - x86_64 from ${fakePackage.downloadUrl}`
)
expect(core.addPath).toHaveBeenCalledWith(cliPath)
})

it('Installs server', async () => {
inputs['cli-version'] = '>=3.2.0 <=3.4.0'
inputs['server-version'] = 'stable'

let libc = ''
if (os.platform() === 'linux') {
libc = 'musl'
}
const baseDist = main.getBaseDist(os.arch(), os.platform(), libc)
const pkgBase = `https://packages.edgedb.com/archive/${baseDist}`
const expectedVer = '3.4.0\\+([0-9a-f]{7})'
const expectedUrl = `${pkgBase}/edgedb-cli-${expectedVer}`
packages.getCliPackage.mockResolvedValue({} as packagesType.Package)

const cliPath = path.normalize('/cache/edgedb/3.4.0')
tc.find.mockReturnValue(cliPath)

const tmpdir = fs.mkdtempSync('edgedb-setup')
let tmp = path.join(tmpdir, 'foo')
fs.closeSync(fs.openSync(tmp, 'w'))
tmp = fs.realpathSync(tmp)

tc.downloadTool.mockImplementation(async () => tmp)

tc.find.mockImplementation(() => '')

exec.exec.mockImplementation(async (cmd, args, opts) => {
if (args && args[0] === 'server' && args[1] === 'install') {
return 0
Expand All @@ -147,23 +139,13 @@ describe('setup-edgedb', () => {
}
})

const cliPath = path.normalize('/cache/edgedb/3.4.0')
tc.cacheFile.mockImplementation(async () => cliPath)
const serverPath = path.dirname(tmp)

await main.run()

fs.unlinkSync(tmp)
fs.rmdirSync(tmpdir)

expect(tc.downloadTool).toHaveBeenCalled()
expect(core.info).toHaveBeenCalledWith(
expect.stringMatching(
new RegExp(
`Downloading edgedb-cli ${expectedVer} - ${os.arch()} from ${expectedUrl}`
)
)
)
expect(core.addPath).toHaveBeenCalledWith(serverPath)
expect(core.addPath).toHaveBeenCalledWith(cliPath)
})
Expand Down
19 changes: 0 additions & 19 deletions __tests__/ver.test.ts

This file was deleted.

Loading

0 comments on commit f8cffdc

Please sign in to comment.