diff options
author | Lewis Russell <lewis6991@gmail.com> | 2023-06-07 13:52:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-07 13:52:23 +0100 |
commit | c0952e62fd0ee16a3275bb69e0de04c836b39015 (patch) | |
tree | ebecfe9f07b4e5d5a306b83a886372da269d38f9 /test/functional/lua/system_spec.lua | |
parent | 4ecc71f6fc7377403ed91ae5bc32992a5d08f678 (diff) | |
download | rneovim-c0952e62fd0ee16a3275bb69e0de04c836b39015.tar.gz rneovim-c0952e62fd0ee16a3275bb69e0de04c836b39015.tar.bz2 rneovim-c0952e62fd0ee16a3275bb69e0de04c836b39015.zip |
feat(lua): add `vim.system()`
feat(lua): add vim.system()
Problem:
Handling system commands in Lua is tedious and error-prone:
- vim.fn.jobstart() is vimscript and comes with all limitations attached to typval.
- vim.loop.spawn is too low level
Solution:
Add vim.system().
Partly inspired by Python's subprocess module
Does not expose any libuv objects.
Diffstat (limited to 'test/functional/lua/system_spec.lua')
-rw-r--r-- | test/functional/lua/system_spec.lua | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/test/functional/lua/system_spec.lua b/test/functional/lua/system_spec.lua new file mode 100644 index 0000000000..836d3a83b0 --- /dev/null +++ b/test/functional/lua/system_spec.lua @@ -0,0 +1,57 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local exec_lua = helpers.exec_lua +local eq = helpers.eq + +local function system_sync(cmd, opts) + return exec_lua([[ + return vim.system(...):wait() + ]], cmd, opts) +end + +local function system_async(cmd, opts) + exec_lua([[ + local cmd, opts = ... + _G.done = false + vim.system(cmd, opts, function(obj) + _G.done = true + _G.ret = obj + end) + ]], cmd, opts) + + while true do + if exec_lua[[return _G.done]] then + break + end + end + + return exec_lua[[return _G.ret]] +end + +describe('vim.system', function() + before_each(function() + clear() + end) + + for name, system in pairs{ sync = system_sync, async = system_async, } do + describe('('..name..')', function() + it('can run simple commands', function() + eq('hello\n', system({'echo', 'hello' }, { text = true }).stdout) + end) + + it('handle input', function() + eq('hellocat', system({ 'cat' }, { stdin = 'hellocat', text = true }).stdout) + end) + + it ('supports timeout', function() + eq({ + code = 0, + signal = 2, + stdout = '', + stderr = "Command timed out: 'sleep 10'" + }, system({ 'sleep', '10' }, { timeout = 1 })) + end) + end) + end + +end) |