robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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