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
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&
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
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!
74endfunc
75
76func SetFlag(timer)
77  let g:flag = 1
78endfunc
79
80func Test_write_quit_split()
81  " Prevent exiting by splitting window on file write.
82  augroup testgroup
83    autocmd BufWritePre * split
84  augroup END
85  e! Xfile
86  call setline(1, 'nothing')
87  wq
88
89  if has('timers')
90    " timer will not run if "exiting" is still set
91    let g:flag = 0
92    call timer_start(1, 'SetFlag')
93    sleep 50m
94    call assert_equal(1, g:flag)
95    unlet g:flag
96  endif
97  au! testgroup
98  bwipe Xfile
99  call delete('Xfile')
100endfunc
101
102func Test_nowrite_quit_split()
103  " Prevent exiting by opening a help window.
104  e! Xfile
105  help
106  wincmd w
107  exe winnr() . 'q'
108
109  if has('timers')
110    " timer will not run if "exiting" is still set
111    let g:flag = 0
112    call timer_start(1, 'SetFlag')
113    sleep 50m
114    call assert_equal(1, g:flag)
115    unlet g:flag
116  endif
117  bwipe Xfile
118endfunc
119
120func Test_writefile_sync_arg()
121  " This doesn't check if fsync() works, only that the argument is accepted.
122  call writefile(['one'], 'Xtest', 's')
123  call writefile(['two'], 'Xtest', 'S')
124  call delete('Xtest')
125endfunc
126
127func Test_writefile_sync_dev_stdout()
128  if !has('unix')
129    return
130  endif
131  if filewritable('/dev/stdout')
132    " Just check that this doesn't cause an error.
133    call writefile(['one'], '/dev/stdout')
134  else
135    throw 'Skipped: /dev/stdout is not writable'
136  endif
137endfunc
138
139func Test_writefile_autowrite()
140  set autowrite
141  new
142  next Xa Xb Xc
143  call setline(1, 'aaa')
144  next
145  call assert_equal(['aaa'], readfile('Xa'))
146  call setline(1, 'bbb')
147  call assert_fails('edit XX')
148  call assert_false(filereadable('Xb'))
149
150  set autowriteall
151  edit XX
152  call assert_equal(['bbb'], readfile('Xb'))
153
154  bwipe!
155  call delete('Xa')
156  call delete('Xb')
157  set noautowrite
158endfunc
159
160func Test_writefile_autowrite_nowrite()
161  set autowrite
162  new
163  next Xa Xb Xc
164  set buftype=nowrite
165  call setline(1, 'aaa')
166  let buf = bufnr('%')
167  " buffer contents silently lost
168  edit XX
169  call assert_false(filereadable('Xa'))
170  rewind
171  call assert_equal('', getline(1))
172
173  bwipe!
174  set noautowrite
175endfunc
176