diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e48b7a9..b283421 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/index.js b/index.js index 8cd4b09..b1753a2 100644 --- a/index.js +++ b/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) diff --git a/package.json b/package.json index cf09137..c22a8e4 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ }, "scripts": { "prepare": "ncc build index.js -o dist", - "test": "standard" + "test": "standard && node test" }, "standard": { "ignore": [ diff --git a/test.js b/test.js new file mode 100644 index 0000000..3860a28 --- /dev/null +++ b/test.js @@ -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) +}) diff --git a/versions.js b/versions.js new file mode 100644 index 0000000..cd08c4f --- /dev/null +++ b/versions.js @@ -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 +}