annotate src/testdir/test_writefile.vim @ 12857:ffdf2e4b5d9a v8.0.1305

patch 8.0.1305: writefile() never calls fsync() commit https://github.com/vim/vim/commit/7567d0b115e332f61a9f390aaccdf7825b891227 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Nov 16 23:04:15 2017 +0100 patch 8.0.1305: writefile() never calls fsync() Problem: Writefile() never calls fsync(). Solution: Follow the 'fsync' option with override to enable or disable.
author Christian Brabandt <cb@256bit.org>
date Thu, 16 Nov 2017 23:15:05 +0100
parents ae2802b6bf7d
children 98503d690368
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11519
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
1 " Tests for the writefile() function.
10068
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2
11519
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
3 func Test_writefile()
10068
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 let f = tempname()
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 call writefile(["over","written"], f, "b")
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 call writefile(["hello","world"], f, "b")
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 call writefile(["!", "good"], f, "a")
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 call writefile(["morning"], f, "ab")
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 call writefile(["", "vimmers"], f, "ab")
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 let l = readfile(f)
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 call assert_equal("hello", l[0])
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 call assert_equal("world!", l[1])
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13 call assert_equal("good", l[2])
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 call assert_equal("morning", l[3])
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 call assert_equal("vimmers", l[4])
c577c6a2e88b commit https://github.com/vim/vim/commit/19a1669ffc796e30a83c5600f82f12ebf63a2261
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 call delete(f)
11519
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
17 endfunc
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
18
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
19 func Test_writefile_fails_gently()
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
20 call assert_fails('call writefile(["test"], "Xfile", [])', 'E730:')
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
21 call assert_false(filereadable("Xfile"))
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
22 call delete("Xfile")
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
23
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
24 call assert_fails('call writefile(["test", [], [], [], "tset"], "Xfile")', 'E730:')
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
25 call assert_false(filereadable("Xfile"))
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
26 call delete("Xfile")
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
27
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
28 call assert_fails('call writefile([], "Xfile", [])', 'E730:')
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
29 call assert_false(filereadable("Xfile"))
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
30 call delete("Xfile")
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
31
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
32 call assert_fails('call writefile([], [])', 'E730:')
4a1f7849fe86 patch 8.0.0642: writefile() continues after detecting an error
Christian Brabandt <cb@256bit.org>
parents: 10068
diff changeset
33 endfunc
11605
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
34
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
35 func Test_writefile_fails_conversion()
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
36 if !has('multi_byte') || !has('iconv')
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
37 return
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
38 endif
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
39 set nobackup nowritebackup
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
40 new
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
41 let contents = ["line one", "line two"]
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
42 call writefile(contents, 'Xfile')
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
43 edit Xfile
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
44 call setline(1, ["first line", "cannot convert \u010b", "third line"])
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
45 call assert_fails('write ++enc=cp932')
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
46 call assert_equal(contents, readfile('Xfile'))
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
47
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
48 call delete('Xfile')
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
49 bwipe!
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
50 set backup& writebackup&
16ccaedce025 patch 8.0.0685: when conversion fails written file may be truncated
Christian Brabandt <cb@256bit.org>
parents: 11519
diff changeset
51 endfunc
12624
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
52
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
53 func SetFlag(timer)
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
54 let g:flag = 1
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
55 endfunc
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
56
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
57 func Test_write_quit_split()
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
58 " Prevent exiting by splitting window on file write.
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
59 augroup testgroup
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
60 autocmd BufWritePre * split
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
61 augroup END
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
62 e! Xfile
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
63 call setline(1, 'nothing')
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
64 wq
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
65
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
66 if has('timers')
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
67 " timer will not run if "exiting" is still set
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
68 let g:flag = 0
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
69 call timer_start(1, 'SetFlag')
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
70 sleep 50m
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
71 call assert_equal(1, g:flag)
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
72 unlet g:flag
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
73 endif
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
74 au! testgroup
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
75 bwipe Xfile
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
76 call delete('Xfile')
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
77 endfunc
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
78
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
79 func Test_nowrite_quit_split()
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
80 " Prevent exiting by opening a help window.
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
81 e! Xfile
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
82 help
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
83 wincmd w
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
84 exe winnr() . 'q'
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
85
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
86 if has('timers')
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
87 " timer will not run if "exiting" is still set
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
88 let g:flag = 0
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
89 call timer_start(1, 'SetFlag')
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
90 sleep 50m
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
91 call assert_equal(1, g:flag)
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
92 unlet g:flag
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
93 endif
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
94 bwipe Xfile
ae2802b6bf7d patch 8.0.1190: unusable after opening new window in BufWritePre event
Christian Brabandt <cb@256bit.org>
parents: 11605
diff changeset
95 endfunc
12857
ffdf2e4b5d9a patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents: 12624
diff changeset
96
ffdf2e4b5d9a patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents: 12624
diff changeset
97 func Test_writefile_sync_arg()
ffdf2e4b5d9a patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents: 12624
diff changeset
98 " This doesn't check if fsync() works, only that the argument is accepted.
ffdf2e4b5d9a patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents: 12624
diff changeset
99 call writefile(['one'], 'Xtest', 's')
ffdf2e4b5d9a patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents: 12624
diff changeset
100 call writefile(['two'], 'Xtest', 'S')
ffdf2e4b5d9a patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents: 12624
diff changeset
101 call delete('Xtest')
ffdf2e4b5d9a patch 8.0.1305: writefile() never calls fsync()
Christian Brabandt <cb@256bit.org>
parents: 12624
diff changeset
102 endfunc