Repository: neovim/neovim
File: test/functional/vimscript/writefile_spec.lua (Lua)

Code before:
    eq('\0a\0b\0', read_file(fname))
    eq(0, funcs.writefile({'a\n\n\nb'}, fname, 'b'))
    eq('a\0\0\0b', read_file(fname))
  end)

  it('writes with s and S', function()
    eq(0, funcs.writefile({'\na\nb\n'}, fname, 'bs'))
    eq('\0a\0b\0', read_file(fname))
    eq(0, funcs.writefile({'a\n\n\nb'}, fname, 'bS'))
    eq('a\0\0\0b', read_file(fname))
  end)

  it('correctly overwrites file', function()
    eq(0, funcs.writefile({'\na\nb\n'}, fname, 'b'))
    eq('\0a\0b\0', read_file(fname))
    eq(0, funcs.writefile({'a\n'}, fname, 'b'))
    eq('a\0', read_file(fname))
  end)

  it('shows correct file name when supplied numbers', function()
    meths.set_current_dir(dname)
    eq('Vim(call):E482: Can\'t open file 2 for writing: illegal operation on a directory',
       pcall_err(command, ('call writefile([42], %s)'):format(ddname_tail)))
  end)

  it('creates missing parent directories', function()
    os.remove(dname)
    eq(nil, read_file(dfname))
    eq(0, funcs.writefile({'abc', 'def', 'ghi'}, dfname, 'p'))
    eq('abc\ndef\nghi\n', read_file(dfname))
    os.remove(dfname)
    os.remove(dname)
    eq(nil, read_file(dfname))
    eq(0, funcs.writefile({'\na\nb\n'}, dfname, 'pb'))
    eq('\0a\0b\0', read_file(dfname))
    os.remove(dfname)
    os.remove(dname)
    eq('Vim(call):E32: No file name',
      pcall_err(command, ('call writefile([], "%s", "p")'):format(dfname .. '.d/')))
    eq(('Vim(call):E482: Can\'t open file ./ for writing: illegal operation on a directory'),
      pcall_err(command, 'call writefile([], "./", "p")'))
    eq(('Vim(call):E482: Can\'t open file . for writing: illegal operation on a directory'),
      pcall_err(command, 'call writefile([], ".", "p")'))
  end)

  it('errors out with invalid arguments', function()
    write_file(fname, 'TEST')
    eq('Vim(call):E119: Not enough arguments for function: writefile',
       pcall_err(command, 'call writefile()'))
    eq('Vim(call):E119: Not enough arguments for function: writefile',
       pcall_err(command, 'call writefile([])'))

Review comment (suggestion):
```suggestion
  it('writefile(...,"p") creates missing parent directories', function()
```

Code after:
    eq('\0a\0b\0', read_file(fname))
    eq(0, funcs.writefile({'a\n\n\nb'}, fname, 'b'))
    eq('a\0\0\0b', read_file(fname))
  end)

  it('writes with s and S', function()
    eq(0, funcs.writefile({'\na\nb\n'}, fname, 'bs'))
    eq('\0a\0b\0', read_file(fname))
    eq(0, funcs.writefile({'a\n\n\nb'}, fname, 'bS'))
    eq('a\0\0\0b', read_file(fname))
  end)

  it('correctly overwrites file', function()
    eq(0, funcs.writefile({'\na\nb\n'}, fname, 'b'))
    eq('\0a\0b\0', read_file(fname))
    eq(0, funcs.writefile({'a\n'}, fname, 'b'))
    eq('a\0', read_file(fname))
  end)

  it('shows correct file name when supplied numbers', function()
    meths.set_current_dir(dname)
    eq('Vim(call):E482: Can\'t open file 2 for writing: illegal operation on a directory',
       pcall_err(command, ('call writefile([42], %s)'):format(ddname_tail)))
  end)

  it('writefile(..., "p") creates missing parent directories', function()
    os.remove(dname)
    eq(nil, read_file(dfname))
    eq(0, funcs.writefile({'abc', 'def', 'ghi'}, dfname, 'p'))
    eq('abc\ndef\nghi\n', read_file(dfname))
    os.remove(dfname)
    os.remove(dname)
    eq(nil, read_file(dfname))
    eq(0, funcs.writefile({'\na\nb\n'}, dfname, 'pb'))
    eq('\0a\0b\0', read_file(dfname))
    os.remove(dfname)
    os.remove(dname)
    eq('Vim(call):E32: No file name',
      pcall_err(command, ('call writefile([], "%s", "p")'):format(dfname .. '.d/')))
    eq(('Vim(call):E482: Can\'t open file ./ for writing: illegal operation on a directory'),
    pcall_err(command, 'call writefile([], "./", "p")'))
    eq(('Vim(call):E482: Can\'t open file . for writing: illegal operation on a directory'),
      pcall_err(command, 'call writefile([], ".", "p")'))
  end)

  it('errors out with invalid arguments', function()
    write_file(fname, 'TEST')
    eq('Vim(call):E119: Not enough arguments for function: writefile',
       pcall_err(command, 'call writefile()'))
    eq('Vim(call):E119: Not enough arguments for function: writefile',
       pcall_err(command, 'call writefile([])'))