blob: 0d3e79c22a18fe6fcad40af99b3dd11303b634a5 [file] [log] [blame]
Bram Moolenaarb782ba42018-08-07 21:39:28 +02001" Tests for the backup function
2
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +02003source check.vim
4
Bram Moolenaarb782ba42018-08-07 21:39:28 +02005func Test_backup()
Bram Moolenaarefe03732018-08-09 22:26:38 +02006 set backup backupdir=. backupskip=
Bram Moolenaarb782ba42018-08-07 21:39:28 +02007 new
8 call setline(1, ['line1', 'line2'])
9 :f Xbackup.txt
10 :w! Xbackup.txt
11 " backup file is only created after
12 " writing a second time (before overwriting)
13 :w! Xbackup.txt
14 let l = readfile('Xbackup.txt~')
15 call assert_equal(['line1', 'line2'], l)
16 bw!
Bram Moolenaarefe03732018-08-09 22:26:38 +020017 set backup&vim backupdir&vim backupskip&vim
Bram Moolenaarb782ba42018-08-07 21:39:28 +020018 call delete('Xbackup.txt')
19 call delete('Xbackup.txt~')
20endfunc
21
22func Test_backup2()
Bram Moolenaarefe03732018-08-09 22:26:38 +020023 set backup backupdir=.// backupskip=
Bram Moolenaarb782ba42018-08-07 21:39:28 +020024 new
25 call setline(1, ['line1', 'line2', 'line3'])
26 :f Xbackup.txt
27 :w! Xbackup.txt
28 " backup file is only created after
29 " writing a second time (before overwriting)
30 :w! Xbackup.txt
31 sp *Xbackup.txt~
32 call assert_equal(['line1', 'line2', 'line3'], getline(1,'$'))
33 let f=expand('%')
Bram Moolenaarf8f88f82018-08-08 11:02:32 +020034 call assert_match('%testdir%Xbackup.txt\~', f)
Bram Moolenaarb782ba42018-08-07 21:39:28 +020035 bw!
36 bw!
37 call delete('Xbackup.txt')
38 call delete(f)
Bram Moolenaarefe03732018-08-09 22:26:38 +020039 set backup&vim backupdir&vim backupskip&vim
Bram Moolenaarb782ba42018-08-07 21:39:28 +020040endfunc
41
42func Test_backup2_backupcopy()
Bram Moolenaarefe03732018-08-09 22:26:38 +020043 set backup backupdir=.// backupcopy=yes backupskip=
Bram Moolenaarb782ba42018-08-07 21:39:28 +020044 new
45 call setline(1, ['line1', 'line2', 'line3'])
46 :f Xbackup.txt
47 :w! Xbackup.txt
48 " backup file is only created after
49 " writing a second time (before overwriting)
50 :w! Xbackup.txt
51 sp *Xbackup.txt~
52 call assert_equal(['line1', 'line2', 'line3'], getline(1,'$'))
53 let f=expand('%')
Bram Moolenaarf8f88f82018-08-08 11:02:32 +020054 call assert_match('%testdir%Xbackup.txt\~', f)
Bram Moolenaarb782ba42018-08-07 21:39:28 +020055 bw!
56 bw!
57 call delete('Xbackup.txt')
58 call delete(f)
Bram Moolenaarefe03732018-08-09 22:26:38 +020059 set backup&vim backupdir&vim backupcopy&vim backupskip&vim
Bram Moolenaarb782ba42018-08-07 21:39:28 +020060endfunc
Bram Moolenaar1de5f7c2020-06-11 19:22:43 +020061
62" Test for using a non-existing directory as a backup directory
63func Test_non_existing_backupdir()
64 CheckNotBSD
65 let save_backup = &backupdir
66 set backupdir=./non_existing_dir
67 call writefile(['line1'], 'Xfile')
68 new Xfile
69 " TODO: write doesn't fail in Cirrus FreeBSD CI test
70 call assert_fails('write', 'E510:')
71 let &backupdir = save_backup
72 call delete('Xfile')
73endfunc
74
75" vim: shiftwidth=2 sts=2 expandtab