aboutsummaryrefslogtreecommitdiff
path: root/test/functional/legacy/012_directory_spec.lua
blob: dd207ca0b47fcae6a5abcac8de7c90c43086dd46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
-- Tests for 'directory' option.
-- - ".", in same dir as file
-- - "./dir", in directory relative to file
-- - "dir", in directory relative to current dir

local helpers = require('test.functional.helpers')(after_each)
local lfs = require('lfs')

local eq = helpers.eq
local neq = helpers.neq
local poke_eventloop = helpers.poke_eventloop
local funcs = helpers.funcs
local meths = helpers.meths
local clear = helpers.clear
local insert = helpers.insert
local command = helpers.command
local write_file = helpers.write_file
local curbufmeths = helpers.curbufmeths
local expect_exit = helpers.expect_exit

local function ls_dir_sorted(dirname)
  local files = {}
  for f in lfs.dir(dirname) do
    if f ~= "." and f~= ".." then
      table.insert(files, f)
    end
  end
  table.sort(files)
  return files
end

describe("'directory' option", function()
  setup(function()
    local text = [[
      start of testfile
      line 2 Abcdefghij
      line 3 Abcdefghij
      end of testfile
      ]]
    write_file('Xtest1', text)
    lfs.mkdir('Xtest.je')
    lfs.mkdir('Xtest2')
    write_file('Xtest2/Xtest3', text)
    clear()
  end)
  teardown(function()
    expect_exit(command, 'qall!')
    helpers.rmdir('Xtest.je')
    helpers.rmdir('Xtest2')
    os.remove('Xtest1')
  end)

  it('is working', function()
    insert([[
      start of testfile
      line 2 Abcdefghij
      line 3 Abcdefghij
      end of testfile]])

    meths.set_option('swapfile', true)
    curbufmeths.set_option('swapfile', true)
    meths.set_option('directory', '.')

    -- sanity check: files should not exist yet.
    eq(nil, lfs.attributes('.Xtest1.swp'))

    command('edit! Xtest1')
    poke_eventloop()
    eq('Xtest1', funcs.buffer_name('%'))
    -- Verify that the swapfile exists. In the legacy test this was done by
    -- reading the output from :!ls.
    neq(nil, lfs.attributes('.Xtest1.swp'))

    meths.set_option('directory', './Xtest2,.')
    command('edit Xtest1')
    poke_eventloop()

    -- swapfile should no longer exist in CWD.
    eq(nil, lfs.attributes('.Xtest1.swp'))

    eq({ "Xtest1.swp", "Xtest3" }, ls_dir_sorted("Xtest2"))

    meths.set_option('directory', 'Xtest.je')
    command('bdelete')
    command('edit Xtest2/Xtest3')
    eq(true, curbufmeths.get_option('swapfile'))
    poke_eventloop()

    eq({ "Xtest3" }, ls_dir_sorted("Xtest2"))
    eq({ "Xtest3.swp" }, ls_dir_sorted("Xtest.je"))
  end)
end)