delete_spec.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local clear, source = helpers.clear, helpers.source
  3. local eq, eval, command = helpers.eq, helpers.eval, helpers.command
  4. describe('Test for delete()', function()
  5. before_each(clear)
  6. after_each(function()
  7. os.remove('Xfile')
  8. end)
  9. it('file delete', function()
  10. command('split Xfile')
  11. command("call setline(1, ['a', 'b'])")
  12. command('wq')
  13. eq(eval("['a', 'b']"), eval("readfile('Xfile')"))
  14. eq(0, eval("delete('Xfile')"))
  15. eq(-1, eval("delete('Xfile')"))
  16. end)
  17. it('directory delete', function()
  18. command("call mkdir('Xdir1')")
  19. eq(1, eval("isdirectory('Xdir1')"))
  20. eq(0, eval("delete('Xdir1', 'd')"))
  21. eq(0, eval("isdirectory('Xdir1')"))
  22. eq(-1, eval("delete('Xdir1', 'd')"))
  23. end)
  24. it('recursive delete', function()
  25. command("call mkdir('Xdir1')")
  26. command("call mkdir('Xdir1/subdir')")
  27. command("call mkdir('Xdir1/empty')")
  28. command('split Xdir1/Xfile')
  29. command("call setline(1, ['a', 'b'])")
  30. command('w')
  31. command('w Xdir1/subdir/Xfile')
  32. command('close')
  33. eq(1, eval("isdirectory('Xdir1')"))
  34. eq(eval("['a', 'b']"), eval("readfile('Xdir1/Xfile')"))
  35. eq(1, eval("isdirectory('Xdir1/subdir')"))
  36. eq(eval("['a', 'b']"), eval("readfile('Xdir1/subdir/Xfile')"))
  37. eq(1, eval("isdirectory('Xdir1/empty')"))
  38. eq(0, eval("delete('Xdir1', 'rf')"))
  39. eq(0, eval("isdirectory('Xdir1')"))
  40. eq(-1, eval("delete('Xdir1', 'd')"))
  41. end)
  42. it('symlink delete', function()
  43. source([[
  44. split Xfile
  45. call setline(1, ['a', 'b'])
  46. wq
  47. if has('win32')
  48. silent !mklink Xlink Xfile
  49. else
  50. silent !ln -s Xfile Xlink
  51. endif
  52. ]])
  53. if eval('v:shell_error') ~= 0 then
  54. pending('Cannot create symlink')
  55. end
  56. -- Delete the link, not the file
  57. eq(0, eval("delete('Xlink')"))
  58. eq(-1, eval("delete('Xlink')"))
  59. eq(0, eval("delete('Xfile')"))
  60. end)
  61. it('symlink directory delete', function()
  62. command("call mkdir('Xdir1')")
  63. if helpers.iswin() then
  64. command("silent !mklink /j Xlink Xdir1")
  65. else
  66. command("silent !ln -s Xdir1 Xlink")
  67. end
  68. eq(1, eval("isdirectory('Xdir1')"))
  69. eq(1, eval("isdirectory('Xlink')"))
  70. -- Delete the link, not the directory
  71. eq(0, eval("delete('Xlink')"))
  72. eq(-1, eval("delete('Xlink')"))
  73. eq(0, eval("delete('Xdir1', 'd')"))
  74. end)
  75. it('symlink recursive delete', function()
  76. source([[
  77. call mkdir('Xdir3')
  78. call mkdir('Xdir3/subdir')
  79. call mkdir('Xdir4')
  80. split Xdir3/Xfile
  81. call setline(1, ['a', 'b'])
  82. w
  83. w Xdir3/subdir/Xfile
  84. w Xdir4/Xfile
  85. close
  86. if has('win32')
  87. silent !mklink /j Xdir3\Xlink Xdir4
  88. else
  89. silent !ln -s ../Xdir4 Xdir3/Xlink
  90. endif
  91. ]])
  92. eq(1, eval("isdirectory('Xdir3')"))
  93. eq(eval("['a', 'b']"), eval("readfile('Xdir3/Xfile')"))
  94. eq(1, eval("isdirectory('Xdir3/subdir')"))
  95. eq(eval("['a', 'b']"), eval("readfile('Xdir3/subdir/Xfile')"))
  96. eq(1, eval("isdirectory('Xdir4')"))
  97. eq(1, eval("isdirectory('Xdir3/Xlink')"))
  98. eq(eval("['a', 'b']"), eval("readfile('Xdir4/Xfile')"))
  99. eq(0, eval("delete('Xdir3', 'rf')"))
  100. eq(0, eval("isdirectory('Xdir3')"))
  101. eq(-1, eval("delete('Xdir3', 'd')"))
  102. -- symlink is deleted, not the directory it points to
  103. eq(1, eval("isdirectory('Xdir4')"))
  104. eq(eval("['a', 'b']"), eval("readfile('Xdir4/Xfile')"))
  105. eq(0, eval("delete('Xdir4/Xfile')"))
  106. eq(0, eval("delete('Xdir4', 'd')"))
  107. end)
  108. end)