aboutsummaryrefslogtreecommitdiff
path: root/test/unit/tempfile_spec.lua
blob: 27bd8729f911cedb551a540ae04509115152b938 (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
local lfs = require('lfs')
local helpers = require('test.unit.helpers')(after_each)
local itp = helpers.gen_itp(it)

local eq = helpers.eq
local neq = helpers.neq
local cimport = helpers.cimport
local deferred_call = helpers.deferred_call
local separate_cleanup = helpers.separate_cleanup

local lib = cimport('./src/nvim/os/os.h', './src/nvim/fileio.h')

describe('tempfile related functions', function()
  before_each(deferred_call(function()
    lib.vim_deltempdir()
  end))
  separate_cleanup(function()
    lib.vim_deltempdir()
  end)

  local vim_gettempdir = function()
    return helpers.ffi.string(lib.vim_gettempdir())
  end

  describe('vim_gettempdir', function()
    itp('returns path to Neovim own temp directory', function()
      local dir = vim_gettempdir()
      assert.True(dir ~= nil and dir:len() > 0)
      -- os_file_is_writable returns 2 for a directory which we have rights
      -- to write into.
      eq(lib.os_file_is_writable(helpers.to_cstr(dir)), 2)
      for entry in lfs.dir(dir) do
        assert.True(entry == '.' or entry == '..')
      end
    end)

    itp('returns the same directory on each call', function()
      local dir1 = vim_gettempdir()
      local dir2 = vim_gettempdir()
      eq(dir1, dir2)
    end)
  end)

  describe('vim_tempname', function()
    local vim_tempname = function()
      return helpers.ffi.string(lib.vim_tempname())
    end

    itp('generate name of non-existing file', function()
      local file = vim_tempname()
      assert.truthy(file)
      assert.False(lib.os_path_exists(file))
    end)

    itp('generate different names on each call', function()
      local fst = vim_tempname()
      local snd = vim_tempname()
      neq(fst, snd)
    end)

    itp('generate file name in Neovim own temp directory', function()
      local dir = vim_gettempdir()
      local file = vim_tempname()
      assert.truthy(file:find('^' .. dir .. '[^/]*$'))
    end)
  end)
end)