1" Tests for the writefile() function and some :write commands.
2
3source check.vim
4
5func Test_writefile()
6  let f = tempname()
7  call writefile(["over","written"], f, "b")
8  call writefile(["hello","world"], f, "b")
9  call writefile(["!", "good"], f, "a")
10  call writefile(["morning"], f, "ab")
11  call writefile(["", "vimmers"], f, "ab")
12  let l = readfile(f)
13  call assert_equal("hello", l[0])
14  call assert_equal("world!", l[1])
15  call assert_equal("good", l[2])
16  call assert_equal("morning", l[3])
17  call assert_equal("vimmers", l[4])
18  call delete(f)
19
20  call assert_fails('call writefile("text", "Xfile")', 'E475: Invalid argument: writefile() first argument must be a List or a Blob')
21endfunc
22
23func Test_writefile_ignore_regexp_error()
24  write Xt[z-a]est.txt
25  call delete('Xt[z-a]est.txt')
26endfunc
27
28func Test_writefile_fails_gently()
29  call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:')
30  call assert_false(filereadable("Xfile"))
31  call delete("Xfile")
32
33  call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:')
34  call assert_false(filereadable("Xfile"))
35  call delete("Xfile")
36
37  call assert_fails('call writefile([], "Xfile", [])', 'E730:')
38  call assert_false(filereadable("Xfile"))
39  call delete("Xfile")
40
41  call assert_fails('call writefile([], [])', 'E730:')
42endfunc
43
44func Test_writefile_fails_conversion()
45  if !has('iconv') || has('sun')
46    return
47  endif
48  " Without a backup file the write won't happen if there is a conversion
49  " error.
50  set nobackup nowritebackup backupdir=. backupskip=
51  new
52  let contents = ["line one", "line two"]
53  call writefile(contents, 'Xfile')
54  edit Xfile
55  call setline(1, ["first line", "cannot convert \u010b", "third line"])
56  call assert_fails('write ++enc=cp932', 'E513:')
57  call assert_equal(contents, readfile('Xfile'))
58
59  call delete('Xfile')
60  bwipe!
61  set backup& writebackup& backupdir&vim backupskip&vim
62endfunc
63
64func Test_writefile_fails_conversion2()
65  if !has('iconv') || has('sun')
66    return
67  endif
68  " With a backup file the write happens even if there is a conversion error,
69  " but then the backup file must remain
70  set nobackup writebackup backupdir=. backupskip=
71  let contents = ["line one", "line two"]
72  call writefile(contents, 'Xfile_conversion_err')
73  edit Xfile_conversion_err
74  call setline(1, ["first line", "cannot convert \u010b", "third line"])
75  set fileencoding=latin1
76  let output = execute('write')
77  call assert_match('CONVERSION ERROR', output)
78  call assert_equal(contents, readfile('Xfile_conversion_err~'))
79
80  call delete('Xfile_conversion_err')
81  call delete('Xfile_conversion_err~')
82  bwipe!
83  set backup& writebackup& backupdir&vim backupskip&vim
84endfunc
85
86func SetFlag(timer)
87  let g:flag = 1
88endfunc
89
90func Test_write_quit_split()
91  " Prevent exiting by splitting window on file write.
92  augroup testgroup
93    autocmd BufWritePre * split
94  augroup END
95  e! Xfile
96  call setline(1, 'nothing')
97  wq
98
99  if has('timers')
100    " timer will not run if "exiting" is still set
101    let g:flag = 0
102    call timer_start(1, 'SetFlag')
103    sleep 50m
104    call assert_equal(1, g:flag)
105    unlet g:flag
106  endif
107  au! testgroup
108  bwipe Xfile
109  call delete('Xfile')
110endfunc
111
112func Test_nowrite_quit_split()
113  " Prevent exiting by opening a help window.
114  e! Xfile
115  help
116  wincmd w
117  exe winnr() . 'q'
118
119  if has('timers')
120    " timer will not run if "exiting" is still set
121    let g:flag = 0
122    call timer_start(1, 'SetFlag')
123    sleep 50m
124    call assert_equal(1, g:flag)
125    unlet g:flag
126  endif
127  bwipe Xfile
128endfunc
129
130func Test_writefile_sync_arg()
131  " This doesn't check if fsync() works, only that the argument is accepted.
132  call writefile(['one'], 'Xtest', 's')
133  call writefile(['two'], 'Xtest', 'S')
134  call delete('Xtest')
135endfunc
136
137func Test_writefile_sync_dev_stdout()
138  if !has('unix')
139    return
140  endif
141  if filewritable('/dev/stdout')
142    " Just check that this doesn't cause an error.
143    call writefile(['one'], '/dev/stdout')
144  else
145    throw 'Skipped: /dev/stdout is not writable'
146  endif
147endfunc
148
149func Test_writefile_autowrite()
150  set autowrite
151  new
152  next Xa Xb Xc
153  call setline(1, 'aaa')
154  next
155  call assert_equal(['aaa'], readfile('Xa'))
156  call setline(1, 'bbb')
157  call assert_fails('edit XX')
158  call assert_false(filereadable('Xb'))
159
160  set autowriteall
161  edit XX
162  call assert_equal(['bbb'], readfile('Xb'))
163
164  bwipe!
165  call delete('Xa')
166  call delete('Xb')
167  set noautowrite
168endfunc
169
170func Test_writefile_autowrite_nowrite()
171  set autowrite
172  new
173  next Xa Xb Xc
174  set buftype=nowrite
175  call setline(1, 'aaa')
176  let buf = bufnr('%')
177  " buffer contents silently lost
178  edit XX
179  call assert_false(filereadable('Xa'))
180  rewind
181  call assert_equal('', getline(1))
182
183  bwipe!
184  set noautowrite
185endfunc
186
187" Test for ':w !<cmd>' to pipe lines from the current buffer to an external
188" command.
189func Test_write_pipe_to_cmd()
190  CheckUnix
191  new
192  call setline(1, ['L1', 'L2', 'L3', 'L4'])
193  2,3w !cat > Xfile
194  call assert_equal(['L2', 'L3'], readfile('Xfile'))
195  close!
196  call delete('Xfile')
197endfunc
198
199" Test for :saveas
200func Test_saveas()
201  call assert_fails('saveas', 'E471:')
202  call writefile(['L1'], 'Xfile')
203  new Xfile
204  new
205  call setline(1, ['L1'])
206  call assert_fails('saveas Xfile', 'E139:')
207  close!
208  enew | only
209  call delete('Xfile')
210
211  call writefile(test_null_list(), 'Xfile')
212  call assert_false(filereadable('Xfile'))
213  call writefile(test_null_blob(), 'Xfile')
214  call assert_false(filereadable('Xfile'))
215  call assert_fails('call writefile([], "")', 'E482:')
216endfunc
217
218func Test_write_errors()
219  " Test for writing partial buffer
220  call writefile(['L1', 'L2', 'L3'], 'Xfile')
221  new Xfile
222  call assert_fails('1,2write', 'E140:')
223  close!
224
225  call assert_fails('w > Xtest', 'E494:')
226
227  " Try to overwrite a directory
228  if has('unix')
229    call mkdir('Xdir1')
230    call assert_fails('write Xdir1', 'E17:')
231    call delete('Xdir1', 'd')
232  endif
233
234  " Test for :wall for a buffer with no name
235  enew | only
236  call setline(1, ['L1'])
237  call assert_fails('wall', 'E141:')
238  enew!
239
240  " Test for writing a 'readonly' file
241  new Xfile
242  set readonly
243  call assert_fails('write', 'E45:')
244  close
245
246  " Test for writing to a read-only file
247  new Xfile
248  call setfperm('Xfile', 'r--r--r--')
249  call assert_fails('write', 'E505:')
250  call setfperm('Xfile', 'rw-rw-rw-')
251  close
252
253  call delete('Xfile')
254
255  call writefile(test_null_list(), 'Xfile')
256  call assert_false(filereadable('Xfile'))
257  call writefile(test_null_blob(), 'Xfile')
258  call assert_false(filereadable('Xfile'))
259  call assert_fails('call writefile([], "")', 'E482:')
260endfunc
261
262" vim: shiftwidth=2 sts=2 expandtab
263