aboutsummaryrefslogtreecommitdiff
path: root/test/unit/os/shell_spec.lua
blob: 870034aad99ba1b7fffddec90b4dd5c6bd3bc4ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- not all operating systems support the system()-tests, as of yet.
local allowed_os = {
  Linux = true,
  OSX = true,
  BSD = true,
  POSIX = true
}

if allowed_os[jit.os] ~= true then
  return
end

local helpers = require('test.unit.helpers')
local shell = helpers.cimport(
  './src/nvim/os/shell.h',
  './src/nvim/option_defs.h',
  './src/nvim/os/event.h',
  './src/nvim/misc1.h'
)
local ffi, eq, neq = helpers.ffi, helpers.eq, helpers.neq
local intern = helpers.internalize
local to_cstr = helpers.to_cstr
local NULL = ffi.cast('void *', 0)

describe('shell functions', function()
  setup(function()
    -- the logging functions are complain if I don't do this
    shell.init_homedir()

    shell.event_init()

    -- os_system() can't work when the p_sh and p_shcf variables are unset
    shell.p_sh = to_cstr('/bin/bash')
    shell.p_shcf = to_cstr('-c')
  end)

  teardown(function()
    shell.event_teardown()
  end)

  local function os_system(cmd, input)
    local input_or = input and to_cstr(input) or NULL
    local input_len = (input ~= nil) and string.len(input) or 0
    local output = ffi.new('char *[1]')
    local nread = ffi.new('size_t[1]')

    local status = shell.os_system(to_cstr(cmd), input_or, input_len, output, nread)

    return status, intern(output[0], nread[0])
  end

  describe('os_system', function()
    it('can echo some output (shell builtin)', function()
      local cmd, text = 'echo -n', 'some text'
      local status, output = os_system(cmd .. ' ' .. text)
      eq(text, output)
      eq(0, status)
    end)

    it('can deal with empty output', function()
      local cmd = 'echo -n'
      local status, output = os_system(cmd)
      eq('', output)
      eq(0, status)
    end)

    it('can pass input on stdin', function()
      local cmd, input = 'cat -', 'some text\nsome other text'
      local status, output = os_system(cmd, input)
      eq(input, output)
      eq(0, status)
    end)
  end)
end)