Repository: neovim/neovim
File: test/functional/ex_cmds/write_spec.lua (Lua)

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

    -- Blocks until a consumer reads the FIFO.
    feed_command("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)

  it("creates missing parent directories", function()
    command("write ++p test/write/p_opt.txt")
    source([[
      edit test/write/p_opt.txt
      call setline(1, ['line1'])
      write
    ]])
    eq(eval("['line1']"), eval("readfile('test/write/p_opt.txt')"))
  end)

  it('errors out correctly', function()
    if isCI('cirrus') then
      pending('FIXME: cirrus')
    end
    command('let $HOME=""')
    eq(funcs.fnamemodify('.', ':p:h'), funcs.fnamemodify('.', ':p:h:~'))
    -- Message from check_overwrite
    if not iswin() then
      eq(('Vim(write):E17: "'..funcs.fnamemodify('.', ':p:h')..'" is a directory'),
        pcall_err(command, 'write .'))
    end
    meths.set_option('writeany', true)
    -- Message from buf_write
    eq(('Vim(write):E502: "." is a directory'), pcall_err(command, 'write .'))
    funcs.mkdir(fname_bak)
    meths.set_option('backupdir', '.')
    meths.set_option('backup', true)
    write_file(fname, 'content0')
    command('edit ' .. fname)
    funcs.setline(1, 'TTY')
    eq('Vim(write):E510: Can\'t make backup file (add ! to override)',
       pcall_err(command, 'write'))
    meths.set_option('backup', false)
    funcs.setfperm(fname, 'r--------')
    eq('Vim(write):E505: "Xtest-functional-ex_cmds-write" is read-only (add ! to override)',
       pcall_err(command, 'write'))
    if iswin() then
      eq(0, os.execute('del /q/f ' .. fname))

Review comment (bug):
Let's also test some edge cases like:

```
command("write ++p test/")
command("write ++p /")
command("write ++p .")
command("write ++p ./p_opt.txt")
command("write ++p p_opt.txt")
```

These could live in the same it().

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

    -- Blocks until a consumer reads the FIFO.
    feed_command("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)

  it("++p creates missing parent directories", function()
    eq(0, eval("filereadable('p_opt.txt')"))
    command("write ++p p_opt.txt")
    eq(1, eval("filereadable('p_opt.txt')"))
    os.remove("p_opt.txt")

    eq(0, eval("filereadable('p_opt.txt')"))
    command("write ++p ./p_opt.txt")
    eq(1, eval("filereadable('p_opt.txt')"))
    os.remove("p_opt.txt")

    eq(0, eval("filereadable('test/write/p_opt.txt')"))
    command("write ++p test/write/p_opt.txt")
    eq(1, eval("filereadable('test/write/p_opt.txt')"))

    eq(('Vim(write):E32: No file name'), pcall_err(command, 'write ++p test_write/'))
    if not iswin() then
      eq(('Vim(write):E17: "'..funcs.fnamemodify('.', ':p:h')..'" is a directory'),
        pcall_err(command, 'write ++p .'))
      eq(('Vim(write):E17: "'..funcs.fnamemodify('.', ':p:h')..'" is a directory'),
        pcall_err(command, 'write ++p ./'))
    end
  end)

  it('errors out correctly', function()
    if isCI('cirrus') then
      pending('FIXME: cirrus')
    end
    command('let $HOME=""')
    eq(funcs.fnamemodify('.', ':p:h'), funcs.fnamemodify('.', ':p:h:~'))
    -- Message from check_overwrite
    if not iswin() then
      eq(('Vim(write):E17: "'..funcs.fnamemodify('.', ':p:h')..'" is a directory'),
        pcall_err(command, 'write .'))
    end
    meths.set_option('writeany', true)
    -- Message from buf_write
    eq(('Vim(write):E502: "." is a directory'), pcall_err(command, 'write .'))