excmd_spec.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local clear = n.clear
  5. local command = n.command
  6. local exec = n.exec
  7. local expect_exit = n.expect_exit
  8. local feed = n.feed
  9. local fn = n.fn
  10. local read_file = t.read_file
  11. local eq = t.eq
  12. local write_file = t.write_file
  13. local is_os = t.is_os
  14. describe(':confirm command dialog', function()
  15. local screen
  16. local function start_new()
  17. clear()
  18. screen = Screen.new(75, 20)
  19. end
  20. -- Test for the :confirm command dialog
  21. -- oldtest: Test_confirm_cmd()
  22. it('works', function()
  23. write_file('Xfoo', 'foo1\n')
  24. write_file('Xbar', 'bar1\n')
  25. -- Test for saving all the modified buffers
  26. start_new()
  27. exec([[
  28. set nomore
  29. new Xfoo
  30. call setline(1, 'foo2')
  31. new Xbar
  32. call setline(1, 'bar2')
  33. wincmd b
  34. ]])
  35. feed(':confirm qall\n')
  36. screen:expect([[
  37. bar2 |
  38. {1:~ }|*5
  39. {2:Xbar [+] }|
  40. foo2 |
  41. {1:~ }|*4
  42. {2:Xfoo [+] }|
  43. |
  44. {1:~ }|*2
  45. {3: }|
  46. :confirm qall |
  47. {6:Save changes to "Xbar"?} |
  48. {6:[Y]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: }^ |
  49. ]])
  50. expect_exit(1000, feed, 'A')
  51. eq('foo2\n', read_file('Xfoo'))
  52. eq('bar2\n', read_file('Xbar'))
  53. -- Test for discarding all the changes to modified buffers
  54. start_new()
  55. exec([[
  56. set nomore
  57. new Xfoo
  58. call setline(1, 'foo3')
  59. new Xbar
  60. call setline(1, 'bar3')
  61. wincmd b
  62. ]])
  63. feed(':confirm qall\n')
  64. screen:expect([[
  65. bar3 |
  66. {1:~ }|*5
  67. {2:Xbar [+] }|
  68. foo3 |
  69. {1:~ }|*4
  70. {2:Xfoo [+] }|
  71. |
  72. {1:~ }|*2
  73. {3: }|
  74. :confirm qall |
  75. {6:Save changes to "Xbar"?} |
  76. {6:[Y]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: }^ |
  77. ]])
  78. expect_exit(1000, feed, 'D')
  79. eq('foo2\n', read_file('Xfoo'))
  80. eq('bar2\n', read_file('Xbar'))
  81. -- Test for saving and discarding changes to some buffers
  82. start_new()
  83. exec([[
  84. set nomore
  85. new Xfoo
  86. call setline(1, 'foo4')
  87. new Xbar
  88. call setline(1, 'bar4')
  89. wincmd b
  90. ]])
  91. feed(':confirm qall\n')
  92. screen:expect([[
  93. bar4 |
  94. {1:~ }|*5
  95. {2:Xbar [+] }|
  96. foo4 |
  97. {1:~ }|*4
  98. {2:Xfoo [+] }|
  99. |
  100. {1:~ }|*2
  101. {3: }|
  102. :confirm qall |
  103. {6:Save changes to "Xbar"?} |
  104. {6:[Y]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: }^ |
  105. ]])
  106. feed('N')
  107. screen:expect([[
  108. bar4 |
  109. {1:~ }|*5
  110. {2:Xbar [+] }|
  111. foo4 |
  112. {1:~ }|*4
  113. {2:Xfoo [+] }|
  114. |
  115. {3: }|
  116. :confirm qall |
  117. {6:Save changes to "Xbar"?} |
  118. |
  119. {6:Save changes to "Xfoo"?} |
  120. {6:[Y]es, (N)o, (C)ancel: }^ |
  121. ]])
  122. expect_exit(1000, feed, 'Y')
  123. eq('foo4\n', read_file('Xfoo'))
  124. eq('bar2\n', read_file('Xbar'))
  125. os.remove('Xfoo')
  126. os.remove('Xbar')
  127. end)
  128. -- oldtest: Test_confirm_cmd_cancel()
  129. it('can be cancelled', function()
  130. -- Test for closing a window with a modified buffer
  131. start_new()
  132. screen:try_resize(75, 10)
  133. exec([[
  134. set nohidden nomore
  135. new
  136. call setline(1, 'abc')
  137. ]])
  138. feed(':confirm close\n')
  139. screen:expect([[
  140. abc |
  141. {1:~ }|*3
  142. {3:[No Name] [+] }|
  143. |
  144. {3: }|
  145. :confirm close |
  146. {6:Save changes to "Untitled"?} |
  147. {6:[Y]es, (N)o, (C)ancel: }^ |
  148. ]])
  149. feed('C')
  150. screen:expect([[
  151. ^abc |
  152. {1:~ }|*3
  153. {3:[No Name] [+] }|
  154. |
  155. {1:~ }|*2
  156. {2:[No Name] }|
  157. |
  158. ]])
  159. feed(':confirm close\n')
  160. screen:expect([[
  161. abc |
  162. {1:~ }|*3
  163. {3:[No Name] [+] }|
  164. |
  165. {3: }|
  166. :confirm close |
  167. {6:Save changes to "Untitled"?} |
  168. {6:[Y]es, (N)o, (C)ancel: }^ |
  169. ]])
  170. feed('N')
  171. screen:expect([[
  172. ^ |
  173. {1:~ }|*8
  174. |
  175. ]])
  176. end)
  177. -- oldtest: Test_confirm_q_wq()
  178. it('works with :q and :wq', function()
  179. write_file('Xfoo', 'foo')
  180. start_new()
  181. screen:try_resize(75, 8)
  182. exec([[
  183. set hidden nomore
  184. call setline(1, 'abc')
  185. edit Xfoo
  186. set nofixendofline
  187. ]])
  188. feed(':confirm q\n')
  189. screen:expect([[
  190. foo |
  191. {1:~ }|*3
  192. {3: }|
  193. :confirm q |
  194. {6:Save changes to "Untitled"?} |
  195. {6:[Y]es, (N)o, (C)ancel: }^ |
  196. ]])
  197. feed('C')
  198. screen:expect([[
  199. ^abc |
  200. {1:~ }|*6
  201. |
  202. ]])
  203. command('edit Xfoo')
  204. feed(':confirm wq\n')
  205. screen:expect([[
  206. foo |
  207. {1:~ }|*3
  208. {3: }|
  209. "Xfoo" [noeol] 1L, 3B written |
  210. {6:Save changes to "Untitled"?} |
  211. {6:[Y]es, (N)o, (C)ancel: }^ |
  212. ]])
  213. feed('C')
  214. screen:expect([[
  215. ^abc |
  216. {1:~ }|*6
  217. "Xfoo" [noeol] 1L, 3B written |
  218. ]])
  219. os.remove('Xfoo')
  220. end)
  221. -- oldtest: Test_confirm_write_ro()
  222. it('works when writing a read-only file', function()
  223. write_file('Xconfirm_write_ro', 'foo\n')
  224. start_new()
  225. screen:try_resize(75, 8)
  226. exec([[
  227. set ruler
  228. set nobackup ff=unix cmdheight=2
  229. edit Xconfirm_write_ro
  230. norm Abar
  231. ]])
  232. -- Try to write with 'ro' option.
  233. feed(':set ro | confirm w\n')
  234. screen:expect([[
  235. foobar |
  236. {1:~ }|*2
  237. {3: }|
  238. :set ro | confirm w |
  239. {6:'readonly' option is set for "Xconfirm_write_ro".} |
  240. {6:Do you wish to write anyway?} |
  241. {6:(Y)es, [N]o: }^ |
  242. ]])
  243. feed('N')
  244. screen:expect([[
  245. fooba^r |
  246. {1:~ }|*5
  247. |
  248. 1,6 All |
  249. ]])
  250. eq('foo\n', read_file('Xconfirm_write_ro'))
  251. feed(':confirm w\n')
  252. screen:expect([[
  253. foobar |
  254. {1:~ }|*2
  255. {3: }|
  256. :confirm w |
  257. {6:'readonly' option is set for "Xconfirm_write_ro".} |
  258. {6:Do you wish to write anyway?} |
  259. {6:(Y)es, [N]o: }^ |
  260. ]])
  261. feed('Y')
  262. if is_os('win') then
  263. screen:expect([[
  264. foobar |
  265. {1:~ }|
  266. {3: }|
  267. :confirm w |
  268. {6:'readonly' option is set for "Xconfirm_write_ro".} |
  269. {6:Do you wish to write anyway?} |
  270. "Xconfirm_write_ro" [unix] 1L, 7B written |
  271. {6:Press ENTER or type command to continue}^ |
  272. ]])
  273. else
  274. screen:expect([[
  275. foobar |
  276. {1:~ }|
  277. {3: }|
  278. :confirm w |
  279. {6:'readonly' option is set for "Xconfirm_write_ro".} |
  280. {6:Do you wish to write anyway?} |
  281. "Xconfirm_write_ro" 1L, 7B written |
  282. {6:Press ENTER or type command to continue}^ |
  283. ]])
  284. end
  285. eq('foobar\n', read_file('Xconfirm_write_ro'))
  286. feed('<CR>') -- suppress hit-enter prompt
  287. -- Try to write with read-only file permissions.
  288. fn.setfperm('Xconfirm_write_ro', 'r--r--r--')
  289. feed(':set noro | silent undo | confirm w\n')
  290. screen:expect([[
  291. foobar |
  292. {1:~ }|
  293. {3: }|
  294. :set noro | silent undo | confirm w |
  295. {6:File permissions of "Xconfirm_write_ro" are read-only.} |
  296. {6:It may still be possible to write it.} |
  297. {6:Do you wish to try?} |
  298. {6:(Y)es, [N]o: }^ |
  299. ]])
  300. feed('Y')
  301. if is_os('win') then
  302. screen:expect([[
  303. foobar |
  304. {3: }|
  305. :set noro | silent undo | confirm w |
  306. {6:File permissions of "Xconfirm_write_ro" are read-only.} |
  307. {6:It may still be possible to write it.} |
  308. {6:Do you wish to try?} |
  309. "Xconfirm_write_ro" [unix] 1L, 4B written |
  310. {6:Press ENTER or type command to continue}^ |
  311. ]])
  312. else
  313. screen:expect([[
  314. foobar |
  315. {3: }|
  316. :set noro | silent undo | confirm w |
  317. {6:File permissions of "Xconfirm_write_ro" are read-only.} |
  318. {6:It may still be possible to write it.} |
  319. {6:Do you wish to try?} |
  320. "Xconfirm_write_ro" 1L, 4B written |
  321. {6:Press ENTER or type command to continue}^ |
  322. ]])
  323. end
  324. eq('foo\n', read_file('Xconfirm_write_ro'))
  325. feed('<CR>') -- suppress hit-enter prompt
  326. os.remove('Xconfirm_write_ro')
  327. end)
  328. -- oldtest: Test_confirm_write_partial_file()
  329. it('works when writing a partial file', function()
  330. write_file('Xwrite_partial', 'a\nb\nc\nd\n')
  331. start_new()
  332. screen:try_resize(75, 8)
  333. exec([[
  334. set ruler
  335. set nobackup ff=unix cmdheight=2
  336. edit Xwrite_partial
  337. ]])
  338. feed(':confirm 2,3w\n')
  339. screen:expect([[
  340. a |
  341. b |
  342. c |
  343. d |
  344. {3: }|
  345. :confirm 2,3w |
  346. {6:Write partial file?} |
  347. {6:(Y)es, [N]o: }^ |
  348. ]])
  349. feed('N')
  350. screen:expect([[
  351. ^a |
  352. b |
  353. c |
  354. d |
  355. {1:~ }|*2
  356. |
  357. 1,1 All |
  358. ]])
  359. eq('a\nb\nc\nd\n', read_file('Xwrite_partial'))
  360. os.remove('Xwrite_partial')
  361. feed(':confirm 2,3w\n')
  362. screen:expect([[
  363. a |
  364. b |
  365. c |
  366. d |
  367. {3: }|
  368. :confirm 2,3w |
  369. {6:Write partial file?} |
  370. {6:(Y)es, [N]o: }^ |
  371. ]])
  372. feed('Y')
  373. if is_os('win') then
  374. screen:expect([[
  375. a |
  376. b |
  377. c |
  378. {3: }|
  379. :confirm 2,3w |
  380. {6:Write partial file?} |
  381. "Xwrite_partial" [New][unix] 2L, 4B written |
  382. {6:Press ENTER or type command to continue}^ |
  383. ]])
  384. else
  385. screen:expect([[
  386. a |
  387. b |
  388. c |
  389. {3: }|
  390. :confirm 2,3w |
  391. {6:Write partial file?} |
  392. "Xwrite_partial" [New] 2L, 4B written |
  393. {6:Press ENTER or type command to continue}^ |
  394. ]])
  395. end
  396. eq('b\nc\n', read_file('Xwrite_partial'))
  397. os.remove('Xwrite_partial')
  398. end)
  399. end)