tempfile_spec.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Nvim 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. eq(vim_gettempdir(), vim_gettempdir())
  34. end)
  35. end)
  36. describe('vim_tempname', function()
  37. local vim_tempname = function()
  38. return helpers.ffi.string(lib.vim_tempname())
  39. end
  40. itp('generate name of non-existing file', function()
  41. local file = vim_tempname()
  42. assert.truthy(file)
  43. assert.False(lib.os_path_exists(file))
  44. end)
  45. itp('generate different names on each call', function()
  46. neq(vim_tempname(), vim_tempname())
  47. end)
  48. itp('generate file name in Nvim own temp directory', function()
  49. local dir = vim_gettempdir()
  50. local file = vim_tempname()
  51. eq(string.sub(file, 1, string.len(dir)), dir)
  52. end)
  53. end)
  54. end)