blob: 621e3152c7a4d9842d316a8ebcad4468a55b6c58 [file] [log] [blame]
Bram Moolenaarc525e3a2017-02-18 16:59:02 +01001" Test :recover
2
Bram Moolenaarf52f0602021-03-10 21:26:37 +01003source check.vim
4
Bram Moolenaarc525e3a2017-02-18 16:59:02 +01005func Test_recover_root_dir()
6 " This used to access invalid memory.
7 split Xtest
8 set dir=/
9 call assert_fails('recover', 'E305:')
10 close!
11
Bram Moolenaar2a0b06d2017-05-18 16:23:43 +020012 if has('win32') || filewritable('/') == 2
Bram Moolenaar80345202017-02-18 22:43:19 +010013 " can write in / directory on MS-Windows
14 set dir=/notexist/
15 endif
Bram Moolenaarc525e3a2017-02-18 16:59:02 +010016 call assert_fails('split Xtest', 'E303:')
Bram Moolenaar00e192b2019-10-19 17:01:28 +020017
18 " No error with empty 'directory' setting.
19 set directory=
20 split XtestOK
21 close!
22
Bram Moolenaarc525e3a2017-02-18 16:59:02 +010023 set dir&
24endfunc
25
Bram Moolenaarf52f0602021-03-10 21:26:37 +010026" Make a copy of the current swap file to "Xswap".
27" Return the name of the swap file.
28func CopySwapfile()
29 preserve
30 " get the name of the swap file
31 let swname = split(execute("swapname"))[0]
32 let swname = substitute(swname, '[[:blank:][:cntrl:]]*\(.\{-}\)[[:blank:][:cntrl:]]*$', '\1', '')
33 " make a copy of the swap file in Xswap
34 set binary
35 exe 'sp ' . swname
36 w! Xswap
37 set nobinary
38 return swname
39endfunc
40
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020041" Inserts 10000 lines with text to fill the swap file with two levels of pointer
42" blocks. Then recovers from the swap file and checks all text is restored.
43"
44" We need about 10000 lines of 100 characters to get two levels of pointer
45" blocks.
46func Test_swap_file()
Bram Moolenaar67418d92017-10-15 22:07:39 +020047 set fileformat=unix undolevels=-1
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020048 edit! Xtest
49 let text = "\tabcdefghijklmnoparstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnoparstuvwxyz0123456789"
50 let i = 1
51 let linecount = 10000
52 while i <= linecount
53 call append(i - 1, i . text)
54 let i += 1
55 endwhile
56 $delete
Bram Moolenaarf52f0602021-03-10 21:26:37 +010057
58 let swname = CopySwapfile()
59
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020060 new
61 only!
62 bwipe! Xtest
63 call rename('Xswap', swname)
64 recover Xtest
65 call delete(swname)
66 let linedollar = line('$')
67 call assert_equal(linecount, linedollar)
68 if linedollar < linecount
69 let linecount = linedollar
70 endif
71 let i = 1
72 while i <= linecount
73 call assert_equal(i . text, getline(i))
74 let i += 1
75 endwhile
76
77 set undolevels&
78 enew! | only
79endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020080
Bram Moolenaarf52f0602021-03-10 21:26:37 +010081func Test_nocatch_process_still_running()
82 " assume Unix means sysinfo.uptime can be used
83 CheckUnix
84 CheckNotGui
85
86 " don't intercept existing swap file here
87 au! SwapExists
88
89 " Edit a file and grab its swapfile.
90 edit Xswaptest
91 call setline(1, ['a', 'b', 'c'])
92 let swname = CopySwapfile()
93
94 " Forget we edited this file
95 new
96 only!
97 bwipe! Xswaptest
98
99 call rename('Xswap', swname)
100 call feedkeys('e', 'tL')
101 redir => editOutput
102 edit Xswaptest
103 redir END
104 call assert_match('E325: ATTENTION', editOutput)
105 call assert_match('file name: .*Xswaptest', editOutput)
106 call assert_match('process ID: \d* (STILL RUNNING)', editOutput)
107
108 " Forget we edited this file
109 new
110 only!
111 bwipe! Xswaptest
112
113 " pretend we rebooted
114 call test_override("uptime", 0)
115 sleep 1
116
117 call rename('Xswap', swname)
118 call feedkeys('e', 'tL')
119 redir => editOutput
120 edit Xswaptest
121 redir END
122 call assert_match('E325: ATTENTION', editOutput)
123 call assert_notmatch('(STILL RUNNING)', editOutput)
124
125 call test_override("ALL", 0)
126 call delete(swname)
127endfunc
128
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200129" vim: shiftwidth=2 sts=2 expandtab