blob: 8eb2e7809f7cfc7c57a28189b83028be332dbc7d [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 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
Bram Moolenaarb40c2572019-10-19 21:01:05 +020019func Test_writefile_ignore_regexp_error()
20 write Xt[z-a]est.txt
21 call delete('Xt[z-a]est.txt')
22endfunc
23
Bram Moolenaar8cf91282017-06-13 19:38:37 +020024func 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
Bram Moolenaare6bf6552017-06-27 22:11:51 +020039
40func Test_writefile_fails_conversion()
Bram Moolenaar39536dd2019-01-29 22:58:21 +010041 if !has('iconv') || has('sun')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020042 return
43 endif
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020044 " Without a backup file the write won't happen if there is a conversion
45 " error.
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020046 set nobackup nowritebackup backupdir=. backupskip=
Bram Moolenaare6bf6552017-06-27 22:11:51 +020047 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"])
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020052 call assert_fails('write ++enc=cp932', 'E513:')
Bram Moolenaare6bf6552017-06-27 22:11:51 +020053 call assert_equal(contents, readfile('Xfile'))
54
55 call delete('Xfile')
56 bwipe!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020057 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaare6bf6552017-06-27 22:11:51 +020058endfunc
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020059
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020060func 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
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020066 set nobackup writebackup backupdir=. backupskip=
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020067 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!
Bram Moolenaarc28cb5b2019-05-31 20:42:09 +020079 set backup& writebackup& backupdir&vim backupskip&vim
Bram Moolenaarcf0bfd92019-05-18 18:52:04 +020080endfunc
81
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020082func 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
Bram Moolenaar7567d0b2017-11-16 23:04:15 +0100125
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
Bram Moolenaar83799a72017-11-25 17:24:09 +0100132
133func Test_writefile_sync_dev_stdout()
134 if !has('unix')
135 return
136 endif
Bram Moolenaar9980b372018-04-21 20:12:35 +0200137 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
Bram Moolenaar83799a72017-11-25 17:24:09 +0100143endfunc
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +0200144
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