fileio_spec.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local clear = helpers.clear
  3. local command = helpers.command
  4. local eq = helpers.eq
  5. local feed = helpers.feed
  6. local funcs = helpers.funcs
  7. local nvim_prog = helpers.nvim_prog
  8. local request = helpers.request
  9. local retry = helpers.retry
  10. local rmdir = helpers.rmdir
  11. local mkdir = helpers.mkdir
  12. local sleep = helpers.sleep
  13. local read_file = helpers.read_file
  14. local trim = helpers.trim
  15. local currentdir = helpers.funcs.getcwd
  16. local iswin = helpers.iswin
  17. describe('fileio', function()
  18. before_each(function()
  19. end)
  20. after_each(function()
  21. command(':qall!')
  22. os.remove('Xtest_startup_shada')
  23. os.remove('Xtest_startup_file1')
  24. os.remove('Xtest_startup_file1~')
  25. os.remove('Xtest_startup_file2')
  26. os.remove('Xtest_тест.md')
  27. rmdir('Xtest_startup_swapdir')
  28. rmdir('Xtest_backupdir')
  29. end)
  30. it('fsync() codepaths #8304', function()
  31. clear({ args={ '-i', 'Xtest_startup_shada',
  32. '--cmd', 'set directory=Xtest_startup_swapdir' } })
  33. -- These cases ALWAYS force fsync (regardless of 'fsync' option):
  34. -- 1. Idle (CursorHold) with modified buffers (+ 'swapfile').
  35. command('write Xtest_startup_file1')
  36. feed('ifoo<esc>h')
  37. command('write')
  38. eq(0, request('nvim__stats').fsync) -- 'nofsync' is the default.
  39. command('set swapfile')
  40. command('set updatetime=1')
  41. feed('izub<esc>h') -- File is 'modified'.
  42. sleep(3) -- Allow 'updatetime' to expire.
  43. retry(3, nil, function()
  44. eq(1, request('nvim__stats').fsync)
  45. end)
  46. command('set updatetime=9999')
  47. -- 2. Exit caused by deadly signal (+ 'swapfile').
  48. local j = funcs.jobstart({ nvim_prog, '-u', 'NONE', '-i',
  49. 'Xtest_startup_shada', '--headless',
  50. '-c', 'set swapfile',
  51. '-c', 'write Xtest_startup_file2',
  52. '-c', 'put =localtime()', })
  53. sleep(10) -- Let Nvim start.
  54. funcs.jobstop(j) -- Send deadly signal.
  55. -- 3. SIGPWR signal.
  56. -- ??
  57. -- 4. Explicit :preserve command.
  58. command('preserve')
  59. eq(2, request('nvim__stats').fsync)
  60. -- 5. Enable 'fsync' option, write file.
  61. command('set fsync')
  62. feed('ibaz<esc>h')
  63. command('write')
  64. eq(4, request('nvim__stats').fsync)
  65. end)
  66. it('backup #9709', function()
  67. clear({ args={ '-i', 'Xtest_startup_shada',
  68. '--cmd', 'set directory=Xtest_startup_swapdir' } })
  69. command('write Xtest_startup_file1')
  70. feed('ifoo<esc>')
  71. command('set backup')
  72. command('set backupcopy=yes')
  73. command('write')
  74. feed('Abar<esc>')
  75. command('write')
  76. local foobar_contents = trim(read_file('Xtest_startup_file1'))
  77. local bar_contents = trim(read_file('Xtest_startup_file1~'))
  78. eq('foobar', foobar_contents);
  79. eq('foo', bar_contents);
  80. end)
  81. it('backup with full path #11214', function()
  82. clear()
  83. mkdir('Xtest_backupdir')
  84. command('set backup')
  85. command('set backupdir=Xtest_backupdir//')
  86. command('write Xtest_startup_file1')
  87. feed('ifoo<esc>')
  88. command('write')
  89. feed('Abar<esc>')
  90. command('write')
  91. -- Backup filename = fullpath, separators replaced with "%".
  92. local backup_file_name = string.gsub(currentdir()..'/Xtest_startup_file1',
  93. iswin() and '[:/\\]' or '/', '%%') .. '~'
  94. local foo_contents = trim(read_file('Xtest_backupdir/'..backup_file_name))
  95. local foobar_contents = trim(read_file('Xtest_startup_file1'))
  96. eq('foobar', foobar_contents);
  97. eq('foo', foo_contents);
  98. end)
  99. it('readfile() on multibyte filename #10586', function()
  100. clear()
  101. local text = {
  102. 'line1',
  103. ' ...line2... ',
  104. '',
  105. 'line3!',
  106. 'тест yay тест.',
  107. '',
  108. }
  109. local fname = 'Xtest_тест.md'
  110. funcs.writefile(text, fname, 's')
  111. table.insert(text, '')
  112. eq(text, funcs.readfile(fname, 'b'))
  113. end)
  114. end)