add tests for version to URL resolution

This commit is contained in:
Renée Kooi 2020-11-11 12:38:41 +01:00
parent ab441cce53
commit d485d46e15
No known key found for this signature in database
GPG Key ID: 9940E33D1A44ADBF
5 changed files with 122 additions and 72 deletions

View File

@ -1,26 +1,31 @@
name: CI name: CI
on: push on: push
jobs: jobs:
lint: test:
name: Unit tests
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - name: Checkout sources
- uses: goto-bus-stop/standard-action@v1 uses: actions/checkout@v2
with: - name: Install dependencies
annotate: true run: npm install
env: - name: Run tests
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} run: npm test
smoke-test: smoke-test:
name: Build test
strategy: strategy:
matrix: matrix:
os: [ubuntu-latest, macos-latest, windows-latest] os: [ubuntu-latest, macos-latest, windows-latest]
zig-version: [0.5.0, master] zig-version: [0.5.0, master]
runs-on: ${{matrix.os}} runs-on: ${{matrix.os}}
steps: steps:
- uses: actions/checkout@v2 - name: Checkout sources
- uses: goto-bus-stop/setup-zig@default uses: actions/checkout@v2
- name: Install Zig
uses: goto-bus-stop/setup-zig@default
with: with:
version: ${{matrix.zig-version}} version: ${{matrix.zig-version}}
- run: zig build test - name: Run tests
run: zig build test
working-directory: test working-directory: test

View File

@ -1,69 +1,13 @@
const os = require('os') const os = require('os')
const path = require('path') const path = require('path')
const semver = require('semver') const semver = require('semver')
const get = require('simple-get').concat
const actions = require('@actions/core') const actions = require('@actions/core')
const cache = require('@actions/tool-cache') const cache = require('@actions/tool-cache')
const {
function getJSON (opts) { extForPlatform,
return new Promise((resolve, reject) => { resolveCommit,
get({ ...opts, json: true }, (err, req, data) => { resolveVersion
if (err) { } = require('./versions')
reject(err)
} else {
resolve(data)
}
})
})
}
function extForPlatform (platform) {
return {
linux: 'tar.xz',
darwin: 'tar.xz',
win32: 'zip'
}[platform]
}
function resolveCommit (platform, version) {
const ext = extForPlatform(platform)
const addrhost = {
linux: 'linux-x86_64',
darwin: 'macos-x86_64',
win32: 'windows-x86_64'
}[platform]
const downloadUrl = `https://ziglang.org/builds/zig-${addrhost}-${version}.${ext}`
const variantName = `zig-${addrhost}-${version}`
return { downloadUrl, variantName }
}
async function resolveVersion (platform, version) {
const ext = extForPlatform(platform)
const host = {
linux: 'x86_64-linux',
darwin: 'x86_64-macos',
win32: 'x86_64-windows'
}[platform] || platform
const index = await getJSON({ url: 'https://ziglang.org/download/index.json' })
const availableVersions = Object.keys(index)
const useVersion = semver.valid(version)
? semver.maxSatisfying(availableVersions.filter((v) => semver.valid(v)), version)
: null
const meta = index[useVersion || version]
if (!meta || !meta[host]) {
throw new Error(`Could not find version ${version} for platform ${host}`)
}
const downloadUrl = meta[host].tarball
const variantName = path.basename(meta[host].tarball).replace(`.${ext}`, '')
return { downloadUrl, variantName }
}
async function downloadZig (platform, version) { async function downloadZig (platform, version) {
const ext = extForPlatform(platform) const ext = extForPlatform(platform)

View File

@ -31,7 +31,7 @@
}, },
"scripts": { "scripts": {
"prepare": "ncc build index.js -o dist", "prepare": "ncc build index.js -o dist",
"test": "standard" "test": "standard && node test"
}, },
"standard": { "standard": {
"ignore": [ "ignore": [

32
test.js Normal file
View File

@ -0,0 +1,32 @@
const assert = require('assert').strict
const {
resolveCommit,
resolveVersion
} = require('./versions')
async function test () {
assert.deepEqual(resolveCommit('linux', '0.6.0+4b48fccad'), {
downloadUrl: 'https://ziglang.org/builds/zig-linux-x86_64-0.6.0+4b48fccad.tar.xz',
variantName: 'zig-linux-x86_64-0.6.0+4b48fccad'
})
assert.deepEqual(resolveCommit('win32', '0.6.0+4b48fccad'), {
downloadUrl: 'https://ziglang.org/builds/zig-windows-x86_64-0.6.0+4b48fccad.zip',
variantName: 'zig-windows-x86_64-0.6.0+4b48fccad'
})
assert.deepEqual(await resolveVersion('linux', '0.7.0'), {
downloadUrl: 'https://ziglang.org/download/0.7.0/zig-linux-x86_64-0.7.0.tar.xz',
variantName: 'zig-linux-x86_64-0.7.0'
})
assert.deepEqual(await resolveVersion('win32', '0.4.0'), {
downloadUrl: 'https://ziglang.org/download/0.4.0/zig-windows-x86_64-0.4.0.zip',
variantName: 'zig-windows-x86_64-0.4.0'
})
await assert.doesNotReject(resolveVersion('linux', 'master'))
await assert.doesNotReject(resolveVersion('win32', 'master'))
}
test().catch((error) => {
console.error(error.stack)
process.exit(1)
})

69
versions.js Normal file
View File

@ -0,0 +1,69 @@
const path = require('path')
const get = require('simple-get').concat
const semver = require('semver')
function extForPlatform (platform) {
return {
linux: 'tar.xz',
darwin: 'tar.xz',
win32: 'zip'
}[platform]
}
function resolveCommit (platform, version) {
const ext = extForPlatform(platform)
const addrhost = {
linux: 'linux-x86_64',
darwin: 'macos-x86_64',
win32: 'windows-x86_64'
}[platform]
const downloadUrl = `https://ziglang.org/builds/zig-${addrhost}-${version}.${ext}`
const variantName = `zig-${addrhost}-${version}`
return { downloadUrl, variantName }
}
function getJSON (opts) {
return new Promise((resolve, reject) => {
get({ ...opts, json: true }, (err, req, data) => {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}
async function resolveVersion (platform, version) {
const ext = extForPlatform(platform)
const host = {
linux: 'x86_64-linux',
darwin: 'x86_64-macos',
win32: 'x86_64-windows'
}[platform] || platform
const index = await getJSON({ url: 'https://ziglang.org/download/index.json' })
const availableVersions = Object.keys(index)
const useVersion = semver.valid(version)
? semver.maxSatisfying(availableVersions.filter((v) => semver.valid(v)), version)
: null
const meta = index[useVersion || version]
if (!meta || !meta[host]) {
throw new Error(`Could not find version ${useVersion || version} for platform ${host}`)
}
const downloadUrl = meta[host].tarball
const variantName = path.basename(meta[host].tarball).replace(`.${ext}`, '')
return { downloadUrl, variantName }
}
module.exports = {
extForPlatform,
resolveCommit,
resolveVersion
}