robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
7.3 KiB222 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
5local mkdir = t.mkdir
6local clear = n.clear
7local eq = t.eq
8local fn = n.fn
9local api = n.api
10local exec_lua = n.exec_lua
11local read_file = t.read_file
12local write_file = t.write_file
13local pcall_err = t.pcall_err
14local command = n.command
15
16local fname = 'Xtest-functional-eval-writefile'
17local dname = fname .. '.d'
18local dfname_tail = '1'
19local dfname = dname .. '/' .. dfname_tail
20local ddname_tail = '2'
21local ddname = dname .. '/' .. ddname_tail
22
23before_each(function()
24  mkdir(dname)
25  mkdir(ddname)
26  clear()
27end)
28
29after_each(function()
30  os.remove(fname)
31  os.remove(dfname)
32  vim.uv.fs_rmdir(ddname)
33  vim.uv.fs_rmdir(dname)
34end)
35
36describe('writefile()', function()
37  it('writes empty list to a file', function()
38    eq(nil, read_file(fname))
39    eq(0, fn.writefile({}, fname))
40    eq('', read_file(fname))
41    os.remove(fname)
42    eq(nil, read_file(fname))
43    eq(0, fn.writefile({}, fname, 'b'))
44    eq('', read_file(fname))
45    os.remove(fname)
46    eq(nil, read_file(fname))
47    eq(0, fn.writefile({}, fname, 'ab'))
48    eq('', read_file(fname))
49    os.remove(fname)
50    eq(nil, read_file(fname))
51    eq(0, fn.writefile({}, fname, 'a'))
52    eq('', read_file(fname))
53  end)
54
55  it('writes list with an empty string to a file', function()
56    command(('call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s", "b")'):format(fname))
57    eq('', read_file(fname))
58    command(('call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s")'):format(fname))
59    eq('\n', read_file(fname))
60  end)
61
62  it('writes list with a null string to a file', function()
63    command(('call writefile([v:_null_string], "%s", "b")'):format(fname))
64    eq('', read_file(fname))
65    command(('call writefile([v:_null_string], "%s")'):format(fname))
66    eq('\n', read_file(fname))
67  end)
68
69  it('writes a null blob to a file', function()
70    eq(0, fn.writefile({ 'line1' }, fname, 'b'))
71    eq('line1', read_file(fname))
72    command(('call writefile(v:_null_blob, "%s")'):format(fname))
73    eq('', read_file(fname))
74  end)
75
76  it('appends to a file', function()
77    eq(nil, read_file(fname))
78    eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, fname))
79    eq('abc\ndef\nghi\n', read_file(fname))
80    eq(0, fn.writefile({ 'jkl' }, fname, 'a'))
81    eq('abc\ndef\nghi\njkl\n', read_file(fname))
82    os.remove(fname)
83    eq(nil, read_file(fname))
84    eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, fname, 'b'))
85    eq('abc\ndef\nghi', read_file(fname))
86    eq(0, fn.writefile({ 'jkl' }, fname, 'ab'))
87    eq('abc\ndef\nghijkl', read_file(fname))
88  end)
89
90  it('correctly treats NLs', function()
91    eq(0, fn.writefile({ '\na\nb\n' }, fname, 'b'))
92    eq('\0a\0b\0', read_file(fname))
93    eq(0, fn.writefile({ 'a\n\n\nb' }, fname, 'b'))
94    eq('a\0\0\0b', read_file(fname))
95  end)
96
97  it('writes with s and S', function()
98    eq(0, fn.writefile({ '\na\nb\n' }, fname, 'bs'))
99    eq('\0a\0b\0', read_file(fname))
100    eq(0, fn.writefile({ 'a\n\n\nb' }, fname, 'bS'))
101    eq('a\0\0\0b', read_file(fname))
102  end)
103
104  it('correctly overwrites file', function()
105    eq(0, fn.writefile({ '\na\nb\n' }, fname, 'b'))
106    eq('\0a\0b\0', read_file(fname))
107    eq(0, fn.writefile({ 'a\n' }, fname, 'b'))
108    eq('a\0', read_file(fname))
109  end)
110
111  it('writes Lua (binary) strings', function()
112    eq(0, exec_lua([[return vim.fn.writefile('foo\0bar', ..., 'b')]], fname))
113    eq('foo\0bar', read_file(fname))
114  end)
115
116  it('writes RPC-API (binary) String', function()
117    eq(0, api.nvim_call_function('writefile', { 'foobar', fname }))
118    eq('foobar', read_file(fname))
119    eq(0, api.nvim_call_function('writefile', { 'foo\0bar', fname }))
120    eq('foo\0bar', read_file(fname))
121  end)
122
123  it('shows correct file name when supplied numbers', function()
124    api.nvim_set_current_dir(dname)
125    eq(
126      "Vim(call):E482: Can't open file 2 for writing: illegal operation on a directory",
127      pcall_err(command, ('call writefile([42], %s)'):format(ddname_tail))
128    )
129  end)
130
131  it('writefile(..., "p") creates missing parent directories', function()
132    os.remove(dname)
133    eq(nil, read_file(dfname))
134    eq(0, fn.writefile({ 'abc', 'def', 'ghi' }, dfname, 'p'))
135    eq('abc\ndef\nghi\n', read_file(dfname))
136    os.remove(dfname)
137    os.remove(dname)
138    eq(nil, read_file(dfname))
139    eq(0, fn.writefile({ '\na\nb\n' }, dfname, 'pb'))
140    eq('\0a\0b\0', read_file(dfname))
141    os.remove(dfname)
142    os.remove(dname)
143    eq(
144      'Vim(call):E32: No file name',
145      pcall_err(command, ('call writefile([], "%s", "p")'):format(dfname .. '.d/'))
146    )
147    eq(
148      "Vim(call):E482: Can't open file ./ for writing: illegal operation on a directory",
149      pcall_err(command, 'call writefile([], "./", "p")')
150    )
151    eq(
152      "Vim(call):E482: Can't open file . for writing: illegal operation on a directory",
153      pcall_err(command, 'call writefile([], ".", "p")')
154    )
155  end)
156
157  it('errors out with invalid arguments', function()
158    write_file(fname, 'TEST')
159    eq(
160      'Vim(call):E119: Not enough arguments for function: writefile',
161      pcall_err(command, 'call writefile()')
162    )
163    eq(
164      'Vim(call):E119: Not enough arguments for function: writefile',
165      pcall_err(command, 'call writefile([])')
166    )
167    eq(
168      'Vim(call):E118: Too many arguments for function: writefile',
169      pcall_err(command, ('call writefile([], "%s", "b", 1)'):format(fname))
170    )
171    for _, arg in ipairs({ '0', '0.0', 'function("tr")', '{}', '"test"' }) do
172      eq(
173        'Vim(call):E475: Invalid argument: writefile() first argument must be a List or a Blob',
174        pcall_err(command, ('call writefile(%s, "%s", "b")'):format(arg, fname))
175      )
176    end
177    for _, args in ipairs({ '[], %s, "b"', '[], "' .. fname .. '", %s' }) do
178      eq(
179        'Vim(call):E730: Using a List as a String',
180        pcall_err(command, ('call writefile(%s)'):format(args:format('[]')))
181      )
182      eq(
183        'Vim(call):E731: Using a Dictionary as a String',
184        pcall_err(command, ('call writefile(%s)'):format(args:format('{}')))
185      )
186      eq(
187        'Vim(call):E729: Using a Funcref as a String',
188        pcall_err(command, ('call writefile(%s)'):format(args:format('function("tr")')))
189      )
190    end
191    eq(
192      'Vim(call):E5060: Unknown flag: «»',
193      pcall_err(command, ('call writefile([], "%s", "bs«»")'):format(fname))
194    )
195    eq('TEST', read_file(fname))
196  end)
197
198  it('does not write to file if error in list', function()
199    local args = '["tset"] + repeat([%s], 3), "' .. fname .. '"'
200    eq(
201      'Vim(call):E805: Expected a Number or a String, Float found',
202      pcall_err(command, ('call writefile(%s)'):format(args:format('0.0')))
203    )
204    eq(nil, read_file(fname))
205    write_file(fname, 'TEST')
206    eq(
207      'Vim(call):E745: Expected a Number or a String, List found',
208      pcall_err(command, ('call writefile(%s)'):format(args:format('[]')))
209    )
210    eq('TEST', read_file(fname))
211    eq(
212      'Vim(call):E728: Expected a Number or a String, Dictionary found',
213      pcall_err(command, ('call writefile(%s)'):format(args:format('{}')))
214    )
215    eq('TEST', read_file(fname))
216    eq(
217      'Vim(call):E703: Expected a Number or a String, Funcref found',
218      pcall_err(command, ('call writefile(%s)'):format(args:format('function("tr")')))
219    )
220    eq('TEST', read_file(fname))
221  end)
222end)