Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 1 | " Tests for the writefile() function. |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 3 | func Test_writefile() |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 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) |
Bram Moolenaar | 8cf9128 | 2017-06-13 19:38:37 +0200 | [diff] [blame] | 17 | endfunc |
| 18 | |
| 19 | func 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:') |
| 33 | endfunc |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 34 | |
| 35 | func Test_writefile_fails_conversion() |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 36 | if !has('iconv') || has('sun') |
Bram Moolenaar | e6bf655 | 2017-06-27 22:11:51 +0200 | [diff] [blame] | 37 | return |
| 38 | endif |
| 39 | set nobackup nowritebackup |
| 40 | new |
| 41 | let contents = ["line one", "line two"] |
| 42 | call writefile(contents, 'Xfile') |
| 43 | edit Xfile |
| 44 | call setline(1, ["first line", "cannot convert \u010b", "third line"]) |
| 45 | call assert_fails('write ++enc=cp932') |
| 46 | call assert_equal(contents, readfile('Xfile')) |
| 47 | |
| 48 | call delete('Xfile') |
| 49 | bwipe! |
| 50 | set backup& writebackup& |
| 51 | endfunc |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 52 | |
| 53 | func SetFlag(timer) |
| 54 | let g:flag = 1 |
| 55 | endfunc |
| 56 | |
| 57 | func Test_write_quit_split() |
| 58 | " Prevent exiting by splitting window on file write. |
| 59 | augroup testgroup |
| 60 | autocmd BufWritePre * split |
| 61 | augroup END |
| 62 | e! Xfile |
| 63 | call setline(1, 'nothing') |
| 64 | wq |
| 65 | |
| 66 | if has('timers') |
| 67 | " timer will not run if "exiting" is still set |
| 68 | let g:flag = 0 |
| 69 | call timer_start(1, 'SetFlag') |
| 70 | sleep 50m |
| 71 | call assert_equal(1, g:flag) |
| 72 | unlet g:flag |
| 73 | endif |
| 74 | au! testgroup |
| 75 | bwipe Xfile |
| 76 | call delete('Xfile') |
| 77 | endfunc |
| 78 | |
| 79 | func Test_nowrite_quit_split() |
| 80 | " Prevent exiting by opening a help window. |
| 81 | e! Xfile |
| 82 | help |
| 83 | wincmd w |
| 84 | exe winnr() . 'q' |
| 85 | |
| 86 | if has('timers') |
| 87 | " timer will not run if "exiting" is still set |
| 88 | let g:flag = 0 |
| 89 | call timer_start(1, 'SetFlag') |
| 90 | sleep 50m |
| 91 | call assert_equal(1, g:flag) |
| 92 | unlet g:flag |
| 93 | endif |
| 94 | bwipe Xfile |
| 95 | endfunc |
Bram Moolenaar | 7567d0b | 2017-11-16 23:04:15 +0100 | [diff] [blame] | 96 | |
| 97 | func Test_writefile_sync_arg() |
| 98 | " This doesn't check if fsync() works, only that the argument is accepted. |
| 99 | call writefile(['one'], 'Xtest', 's') |
| 100 | call writefile(['two'], 'Xtest', 'S') |
| 101 | call delete('Xtest') |
| 102 | endfunc |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 103 | |
| 104 | func Test_writefile_sync_dev_stdout() |
| 105 | if !has('unix') |
| 106 | return |
| 107 | endif |
Bram Moolenaar | 9980b37 | 2018-04-21 20:12:35 +0200 | [diff] [blame] | 108 | if filewritable('/dev/stdout') |
| 109 | " Just check that this doesn't cause an error. |
| 110 | call writefile(['one'], '/dev/stdout') |
| 111 | else |
| 112 | throw 'Skipped: /dev/stdout is not writable' |
| 113 | endif |
Bram Moolenaar | 83799a7 | 2017-11-25 17:24:09 +0100 | [diff] [blame] | 114 | endfunc |
Bram Moolenaar | 8c9e7b0 | 2018-08-30 13:07:17 +0200 | [diff] [blame] | 115 | |
| 116 | func Test_writefile_autowrite() |
| 117 | set autowrite |
| 118 | new |
| 119 | next Xa Xb Xc |
| 120 | call setline(1, 'aaa') |
| 121 | next |
| 122 | call assert_equal(['aaa'], readfile('Xa')) |
| 123 | call setline(1, 'bbb') |
| 124 | call assert_fails('edit XX') |
| 125 | call assert_false(filereadable('Xb')) |
| 126 | |
| 127 | set autowriteall |
| 128 | edit XX |
| 129 | call assert_equal(['bbb'], readfile('Xb')) |
| 130 | |
| 131 | bwipe! |
| 132 | call delete('Xa') |
| 133 | call delete('Xb') |
| 134 | set noautowrite |
| 135 | endfunc |
| 136 | |
| 137 | func Test_writefile_autowrite_nowrite() |
| 138 | set autowrite |
| 139 | new |
| 140 | next Xa Xb Xc |
| 141 | set buftype=nowrite |
| 142 | call setline(1, 'aaa') |
| 143 | let buf = bufnr('%') |
| 144 | " buffer contents silently lost |
| 145 | edit XX |
| 146 | call assert_false(filereadable('Xa')) |
| 147 | rewind |
| 148 | call assert_equal('', getline(1)) |
| 149 | |
| 150 | bwipe! |
| 151 | set noautowrite |
| 152 | endfunc |