tempfile_spec.lua 1.8 KB

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