xref: /sqlite-3.40.0/test/shell8.test (revision ac15e2d7)
1# 2017 December 9
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# Test the shell tool ".ar" command.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17set testprefix shell8
18set CLI [test_find_cli]
19
20proc populate_dir {dirname spec} {
21  # First delete the current tree, if one exists.
22  file delete -force $dirname
23
24  # Recreate the root of the new tree.
25  file mkdir $dirname
26
27  # Add each file to the new tree.
28  foreach {f d} $spec {
29    set path [file join $dirname $f]
30    file mkdir [file dirname $path]
31    set fd [open $path w]
32    puts -nonewline $fd $d
33    close $fd
34  }
35}
36
37proc dir_to_list {dirname {n -1}} {
38  if {$n<0} {set n [llength [file split $dirname]]}
39
40  set res [list]
41  foreach f [glob -nocomplain $dirname/*] {
42    set mtime [file mtime $f]
43    set perm [file attributes $f -perm]
44    set relpath [file join {*}[lrange [file split $f] $n end]]
45    lappend res
46    if {[file isdirectory $f]} {
47      lappend res [list $relpath / $mtime $perm]
48      lappend res {*}[dir_to_list $f]
49    } else {
50      set fd [open $f]
51      set data [read $fd]
52      close $fd
53      lappend res [list $relpath $data $mtime $perm]
54    }
55  }
56  lsort $res
57}
58
59proc dir_compare {d1 d2} {
60  set l1 [dir_to_list $d1]
61  set l2 [dir_to_list $d1]
62  string compare $l1 $l2
63}
64
65foreach {tn tcl} {
66  1 {
67    set c1 ".ar c ar1"
68    set x1 ".ar x"
69
70    set c2 ".ar cC ar1 ."
71    set x2 ".ar Cx ar3"
72
73    set c3 ".ar cCf ar1 test_xyz.db ."
74    set x3 ".ar Cfx ar3 test_xyz.db"
75  }
76
77  2 {
78    set c1 ".ar -c ar1"
79    set x1 ".ar -x"
80
81    set c2 ".ar -cC ar1 ."
82    set x2 ".ar -xC ar3"
83
84    set c3 ".ar -cCar1 -ftest_xyz.db ."
85    set x3 ".ar -x -C ar3 -f test_xyz.db"
86  }
87
88  3 {
89    set c1 ".ar --create ar1"
90    set x1 ".ar --extract"
91
92    set c2 ".ar --directory ar1 --create ."
93    set x2 ".ar --extract --dir ar3"
94
95    set c3 ".ar --creat --dir ar1 --file test_xyz.db ."
96    set x3 ".ar --e  --d ar3 --f test_xyz.db"
97  }
98
99  4 {
100    set c1 ".ar --cr ar1"
101    set x1 ".ar --e"
102
103    set c2 ".ar -C ar1 -c ."
104    set x2 ".ar -x -C ar3"
105
106    set c3 ".ar -c --directory ar1 --file test_xyz.db ."
107    set x3 ".ar -x --directory ar3 --file test_xyz.db"
108  }
109} {
110  eval $tcl
111
112  # Populate directory "ar1" with some files.
113  #
114  populate_dir ar1 {
115    file1 "abcd"
116    file2 "efgh"
117    dir1/file3 "ijkl"
118  }
119  set expected [dir_to_list ar1]
120
121  do_test 1.$tn.1 {
122    catchcmd test_ar.db $c1
123    file delete -force ar1
124    catchcmd test_ar.db $x1
125    dir_to_list ar1
126  } $expected
127
128  do_test 1.$tn.2 {
129    file delete -force ar3
130    catchcmd test_ar.db $c2
131    catchcmd test_ar.db $x2
132    dir_to_list ar3
133  } $expected
134
135  do_test 1.$tn.3 {
136    file delete -force ar3
137    file delete -force test_xyz.db
138    catchcmd ":memory:" $c3
139    catchcmd ":memory:" $x3
140    dir_to_list ar3
141  } $expected
142
143  # This is a repeat of test 1.$tn.1, except that there is a 2 second
144  # pause between creating the archive and extracting its contents.
145  # This is to test that timestamps are set correctly.
146  #
147  # Because it is slow, only do this for $tn==1.
148  if {$tn==1} {
149    do_test 1.$tn.1 {
150      catchcmd test_ar.db $c1
151      file delete -force ar1
152      after 2000
153      catchcmd test_ar.db $x1
154      dir_to_list ar1
155    } $expected
156  }
157}
158
159finish_test
160
161
162
163finish_test
164
165