blob: 4b0a6d6c738f748e4d8c0fe77cf14833fcfbb34c [file] [log] [blame]
Bram Moolenaar8cf91282017-06-13 19:38:37 +02001" Tests for the writefile() function.
Bram Moolenaar19a16692016-09-01 22:19:47 +02002
Bram Moolenaar8cf91282017-06-13 19:38:37 +02003func Test_writefile()
Bram Moolenaar19a16692016-09-01 22:19:47 +02004 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)
Bram Moolenaar8cf91282017-06-13 19:38:37 +020017endfunc
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
Bram Moolenaare6bf6552017-06-27 22:11:51 +020034
35func Test_writefile_fails_conversion()
Bram Moolenaar39536dd2019-01-29 22:58:21 +010036 if !has('iconv') || has('sun')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020037 return
38 endif
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020039 " Without a backup file the write won't happen if there is a conversion
40 " error.
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020041 set nobackup nowritebackup backupdir=. backupskip=
Bram Moolenaare6bf6552017-06-27 22:11:51 +020042 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"])
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020047 call assert_fails('write ++enc=cp932', 'E513:')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020048 call assert_equal(contents, readfile('Xfile'))
49
50 call delete('Xfile')
51 bwipe!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020052 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaare6bf6552017-06-27 22:11:51 +020053endfunc
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020054
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020055func 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
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020061 set nobackup writebackup backupdir=. backupskip=
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020062 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!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020074 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020075endfunc
76
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020077func 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
Bram Moolenaar7567d0b2017-11-16 23:04:15 +0100120
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
Bram Moolenaar83799a72017-11-25 17:24:09 +0100127
128func Test_writefile_sync_dev_stdout()
129 if !has('unix')
130 return
131 endif
Bram Moolenaar9980b372018-04-21 20:12:35 +0200132 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
Bram Moolenaar83799a72017-11-25 17:24:09 +0100138endfunc
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +0200139
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