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