From a1adfdc7d59979824addce2f519a527f9a5c0290 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 29 Nov 2017 01:50:11 -0500 Subject: [PATCH] ci: nodejs client acceptance-test #7706 ci: install nodejs 8 in Appveyor, Travis provider: check node version for debug support Resolve https://github.com/neovim/neovim/pull/7577#issuecomment-350590592 for Unix. provider: test if nodejs in ci supports --inspect-brk nodejs host for neovim requires nodejs 6+ to work properly. nodejs 6.12+ or 7.6+ is required for debug support via `node --inspect-brk`. provider: run cli.js of nodejs host directly npm shims are useless because the user cannot set node to debug mode via --inspect-brk. This is problematic on Windows which use batchfiles and shell scripts to compensate for not supporting shebang. The patch uses `npm root -g` to get the absolute path of the global npm modules. If that fails, then the user did not install neovim npm package globally. Use that absolute path to find `neovim/bin/cli.js`, which is what the npm shim actually runs with node. glob() is for a simple file check in case bin/ is removed because the npm shims are ignored now. --- appveyor.yml | 2 + ci/before_install.sh | 12 ++++ ci/build.bat | 6 ++ ci/install.sh | 5 ++ ci/run_tests.sh | 1 + runtime/autoload/health/provider.vim | 15 +++-- runtime/autoload/provider/node.vim | 40 ++++++++++---- test/functional/helpers.lua | 2 +- test/functional/provider/nodejs_spec.lua | 70 ++++++++++++++++++++++++ 9 files changed, 138 insertions(+), 15 deletions(-) create mode 100644 test/functional/provider/nodejs_spec.lua diff --git a/appveyor.yml b/appveyor.yml index 2d6135c7a2..30b7947da0 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -9,6 +9,8 @@ matrix: allow_failures: - configuration: MINGW_64-gcov install: [] +before_build: +- ps: Install-Product node 8 build_script: - call ci\build.bat cache: diff --git a/ci/before_install.sh b/ci/before_install.sh index f84ad935bc..f5a57ad657 100755 --- a/ci/before_install.sh +++ b/ci/before_install.sh @@ -37,3 +37,15 @@ else # https://github.com/travis-ci/travis-ci/issues/8363 pip3 -q install --user --upgrade pip || true fi + +if [[ "${TRAVIS_OS_NAME}" == linux ]]; then + echo "Install node (LTS)" + + if [ ! -f ~/.nvm/nvm.sh ]; then + curl -o ~/.nvm/nvm.sh https://raw.githubusercontent.com/creationix/nvm/master/nvm.sh + fi + + source ~/.nvm/nvm.sh + nvm install --lts + nvm use --lts +fi diff --git a/ci/build.bat b/ci/build.bat index 25f949b5e4..c5353ec5d1 100644 --- a/ci/build.bat +++ b/ci/build.bat @@ -37,6 +37,12 @@ set PATH=C:\Ruby24\bin;%PATH% cmd /c gem.cmd install neovim || goto :error where.exe neovim-ruby-host.bat || goto :error +cmd /c npm.cmd install -g neovim || goto :error +where.exe neovim-node-host.cmd || goto :error +for /f %%F in ('cmd /c npm root -g') do ( + set NODE_PATH=%%F +) + mkdir .deps cd .deps cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=RelWithDebInfo ..\third-party\ || goto :error diff --git a/ci/install.sh b/ci/install.sh index c8a0c8825d..2fe4f88822 100755 --- a/ci/install.sh +++ b/ci/install.sh @@ -23,3 +23,8 @@ CC=cc pip3 -q install --user --upgrade neovim || true echo "Install neovim RubyGem." gem install --no-document --version ">= 0.2.0" neovim + +if [[ "${TRAVIS_OS_NAME}" == linux ]]; then + echo "Install neovim npm package" + npm install -g neovim +fi diff --git a/ci/run_tests.sh b/ci/run_tests.sh index a0bf6e010d..ee3fa4a5af 100755 --- a/ci/run_tests.sh +++ b/ci/run_tests.sh @@ -23,6 +23,7 @@ if test "$CLANG_SANITIZER" != "TSAN" ; then # Additional threads are only created when the builtin UI starts, which # doesn't happen in the unit/functional tests run_test run_unittests + export NODE_PATH="$(npm root -g)" run_test run_functionaltests fi run_test run_oldtests diff --git a/runtime/autoload/health/provider.vim b/runtime/autoload/health/provider.vim index 0201ed8062..188b67c4c1 100644 --- a/runtime/autoload/health/provider.vim +++ b/runtime/autoload/health/provider.vim @@ -488,7 +488,7 @@ function! s:check_ruby() abort endfunction function! s:check_node() abort - call health#report_start('Node provider (optional)') + call health#report_start('Node.js provider (optional)') let loaded_var = 'g:loaded_node_provider' if exists(loaded_var) && !exists('*provider#node#Call') @@ -502,7 +502,14 @@ function! s:check_node() abort \ ['Install Node.js and verify that `node` and `npm` commands work.']) return endif - call health#report_info('Node: '. s:system('node -v')) + let node_v = get(split(s:system('node -v'), "\n"), 0, '') + call health#report_info('Node.js: '. node_v) + if !s:shell_error && s:version_cmp(node_v[1:], '6.0.0') < 0 + call health#report_warn('Neovim node.js host does not support '.node_v) + endif + if !provider#node#can_inspect() + call health#report_warn('node.js on this system does not support --inspect-brk so $NVIM_NODE_HOST_DEBUG is ignored.') + endif let host = provider#node#Detect() if empty(host) @@ -511,7 +518,7 @@ function! s:check_node() abort \ 'Is the npm bin directory in $PATH?']) return endif - call health#report_info('Host: '. host) + call health#report_info('Neovim node.js host: '. host) let latest_npm_cmd = has('win32') ? 'cmd /c npm info neovim --json' : 'npm info neovim --json' let latest_npm = s:system(split(latest_npm_cmd)) @@ -530,7 +537,7 @@ function! s:check_node() abort let latest_npm = get(get(pkg_data, 'dist-tags', {}), 'latest', 'unable to parse') endif - let current_npm_cmd = host .' --version' + let current_npm_cmd = ['node', host, '--version'] let current_npm = s:system(current_npm_cmd) if s:shell_error call health#report_error('Failed to run: '. current_npm_cmd, diff --git a/runtime/autoload/provider/node.vim b/runtime/autoload/provider/node.vim index b08ad4f316..67b54c6439 100644 --- a/runtime/autoload/provider/node.vim +++ b/runtime/autoload/provider/node.vim @@ -5,8 +5,32 @@ let g:loaded_node_provider = 1 let s:job_opts = {'rpc': v:true, 'on_stderr': function('provider#stderr_collector')} +" Support for --inspect-brk requires node 6.12+ or 7.6+ or 8+ +" Return 1 if it is supported +" Return 0 otherwise +function! provider#node#can_inspect() + if !executable('node') + return 0 + endif + let node_v = get(split(system(['node', '-v']), "\n"), 0, '') + if v:shell_error || node_v[0] !=# 'v' + return 0 + endif + " [major, minor, patch] + let node_v = split(node_v[1:], '\.') + return len(node_v) == 3 && ( + \ (node_v[0] > 7) || + \ (node_v[0] == 7 && node_v[1] >= 6) || + \ (node_v[0] == 6 && node_v[1] >= 12) + \ ) +endfunction + function! provider#node#Detect() abort - return has('win32') ? exepath('neovim-node-host.cmd') : exepath('neovim-node-host') + let global_modules = get(split(system('npm root -g'), "\n"), 0, '') + if v:shell_error || !isdirectory(global_modules) + return '' + endif + return glob(global_modules . '/neovim/bin/cli.js') endfunction function! provider#node#Prog() @@ -19,18 +43,14 @@ function! provider#node#Require(host) abort return endif - if has('win32') - let args = provider#node#Prog() - else - let args = ['node'] + let args = ['node'] - if !empty($NVIM_NODE_HOST_DEBUG) - call add(args, '--inspect-brk') - endif - - call add(args , provider#node#Prog()) + if !empty($NVIM_NODE_HOST_DEBUG) && provider#node#can_inspect() + call add(args, '--inspect-brk') endif + call add(args, provider#node#Prog()) + try let channel_id = jobstart(args, s:job_opts) if rpcrequest(channel_id, 'poll') ==# 'ok' diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index f939567693..bff8d065f8 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -614,7 +614,7 @@ local function new_pipename() end local function missing_provider(provider) - if provider == 'ruby' then + if provider == 'ruby' or provider == 'node' then local prog = funcs['provider#' .. provider .. '#Detect']() return prog == '' and (provider .. ' not detected') or false elseif provider == 'python' or provider == 'python3' then diff --git a/test/functional/provider/nodejs_spec.lua b/test/functional/provider/nodejs_spec.lua new file mode 100644 index 0000000000..9423243607 --- /dev/null +++ b/test/functional/provider/nodejs_spec.lua @@ -0,0 +1,70 @@ +local helpers = require('test.functional.helpers')(after_each) +local eq, clear = helpers.eq, helpers.clear +local missing_provider = helpers.missing_provider +local command = helpers.command +local write_file = helpers.write_file +local eval = helpers.eval +local sleep = helpers.sleep +local funcs = helpers.funcs +local retry = helpers.retry + +do + clear() + if missing_provider('node') then + pending( + "Cannot find the neovim nodejs host. Try :checkhealth", + function() end) + return + end +end + +before_each(function() + clear() +end) + +describe('nodejs', function() + it('can inspect', function() + eq(1, funcs['provider#node#can_inspect']()) + end) +end) + +describe('nodejs host', function() + teardown(function () + os.remove('Xtest-nodejs-hello.js') + os.remove('Xtest-nodejs-hello-plugin.js') + end) + + it('works', function() + local fname = 'Xtest-nodejs-hello.js' + write_file(fname, [[ + const socket = process.env.NVIM_LISTEN_ADDRESS; + const neovim = require('neovim'); + const nvim = neovim.attach({socket: socket}); + nvim.command('let g:job_out = "hello"'); + nvim.command('call jobstop(g:job_id)'); + ]]) + command('let g:job_id = jobstart(["node", "'..fname..'"])') + retry(nil, 1000, function() eq('hello', eval('g:job_out')) end) + end) + it('plugin works', function() + local fname = 'Xtest-nodejs-hello-plugin.js' + write_file(fname, [[ + const socket = process.env.NVIM_LISTEN_ADDRESS; + const neovim = require('neovim'); + const nvim = neovim.attach({socket: socket}); + + class TestPlugin { + hello() { + this.nvim.command('let g:job_out = "hello-plugin"') + } + } + + const PluginClass = neovim.Plugin(TestPlugin); + const plugin = new PluginClass(nvim); + plugin.hello(); + nvim.command('call jobstop(g:job_id)'); + ]]) + command('let g:job_id = jobstart(["node", "'..fname..'"])') + retry(nil, 1000, function() eq('hello-plugin', eval('g:job_out')) end) + end) +end)