neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

users_spec.lua (2688B)


      1 local t = require('test.unit.testutil')
      2 local itp = t.gen_itp(it)
      3 
      4 local cimport = t.cimport
      5 local eq = t.eq
      6 local ffi = t.ffi
      7 local lib = t.lib
      8 local NULL = t.NULL
      9 local OK = t.OK
     10 local FAIL = t.FAIL
     11 
     12 local users = cimport('./src/nvim/os/os.h', 'unistd.h')
     13 
     14 local function garray_new()
     15  return ffi.new('garray_T[1]')
     16 end
     17 
     18 local function garray_get_len(array)
     19  return array[0].ga_len
     20 end
     21 
     22 local function garray_get_item(array, index)
     23  return (ffi.cast('void **', array[0].ga_data))[index]
     24 end
     25 
     26 describe('users function', function()
     27  -- will probably not work on windows
     28  local current_username = os.getenv('USER')
     29 
     30  describe('os_get_usernames', function()
     31    itp('returns FAIL if called with NULL', function()
     32      eq(FAIL, users.os_get_usernames(NULL))
     33    end)
     34 
     35    itp('fills the names garray with os usernames and returns OK', function()
     36      local ga_users = garray_new()
     37      eq(OK, users.os_get_usernames(ga_users))
     38      local user_count = garray_get_len(ga_users)
     39      assert.is_true(user_count > 0)
     40      local current_username_found = false
     41      for i = 0, user_count - 1 do
     42        local name = ffi.string((garray_get_item(ga_users, i)))
     43        if name == current_username then
     44          current_username_found = true
     45        end
     46      end
     47      assert.is_true(current_username_found)
     48    end)
     49  end)
     50 
     51  describe('os_get_username', function()
     52    itp('should write the username into the buffer and return OK', function()
     53      local name_out = ffi.new('char[100]')
     54      eq(OK, users.os_get_username(name_out, 100))
     55      eq(current_username, ffi.string(name_out))
     56    end)
     57  end)
     58 
     59  describe('os_get_uname', function()
     60    itp('should write the username into the buffer and return OK', function()
     61      local name_out = ffi.new('char[100]')
     62      local user_id = lib.getuid()
     63      eq(OK, users.os_get_uname(user_id, name_out, 100))
     64      eq(current_username, ffi.string(name_out))
     65    end)
     66 
     67    itp('should FAIL if the userid is not found', function()
     68      local name_out = ffi.new('char[100]')
     69      -- hoping nobody has this uid
     70      local user_id = 2342
     71      eq(FAIL, users.os_get_uname(user_id, name_out, 100))
     72      eq('2342', ffi.string(name_out))
     73    end)
     74  end)
     75 
     76  describe('os_get_userdir', function()
     77    itp('should return NULL if called with NULL', function()
     78      eq(NULL, users.os_get_userdir(NULL))
     79    end)
     80 
     81    itp('should return $HOME for the current user', function()
     82      local home = os.getenv('HOME')
     83      eq(home, ffi.string((users.os_get_userdir(current_username))))
     84    end)
     85 
     86    itp('should return NULL if the user is not found', function()
     87      eq(NULL, users.os_get_userdir('neovim_user_not_found_test'))
     88    end)
     89  end)
     90 end)