add tests for version to URL resolution
parent
ab441cce53
commit
d485d46e15
|
@ -1,26 +1,31 @@
|
|||
name: CI
|
||||
on: push
|
||||
jobs:
|
||||
lint:
|
||||
test:
|
||||
name: Unit tests
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: goto-bus-stop/standard-action@v1
|
||||
with:
|
||||
annotate: true
|
||||
env:
|
||||
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Install dependencies
|
||||
run: npm install
|
||||
- name: Run tests
|
||||
run: npm test
|
||||
|
||||
smoke-test:
|
||||
name: Build test
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
zig-version: [0.5.0, master]
|
||||
runs-on: ${{matrix.os}}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: goto-bus-stop/setup-zig@default
|
||||
- name: Checkout sources
|
||||
uses: actions/checkout@v2
|
||||
- name: Install Zig
|
||||
uses: goto-bus-stop/setup-zig@default
|
||||
with:
|
||||
version: ${{matrix.zig-version}}
|
||||
- run: zig build test
|
||||
- name: Run tests
|
||||
run: zig build test
|
||||
working-directory: test
|
||||
|
|
66
index.js
66
index.js
|
@ -1,69 +1,13 @@
|
|||
const os = require('os')
|
||||
const path = require('path')
|
||||
const semver = require('semver')
|
||||
const get = require('simple-get').concat
|
||||
const actions = require('@actions/core')
|
||||
const cache = require('@actions/tool-cache')
|
||||
|
||||
function getJSON (opts) {
|
||||
return new Promise((resolve, reject) => {
|
||||
get({ ...opts, json: true }, (err, req, data) => {
|
||||
if (err) {
|
||||
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 }
|
||||
}
|
||||
const {
|
||||
extForPlatform,
|
||||
resolveCommit,
|
||||
resolveVersion
|
||||
} = require('./versions')
|
||||
|
||||
async function downloadZig (platform, version) {
|
||||
const ext = extForPlatform(platform)
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
},
|
||||
"scripts": {
|
||||
"prepare": "ncc build index.js -o dist",
|
||||
"test": "standard"
|
||||
"test": "standard && node test"
|
||||
},
|
||||
"standard": {
|
||||
"ignore": [
|
||||
|
|
|
@ -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)
|
||||
})
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue