1" Tests for the writefile() function and some :write commands.
2
3func Test_writefile()
4  let f = tempname()
5  call writefile(["over","written"], f, "b")
6  call writefile(["hello","world"], f, "b")
7  call writefile(["!", "good"], f, "a")
8  call writefile(["morning"], f, "ab")
9  call writefile(["", "vimmers"], f, "ab")
10  let l = readfile(f)
11  call assert_equal("hello", l[0])
12  call assert_equal("world!", l[1])
13  call assert_equal("good", l[2])
14  call assert_equal("morning", l[3])
15  call assert_equal("vimmers", l[4])
16  call delete(f)
17endfunc
18
19func Test_writefile_ignore_regexp_error()
20  write Xt[z-a]est.txt
21  call delete('Xt[z-a]est.txt')
22endfunc
23
24func Test_writefile_fails_gently()
25  call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:')
26  call assert_false(filereadable("Xfile"))
27  call delete("Xfile")
28
29  call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:')
30  call assert_false(filereadable("Xfile"))
31  call delete("Xfile")
32
33  call assert_fails('call writefile([], "Xfile", [])', 'E730:')
34  call assert_false(filereadable("Xfile"))
35  call delete("Xfile")
36
37  call assert_fails('call writefile([], [])', 'E730:')
38endfunc
39
40func Test_writefile_fails_conversion()
41  if !has('iconv') || has('sun')
42    return
43  endif
44  " Without a backup file the write won't happen if there is a conversion
45  " error.
46  set nobackup nowritebackup backupdir=. backupskip=
47  new
48  let contents = ["line one", "line two"]
49  call writefile(contents, 'Xfile')
50  edit Xfile
51  call setline(1, ["first line", "cannot convert \u010b", "third line"])
52  call assert_fails('write ++enc=cp932', 'E513:')
53  call assert_equal(contents, readfile('Xfile'))
54
55  call delete('Xfile')
56  bwipe!
57  set backup& writebackup& backupdir&vim backupskip&vim
58endfunc
59
60func Test_writefile_fails_conversion2()
61  if !has('iconv') || has('sun')
62    return
63  endif
64  " With a backup file the write happens even if there is a conversion error,
65  " but then the backup file must remain
66  set nobackup writebackup backupdir=. backupskip=
67  let contents = ["line one", "line two"]
68  call writefile(contents, 'Xfile_conversion_err')
69  edit Xfile_conversion_err
70  call setline(1, ["first line", "cannot convert \u010b", "third line"])
71  set fileencoding=latin1
72  let output = execute('write')
73  call assert_match('CONVERSION ERROR', output)
74  call assert_equal(contents, readfile('Xfile_conversion_err~'))
75
76  call delete('Xfile_conversion_err')
77  call delete('Xfile_conversion_err~')
78  bwipe!
79  set backup& writebackup& backupdir&vim backupskip&vim
80endfunc
81
82func SetFlag(timer)
83  let g:flag = 1
84endfunc
85
86func Test_write_quit_split()
87  " Prevent exiting by splitting window on file write.
88  augroup testgroup
89    autocmd BufWritePre * split
90  augroup END
91  e! Xfile
92  call setline(1, 'nothing')
93  wq
94
95  if has('timers')
96    " timer will not run if "exiting" is still set
97    let g:flag = 0
98    call timer_start(1, 'SetFlag')
99    sleep 50m
100    call assert_equal(1, g:flag)
101    unlet g:flag
102  endif
103  au! testgroup
104  bwipe Xfile
105  call delete('Xfile')
106endfunc
107
108func Test_nowrite_quit_split()
109  " Prevent exiting by opening a help window.
110  e! Xfile
111  help
112  wincmd w
113  exe winnr() . 'q'
114
115  if has('timers')
116    " timer will not run if "exiting" is still set
117    let g:flag = 0
118    call timer_start(1, 'SetFlag')
119    sleep 50m
120    call assert_equal(1, g:flag)
121    unlet g:flag
122  endif
123  bwipe Xfile
124endfunc
125
126func Test_writefile_sync_arg()
127  " This doesn't check if fsync() works, only that the argument is accepted.
128  call writefile(['one'], 'Xtest', 's')
129  call writefile(['two'], 'Xtest', 'S')
130  call delete('Xtest')
131endfunc
132
133func Test_writefile_sync_dev_stdout()
134  if !has('unix')
135    return
136  endif
137  if filewritable('/dev/stdout')
138    " Just check that this doesn't cause an error.
139    call writefile(['one'], '/dev/stdout')
140  else
141    throw 'Skipped: /dev/stdout is not writable'
142  endif
143endfunc
144
145func Test_writefile_autowrite()
146  set autowrite
147  new
148  next Xa Xb Xc
149  call setline(1, 'aaa')
150  next
151  call assert_equal(['aaa'], readfile('Xa'))
152  call setline(1, 'bbb')
153  call assert_fails('edit XX')
154  call assert_false(filereadable('Xb'))
155
156  set autowriteall
157  edit XX
158  call assert_equal(['bbb'], readfile('Xb'))
159
160  bwipe!
161  call delete('Xa')
162  call delete('Xb')
163  set noautowrite
164endfunc
165
166func Test_writefile_autowrite_nowrite()
167  set autowrite
168  new
169  next Xa Xb Xc
170  set buftype=nowrite
171  call setline(1, 'aaa')
172  let buf = bufnr('%')
173  " buffer contents silently lost
174  edit XX
175  call assert_false(filereadable('Xa'))
176  rewind
177  call assert_equal('', getline(1))
178
179  bwipe!
180  set noautowrite
181endfunc
182