tempfile_spec.lua 1.9 KB

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