blob: 497fa0bc7f4fe8fd3277f934b39c2dce642984fe [file] [log] [blame]
Bram Moolenaarb40c2572019-10-19 21:01:05 +02001" Tests for the writefile() function and some :write commands.
Bram Moolenaar19a16692016-09-01 22:19:47 +02002
Bram Moolenaarea3db912020-02-02 15:32:13 +01003source check.vim
4
Bram Moolenaar8cf91282017-06-13 19:38:37 +02005func Test_writefile()
Bram Moolenaar19a16692016-09-01 22:19:47 +02006 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)
Bram Moolenaar18a2b872020-03-19 13:08:45 +010019
20 call assert_fails('call writefile("text", "Xfile")', 'E475: Invalid argument: writefile() first argument must be a List or a Blob')
Bram Moolenaar8cf91282017-06-13 19:38:37 +020021endfunc
22
Bram Moolenaarb40c2572019-10-19 21:01:05 +020023func Test_writefile_ignore_regexp_error()
24 write Xt[z-a]est.txt
25 call delete('Xt[z-a]est.txt')
26endfunc
27
Bram Moolenaar8cf91282017-06-13 19:38:37 +020028func 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
Bram Moolenaare6bf6552017-06-27 22:11:51 +020043
44func Test_writefile_fails_conversion()
Bram Moolenaar39536dd2019-01-29 22:58:21 +010045 if !has('iconv') || has('sun')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020046 return
47 endif
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020048 " Without a backup file the write won't happen if there is a conversion
49 " error.
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020050 set nobackup nowritebackup backupdir=. backupskip=
Bram Moolenaare6bf6552017-06-27 22:11:51 +020051 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"])
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020056 call assert_fails('write ++enc=cp932', 'E513:')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020057 call assert_equal(contents, readfile('Xfile'))
58
59 call delete('Xfile')
60 bwipe!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020061 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaare6bf6552017-06-27 22:11:51 +020062endfunc
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020063
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020064func 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
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020070 set nobackup writebackup backupdir=. backupskip=
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020071 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!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020083 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020084endfunc
85
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020086func 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
Bram Moolenaar7567d0b2017-11-16 23:04:15 +0100129
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
Bram Moolenaar83799a72017-11-25 17:24:09 +0100136
137func Test_writefile_sync_dev_stdout()
138 if !has('unix')
139 return
140 endif
Bram Moolenaar9980b372018-04-21 20:12:35 +0200141 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
Bram Moolenaar83799a72017-11-25 17:24:09 +0100147endfunc
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +0200148
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
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100186
187" Test for ':w !<cmd>' to pipe lines from the current buffer to an external
188" command.
189func Test_write_pipe_to_cmd()
Bram Moolenaarea3db912020-02-02 15:32:13 +0100190 CheckUnix
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100191 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')
210endfunc
211
212func Test_write_errors()
213 " Test for writing partial buffer
214 call writefile(['L1', 'L2', 'L3'], 'Xfile')
215 new Xfile
216 call assert_fails('1,2write', 'E140:')
217 close!
218
Bram Moolenaar4f5776c2020-02-12 22:15:19 +0100219 call assert_fails('w > Xtest', 'E494:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100220
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100221 " Try to overwrite a directory
222 if has('unix')
223 call mkdir('Xdir1')
224 call assert_fails('write Xdir1', 'E17:')
225 call delete('Xdir1', 'd')
226 endif
227
228 " Test for :wall for a buffer with no name
229 enew | only
230 call setline(1, ['L1'])
231 call assert_fails('wall', 'E141:')
232 enew!
233
234 " Test for writing a 'readonly' file
235 new Xfile
236 set readonly
237 call assert_fails('write', 'E45:')
238 close
239
240 " Test for writing to a read-only file
241 new Xfile
242 call setfperm('Xfile', 'r--r--r--')
243 call assert_fails('write', 'E505:')
244 call setfperm('Xfile', 'rw-rw-rw-')
245 close
246
247 call delete('Xfile')
248endfunc
249
250" vim: shiftwidth=2 sts=2 expandtab