aboutsummaryrefslogtreecommitdiff
path: root/test/functional/ex_cmds/write_spec.lua
blob: 4ac9f312ef8eca21bc85ad23cfab33e533cfaa18 (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
local helpers = require('test.functional.helpers')(after_each)
local eq, eval, clear, write_file, execute, source, insert =
  helpers.eq, helpers.eval, helpers.clear, helpers.write_file,
  helpers.execute, helpers.source, helpers.insert

if helpers.pending_win32(pending) then return end

describe(':write', function()
  local function cleanup()
    os.remove('test_bkc_file.txt')
    os.remove('test_bkc_link.txt')
    os.remove('test_fifo')
  end
  before_each(function()
    clear()
    cleanup()
  end)
  after_each(function()
    cleanup()
  end)

  it('&backupcopy=auto preserves symlinks', function()
    execute('set backupcopy=auto')
    write_file('test_bkc_file.txt', 'content0')
    execute("silent !ln -s test_bkc_file.txt test_bkc_link.txt")
    source([[
      edit test_bkc_link.txt
      call setline(1, ['content1'])
      write
    ]])
    eq(eval("['content1']"), eval("readfile('test_bkc_file.txt')"))
    eq(eval("['content1']"), eval("readfile('test_bkc_link.txt')"))
  end)

  it('&backupcopy=no replaces symlink with new file', function()
    execute('set backupcopy=no')
    write_file('test_bkc_file.txt', 'content0')
    execute("silent !ln -s test_bkc_file.txt test_bkc_link.txt")
    source([[
      edit test_bkc_link.txt
      call setline(1, ['content1'])
      write
    ]])
    eq(eval("['content0']"), eval("readfile('test_bkc_file.txt')"))
    eq(eval("['content1']"), eval("readfile('test_bkc_link.txt')"))
  end)

  it("appends FIFO file", function()
    if eval("executable('mkfifo')") == 0 then
      pending('missing "mkfifo" command', function()end)
      return
    end

    local text = "some fifo text from write_spec"
    assert(os.execute("mkfifo test_fifo"))
    insert(text)

    -- Blocks until a consumer reads the FIFO.
    execute("write >> test_fifo")

    -- Read the FIFO, this will unblock the :write above.
    local fifo = assert(io.open("test_fifo"))
    eq(text.."\n", fifo:read("*all"))
    fifo:close()
  end)
end)