Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test Plugin

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
persist-credentials: false
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6
with:
go-version: '1.24.0'
cache: false
- name: Test Node.js hooks with vfox's Lua interpreter
run: go run github.com/yuin/gopher-lua/cmd/glua@v1.1.1 tests/hooks_test.lua
9 changes: 6 additions & 3 deletions hooks/available.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ function PLUGIN:Available(ctx)
local resp, err = http.get({
url = util.getBaseUrl() .. util.VersionSourceUrl
})
if err ~= nil or resp.status_code ~= 200 then
return {}
if err ~= nil then
error("Failed to fetch Node.js versions: " .. tostring(err))
end
if resp.status_code ~= 200 then
error("Failed to fetch Node.js versions: HTTP " .. tostring(resp.status_code))
end
local body = json.decode(resp.body)
local result = {}
Expand All @@ -34,4 +37,4 @@ function PLUGIN:Available(ctx)
table.sort(result, util.compare_versions)
available_result = result
return result
end
end
8 changes: 8 additions & 0 deletions hooks/pre_install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ function PLUGIN:PreInstall(ctx)
end

local arch_type = RUNTIME.archType
if arch_type == nil or arch_type == "" then
error("Cannot determine Node.js package architecture from RUNTIME.archType")
end
local ext = ".tar.gz"
local osType = RUNTIME.osType
if RUNTIME.archType == "amd64" then
arch_type = "x64"
elseif RUNTIME.archType == "386" then
arch_type = "x86"
end
if RUNTIME.osType == "windows" then
ext = ".zip"
Expand All @@ -52,6 +57,9 @@ function PLUGIN:PreInstall(ctx)
error("get checksum failed")
end
local checksum = util.get_checksum(resp.body, filename)
if checksum == nil then
error("Node.js package " .. filename .. " is not listed in " .. baseUrl .. "SHASUMS256.txt")
end
return {
version = version,
url = baseUrl .. filename,
Expand Down
61 changes: 61 additions & 0 deletions tests/hooks_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package.path = './lib/?.lua;' .. package.path
local state = {failed=0, passed=0}
package.preload.http = function() return {get=function(args)
state.requests = state.requests + 1
return state.response, state.request_error
end} end
package.preload.json = function() return {decode=function() return state.decoded end} end
PLUGIN = {}
dofile('hooks/available.lua')
dofile('hooks/pre_install.lua')
local function test(name, fn)
state.response, state.request_error, state.decoded, state.requests = nil, nil, nil, 0
available_result = nil
local ok, err = pcall(fn)
if ok then state.passed=state.passed+1 else state.failed=state.failed+1; print('FAIL '..name..': '..tostring(err)) end
end
local function expect_error(fn, message)
local ok, err=pcall(fn)
assert(not ok, 'expected error')
assert(tostring(err):find(message,1,true), tostring(err))
end
for _, failure in ipairs({'timeout','HTTP 503'}) do
test('search failure and recovery '..failure, function()
if failure=='timeout' then state.request_error='timeout' else state.response={status_code=503} end
expect_error(function() PLUGIN:Available({}) end, failure)
state.request_error=nil; state.response={status_code=200,body='index'}
state.decoded={{version='v20.16.0',npm='10.8.1',lts='Iron'}}
assert(PLUGIN:Available({})[1].version=='20.16.0')
assert(state.requests==2, 'failed response was cached')
end)
end
local cases = {
{'windows','amd64','win-x64.zip'},
{'windows','386','win-x86.zip'},
{'windows','arm64','win-arm64.zip'},
{'linux','amd64','linux-x64.tar.gz'},
{'darwin','arm64','darwin-arm64.tar.gz'},
}
for _, case in ipairs(cases) do
test('package '..case[1]..'/'..case[2], function()
RUNTIME={osType=case[1],archType=case[2]}
local filename='node-v20.16.0-'..case[3]
local hash=string.rep('a',64)
state.response={status_code=200,body=hash..' '..filename..'\n'}
local result=PLUGIN:PreInstall({version='20.16.0'})
assert(result.url:sub(-#filename)==filename,result.url)
assert(result.sha256==hash,'checksum missing')
end)
end
test('missing architecture',function()
RUNTIME={osType='windows'}
expect_error(function() PLUGIN:PreInstall({version='20.16.0'}) end,'architecture')
assert(state.requests==0)
end)
test('missing package',function()
RUNTIME={osType='windows',archType='arm64'}
state.response={status_code=200,body=string.rep('a',64)..' node-v20.16.0-win-x64.zip\n'}
expect_error(function() PLUGIN:PreInstall({version='20.16.0'}) end,'node-v20.16.0-win-arm64.zip')
end)
print(state.passed..' passed, '..state.failed..' failed')
assert(state.failed==0)