robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
1.8 KiB64 linesraw
1local t = require('test.unit.testutil')
2local describe, before_each = t.describe, t.before_each
3local itp = t.gen_itp(t.it)
4
5local eq = t.eq
6local neq = t.neq
7local cimport = t.cimport
8local child_call_once = t.child_call_once
9local child_cleanup_once = t.child_cleanup_once
10
11local lib = cimport('./src/nvim/os/os.h', './src/nvim/fileio.h')
12
13describe('tempfile related functions', function()
14  before_each(function()
15    local function vim_deltempdir()
16      lib.vim_deltempdir()
17    end
18    child_call_once(vim_deltempdir)
19    child_cleanup_once(vim_deltempdir)
20  end)
21
22  local vim_gettempdir = function()
23    return t.ffi.string(lib.vim_gettempdir())
24  end
25
26  describe('vim_gettempdir', function()
27    itp('returns path to Nvim own temp directory', function()
28      local dir = vim_gettempdir()
29      t.ok(dir ~= nil and dir:len() > 0)
30      -- os_file_is_writable returns 2 for a directory which we have rights
31      -- to write into.
32      eq(2, lib.os_file_is_writable(t.to_cstr(dir)))
33      for entry in vim.fs.dir(dir) do
34        t.ok(entry == '.' or entry == '..')
35      end
36    end)
37
38    itp('returns the same directory on each call', function()
39      eq(vim_gettempdir(), vim_gettempdir())
40    end)
41  end)
42
43  describe('vim_tempname', function()
44    local vim_tempname = function()
45      return t.ffi.string(lib.vim_tempname())
46    end
47
48    itp('generate name of non-existing file', function()
49      local file = vim_tempname()
50      assert(file)
51      assert(not lib.os_path_exists(file))
52    end)
53
54    itp('generate different names on each call', function()
55      neq(vim_tempname(), vim_tempname())
56    end)
57
58    itp('generate file name in Nvim own temp directory', function()
59      local dir = vim_gettempdir()
60      local file = vim_tempname()
61      eq(dir, string.sub(file, 1, string.len(dir)))
62    end)
63  end)
64end)