xref: /sqlite-3.40.0/test/shell8.test (revision dfdfd8c7)
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    if {$::tcl_platform(platform)!="windows"} {
44      set perm [file attributes $f -perm]
45    } else {
46      set perm 0
47    }
48    set relpath [file join {*}[lrange [file split $f] $n end]]
49    lappend res
50    if {[file isdirectory $f]} {
51      lappend res [list $relpath / $mtime $perm]
52      lappend res {*}[dir_to_list $f]
53    } else {
54      set fd [open $f]
55      set data [read $fd]
56      close $fd
57      lappend res [list $relpath $data $mtime $perm]
58    }
59  }
60  lsort $res
61}
62
63proc dir_compare {d1 d2} {
64  set l1 [dir_to_list $d1]
65  set l2 [dir_to_list $d1]
66  string compare $l1 $l2
67}
68
69foreach {tn tcl} {
70  1 {
71    set c1 ".ar c ar1"
72    set x1 ".ar x"
73
74    set c2 ".ar cC ar1 ."
75    set x2 ".ar Cx ar3"
76
77    set c3 ".ar cCf ar1 test_xyz.db ."
78    set x3 ".ar Cfx ar3 test_xyz.db"
79  }
80
81  2 {
82    set c1 ".ar -c ar1"
83    set x1 ".ar -x"
84
85    set c2 ".ar -cC ar1 ."
86    set x2 ".ar -xC ar3"
87
88    set c3 ".ar -cCar1 -ftest_xyz.db ."
89    set x3 ".ar -x -C ar3 -f test_xyz.db"
90  }
91
92  3 {
93    set c1 ".ar --create ar1"
94    set x1 ".ar --extract"
95
96    set c2 ".ar --directory ar1 --create ."
97    set x2 ".ar --extract --dir ar3"
98
99    set c3 ".ar --creat --dir ar1 --file test_xyz.db ."
100    set x3 ".ar --e  --d ar3 --f test_xyz.db"
101  }
102
103  4 {
104    set c1 ".ar --cr ar1"
105    set x1 ".ar --e"
106
107    set c2 ".ar -C ar1 -c ."
108    set x2 ".ar -x -C ar3"
109
110    set c3 ".ar -c --directory ar1 --file test_xyz.db ."
111    set x3 ".ar -x --directory ar3 --file test_xyz.db"
112  }
113} {
114  eval $tcl
115
116  # Populate directory "ar1" with some files.
117  #
118  populate_dir ar1 {
119    file1 "abcd"
120    file2 "efgh"
121    dir1/file3 "ijkl"
122  }
123  set expected [dir_to_list ar1]
124
125  do_test 1.$tn.1 {
126    catchcmd test_ar.db $c1
127    file delete -force ar1
128    catchcmd test_ar.db $x1
129    dir_to_list ar1
130  } $expected
131
132  do_test 1.$tn.2 {
133    file delete -force ar3
134    catchcmd test_ar.db $c2
135    catchcmd test_ar.db $x2
136    dir_to_list ar3
137  } $expected
138
139  do_test 1.$tn.3 {
140    file delete -force ar3
141    file delete -force test_xyz.db
142    catchcmd ":memory:" $c3
143    catchcmd ":memory:" $x3
144    dir_to_list ar3
145  } $expected
146
147  # This is a repeat of test 1.$tn.1, except that there is a 2 second
148  # pause between creating the archive and extracting its contents.
149  # This is to test that timestamps are set correctly.
150  #
151  # Because it is slow, only do this for $tn==1.
152  if {$tn==1} {
153    do_test 1.$tn.1 {
154      catchcmd test_ar.db $c1
155      file delete -force ar1
156      after 2000
157      catchcmd test_ar.db $x1
158      dir_to_list ar1
159    } $expected
160  }
161}
162
163finish_test
164
165
166
167finish_test
168
169