|
| 1 | +package.path = './lib/?.lua;' .. package.path |
| 2 | +local state = {failed=0, passed=0} |
| 3 | +package.preload.http = function() return {get=function(args) |
| 4 | + state.requests = state.requests + 1 |
| 5 | + return state.response, state.request_error |
| 6 | +end} end |
| 7 | +package.preload.json = function() return {decode=function() return state.decoded end} end |
| 8 | +PLUGIN = {} |
| 9 | +dofile('hooks/available.lua') |
| 10 | +dofile('hooks/pre_install.lua') |
| 11 | +local function test(name, fn) |
| 12 | + state.response, state.request_error, state.decoded, state.requests = nil, nil, nil, 0 |
| 13 | + available_result = nil |
| 14 | + local ok, err = pcall(fn) |
| 15 | + if ok then state.passed=state.passed+1 else state.failed=state.failed+1; print('FAIL '..name..': '..tostring(err)) end |
| 16 | +end |
| 17 | +local function expect_error(fn, message) |
| 18 | + local ok, err=pcall(fn) |
| 19 | + assert(not ok, 'expected error') |
| 20 | + assert(tostring(err):find(message,1,true), tostring(err)) |
| 21 | +end |
| 22 | +for _, failure in ipairs({'timeout','HTTP 503'}) do |
| 23 | + test('search failure and recovery '..failure, function() |
| 24 | + if failure=='timeout' then state.request_error='timeout' else state.response={status_code=503} end |
| 25 | + expect_error(function() PLUGIN:Available({}) end, failure) |
| 26 | + state.request_error=nil; state.response={status_code=200,body='index'} |
| 27 | + state.decoded={{version='v20.16.0',npm='10.8.1',lts='Iron'}} |
| 28 | + assert(PLUGIN:Available({})[1].version=='20.16.0') |
| 29 | + assert(state.requests==2, 'failed response was cached') |
| 30 | + end) |
| 31 | +end |
| 32 | +local cases = { |
| 33 | + {'windows','amd64','win-x64.zip'}, |
| 34 | + {'windows','386','win-x86.zip'}, |
| 35 | + {'windows','arm64','win-arm64.zip'}, |
| 36 | + {'linux','amd64','linux-x64.tar.gz'}, |
| 37 | + {'darwin','arm64','darwin-arm64.tar.gz'}, |
| 38 | +} |
| 39 | +for _, case in ipairs(cases) do |
| 40 | + test('package '..case[1]..'/'..case[2], function() |
| 41 | + RUNTIME={osType=case[1],archType=case[2]} |
| 42 | + local filename='node-v20.16.0-'..case[3] |
| 43 | + local hash=string.rep('a',64) |
| 44 | + state.response={status_code=200,body=hash..' '..filename..'\n'} |
| 45 | + local result=PLUGIN:PreInstall({version='20.16.0'}) |
| 46 | + assert(result.url:sub(-#filename)==filename,result.url) |
| 47 | + assert(result.sha256==hash,'checksum missing') |
| 48 | + end) |
| 49 | +end |
| 50 | +test('missing architecture',function() |
| 51 | + RUNTIME={osType='windows'} |
| 52 | + expect_error(function() PLUGIN:PreInstall({version='20.16.0'}) end,'architecture') |
| 53 | + assert(state.requests==0) |
| 54 | +end) |
| 55 | +test('missing package',function() |
| 56 | + RUNTIME={osType='windows',archType='arm64'} |
| 57 | + state.response={status_code=200,body=string.rep('a',64)..' node-v20.16.0-win-x64.zip\n'} |
| 58 | + expect_error(function() PLUGIN:PreInstall({version='20.16.0'}) end,'node-v20.16.0-win-arm64.zip') |
| 59 | +end) |
| 60 | +print(state.passed..' passed, '..state.failed..' failed') |
| 61 | +assert(state.failed==0) |
0 commit comments