blob: a06333d806dbc3bed8d3e3edc47b4b88aeb988ef [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 Moolenaare6bf6552017-06-27 22:11:51 +020041 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"])
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!
52 set backup& writebackup&
53endfunc
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
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
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +020076func 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
Bram Moolenaar7567d0b2017-11-16 23:04:15 +0100119
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
Bram Moolenaar83799a72017-11-25 17:24:09 +0100126
127func Test_writefile_sync_dev_stdout()
128 if !has('unix')
129 return
130 endif
Bram Moolenaar9980b372018-04-21 20:12:35 +0200131 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
Bram Moolenaar83799a72017-11-25 17:24:09 +0100137endfunc
Bram Moolenaar8c9e7b02018-08-30 13:07:17 +0200138
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