neovim

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

tempfile_spec.lua (1796B)


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